xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
10b57cec5SDimitry Andric //===- LegalizeDAG.cpp - Implement SelectionDAG::Legalize -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the SelectionDAG::Legalize method.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
140b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
1681ad6265SDimitry Andric #include "llvm/ADT/FloatingPointMode.h"
170b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
190b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
200b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
218bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
220b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
26*06c3fb27SDimitry Andric #include "llvm/CodeGen/MachineValueType.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
340b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
350b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
360b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
370b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
380b57cec5SDimitry Andric #include "llvm/IR/Function.h"
390b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
400b57cec5SDimitry Andric #include "llvm/IR/Type.h"
410b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
420b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
430b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
440b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
450b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
460b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
470b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
480b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
490b57cec5SDimitry Andric #include <cassert>
500b57cec5SDimitry Andric #include <cstdint>
510b57cec5SDimitry Andric #include <tuple>
520b57cec5SDimitry Andric #include <utility>
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric using namespace llvm;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric #define DEBUG_TYPE "legalizedag"
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric namespace {
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric /// Keeps track of state when getting the sign of a floating-point value as an
610b57cec5SDimitry Andric /// integer.
620b57cec5SDimitry Andric struct FloatSignAsInt {
630b57cec5SDimitry Andric   EVT FloatVT;
640b57cec5SDimitry Andric   SDValue Chain;
650b57cec5SDimitry Andric   SDValue FloatPtr;
660b57cec5SDimitry Andric   SDValue IntPtr;
670b57cec5SDimitry Andric   MachinePointerInfo IntPointerInfo;
680b57cec5SDimitry Andric   MachinePointerInfo FloatPointerInfo;
690b57cec5SDimitry Andric   SDValue IntValue;
700b57cec5SDimitry Andric   APInt SignMask;
710b57cec5SDimitry Andric   uint8_t SignBit;
720b57cec5SDimitry Andric };
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
750b57cec5SDimitry Andric /// This takes an arbitrary SelectionDAG as input and
760b57cec5SDimitry Andric /// hacks on it until the target machine can handle it.  This involves
770b57cec5SDimitry Andric /// eliminating value sizes the machine cannot handle (promoting small sizes to
780b57cec5SDimitry Andric /// large sizes or splitting up large values into small values) as well as
790b57cec5SDimitry Andric /// eliminating operations the machine cannot handle.
800b57cec5SDimitry Andric ///
810b57cec5SDimitry Andric /// This code also does a small amount of optimization and recognition of idioms
820b57cec5SDimitry Andric /// as part of its processing.  For example, if a target does not support a
830b57cec5SDimitry Andric /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
840b57cec5SDimitry Andric /// will attempt merge setcc and brc instructions into brcc's.
850b57cec5SDimitry Andric class SelectionDAGLegalize {
860b57cec5SDimitry Andric   const TargetMachine &TM;
870b57cec5SDimitry Andric   const TargetLowering &TLI;
880b57cec5SDimitry Andric   SelectionDAG &DAG;
890b57cec5SDimitry Andric 
900b57cec5SDimitry Andric   /// The set of nodes which have already been legalized. We hold a
910b57cec5SDimitry Andric   /// reference to it in order to update as necessary on node deletion.
920b57cec5SDimitry Andric   SmallPtrSetImpl<SDNode *> &LegalizedNodes;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   /// A set of all the nodes updated during legalization.
950b57cec5SDimitry Andric   SmallSetVector<SDNode *, 16> *UpdatedNodes;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric   EVT getSetCCResultType(EVT VT) const {
980b57cec5SDimitry Andric     return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
990b57cec5SDimitry Andric   }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   // Libcall insertion helpers.
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric public:
1040b57cec5SDimitry Andric   SelectionDAGLegalize(SelectionDAG &DAG,
1050b57cec5SDimitry Andric                        SmallPtrSetImpl<SDNode *> &LegalizedNodes,
1060b57cec5SDimitry Andric                        SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
1070b57cec5SDimitry Andric       : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
1080b57cec5SDimitry Andric         LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
1090b57cec5SDimitry Andric 
1100b57cec5SDimitry Andric   /// Legalizes the given operation.
1110b57cec5SDimitry Andric   void LegalizeOp(SDNode *Node);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric private:
1140b57cec5SDimitry Andric   SDValue OptimizeFloatStore(StoreSDNode *ST);
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   void LegalizeLoadOps(SDNode *Node);
1170b57cec5SDimitry Andric   void LegalizeStoreOps(SDNode *Node);
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   /// Some targets cannot handle a variable
1200b57cec5SDimitry Andric   /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
1210b57cec5SDimitry Andric   /// is necessary to spill the vector being inserted into to memory, perform
1220b57cec5SDimitry Andric   /// the insert there, and then read the result back.
1230b57cec5SDimitry Andric   SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
1240b57cec5SDimitry Andric                                          const SDLoc &dl);
1250b57cec5SDimitry Andric   SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx,
1260b57cec5SDimitry Andric                                   const SDLoc &dl);
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric   /// Return a vector shuffle operation which
1290b57cec5SDimitry Andric   /// performs the same shuffe in terms of order or result bytes, but on a type
1300b57cec5SDimitry Andric   /// whose vector element type is narrower than the original shuffle type.
1310b57cec5SDimitry Andric   /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
1320b57cec5SDimitry Andric   SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
1330b57cec5SDimitry Andric                                      SDValue N1, SDValue N2,
1340b57cec5SDimitry Andric                                      ArrayRef<int> Mask) const;
1350b57cec5SDimitry Andric 
136*06c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
137*06c3fb27SDimitry Andric                         TargetLowering::ArgListTy &&Args, bool isSigned);
138*06c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
1390b57cec5SDimitry Andric 
140*06c3fb27SDimitry Andric   void ExpandFrexpLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
141fe6060f1SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC,
142fe6060f1SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
143480093f4SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
1440b57cec5SDimitry Andric                        RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
1450b57cec5SDimitry Andric                        RTLIB::Libcall Call_F128,
146480093f4SDimitry Andric                        RTLIB::Libcall Call_PPCF128,
147480093f4SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
148bdd1243dSDimitry Andric   SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
149bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I8,
150bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I16,
151bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I32,
152bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I64,
153bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I128);
154480093f4SDimitry Andric   void ExpandArgFPLibCall(SDNode *Node,
1550b57cec5SDimitry Andric                           RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
1560b57cec5SDimitry Andric                           RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
157480093f4SDimitry Andric                           RTLIB::Libcall Call_PPCF128,
158480093f4SDimitry Andric                           SmallVectorImpl<SDValue> &Results);
1590b57cec5SDimitry Andric   void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1600b57cec5SDimitry Andric   void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1610b57cec5SDimitry Andric 
1620b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1630b57cec5SDimitry Andric                            const SDLoc &dl);
1640b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1650b57cec5SDimitry Andric                            const SDLoc &dl, SDValue ChainIn);
1660b57cec5SDimitry Andric   SDValue ExpandBUILD_VECTOR(SDNode *Node);
1678bcb0991SDimitry Andric   SDValue ExpandSPLAT_VECTOR(SDNode *Node);
1680b57cec5SDimitry Andric   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
1690b57cec5SDimitry Andric   void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
1700b57cec5SDimitry Andric                                 SmallVectorImpl<SDValue> &Results);
1710b57cec5SDimitry Andric   void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
1720b57cec5SDimitry Andric                          SDValue Value) const;
1730b57cec5SDimitry Andric   SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
1740b57cec5SDimitry Andric                           SDValue NewIntValue) const;
1750b57cec5SDimitry Andric   SDValue ExpandFCOPYSIGN(SDNode *Node) const;
1760b57cec5SDimitry Andric   SDValue ExpandFABS(SDNode *Node) const;
177e8d8bef9SDimitry Andric   SDValue ExpandFNEG(SDNode *Node) const;
178*06c3fb27SDimitry Andric   SDValue expandLdexp(SDNode *Node) const;
179*06c3fb27SDimitry Andric   SDValue expandFrexp(SDNode *Node) const;
180*06c3fb27SDimitry Andric 
181480093f4SDimitry Andric   SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain);
182480093f4SDimitry Andric   void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl,
183480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
184480093f4SDimitry Andric   void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
185480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
186e8d8bef9SDimitry Andric   SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl);
1870b57cec5SDimitry Andric 
188e8d8bef9SDimitry Andric   SDValue ExpandPARITY(SDValue Op, const SDLoc &dl);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
1910b57cec5SDimitry Andric   SDValue ExpandInsertToVectorThroughStack(SDValue Op);
1920b57cec5SDimitry Andric   SDValue ExpandVectorBuildThroughStack(SDNode* Node);
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
1950b57cec5SDimitry Andric   SDValue ExpandConstant(ConstantSDNode *CP);
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric   // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
1980b57cec5SDimitry Andric   bool ExpandNode(SDNode *Node);
1990b57cec5SDimitry Andric   void ConvertNodeToLibcall(SDNode *Node);
2000b57cec5SDimitry Andric   void PromoteNode(SDNode *Node);
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric public:
2030b57cec5SDimitry Andric   // Node replacement helpers
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   void ReplacedNode(SDNode *N) {
2060b57cec5SDimitry Andric     LegalizedNodes.erase(N);
2070b57cec5SDimitry Andric     if (UpdatedNodes)
2080b57cec5SDimitry Andric       UpdatedNodes->insert(N);
2090b57cec5SDimitry Andric   }
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, SDNode *New) {
2120b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2130b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric     assert(Old->getNumValues() == New->getNumValues() &&
2160b57cec5SDimitry Andric            "Replacing one node with another that produces a different number "
2170b57cec5SDimitry Andric            "of values!");
2180b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2190b57cec5SDimitry Andric     if (UpdatedNodes)
2200b57cec5SDimitry Andric       UpdatedNodes->insert(New);
2210b57cec5SDimitry Andric     ReplacedNode(Old);
2220b57cec5SDimitry Andric   }
2230b57cec5SDimitry Andric 
2240b57cec5SDimitry Andric   void ReplaceNode(SDValue Old, SDValue New) {
2250b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2260b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2270b57cec5SDimitry Andric 
2280b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2290b57cec5SDimitry Andric     if (UpdatedNodes)
2300b57cec5SDimitry Andric       UpdatedNodes->insert(New.getNode());
2310b57cec5SDimitry Andric     ReplacedNode(Old.getNode());
2320b57cec5SDimitry Andric   }
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, const SDValue *New) {
2350b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2380b57cec5SDimitry Andric     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
2390b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << (i == 0 ? "     with:      " : "      and:      ");
2400b57cec5SDimitry Andric                  New[i]->dump(&DAG));
2410b57cec5SDimitry Andric       if (UpdatedNodes)
2420b57cec5SDimitry Andric         UpdatedNodes->insert(New[i].getNode());
2430b57cec5SDimitry Andric     }
2440b57cec5SDimitry Andric     ReplacedNode(Old);
2450b57cec5SDimitry Andric   }
2468bcb0991SDimitry Andric 
2478bcb0991SDimitry Andric   void ReplaceNodeWithValue(SDValue Old, SDValue New) {
2488bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2498bcb0991SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2508bcb0991SDimitry Andric 
2518bcb0991SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Old, New);
2528bcb0991SDimitry Andric     if (UpdatedNodes)
2538bcb0991SDimitry Andric       UpdatedNodes->insert(New.getNode());
2548bcb0991SDimitry Andric     ReplacedNode(Old.getNode());
2558bcb0991SDimitry Andric   }
2560b57cec5SDimitry Andric };
2570b57cec5SDimitry Andric 
2580b57cec5SDimitry Andric } // end anonymous namespace
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric /// Return a vector shuffle operation which
2610b57cec5SDimitry Andric /// performs the same shuffle in terms of order or result bytes, but on a type
2620b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type.
2630b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
2640b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
2650b57cec5SDimitry Andric     EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
2660b57cec5SDimitry Andric     ArrayRef<int> Mask) const {
2670b57cec5SDimitry Andric   unsigned NumMaskElts = VT.getVectorNumElements();
2680b57cec5SDimitry Andric   unsigned NumDestElts = NVT.getVectorNumElements();
2690b57cec5SDimitry Andric   unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric   if (NumEltsGrowth == 1)
2740b57cec5SDimitry Andric     return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   SmallVector<int, 8> NewMask;
2770b57cec5SDimitry Andric   for (unsigned i = 0; i != NumMaskElts; ++i) {
2780b57cec5SDimitry Andric     int Idx = Mask[i];
2790b57cec5SDimitry Andric     for (unsigned j = 0; j != NumEltsGrowth; ++j) {
2800b57cec5SDimitry Andric       if (Idx < 0)
2810b57cec5SDimitry Andric         NewMask.push_back(-1);
2820b57cec5SDimitry Andric       else
2830b57cec5SDimitry Andric         NewMask.push_back(Idx * NumEltsGrowth + j);
2840b57cec5SDimitry Andric     }
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric   assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
2870b57cec5SDimitry Andric   assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
2880b57cec5SDimitry Andric   return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric /// Expands the ConstantFP node to an integer constant or
2920b57cec5SDimitry Andric /// a load from the constant pool.
2930b57cec5SDimitry Andric SDValue
2940b57cec5SDimitry Andric SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
2950b57cec5SDimitry Andric   bool Extend = false;
2960b57cec5SDimitry Andric   SDLoc dl(CFP);
2970b57cec5SDimitry Andric 
2980b57cec5SDimitry Andric   // If a FP immediate is precise when represented as a float and if the
2990b57cec5SDimitry Andric   // target can do an extending load from float to double, we put it into
3000b57cec5SDimitry Andric   // the constant pool as a float, even if it's is statically typed as a
3010b57cec5SDimitry Andric   // double.  This shrinks FP constants and canonicalizes them for targets where
3020b57cec5SDimitry Andric   // an FP extending load is the same cost as a normal load (such as on the x87
3030b57cec5SDimitry Andric   // fp stack or PPC FP unit).
3040b57cec5SDimitry Andric   EVT VT = CFP->getValueType(0);
3050b57cec5SDimitry Andric   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
3060b57cec5SDimitry Andric   if (!UseCP) {
3070b57cec5SDimitry Andric     assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
3080b57cec5SDimitry Andric     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
3090b57cec5SDimitry Andric                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
3100b57cec5SDimitry Andric   }
3110b57cec5SDimitry Andric 
3120b57cec5SDimitry Andric   APFloat APF = CFP->getValueAPF();
3130b57cec5SDimitry Andric   EVT OrigVT = VT;
3140b57cec5SDimitry Andric   EVT SVT = VT;
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric   // We don't want to shrink SNaNs. Converting the SNaN back to its real type
3170b57cec5SDimitry Andric   // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
3180b57cec5SDimitry Andric   if (!APF.isSignaling()) {
319bdd1243dSDimitry Andric     while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) {
3200b57cec5SDimitry Andric       SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
3210b57cec5SDimitry Andric       if (ConstantFPSDNode::isValueValidForType(SVT, APF) &&
3220b57cec5SDimitry Andric           // Only do this if the target has a native EXTLOAD instruction from
3230b57cec5SDimitry Andric           // smaller type.
3240b57cec5SDimitry Andric           TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
3250b57cec5SDimitry Andric           TLI.ShouldShrinkFPConstant(OrigVT)) {
3260b57cec5SDimitry Andric         Type *SType = SVT.getTypeForEVT(*DAG.getContext());
3270b57cec5SDimitry Andric         LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
3280b57cec5SDimitry Andric         VT = SVT;
3290b57cec5SDimitry Andric         Extend = true;
3300b57cec5SDimitry Andric       }
3310b57cec5SDimitry Andric     }
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   SDValue CPIdx =
3350b57cec5SDimitry Andric       DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
3365ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3370b57cec5SDimitry Andric   if (Extend) {
3380b57cec5SDimitry Andric     SDValue Result = DAG.getExtLoad(
3390b57cec5SDimitry Andric         ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
3400b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
3410b57cec5SDimitry Andric         Alignment);
3420b57cec5SDimitry Andric     return Result;
3430b57cec5SDimitry Andric   }
3440b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3450b57cec5SDimitry Andric       OrigVT, dl, DAG.getEntryNode(), CPIdx,
3460b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3470b57cec5SDimitry Andric   return Result;
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric /// Expands the Constant node to a load from the constant pool.
3510b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
3520b57cec5SDimitry Andric   SDLoc dl(CP);
3530b57cec5SDimitry Andric   EVT VT = CP->getValueType(0);
3540b57cec5SDimitry Andric   SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
3550b57cec5SDimitry Andric                                       TLI.getPointerTy(DAG.getDataLayout()));
3565ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3570b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3580b57cec5SDimitry Andric       VT, dl, DAG.getEntryNode(), CPIdx,
3590b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3600b57cec5SDimitry Andric   return Result;
3610b57cec5SDimitry Andric }
3620b57cec5SDimitry Andric 
3630b57cec5SDimitry Andric /// Some target cannot handle a variable insertion index for the
3640b57cec5SDimitry Andric /// INSERT_VECTOR_ELT instruction.  In this case, it
3650b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform
3660b57cec5SDimitry Andric /// the insert there, and then read the result back.
3670b57cec5SDimitry Andric SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec,
3680b57cec5SDimitry Andric                                                              SDValue Val,
3690b57cec5SDimitry Andric                                                              SDValue Idx,
3700b57cec5SDimitry Andric                                                              const SDLoc &dl) {
3710b57cec5SDimitry Andric   SDValue Tmp1 = Vec;
3720b57cec5SDimitry Andric   SDValue Tmp2 = Val;
3730b57cec5SDimitry Andric   SDValue Tmp3 = Idx;
3740b57cec5SDimitry Andric 
3750b57cec5SDimitry Andric   // If the target doesn't support this, we have to spill the input vector
3760b57cec5SDimitry Andric   // to a temporary stack slot, update the element, then reload it.  This is
3770b57cec5SDimitry Andric   // badness.  We could also load the value into a vector register (either
3780b57cec5SDimitry Andric   // with a "move to register" or "extload into register" instruction, then
3790b57cec5SDimitry Andric   // permute it into place, if the idx is a constant and if the idx is
3800b57cec5SDimitry Andric   // supported by the target.
3810b57cec5SDimitry Andric   EVT VT    = Tmp1.getValueType();
3820b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
3830b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(VT);
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   // Store the vector.
3880b57cec5SDimitry Andric   SDValue Ch = DAG.getStore(
3890b57cec5SDimitry Andric       DAG.getEntryNode(), dl, Tmp1, StackPtr,
3900b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   SDValue StackPtr2 = TLI.getVectorElementPointer(DAG, StackPtr, VT, Tmp3);
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   // Store the scalar value.
3955ffd83dbSDimitry Andric   Ch = DAG.getTruncStore(
3965ffd83dbSDimitry Andric       Ch, dl, Tmp2, StackPtr2,
3975ffd83dbSDimitry Andric       MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltVT);
3980b57cec5SDimitry Andric   // Load the updated vector.
3990b57cec5SDimitry Andric   return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
4000b57cec5SDimitry Andric                                                DAG.getMachineFunction(), SPFI));
4010b57cec5SDimitry Andric }
4020b57cec5SDimitry Andric 
4030b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
4040b57cec5SDimitry Andric                                                       SDValue Idx,
4050b57cec5SDimitry Andric                                                       const SDLoc &dl) {
4060b57cec5SDimitry Andric   if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
4070b57cec5SDimitry Andric     // SCALAR_TO_VECTOR requires that the type of the value being inserted
4080b57cec5SDimitry Andric     // match the element type of the vector being created, except for
4090b57cec5SDimitry Andric     // integers in which case the inserted value can be over width.
4100b57cec5SDimitry Andric     EVT EltVT = Vec.getValueType().getVectorElementType();
4110b57cec5SDimitry Andric     if (Val.getValueType() == EltVT ||
4120b57cec5SDimitry Andric         (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
4130b57cec5SDimitry Andric       SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4140b57cec5SDimitry Andric                                   Vec.getValueType(), Val);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric       unsigned NumElts = Vec.getValueType().getVectorNumElements();
4170b57cec5SDimitry Andric       // We generate a shuffle of InVec and ScVec, so the shuffle mask
4180b57cec5SDimitry Andric       // should be 0,1,2,3,4,5... with the appropriate element replaced with
4190b57cec5SDimitry Andric       // elt 0 of the RHS.
4200b57cec5SDimitry Andric       SmallVector<int, 8> ShufOps;
4210b57cec5SDimitry Andric       for (unsigned i = 0; i != NumElts; ++i)
4220b57cec5SDimitry Andric         ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
4230b57cec5SDimitry Andric 
4240b57cec5SDimitry Andric       return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
4250b57cec5SDimitry Andric     }
4260b57cec5SDimitry Andric   }
4270b57cec5SDimitry Andric   return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
4280b57cec5SDimitry Andric }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
431480093f4SDimitry Andric   if (!ISD::isNormalStore(ST))
432480093f4SDimitry Andric     return SDValue();
433480093f4SDimitry Andric 
4340b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Optimizing float store operations\n");
4350b57cec5SDimitry Andric   // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4360b57cec5SDimitry Andric   // FIXME: move this to the DAG Combiner!  Note that we can't regress due
4370b57cec5SDimitry Andric   // to phase ordering between legalized code and the dag combiner.  This
4380b57cec5SDimitry Andric   // probably means that we need to integrate dag combiner and legalizer
4390b57cec5SDimitry Andric   // together.
4400b57cec5SDimitry Andric   // We generally can't do this one for long doubles.
4410b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
4420b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
443e8d8bef9SDimitry Andric   SDValue Value = ST->getValue();
4440b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
4450b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
4460b57cec5SDimitry Andric   SDLoc dl(ST);
447e8d8bef9SDimitry Andric 
448e8d8bef9SDimitry Andric   // Don't optimise TargetConstantFP
449e8d8bef9SDimitry Andric   if (Value.getOpcode() == ISD::TargetConstantFP)
450e8d8bef9SDimitry Andric     return SDValue();
451e8d8bef9SDimitry Andric 
452e8d8bef9SDimitry Andric   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
4530b57cec5SDimitry Andric     if (CFP->getValueType(0) == MVT::f32 &&
4540b57cec5SDimitry Andric         TLI.isTypeLegal(MVT::i32)) {
4550b57cec5SDimitry Andric       SDValue Con = DAG.getConstant(CFP->getValueAPF().
4560b57cec5SDimitry Andric                                       bitcastToAPInt().zextOrTrunc(32),
4570b57cec5SDimitry Andric                                     SDLoc(CFP), MVT::i32);
4585ffd83dbSDimitry Andric       return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4595ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4600b57cec5SDimitry Andric     }
4610b57cec5SDimitry Andric 
4620b57cec5SDimitry Andric     if (CFP->getValueType(0) == MVT::f64) {
4630b57cec5SDimitry Andric       // If this target supports 64-bit registers, do a single 64-bit store.
4640b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i64)) {
4650b57cec5SDimitry Andric         SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
4660b57cec5SDimitry Andric                                       zextOrTrunc(64), SDLoc(CFP), MVT::i64);
4670b57cec5SDimitry Andric         return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4685ffd83dbSDimitry Andric                             ST->getOriginalAlign(), MMOFlags, AAInfo);
4690b57cec5SDimitry Andric       }
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
4720b57cec5SDimitry Andric         // Otherwise, if the target supports 32-bit registers, use 2 32-bit
4730b57cec5SDimitry Andric         // stores.  If the target supports neither 32- nor 64-bits, this
4740b57cec5SDimitry Andric         // xform is certainly not worth it.
4750b57cec5SDimitry Andric         const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
4760b57cec5SDimitry Andric         SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
4770b57cec5SDimitry Andric         SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
4780b57cec5SDimitry Andric         if (DAG.getDataLayout().isBigEndian())
4790b57cec5SDimitry Andric           std::swap(Lo, Hi);
4800b57cec5SDimitry Andric 
4815ffd83dbSDimitry Andric         Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(),
4825ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
483e8d8bef9SDimitry Andric         Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(4), dl);
4840b57cec5SDimitry Andric         Hi = DAG.getStore(Chain, dl, Hi, Ptr,
4850b57cec5SDimitry Andric                           ST->getPointerInfo().getWithOffset(4),
4865ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4870b57cec5SDimitry Andric 
4880b57cec5SDimitry Andric         return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4890b57cec5SDimitry Andric       }
4900b57cec5SDimitry Andric     }
4910b57cec5SDimitry Andric   }
492e8d8bef9SDimitry Andric   return SDValue();
4930b57cec5SDimitry Andric }
4940b57cec5SDimitry Andric 
4950b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
4960b57cec5SDimitry Andric   StoreSDNode *ST = cast<StoreSDNode>(Node);
4970b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
4980b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
4990b57cec5SDimitry Andric   SDLoc dl(Node);
5000b57cec5SDimitry Andric 
5010b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
5020b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   if (!ST->isTruncatingStore()) {
5050b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing store operation\n");
5060b57cec5SDimitry Andric     if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
5070b57cec5SDimitry Andric       ReplaceNode(ST, OptStore);
5080b57cec5SDimitry Andric       return;
5090b57cec5SDimitry Andric     }
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric     SDValue Value = ST->getValue();
5120b57cec5SDimitry Andric     MVT VT = Value.getSimpleValueType();
5130b57cec5SDimitry Andric     switch (TLI.getOperationAction(ISD::STORE, VT)) {
5140b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
5150b57cec5SDimitry Andric     case TargetLowering::Legal: {
5160b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
5170b57cec5SDimitry Andric       // expand it.
5180b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
5190b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
5208bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
5210b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
5220b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n");
5230b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
5240b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
5250b57cec5SDimitry Andric       } else
5260b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Legal store\n");
5270b57cec5SDimitry Andric       break;
5280b57cec5SDimitry Andric     }
5290b57cec5SDimitry Andric     case TargetLowering::Custom: {
5300b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom lowering\n");
5310b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
5320b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
5330b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
5340b57cec5SDimitry Andric       return;
5350b57cec5SDimitry Andric     }
5360b57cec5SDimitry Andric     case TargetLowering::Promote: {
5370b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
5380b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
5390b57cec5SDimitry Andric              "Can only promote stores to same size type");
5400b57cec5SDimitry Andric       Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
5415ffd83dbSDimitry Andric       SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5425ffd83dbSDimitry Andric                                     ST->getOriginalAlign(), MMOFlags, AAInfo);
5430b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
5440b57cec5SDimitry Andric       break;
5450b57cec5SDimitry Andric     }
5460b57cec5SDimitry Andric     }
5470b57cec5SDimitry Andric     return;
5480b57cec5SDimitry Andric   }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n");
5510b57cec5SDimitry Andric   SDValue Value = ST->getValue();
5520b57cec5SDimitry Andric   EVT StVT = ST->getMemoryVT();
553e8d8bef9SDimitry Andric   TypeSize StWidth = StVT.getSizeInBits();
554e8d8bef9SDimitry Andric   TypeSize StSize = StVT.getStoreSizeInBits();
5550b57cec5SDimitry Andric   auto &DL = DAG.getDataLayout();
5560b57cec5SDimitry Andric 
557e8d8bef9SDimitry Andric   if (StWidth != StSize) {
5580b57cec5SDimitry Andric     // Promote to a byte-sized store with upper bits zero if not
5590b57cec5SDimitry Andric     // storing an integral number of bytes.  For example, promote
5600b57cec5SDimitry Andric     // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
561bdd1243dSDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue());
5620b57cec5SDimitry Andric     Value = DAG.getZeroExtendInReg(Value, dl, StVT);
5630b57cec5SDimitry Andric     SDValue Result =
5640b57cec5SDimitry Andric         DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
5655ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
5660b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
567bdd1243dSDimitry Andric   } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) {
5680b57cec5SDimitry Andric     // If not storing a power-of-2 number of bits, expand as two stores.
5690b57cec5SDimitry Andric     assert(!StVT.isVector() && "Unsupported truncstore!");
570bdd1243dSDimitry Andric     unsigned StWidthBits = StWidth.getFixedValue();
571e8d8bef9SDimitry Andric     unsigned LogStWidth = Log2_32(StWidthBits);
5720b57cec5SDimitry Andric     assert(LogStWidth < 32);
5730b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogStWidth;
574e8d8bef9SDimitry Andric     assert(RoundWidth < StWidthBits);
575e8d8bef9SDimitry Andric     unsigned ExtraWidth = StWidthBits - RoundWidth;
5760b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
5770b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
5780b57cec5SDimitry Andric            "Store size not an integral number of bytes!");
5790b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
5800b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
5810b57cec5SDimitry Andric     SDValue Lo, Hi;
5820b57cec5SDimitry Andric     unsigned IncrementSize;
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
5850b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
5860b57cec5SDimitry Andric       // Store the bottom RoundWidth bits.
5870b57cec5SDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5885ffd83dbSDimitry Andric                              RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
5910b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
592e8d8bef9SDimitry Andric       Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
5930b57cec5SDimitry Andric       Hi = DAG.getNode(
5940b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
5950b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
5960b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
5975ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
5985ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
5995ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
6000b57cec5SDimitry Andric     } else {
6010b57cec5SDimitry Andric       // Big endian - avoid unaligned stores.
6020b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
6030b57cec5SDimitry Andric       // Store the top RoundWidth bits.
6040b57cec5SDimitry Andric       Hi = DAG.getNode(
6050b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
6060b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
6070b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
6085ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT,
6095ffd83dbSDimitry Andric                              ST->getOriginalAlign(), MMOFlags, AAInfo);
6100b57cec5SDimitry Andric 
6110b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
6120b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
6130b57cec5SDimitry Andric       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
6140b57cec5SDimitry Andric                         DAG.getConstant(IncrementSize, dl,
6150b57cec5SDimitry Andric                                         Ptr.getValueType()));
6165ffd83dbSDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
6175ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
6185ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
6190b57cec5SDimitry Andric     }
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric     // The order of the stores doesn't matter.
6220b57cec5SDimitry Andric     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6230b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
6240b57cec5SDimitry Andric   } else {
6250b57cec5SDimitry Andric     switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
6260b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6270b57cec5SDimitry Andric     case TargetLowering::Legal: {
6280b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
6290b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
6300b57cec5SDimitry Andric       // expand it.
6318bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6320b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
6330b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
6340b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
6350b57cec5SDimitry Andric       }
6360b57cec5SDimitry Andric       break;
6370b57cec5SDimitry Andric     }
6380b57cec5SDimitry Andric     case TargetLowering::Custom: {
6390b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
6400b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
6410b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
6420b57cec5SDimitry Andric       return;
6430b57cec5SDimitry Andric     }
6440b57cec5SDimitry Andric     case TargetLowering::Expand:
6450b57cec5SDimitry Andric       assert(!StVT.isVector() &&
6460b57cec5SDimitry Andric              "Vector Stores are handled in LegalizeVectorOps");
6470b57cec5SDimitry Andric 
6480b57cec5SDimitry Andric       SDValue Result;
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric       // TRUNCSTORE:i16 i32 -> STORE i16
6510b57cec5SDimitry Andric       if (TLI.isTypeLegal(StVT)) {
6520b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
6530b57cec5SDimitry Andric         Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
6545ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6550b57cec5SDimitry Andric       } else {
6560b57cec5SDimitry Andric         // The in-memory type isn't legal. Truncate to the type it would promote
6570b57cec5SDimitry Andric         // to, and then do a truncstore.
6580b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl,
6590b57cec5SDimitry Andric                             TLI.getTypeToTransformTo(*DAG.getContext(), StVT),
6600b57cec5SDimitry Andric                             Value);
6615ffd83dbSDimitry Andric         Result =
6625ffd83dbSDimitry Andric             DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT,
6635ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6640b57cec5SDimitry Andric       }
6650b57cec5SDimitry Andric 
6660b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
6670b57cec5SDimitry Andric       break;
6680b57cec5SDimitry Andric     }
6690b57cec5SDimitry Andric   }
6700b57cec5SDimitry Andric }
6710b57cec5SDimitry Andric 
6720b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
6730b57cec5SDimitry Andric   LoadSDNode *LD = cast<LoadSDNode>(Node);
6740b57cec5SDimitry Andric   SDValue Chain = LD->getChain();  // The chain.
6750b57cec5SDimitry Andric   SDValue Ptr = LD->getBasePtr();  // The base pointer.
6760b57cec5SDimitry Andric   SDValue Value;                   // The value returned by the load op.
6770b57cec5SDimitry Andric   SDLoc dl(Node);
6780b57cec5SDimitry Andric 
6790b57cec5SDimitry Andric   ISD::LoadExtType ExtType = LD->getExtensionType();
6800b57cec5SDimitry Andric   if (ExtType == ISD::NON_EXTLOAD) {
6810b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n");
6820b57cec5SDimitry Andric     MVT VT = Node->getSimpleValueType(0);
6830b57cec5SDimitry Andric     SDValue RVal = SDValue(Node, 0);
6840b57cec5SDimitry Andric     SDValue RChain = SDValue(Node, 1);
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
6870b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6880b57cec5SDimitry Andric     case TargetLowering::Legal: {
6890b57cec5SDimitry Andric       EVT MemVT = LD->getMemoryVT();
6900b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
6910b57cec5SDimitry Andric       // If this is an unaligned load and the target doesn't support it,
6920b57cec5SDimitry Andric       // expand it.
6938bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6940b57cec5SDimitry Andric                                               *LD->getMemOperand())) {
6950b57cec5SDimitry Andric         std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
6960b57cec5SDimitry Andric       }
6970b57cec5SDimitry Andric       break;
6980b57cec5SDimitry Andric     }
6990b57cec5SDimitry Andric     case TargetLowering::Custom:
7000b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
7010b57cec5SDimitry Andric         RVal = Res;
7020b57cec5SDimitry Andric         RChain = Res.getValue(1);
7030b57cec5SDimitry Andric       }
7040b57cec5SDimitry Andric       break;
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric     case TargetLowering::Promote: {
7070b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
7080b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
7090b57cec5SDimitry Andric              "Can only promote loads to same size type");
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric       SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
7120b57cec5SDimitry Andric       RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
7130b57cec5SDimitry Andric       RChain = Res.getValue(1);
7140b57cec5SDimitry Andric       break;
7150b57cec5SDimitry Andric     }
7160b57cec5SDimitry Andric     }
7170b57cec5SDimitry Andric     if (RChain.getNode() != Node) {
7180b57cec5SDimitry Andric       assert(RVal.getNode() != Node && "Load must be completely replaced");
7190b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
7200b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
7210b57cec5SDimitry Andric       if (UpdatedNodes) {
7220b57cec5SDimitry Andric         UpdatedNodes->insert(RVal.getNode());
7230b57cec5SDimitry Andric         UpdatedNodes->insert(RChain.getNode());
7240b57cec5SDimitry Andric       }
7250b57cec5SDimitry Andric       ReplacedNode(Node);
7260b57cec5SDimitry Andric     }
7270b57cec5SDimitry Andric     return;
7280b57cec5SDimitry Andric   }
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n");
7310b57cec5SDimitry Andric   EVT SrcVT = LD->getMemoryVT();
732e8d8bef9SDimitry Andric   TypeSize SrcWidth = SrcVT.getSizeInBits();
7330b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
7340b57cec5SDimitry Andric   AAMDNodes AAInfo = LD->getAAInfo();
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric   if (SrcWidth != SrcVT.getStoreSizeInBits() &&
7370b57cec5SDimitry Andric       // Some targets pretend to have an i1 loading operation, and actually
7380b57cec5SDimitry Andric       // load an i8.  This trick is correct for ZEXTLOAD because the top 7
7390b57cec5SDimitry Andric       // bits are guaranteed to be zero; it helps the optimizers understand
7400b57cec5SDimitry Andric       // that these bits are zero.  It is also useful for EXTLOAD, since it
7410b57cec5SDimitry Andric       // tells the optimizers that those bits are undefined.  It would be
7420b57cec5SDimitry Andric       // nice to have an effective generic way of getting these benefits...
7430b57cec5SDimitry Andric       // Until such a way is found, don't insist on promoting i1 here.
7440b57cec5SDimitry Andric       (SrcVT != MVT::i1 ||
7450b57cec5SDimitry Andric        TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
7460b57cec5SDimitry Andric          TargetLowering::Promote)) {
7470b57cec5SDimitry Andric     // Promote to a byte-sized load if not loading an integral number of
7480b57cec5SDimitry Andric     // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
7490b57cec5SDimitry Andric     unsigned NewWidth = SrcVT.getStoreSizeInBits();
7500b57cec5SDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
7510b57cec5SDimitry Andric     SDValue Ch;
7520b57cec5SDimitry Andric 
7530b57cec5SDimitry Andric     // The extra bits are guaranteed to be zero, since we stored them that
7540b57cec5SDimitry Andric     // way.  A zext load from NVT thus automatically gives zext from SrcVT.
7550b57cec5SDimitry Andric 
7560b57cec5SDimitry Andric     ISD::LoadExtType NewExtType =
7570b57cec5SDimitry Andric       ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7580b57cec5SDimitry Andric 
7595ffd83dbSDimitry Andric     SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
7605ffd83dbSDimitry Andric                                     Chain, Ptr, LD->getPointerInfo(), NVT,
7615ffd83dbSDimitry Andric                                     LD->getOriginalAlign(), MMOFlags, AAInfo);
7620b57cec5SDimitry Andric 
7630b57cec5SDimitry Andric     Ch = Result.getValue(1); // The chain.
7640b57cec5SDimitry Andric 
7650b57cec5SDimitry Andric     if (ExtType == ISD::SEXTLOAD)
7660b57cec5SDimitry Andric       // Having the top bits zero doesn't help when sign extending.
7670b57cec5SDimitry Andric       Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
7680b57cec5SDimitry Andric                            Result.getValueType(),
7690b57cec5SDimitry Andric                            Result, DAG.getValueType(SrcVT));
7700b57cec5SDimitry Andric     else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
7710b57cec5SDimitry Andric       // All the top bits are guaranteed to be zero - inform the optimizers.
7720b57cec5SDimitry Andric       Result = DAG.getNode(ISD::AssertZext, dl,
7730b57cec5SDimitry Andric                            Result.getValueType(), Result,
7740b57cec5SDimitry Andric                            DAG.getValueType(SrcVT));
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric     Value = Result;
7770b57cec5SDimitry Andric     Chain = Ch;
778bdd1243dSDimitry Andric   } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) {
7790b57cec5SDimitry Andric     // If not loading a power-of-2 number of bits, expand as two loads.
7800b57cec5SDimitry Andric     assert(!SrcVT.isVector() && "Unsupported extload!");
781bdd1243dSDimitry Andric     unsigned SrcWidthBits = SrcWidth.getFixedValue();
782e8d8bef9SDimitry Andric     unsigned LogSrcWidth = Log2_32(SrcWidthBits);
7830b57cec5SDimitry Andric     assert(LogSrcWidth < 32);
7840b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogSrcWidth;
785e8d8bef9SDimitry Andric     assert(RoundWidth < SrcWidthBits);
786e8d8bef9SDimitry Andric     unsigned ExtraWidth = SrcWidthBits - RoundWidth;
7870b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
7880b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
7890b57cec5SDimitry Andric            "Load size not an integral number of bytes!");
7900b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
7910b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
7920b57cec5SDimitry Andric     SDValue Lo, Hi, Ch;
7930b57cec5SDimitry Andric     unsigned IncrementSize;
7940b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
7950b57cec5SDimitry Andric 
7960b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
7970b57cec5SDimitry Andric       // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
7980b57cec5SDimitry Andric       // Load the bottom RoundWidth bits.
7990b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
8005ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
8015ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
8040b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
805e8d8bef9SDimitry Andric       Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
8060b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
8070b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
8085ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
8110b57cec5SDimitry Andric       // the other one.
8120b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
8130b57cec5SDimitry Andric                        Hi.getValue(1));
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric       // Move the top bits to the right place.
8160b57cec5SDimitry Andric       Hi = DAG.getNode(
8170b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8180b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
8190b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric       // Join the hi and lo parts.
8220b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8230b57cec5SDimitry Andric     } else {
8240b57cec5SDimitry Andric       // Big endian - avoid unaligned loads.
8250b57cec5SDimitry Andric       // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
8260b57cec5SDimitry Andric       // Load the top RoundWidth bits.
8270b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
8285ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
8295ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
8300b57cec5SDimitry Andric 
8310b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
8320b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
833e8d8bef9SDimitry Andric       Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl);
8340b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
8350b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
8365ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
8390b57cec5SDimitry Andric       // the other one.
8400b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
8410b57cec5SDimitry Andric                        Hi.getValue(1));
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric       // Move the top bits to the right place.
8440b57cec5SDimitry Andric       Hi = DAG.getNode(
8450b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8460b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
8470b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric       // Join the hi and lo parts.
8500b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8510b57cec5SDimitry Andric     }
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric     Chain = Ch;
8540b57cec5SDimitry Andric   } else {
8550b57cec5SDimitry Andric     bool isCustom = false;
8560b57cec5SDimitry Andric     switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
8570b57cec5SDimitry Andric                                  SrcVT.getSimpleVT())) {
8580b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
8590b57cec5SDimitry Andric     case TargetLowering::Custom:
8600b57cec5SDimitry Andric       isCustom = true;
861bdd1243dSDimitry Andric       [[fallthrough]];
8620b57cec5SDimitry Andric     case TargetLowering::Legal:
8630b57cec5SDimitry Andric       Value = SDValue(Node, 0);
8640b57cec5SDimitry Andric       Chain = SDValue(Node, 1);
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric       if (isCustom) {
8670b57cec5SDimitry Andric         if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
8680b57cec5SDimitry Andric           Value = Res;
8690b57cec5SDimitry Andric           Chain = Res.getValue(1);
8700b57cec5SDimitry Andric         }
8710b57cec5SDimitry Andric       } else {
8720b57cec5SDimitry Andric         // If this is an unaligned load and the target doesn't support it,
8730b57cec5SDimitry Andric         // expand it.
8740b57cec5SDimitry Andric         EVT MemVT = LD->getMemoryVT();
8750b57cec5SDimitry Andric         const DataLayout &DL = DAG.getDataLayout();
8760b57cec5SDimitry Andric         if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT,
8770b57cec5SDimitry Andric                                     *LD->getMemOperand())) {
8780b57cec5SDimitry Andric           std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
8790b57cec5SDimitry Andric         }
8800b57cec5SDimitry Andric       }
8810b57cec5SDimitry Andric       break;
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric     case TargetLowering::Expand: {
8840b57cec5SDimitry Andric       EVT DestVT = Node->getValueType(0);
8850b57cec5SDimitry Andric       if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
8860b57cec5SDimitry Andric         // If the source type is not legal, see if there is a legal extload to
8870b57cec5SDimitry Andric         // an intermediate type that we can then extend further.
8880b57cec5SDimitry Andric         EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
889*06c3fb27SDimitry Andric         if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) &&
890*06c3fb27SDimitry Andric             (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
891*06c3fb27SDimitry Andric              TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) {
8920b57cec5SDimitry Andric           // If we are loading a legal type, this is a non-extload followed by a
8930b57cec5SDimitry Andric           // full extend.
8940b57cec5SDimitry Andric           ISD::LoadExtType MidExtType =
8950b57cec5SDimitry Andric               (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric           SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
8980b57cec5SDimitry Andric                                         SrcVT, LD->getMemOperand());
8990b57cec5SDimitry Andric           unsigned ExtendOp =
9000b57cec5SDimitry Andric               ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
9010b57cec5SDimitry Andric           Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
9020b57cec5SDimitry Andric           Chain = Load.getValue(1);
9030b57cec5SDimitry Andric           break;
9040b57cec5SDimitry Andric         }
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric         // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
9070b57cec5SDimitry Andric         // normal undefined upper bits behavior to allow using an in-reg extend
9080b57cec5SDimitry Andric         // with the illegal FP type, so load as an integer and do the
9090b57cec5SDimitry Andric         // from-integer conversion.
9100b57cec5SDimitry Andric         if (SrcVT.getScalarType() == MVT::f16) {
9110b57cec5SDimitry Andric           EVT ISrcVT = SrcVT.changeTypeToInteger();
9120b57cec5SDimitry Andric           EVT IDestVT = DestVT.changeTypeToInteger();
9138bcb0991SDimitry Andric           EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
9140b57cec5SDimitry Andric 
9158bcb0991SDimitry Andric           SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain,
9168bcb0991SDimitry Andric                                           Ptr, ISrcVT, LD->getMemOperand());
9170b57cec5SDimitry Andric           Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
9180b57cec5SDimitry Andric           Chain = Result.getValue(1);
9190b57cec5SDimitry Andric           break;
9200b57cec5SDimitry Andric         }
9210b57cec5SDimitry Andric       }
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric       assert(!SrcVT.isVector() &&
9240b57cec5SDimitry Andric              "Vector Loads are handled in LegalizeVectorOps");
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric       // FIXME: This does not work for vectors on most targets.  Sign-
9270b57cec5SDimitry Andric       // and zero-extend operations are currently folded into extending
9280b57cec5SDimitry Andric       // loads, whether they are legal or not, and then we end up here
9290b57cec5SDimitry Andric       // without any support for legalizing them.
9300b57cec5SDimitry Andric       assert(ExtType != ISD::EXTLOAD &&
9310b57cec5SDimitry Andric              "EXTLOAD should always be supported!");
9320b57cec5SDimitry Andric       // Turn the unsupported load into an EXTLOAD followed by an
9330b57cec5SDimitry Andric       // explicit zero/sign extend inreg.
9340b57cec5SDimitry Andric       SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
9350b57cec5SDimitry Andric                                       Node->getValueType(0),
9360b57cec5SDimitry Andric                                       Chain, Ptr, SrcVT,
9370b57cec5SDimitry Andric                                       LD->getMemOperand());
9380b57cec5SDimitry Andric       SDValue ValRes;
9390b57cec5SDimitry Andric       if (ExtType == ISD::SEXTLOAD)
9400b57cec5SDimitry Andric         ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
9410b57cec5SDimitry Andric                              Result.getValueType(),
9420b57cec5SDimitry Andric                              Result, DAG.getValueType(SrcVT));
9430b57cec5SDimitry Andric       else
9445ffd83dbSDimitry Andric         ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
9450b57cec5SDimitry Andric       Value = ValRes;
9460b57cec5SDimitry Andric       Chain = Result.getValue(1);
9470b57cec5SDimitry Andric       break;
9480b57cec5SDimitry Andric     }
9490b57cec5SDimitry Andric     }
9500b57cec5SDimitry Andric   }
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   // Since loads produce two values, make sure to remember that we legalized
9530b57cec5SDimitry Andric   // both of them.
9540b57cec5SDimitry Andric   if (Chain.getNode() != Node) {
9550b57cec5SDimitry Andric     assert(Value.getNode() != Node && "Load must be completely replaced");
9560b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
9570b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
9580b57cec5SDimitry Andric     if (UpdatedNodes) {
9590b57cec5SDimitry Andric       UpdatedNodes->insert(Value.getNode());
9600b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
9610b57cec5SDimitry Andric     }
9620b57cec5SDimitry Andric     ReplacedNode(Node);
9630b57cec5SDimitry Andric   }
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric /// Return a legal replacement for the given operation, with all legal operands.
9670b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
9680b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   // Allow illegal target nodes and illegal registers.
9710b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::TargetConstant ||
9720b57cec5SDimitry Andric       Node->getOpcode() == ISD::Register)
9730b57cec5SDimitry Andric     return;
9740b57cec5SDimitry Andric 
9750b57cec5SDimitry Andric #ifndef NDEBUG
9760b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
9778bcb0991SDimitry Andric     assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
9788bcb0991SDimitry Andric              TargetLowering::TypeLegal &&
9790b57cec5SDimitry Andric            "Unexpected illegal type!");
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values())
9820b57cec5SDimitry Andric     assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
9830b57cec5SDimitry Andric               TargetLowering::TypeLegal ||
9840b57cec5SDimitry Andric             Op.getOpcode() == ISD::TargetConstant ||
9850b57cec5SDimitry Andric             Op.getOpcode() == ISD::Register) &&
9860b57cec5SDimitry Andric             "Unexpected illegal type!");
9870b57cec5SDimitry Andric #endif
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   // Figure out the correct action; the way to query this varies by opcode
9900b57cec5SDimitry Andric   TargetLowering::LegalizeAction Action = TargetLowering::Legal;
9910b57cec5SDimitry Andric   bool SimpleFinishLegalizing = true;
9920b57cec5SDimitry Andric   switch (Node->getOpcode()) {
9930b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
9940b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
9950b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
9960b57cec5SDimitry Andric   case ISD::STACKSAVE:
9970b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
9980b57cec5SDimitry Andric     break;
9990b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
10000b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10010b57cec5SDimitry Andric                                     Node->getValueType(0));
10020b57cec5SDimitry Andric     break;
10030b57cec5SDimitry Andric   case ISD::VAARG:
10040b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10050b57cec5SDimitry Andric                                     Node->getValueType(0));
10060b57cec5SDimitry Andric     if (Action != TargetLowering::Promote)
10070b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
10080b57cec5SDimitry Andric     break;
1009*06c3fb27SDimitry Andric   case ISD::SET_FPENV:
1010*06c3fb27SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1011*06c3fb27SDimitry Andric                                     Node->getOperand(1).getValueType());
1012*06c3fb27SDimitry Andric     break;
10130b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
101481ad6265SDimitry Andric   case ISD::FP_TO_BF16:
10150b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
10160b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
10170b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
10180b57cec5SDimitry Andric   case ISD::LROUND:
10190b57cec5SDimitry Andric   case ISD::LLROUND:
10200b57cec5SDimitry Andric   case ISD::LRINT:
10210b57cec5SDimitry Andric   case ISD::LLRINT:
10220b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10230b57cec5SDimitry Andric                                     Node->getOperand(0).getValueType());
10240b57cec5SDimitry Andric     break;
10255ffd83dbSDimitry Andric   case ISD::STRICT_FP_TO_FP16:
1026480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
1027480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
1028480093f4SDimitry Andric   case ISD::STRICT_LRINT:
1029480093f4SDimitry Andric   case ISD::STRICT_LLRINT:
1030480093f4SDimitry Andric   case ISD::STRICT_LROUND:
1031480093f4SDimitry Andric   case ISD::STRICT_LLROUND:
1032480093f4SDimitry Andric     // These pseudo-ops are the same as the other STRICT_ ops except
1033480093f4SDimitry Andric     // they are registered with setOperationAction() using the input type
1034480093f4SDimitry Andric     // instead of the output type.
1035480093f4SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1036480093f4SDimitry Andric                                     Node->getOperand(1).getValueType());
1037480093f4SDimitry Andric     break;
10380b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
10390b57cec5SDimitry Andric     EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
10400b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
10410b57cec5SDimitry Andric     break;
10420b57cec5SDimitry Andric   }
10430b57cec5SDimitry Andric   case ISD::ATOMIC_STORE:
10440b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10450b57cec5SDimitry Andric                                     Node->getOperand(2).getValueType());
10460b57cec5SDimitry Andric     break;
10470b57cec5SDimitry Andric   case ISD::SELECT_CC:
1048480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
1049480093f4SDimitry Andric   case ISD::STRICT_FSETCCS:
10500b57cec5SDimitry Andric   case ISD::SETCC:
1051bdd1243dSDimitry Andric   case ISD::SETCCCARRY:
105281ad6265SDimitry Andric   case ISD::VP_SETCC:
10530b57cec5SDimitry Andric   case ISD::BR_CC: {
105481ad6265SDimitry Andric     unsigned Opc = Node->getOpcode();
105581ad6265SDimitry Andric     unsigned CCOperand = Opc == ISD::SELECT_CC                         ? 4
105681ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCC                   ? 3
105781ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCCS                  ? 3
1058bdd1243dSDimitry Andric                          : Opc == ISD::SETCCCARRY                      ? 3
105981ad6265SDimitry Andric                          : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2
106081ad6265SDimitry Andric                                                                        : 1;
106181ad6265SDimitry Andric     unsigned CompareOperand = Opc == ISD::BR_CC            ? 2
106281ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCC  ? 1
106381ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCCS ? 1
106481ad6265SDimitry Andric                                                            : 0;
10650b57cec5SDimitry Andric     MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
10660b57cec5SDimitry Andric     ISD::CondCode CCCode =
10670b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
10680b57cec5SDimitry Andric     Action = TLI.getCondCodeAction(CCCode, OpVT);
10690b57cec5SDimitry Andric     if (Action == TargetLowering::Legal) {
10700b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::SELECT_CC)
10710b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(),
10720b57cec5SDimitry Andric                                         Node->getValueType(0));
10730b57cec5SDimitry Andric       else
10740b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
10750b57cec5SDimitry Andric     }
10760b57cec5SDimitry Andric     break;
10770b57cec5SDimitry Andric   }
10780b57cec5SDimitry Andric   case ISD::LOAD:
10790b57cec5SDimitry Andric   case ISD::STORE:
10800b57cec5SDimitry Andric     // FIXME: Model these properly.  LOAD and STORE are complicated, and
10810b57cec5SDimitry Andric     // STORE expects the unlegalized operand in some cases.
10820b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10830b57cec5SDimitry Andric     break;
10840b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
10850b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
10860b57cec5SDimitry Andric     // FIXME: This shouldn't be necessary.  These nodes have special properties
10870b57cec5SDimitry Andric     // dealing with the recursive nature of legalization.  Removing this
10880b57cec5SDimitry Andric     // special case should be done as part of making LegalizeDAG non-recursive.
10890b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10900b57cec5SDimitry Andric     break;
10910b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT:
1092bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
10930b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
10940b57cec5SDimitry Andric   case ISD::EH_RETURN:
10950b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
10960b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA:
10970b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
10980b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
10990b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETUP_DISPATCH:
11000b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
11010b57cec5SDimitry Andric     // they should actually be expanded.
11020b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11030b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
11040b57cec5SDimitry Andric       Action = TargetLowering::Expand;
11050b57cec5SDimitry Andric     break;
11060b57cec5SDimitry Andric   case ISD::INIT_TRAMPOLINE:
11070b57cec5SDimitry Andric   case ISD::ADJUST_TRAMPOLINE:
11080b57cec5SDimitry Andric   case ISD::FRAMEADDR:
11090b57cec5SDimitry Andric   case ISD::RETURNADDR:
11100b57cec5SDimitry Andric   case ISD::ADDROFRETURNADDR:
11110b57cec5SDimitry Andric   case ISD::SPONENTRY:
11120b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
11130b57cec5SDimitry Andric     // they should actually be custom-lowered.
11140b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11150b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
11160b57cec5SDimitry Andric       Action = TargetLowering::Custom;
11170b57cec5SDimitry Andric     break;
11180b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
11190b57cec5SDimitry Andric     // READCYCLECOUNTER returns an i64, even if type legalization might have
11200b57cec5SDimitry Andric     // expanded that to several smaller types.
11210b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
11220b57cec5SDimitry Andric     break;
11230b57cec5SDimitry Andric   case ISD::READ_REGISTER:
11240b57cec5SDimitry Andric   case ISD::WRITE_REGISTER:
11250b57cec5SDimitry Andric     // Named register is legal in the DAG, but blocked by register name
11260b57cec5SDimitry Andric     // selection if not implemented by target (to chose the correct register)
11270b57cec5SDimitry Andric     // They'll be converted to Copy(To/From)Reg.
11280b57cec5SDimitry Andric     Action = TargetLowering::Legal;
11290b57cec5SDimitry Andric     break;
1130e8d8bef9SDimitry Andric   case ISD::UBSANTRAP:
1131e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1132e8d8bef9SDimitry Andric     if (Action == TargetLowering::Expand) {
1133e8d8bef9SDimitry Andric       // replace ISD::UBSANTRAP with ISD::TRAP
1134e8d8bef9SDimitry Andric       SDValue NewVal;
1135e8d8bef9SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1136e8d8bef9SDimitry Andric                            Node->getOperand(0));
1137e8d8bef9SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
1138e8d8bef9SDimitry Andric       LegalizeOp(NewVal.getNode());
1139e8d8bef9SDimitry Andric       return;
1140e8d8bef9SDimitry Andric     }
1141e8d8bef9SDimitry Andric     break;
11420b57cec5SDimitry Andric   case ISD::DEBUGTRAP:
11430b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11440b57cec5SDimitry Andric     if (Action == TargetLowering::Expand) {
11450b57cec5SDimitry Andric       // replace ISD::DEBUGTRAP with ISD::TRAP
11460b57cec5SDimitry Andric       SDValue NewVal;
11470b57cec5SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
11480b57cec5SDimitry Andric                            Node->getOperand(0));
11490b57cec5SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
11500b57cec5SDimitry Andric       LegalizeOp(NewVal.getNode());
11510b57cec5SDimitry Andric       return;
11520b57cec5SDimitry Andric     }
11530b57cec5SDimitry Andric     break;
11540b57cec5SDimitry Andric   case ISD::SADDSAT:
11550b57cec5SDimitry Andric   case ISD::UADDSAT:
11560b57cec5SDimitry Andric   case ISD::SSUBSAT:
1157e8d8bef9SDimitry Andric   case ISD::USUBSAT:
1158e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
1159e8d8bef9SDimitry Andric   case ISD::USHLSAT:
1160e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
1161e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
11620b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11630b57cec5SDimitry Andric     break;
11640b57cec5SDimitry Andric   case ISD::SMULFIX:
11650b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
11668bcb0991SDimitry Andric   case ISD::UMULFIX:
1167480093f4SDimitry Andric   case ISD::UMULFIXSAT:
1168480093f4SDimitry Andric   case ISD::SDIVFIX:
11695ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
11705ffd83dbSDimitry Andric   case ISD::UDIVFIX:
11715ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT: {
11720b57cec5SDimitry Andric     unsigned Scale = Node->getConstantOperandVal(2);
11730b57cec5SDimitry Andric     Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
11740b57cec5SDimitry Andric                                               Node->getValueType(0), Scale);
11750b57cec5SDimitry Andric     break;
11760b57cec5SDimitry Andric   }
11770b57cec5SDimitry Andric   case ISD::MSCATTER:
11780b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11790b57cec5SDimitry Andric                     cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
11800b57cec5SDimitry Andric     break;
11810b57cec5SDimitry Andric   case ISD::MSTORE:
11820b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11830b57cec5SDimitry Andric                     cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
11840b57cec5SDimitry Andric     break;
1185349cc55cSDimitry Andric   case ISD::VP_SCATTER:
1186349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1187349cc55cSDimitry Andric         Node->getOpcode(),
1188349cc55cSDimitry Andric         cast<VPScatterSDNode>(Node)->getValue().getValueType());
1189349cc55cSDimitry Andric     break;
1190349cc55cSDimitry Andric   case ISD::VP_STORE:
1191349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1192349cc55cSDimitry Andric         Node->getOpcode(),
1193349cc55cSDimitry Andric         cast<VPStoreSDNode>(Node)->getValue().getValueType());
1194349cc55cSDimitry Andric     break;
119581ad6265SDimitry Andric   case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
119681ad6265SDimitry Andric     Action = TLI.getOperationAction(
119781ad6265SDimitry Andric         Node->getOpcode(),
119881ad6265SDimitry Andric         cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
119981ad6265SDimitry Andric     break;
12000b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
12010b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
12020b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
12030b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
12040b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
12050b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
12060b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
12070b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
12080b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
12090b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
12100b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
12110b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
12120b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
1213*06c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
1214*06c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
121581ad6265SDimitry Andric   case ISD::IS_FPCLASS:
12160b57cec5SDimitry Andric     Action = TLI.getOperationAction(
12170b57cec5SDimitry Andric         Node->getOpcode(), Node->getOperand(0).getValueType());
12180b57cec5SDimitry Andric     break;
1219e8d8bef9SDimitry Andric   case ISD::VECREDUCE_SEQ_FADD:
1220349cc55cSDimitry Andric   case ISD::VECREDUCE_SEQ_FMUL:
1221349cc55cSDimitry Andric   case ISD::VP_REDUCE_FADD:
1222349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMUL:
1223349cc55cSDimitry Andric   case ISD::VP_REDUCE_ADD:
1224349cc55cSDimitry Andric   case ISD::VP_REDUCE_MUL:
1225349cc55cSDimitry Andric   case ISD::VP_REDUCE_AND:
1226349cc55cSDimitry Andric   case ISD::VP_REDUCE_OR:
1227349cc55cSDimitry Andric   case ISD::VP_REDUCE_XOR:
1228349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMAX:
1229349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMIN:
1230349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMAX:
1231349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMIN:
1232349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMAX:
1233349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMIN:
1234349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FADD:
1235349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FMUL:
1236e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(
1237e8d8bef9SDimitry Andric         Node->getOpcode(), Node->getOperand(1).getValueType());
1238e8d8bef9SDimitry Andric     break;
12390b57cec5SDimitry Andric   default:
12400b57cec5SDimitry Andric     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
124181ad6265SDimitry Andric       Action = TLI.getCustomOperationAction(*Node);
12420b57cec5SDimitry Andric     } else {
12430b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
12440b57cec5SDimitry Andric     }
12450b57cec5SDimitry Andric     break;
12460b57cec5SDimitry Andric   }
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric   if (SimpleFinishLegalizing) {
12490b57cec5SDimitry Andric     SDNode *NewNode = Node;
12500b57cec5SDimitry Andric     switch (Node->getOpcode()) {
12510b57cec5SDimitry Andric     default: break;
12520b57cec5SDimitry Andric     case ISD::SHL:
12530b57cec5SDimitry Andric     case ISD::SRL:
12540b57cec5SDimitry Andric     case ISD::SRA:
12550b57cec5SDimitry Andric     case ISD::ROTL:
12560b57cec5SDimitry Andric     case ISD::ROTR: {
12570b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12580b57cec5SDimitry Andric       // to the appropriate width.
12590b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12600b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12610b57cec5SDimitry Andric       if (!Op1.getValueType().isVector()) {
12620b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
12630b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12640b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12650b57cec5SDimitry Andric         // to update the parent node.
12660b57cec5SDimitry Andric         // Do not try to legalize SAO here! It will be automatically legalized
12670b57cec5SDimitry Andric         // in the next round.
12680b57cec5SDimitry Andric         if (SAO != Op1)
12690b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
12700b57cec5SDimitry Andric       }
12710b57cec5SDimitry Andric     }
12720b57cec5SDimitry Andric     break;
12730b57cec5SDimitry Andric     case ISD::FSHL:
12740b57cec5SDimitry Andric     case ISD::FSHR:
12750b57cec5SDimitry Andric     case ISD::SRL_PARTS:
12760b57cec5SDimitry Andric     case ISD::SRA_PARTS:
12770b57cec5SDimitry Andric     case ISD::SHL_PARTS: {
12780b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12790b57cec5SDimitry Andric       // to the appropriate width.
12800b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12810b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12820b57cec5SDimitry Andric       SDValue Op2 = Node->getOperand(2);
12830b57cec5SDimitry Andric       if (!Op2.getValueType().isVector()) {
12840b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
12850b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12860b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12870b57cec5SDimitry Andric         // to update the parent node.
12880b57cec5SDimitry Andric         if (SAO != Op2)
12890b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
12900b57cec5SDimitry Andric       }
12910b57cec5SDimitry Andric       break;
12920b57cec5SDimitry Andric     }
12930b57cec5SDimitry Andric     }
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric     if (NewNode != Node) {
12960b57cec5SDimitry Andric       ReplaceNode(Node, NewNode);
12970b57cec5SDimitry Andric       Node = NewNode;
12980b57cec5SDimitry Andric     }
12990b57cec5SDimitry Andric     switch (Action) {
13000b57cec5SDimitry Andric     case TargetLowering::Legal:
13010b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
13020b57cec5SDimitry Andric       return;
13030b57cec5SDimitry Andric     case TargetLowering::Custom:
13040b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
13050b57cec5SDimitry Andric       // FIXME: The handling for custom lowering with multiple results is
13060b57cec5SDimitry Andric       // a complete mess.
13070b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
13080b57cec5SDimitry Andric         if (!(Res.getNode() != Node || Res.getResNo() != 0))
13090b57cec5SDimitry Andric           return;
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric         if (Node->getNumValues() == 1) {
1312fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1313fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1314fe6060f1SDimitry Andric           assert((Res.getValueType() == Node->getValueType(0) ||
1315fe6060f1SDimitry Andric                   Node->getValueType(0) == MVT::Glue) &&
1316fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13170b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13180b57cec5SDimitry Andric           // We can just directly replace this node with the lowered value.
13190b57cec5SDimitry Andric           ReplaceNode(SDValue(Node, 0), Res);
13200b57cec5SDimitry Andric           return;
13210b57cec5SDimitry Andric         }
13220b57cec5SDimitry Andric 
13230b57cec5SDimitry Andric         SmallVector<SDValue, 8> ResultVals;
1324fe6060f1SDimitry Andric         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1325fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1326fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1327fe6060f1SDimitry Andric           assert((Res->getValueType(i) == Node->getValueType(i) ||
1328fe6060f1SDimitry Andric                   Node->getValueType(i) == MVT::Glue) &&
1329fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13300b57cec5SDimitry Andric           ResultVals.push_back(Res.getValue(i));
1331fe6060f1SDimitry Andric         }
13320b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13330b57cec5SDimitry Andric         ReplaceNode(Node, ResultVals.data());
13340b57cec5SDimitry Andric         return;
13350b57cec5SDimitry Andric       }
13360b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1337bdd1243dSDimitry Andric       [[fallthrough]];
13380b57cec5SDimitry Andric     case TargetLowering::Expand:
13390b57cec5SDimitry Andric       if (ExpandNode(Node))
13400b57cec5SDimitry Andric         return;
1341bdd1243dSDimitry Andric       [[fallthrough]];
13420b57cec5SDimitry Andric     case TargetLowering::LibCall:
13430b57cec5SDimitry Andric       ConvertNodeToLibcall(Node);
13440b57cec5SDimitry Andric       return;
13450b57cec5SDimitry Andric     case TargetLowering::Promote:
13460b57cec5SDimitry Andric       PromoteNode(Node);
13470b57cec5SDimitry Andric       return;
13480b57cec5SDimitry Andric     }
13490b57cec5SDimitry Andric   }
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   switch (Node->getOpcode()) {
13520b57cec5SDimitry Andric   default:
13530b57cec5SDimitry Andric #ifndef NDEBUG
13540b57cec5SDimitry Andric     dbgs() << "NODE: ";
13550b57cec5SDimitry Andric     Node->dump( &DAG);
13560b57cec5SDimitry Andric     dbgs() << "\n";
13570b57cec5SDimitry Andric #endif
13580b57cec5SDimitry Andric     llvm_unreachable("Do not know how to legalize this operator!");
13590b57cec5SDimitry Andric 
13600b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
13610b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
13620b57cec5SDimitry Andric     break;
13630b57cec5SDimitry Andric   case ISD::LOAD:
13640b57cec5SDimitry Andric     return LegalizeLoadOps(Node);
13650b57cec5SDimitry Andric   case ISD::STORE:
13660b57cec5SDimitry Andric     return LegalizeStoreOps(Node);
13670b57cec5SDimitry Andric   }
13680b57cec5SDimitry Andric }
13690b57cec5SDimitry Andric 
13700b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
13710b57cec5SDimitry Andric   SDValue Vec = Op.getOperand(0);
13720b57cec5SDimitry Andric   SDValue Idx = Op.getOperand(1);
13730b57cec5SDimitry Andric   SDLoc dl(Op);
13740b57cec5SDimitry Andric 
13750b57cec5SDimitry Andric   // Before we generate a new store to a temporary stack slot, see if there is
13760b57cec5SDimitry Andric   // already one that we can use. There often is because when we scalarize
13770b57cec5SDimitry Andric   // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
13780b57cec5SDimitry Andric   // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
13790b57cec5SDimitry Andric   // the vector. If all are expanded here, we don't want one store per vector
13800b57cec5SDimitry Andric   // element.
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   // Caches for hasPredecessorHelper
13830b57cec5SDimitry Andric   SmallPtrSet<const SDNode *, 32> Visited;
13840b57cec5SDimitry Andric   SmallVector<const SDNode *, 16> Worklist;
13850b57cec5SDimitry Andric   Visited.insert(Op.getNode());
13860b57cec5SDimitry Andric   Worklist.push_back(Idx.getNode());
13870b57cec5SDimitry Andric   SDValue StackPtr, Ch;
1388349cc55cSDimitry Andric   for (SDNode *User : Vec.getNode()->uses()) {
13890b57cec5SDimitry Andric     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
13900b57cec5SDimitry Andric       if (ST->isIndexed() || ST->isTruncatingStore() ||
13910b57cec5SDimitry Andric           ST->getValue() != Vec)
13920b57cec5SDimitry Andric         continue;
13930b57cec5SDimitry Andric 
13940b57cec5SDimitry Andric       // Make sure that nothing else could have stored into the destination of
13950b57cec5SDimitry Andric       // this store.
13960b57cec5SDimitry Andric       if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
13970b57cec5SDimitry Andric         continue;
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric       // If the index is dependent on the store we will introduce a cycle when
14000b57cec5SDimitry Andric       // creating the load (the load uses the index, and by replacing the chain
14010b57cec5SDimitry Andric       // we will make the index dependent on the load). Also, the store might be
14020b57cec5SDimitry Andric       // dependent on the extractelement and introduce a cycle when creating
14030b57cec5SDimitry Andric       // the load.
14040b57cec5SDimitry Andric       if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
14050b57cec5SDimitry Andric           ST->hasPredecessor(Op.getNode()))
14060b57cec5SDimitry Andric         continue;
14070b57cec5SDimitry Andric 
14080b57cec5SDimitry Andric       StackPtr = ST->getBasePtr();
14090b57cec5SDimitry Andric       Ch = SDValue(ST, 0);
14100b57cec5SDimitry Andric       break;
14110b57cec5SDimitry Andric     }
14120b57cec5SDimitry Andric   }
14130b57cec5SDimitry Andric 
14140b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric   if (!Ch.getNode()) {
14170b57cec5SDimitry Andric     // Store the value to a temporary stack slot, then LOAD the returned part.
14180b57cec5SDimitry Andric     StackPtr = DAG.CreateStackTemporary(VecVT);
14190b57cec5SDimitry Andric     Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
14200b57cec5SDimitry Andric                       MachinePointerInfo());
14210b57cec5SDimitry Andric   }
14220b57cec5SDimitry Andric 
14230b57cec5SDimitry Andric   SDValue NewLoad;
1424fcaf7f86SDimitry Andric   Align ElementAlignment =
1425fcaf7f86SDimitry Andric       std::min(cast<StoreSDNode>(Ch)->getAlign(),
1426fcaf7f86SDimitry Andric                DAG.getDataLayout().getPrefTypeAlign(
1427fcaf7f86SDimitry Andric                    Op.getValueType().getTypeForEVT(*DAG.getContext())));
14280b57cec5SDimitry Andric 
1429fe6060f1SDimitry Andric   if (Op.getValueType().isVector()) {
1430fe6060f1SDimitry Andric     StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1431fe6060f1SDimitry Andric                                           Op.getValueType(), Idx);
1432fcaf7f86SDimitry Andric     NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1433fcaf7f86SDimitry Andric                           MachinePointerInfo(), ElementAlignment);
1434fe6060f1SDimitry Andric   } else {
1435fe6060f1SDimitry Andric     StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
14360b57cec5SDimitry Andric     NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1437fcaf7f86SDimitry Andric                              MachinePointerInfo(), VecVT.getVectorElementType(),
1438fcaf7f86SDimitry Andric                              ElementAlignment);
1439fe6060f1SDimitry Andric   }
14400b57cec5SDimitry Andric 
14410b57cec5SDimitry Andric   // Replace the chain going out of the store, by the one out of the load.
14420b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
14430b57cec5SDimitry Andric 
14440b57cec5SDimitry Andric   // We introduced a cycle though, so update the loads operands, making sure
14450b57cec5SDimitry Andric   // to use the original store's chain as an incoming chain.
14460b57cec5SDimitry Andric   SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
14470b57cec5SDimitry Andric                                           NewLoad->op_end());
14480b57cec5SDimitry Andric   NewLoadOperands[0] = Ch;
14490b57cec5SDimitry Andric   NewLoad =
14500b57cec5SDimitry Andric       SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
14510b57cec5SDimitry Andric   return NewLoad;
14520b57cec5SDimitry Andric }
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
14550b57cec5SDimitry Andric   assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
14560b57cec5SDimitry Andric 
14570b57cec5SDimitry Andric   SDValue Vec  = Op.getOperand(0);
14580b57cec5SDimitry Andric   SDValue Part = Op.getOperand(1);
14590b57cec5SDimitry Andric   SDValue Idx  = Op.getOperand(2);
14600b57cec5SDimitry Andric   SDLoc dl(Op);
14610b57cec5SDimitry Andric 
14620b57cec5SDimitry Andric   // Store the value to a temporary stack slot, then LOAD the returned part.
14630b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
1464fe6060f1SDimitry Andric   EVT SubVecVT = Part.getValueType();
14650b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
14660b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
14670b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
14680b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   // First store the whole vector.
14710b57cec5SDimitry Andric   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
14720b57cec5SDimitry Andric 
14730b57cec5SDimitry Andric   // Then store the inserted part.
1474fe6060f1SDimitry Andric   SDValue SubStackPtr =
1475fe6060f1SDimitry Andric       TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx);
14760b57cec5SDimitry Andric 
14770b57cec5SDimitry Andric   // Store the subvector.
14785ffd83dbSDimitry Andric   Ch = DAG.getStore(
14795ffd83dbSDimitry Andric       Ch, dl, Part, SubStackPtr,
14805ffd83dbSDimitry Andric       MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
14810b57cec5SDimitry Andric 
14820b57cec5SDimitry Andric   // Finally, load the updated vector.
14830b57cec5SDimitry Andric   return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo);
14840b57cec5SDimitry Andric }
14850b57cec5SDimitry Andric 
14860b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
14875ffd83dbSDimitry Andric   assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
14885ffd83dbSDimitry Andric           Node->getOpcode() == ISD::CONCAT_VECTORS) &&
14895ffd83dbSDimitry Andric          "Unexpected opcode!");
14905ffd83dbSDimitry Andric 
14910b57cec5SDimitry Andric   // We can't handle this case efficiently.  Allocate a sufficiently
14925ffd83dbSDimitry Andric   // aligned object on the stack, store each operand into it, then load
14930b57cec5SDimitry Andric   // the result as a vector.
14940b57cec5SDimitry Andric   // Create the stack frame object.
14950b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
14965ffd83dbSDimitry Andric   EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
14975ffd83dbSDimitry Andric                                            : Node->getOperand(0).getValueType();
14980b57cec5SDimitry Andric   SDLoc dl(Node);
14990b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(VT);
15000b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
15010b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
15020b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   // Emit a store of each element to the stack slot.
15050b57cec5SDimitry Andric   SmallVector<SDValue, 8> Stores;
15065ffd83dbSDimitry Andric   unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
15070b57cec5SDimitry Andric   assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1508e8d8bef9SDimitry Andric 
1509e8d8bef9SDimitry Andric   // If the destination vector element type of a BUILD_VECTOR is narrower than
1510e8d8bef9SDimitry Andric   // the source element type, only store the bits necessary.
1511e8d8bef9SDimitry Andric   bool Truncate = isa<BuildVectorSDNode>(Node) &&
1512e8d8bef9SDimitry Andric                   MemVT.bitsLT(Node->getOperand(0).getValueType());
1513e8d8bef9SDimitry Andric 
15140b57cec5SDimitry Andric   // Store (in the right endianness) the elements to memory.
15150b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
15160b57cec5SDimitry Andric     // Ignore undef elements.
15170b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef()) continue;
15180b57cec5SDimitry Andric 
15190b57cec5SDimitry Andric     unsigned Offset = TypeByteSize*i;
15200b57cec5SDimitry Andric 
1521e8d8bef9SDimitry Andric     SDValue Idx = DAG.getMemBasePlusOffset(FIPtr, TypeSize::Fixed(Offset), dl);
15220b57cec5SDimitry Andric 
1523e8d8bef9SDimitry Andric     if (Truncate)
15240b57cec5SDimitry Andric       Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
15250b57cec5SDimitry Andric                                          Node->getOperand(i), Idx,
15265ffd83dbSDimitry Andric                                          PtrInfo.getWithOffset(Offset), MemVT));
15275ffd83dbSDimitry Andric     else
15280b57cec5SDimitry Andric       Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
15290b57cec5SDimitry Andric                                     Idx, PtrInfo.getWithOffset(Offset)));
15300b57cec5SDimitry Andric   }
15310b57cec5SDimitry Andric 
15320b57cec5SDimitry Andric   SDValue StoreChain;
15330b57cec5SDimitry Andric   if (!Stores.empty())    // Not all undef elements?
15340b57cec5SDimitry Andric     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
15350b57cec5SDimitry Andric   else
15360b57cec5SDimitry Andric     StoreChain = DAG.getEntryNode();
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric   // Result is a load from the stack slot.
15390b57cec5SDimitry Andric   return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
15400b57cec5SDimitry Andric }
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part
15430b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of
15440b57cec5SDimitry Andric /// holding all bits of the floating-point value.
15450b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
15460b57cec5SDimitry Andric                                              const SDLoc &DL,
15470b57cec5SDimitry Andric                                              SDValue Value) const {
15480b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
1549e8d8bef9SDimitry Andric   unsigned NumBits = FloatVT.getScalarSizeInBits();
15500b57cec5SDimitry Andric   State.FloatVT = FloatVT;
15510b57cec5SDimitry Andric   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
15520b57cec5SDimitry Andric   // Convert to an integer of the same size.
15530b57cec5SDimitry Andric   if (TLI.isTypeLegal(IVT)) {
15540b57cec5SDimitry Andric     State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
15550b57cec5SDimitry Andric     State.SignMask = APInt::getSignMask(NumBits);
15560b57cec5SDimitry Andric     State.SignBit = NumBits - 1;
15570b57cec5SDimitry Andric     return;
15580b57cec5SDimitry Andric   }
15590b57cec5SDimitry Andric 
15600b57cec5SDimitry Andric   auto &DataLayout = DAG.getDataLayout();
15610b57cec5SDimitry Andric   // Store the float to memory, then load the sign part out as an integer.
1562*06c3fb27SDimitry Andric   MVT LoadTy = TLI.getRegisterType(MVT::i8);
15630b57cec5SDimitry Andric   // First create a temporary that is aligned for both the load and store.
15640b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
15650b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
15660b57cec5SDimitry Andric   // Then store the float to it.
15670b57cec5SDimitry Andric   State.FloatPtr = StackPtr;
15680b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
15690b57cec5SDimitry Andric   State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
15700b57cec5SDimitry Andric   State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
15710b57cec5SDimitry Andric                              State.FloatPointerInfo);
15720b57cec5SDimitry Andric 
15730b57cec5SDimitry Andric   SDValue IntPtr;
15740b57cec5SDimitry Andric   if (DataLayout.isBigEndian()) {
15750b57cec5SDimitry Andric     assert(FloatVT.isByteSized() && "Unsupported floating point type!");
15760b57cec5SDimitry Andric     // Load out a legal integer with the same sign bit as the float.
15770b57cec5SDimitry Andric     IntPtr = StackPtr;
15780b57cec5SDimitry Andric     State.IntPointerInfo = State.FloatPointerInfo;
15790b57cec5SDimitry Andric   } else {
15800b57cec5SDimitry Andric     // Advance the pointer so that the loaded byte will contain the sign bit.
1581e8d8bef9SDimitry Andric     unsigned ByteOffset = (NumBits / 8) - 1;
1582e8d8bef9SDimitry Andric     IntPtr =
1583e8d8bef9SDimitry Andric         DAG.getMemBasePlusOffset(StackPtr, TypeSize::Fixed(ByteOffset), DL);
15840b57cec5SDimitry Andric     State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
15850b57cec5SDimitry Andric                                                              ByteOffset);
15860b57cec5SDimitry Andric   }
15870b57cec5SDimitry Andric 
15880b57cec5SDimitry Andric   State.IntPtr = IntPtr;
15890b57cec5SDimitry Andric   State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
15900b57cec5SDimitry Andric                                   State.IntPointerInfo, MVT::i8);
1591e8d8bef9SDimitry Andric   State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
15920b57cec5SDimitry Andric   State.SignBit = 7;
15930b57cec5SDimitry Andric }
15940b57cec5SDimitry Andric 
15950b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value
15960b57cec5SDimitry Andric /// and cast the result back to a floating-point type.
15970b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
15980b57cec5SDimitry Andric                                               const SDLoc &DL,
15990b57cec5SDimitry Andric                                               SDValue NewIntValue) const {
16000b57cec5SDimitry Andric   if (!State.Chain)
16010b57cec5SDimitry Andric     return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric   // Override the part containing the sign bit in the value stored on the stack.
16040b57cec5SDimitry Andric   SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
16050b57cec5SDimitry Andric                                     State.IntPointerInfo, MVT::i8);
16060b57cec5SDimitry Andric   return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
16070b57cec5SDimitry Andric                      State.FloatPointerInfo);
16080b57cec5SDimitry Andric }
16090b57cec5SDimitry Andric 
16100b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
16110b57cec5SDimitry Andric   SDLoc DL(Node);
16120b57cec5SDimitry Andric   SDValue Mag = Node->getOperand(0);
16130b57cec5SDimitry Andric   SDValue Sign = Node->getOperand(1);
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   // Get sign bit into an integer value.
16160b57cec5SDimitry Andric   FloatSignAsInt SignAsInt;
16170b57cec5SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Sign);
16180b57cec5SDimitry Andric 
16190b57cec5SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
16200b57cec5SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
16210b57cec5SDimitry Andric   SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
16220b57cec5SDimitry Andric                                 SignMask);
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
16250b57cec5SDimitry Andric   EVT FloatVT = Mag.getValueType();
16260b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
16270b57cec5SDimitry Andric       TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
16280b57cec5SDimitry Andric     SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
16290b57cec5SDimitry Andric     SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
16300b57cec5SDimitry Andric     SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
16310b57cec5SDimitry Andric                                 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
16320b57cec5SDimitry Andric     return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
16330b57cec5SDimitry Andric   }
16340b57cec5SDimitry Andric 
16350b57cec5SDimitry Andric   // Transform Mag value to integer, and clear the sign bit.
16360b57cec5SDimitry Andric   FloatSignAsInt MagAsInt;
16370b57cec5SDimitry Andric   getSignAsIntValue(MagAsInt, DL, Mag);
16380b57cec5SDimitry Andric   EVT MagVT = MagAsInt.IntValue.getValueType();
16390b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
16400b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
16410b57cec5SDimitry Andric                                     ClearSignMask);
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric   // Get the signbit at the right position for MagAsInt.
16440b57cec5SDimitry Andric   int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
16450b57cec5SDimitry Andric   EVT ShiftVT = IntVT;
1646e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() <
1647e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16480b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
16490b57cec5SDimitry Andric     ShiftVT = MagVT;
16500b57cec5SDimitry Andric   }
16510b57cec5SDimitry Andric   if (ShiftAmount > 0) {
16520b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
16530b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
16540b57cec5SDimitry Andric   } else if (ShiftAmount < 0) {
16550b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
16560b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
16570b57cec5SDimitry Andric   }
1658e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() >
1659e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16600b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
16610b57cec5SDimitry Andric   }
16620b57cec5SDimitry Andric 
16630b57cec5SDimitry Andric   // Store the part with the modified sign and convert back to float.
16640b57cec5SDimitry Andric   SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit);
16650b57cec5SDimitry Andric   return modifySignAsInt(MagAsInt, DL, CopiedSign);
16660b57cec5SDimitry Andric }
16670b57cec5SDimitry Andric 
1668e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1669e8d8bef9SDimitry Andric   // Get the sign bit as an integer.
1670e8d8bef9SDimitry Andric   SDLoc DL(Node);
1671e8d8bef9SDimitry Andric   FloatSignAsInt SignAsInt;
1672e8d8bef9SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1673e8d8bef9SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
1674e8d8bef9SDimitry Andric 
1675e8d8bef9SDimitry Andric   // Flip the sign.
1676e8d8bef9SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1677e8d8bef9SDimitry Andric   SDValue SignFlip =
1678e8d8bef9SDimitry Andric       DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1679e8d8bef9SDimitry Andric 
1680e8d8bef9SDimitry Andric   // Convert back to float.
1681e8d8bef9SDimitry Andric   return modifySignAsInt(SignAsInt, DL, SignFlip);
1682e8d8bef9SDimitry Andric }
1683e8d8bef9SDimitry Andric 
16840b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
16850b57cec5SDimitry Andric   SDLoc DL(Node);
16860b57cec5SDimitry Andric   SDValue Value = Node->getOperand(0);
16870b57cec5SDimitry Andric 
16880b57cec5SDimitry Andric   // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
16890b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
16900b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
16910b57cec5SDimitry Andric     SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
16920b57cec5SDimitry Andric     return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
16930b57cec5SDimitry Andric   }
16940b57cec5SDimitry Andric 
16950b57cec5SDimitry Andric   // Transform value to integer, clear the sign bit and transform back.
16960b57cec5SDimitry Andric   FloatSignAsInt ValueAsInt;
16970b57cec5SDimitry Andric   getSignAsIntValue(ValueAsInt, DL, Value);
16980b57cec5SDimitry Andric   EVT IntVT = ValueAsInt.IntValue.getValueType();
16990b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
17000b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
17010b57cec5SDimitry Andric                                     ClearSignMask);
17020b57cec5SDimitry Andric   return modifySignAsInt(ValueAsInt, DL, ClearedSign);
17030b57cec5SDimitry Andric }
17040b57cec5SDimitry Andric 
17050b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
17060b57cec5SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
1707e8d8bef9SDimitry Andric   Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
17080b57cec5SDimitry Andric   assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
17090b57cec5SDimitry Andric           " not tell us which reg is the stack pointer!");
17100b57cec5SDimitry Andric   SDLoc dl(Node);
17110b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
17120b57cec5SDimitry Andric   SDValue Tmp1 = SDValue(Node, 0);
17130b57cec5SDimitry Andric   SDValue Tmp2 = SDValue(Node, 1);
17140b57cec5SDimitry Andric   SDValue Tmp3 = Node->getOperand(2);
17150b57cec5SDimitry Andric   SDValue Chain = Tmp1.getOperand(0);
17160b57cec5SDimitry Andric 
17170b57cec5SDimitry Andric   // Chain the dynamic stack allocation so that it doesn't modify the stack
17180b57cec5SDimitry Andric   // pointer when other instructions are using the stack.
17190b57cec5SDimitry Andric   Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   SDValue Size  = Tmp2.getOperand(1);
17220b57cec5SDimitry Andric   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
17230b57cec5SDimitry Andric   Chain = SP.getValue(1);
17245ffd83dbSDimitry Andric   Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
17255ffd83dbSDimitry Andric   const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
17265ffd83dbSDimitry Andric   unsigned Opc =
17275ffd83dbSDimitry Andric     TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ?
17285ffd83dbSDimitry Andric     ISD::ADD : ISD::SUB;
17295ffd83dbSDimitry Andric 
17305ffd83dbSDimitry Andric   Align StackAlign = TFL->getStackAlign();
17315ffd83dbSDimitry Andric   Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size);       // Value
17325ffd83dbSDimitry Andric   if (Alignment > StackAlign)
17330b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
17345ffd83dbSDimitry Andric                        DAG.getConstant(-Alignment.value(), dl, VT));
17350b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);     // Output chain
17360b57cec5SDimitry Andric 
1737bdd1243dSDimitry Andric   Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric   Results.push_back(Tmp1);
17400b57cec5SDimitry Andric   Results.push_back(Tmp2);
17410b57cec5SDimitry Andric }
17420b57cec5SDimitry Andric 
17430b57cec5SDimitry Andric /// Emit a store/load combination to the stack.  This stores
17440b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
17450b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed.
17460b57cec5SDimitry Andric /// The resultant code need not be legal.
17470b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17480b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl) {
17490b57cec5SDimitry Andric   return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
17500b57cec5SDimitry Andric }
17510b57cec5SDimitry Andric 
17520b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17530b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl,
17540b57cec5SDimitry Andric                                                SDValue Chain) {
175581ad6265SDimitry Andric   EVT SrcVT = SrcOp.getValueType();
1756e8d8bef9SDimitry Andric   Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1757e8d8bef9SDimitry Andric   Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1758e8d8bef9SDimitry Andric 
1759e8d8bef9SDimitry Andric   // Don't convert with stack if the load/store is expensive.
176081ad6265SDimitry Andric   if ((SrcVT.bitsGT(SlotVT) &&
1761e8d8bef9SDimitry Andric        !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
176281ad6265SDimitry Andric       (SlotVT.bitsLT(DestVT) &&
1763e8d8bef9SDimitry Andric        !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1764e8d8bef9SDimitry Andric     return SDValue();
1765e8d8bef9SDimitry Andric 
17660b57cec5SDimitry Andric   // Create the stack frame object.
1767e8d8bef9SDimitry Andric   Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
17680b57cec5SDimitry Andric       SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1769e8d8bef9SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
17720b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
17730b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
17740b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
17750b57cec5SDimitry Andric 
17760b57cec5SDimitry Andric   // Emit a store to the stack slot.  Use a truncstore if the input value is
17770b57cec5SDimitry Andric   // later than DestVT.
17780b57cec5SDimitry Andric   SDValue Store;
17790b57cec5SDimitry Andric 
178081ad6265SDimitry Andric   if (SrcVT.bitsGT(SlotVT))
17810b57cec5SDimitry Andric     Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
17820b57cec5SDimitry Andric                               SlotVT, SrcAlign);
17830b57cec5SDimitry Andric   else {
178481ad6265SDimitry Andric     assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
178581ad6265SDimitry Andric     Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
17860b57cec5SDimitry Andric   }
17870b57cec5SDimitry Andric 
17880b57cec5SDimitry Andric   // Result is a load from the stack slot.
178981ad6265SDimitry Andric   if (SlotVT.bitsEq(DestVT))
17900b57cec5SDimitry Andric     return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
17910b57cec5SDimitry Andric 
179281ad6265SDimitry Andric   assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
17930b57cec5SDimitry Andric   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
17940b57cec5SDimitry Andric                         DestAlign);
17950b57cec5SDimitry Andric }
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
17980b57cec5SDimitry Andric   SDLoc dl(Node);
17990b57cec5SDimitry Andric   // Create a vector sized/aligned stack slot, store the value to element #0,
18000b57cec5SDimitry Andric   // then load the whole vector back out.
18010b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
18020b57cec5SDimitry Andric 
18030b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
18040b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric   SDValue Ch = DAG.getTruncStore(
18070b57cec5SDimitry Andric       DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
18080b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
18090b57cec5SDimitry Andric       Node->getValueType(0).getVectorElementType());
18100b57cec5SDimitry Andric   return DAG.getLoad(
18110b57cec5SDimitry Andric       Node->getValueType(0), dl, Ch, StackPtr,
18120b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
18130b57cec5SDimitry Andric }
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric static bool
18160b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
18170b57cec5SDimitry Andric                      const TargetLowering &TLI, SDValue &Res) {
18180b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
18190b57cec5SDimitry Andric   SDLoc dl(Node);
18200b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
18210b57cec5SDimitry Andric 
18220b57cec5SDimitry Andric   // Try to group the scalars into pairs, shuffle the pairs together, then
18230b57cec5SDimitry Andric   // shuffle the pairs of pairs together, etc. until the vector has
18240b57cec5SDimitry Andric   // been built. This will work only if all of the necessary shuffle masks
18250b57cec5SDimitry Andric   // are legal.
18260b57cec5SDimitry Andric 
18270b57cec5SDimitry Andric   // We do this in two phases; first to check the legality of the shuffles,
18280b57cec5SDimitry Andric   // and next, assuming that all shuffles are legal, to create the new nodes.
18290b57cec5SDimitry Andric   for (int Phase = 0; Phase < 2; ++Phase) {
18300b57cec5SDimitry Andric     SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals,
18310b57cec5SDimitry Andric                                                               NewIntermedVals;
18320b57cec5SDimitry Andric     for (unsigned i = 0; i < NumElems; ++i) {
18330b57cec5SDimitry Andric       SDValue V = Node->getOperand(i);
18340b57cec5SDimitry Andric       if (V.isUndef())
18350b57cec5SDimitry Andric         continue;
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric       SDValue Vec;
18380b57cec5SDimitry Andric       if (Phase)
18390b57cec5SDimitry Andric         Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
18400b57cec5SDimitry Andric       IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
18410b57cec5SDimitry Andric     }
18420b57cec5SDimitry Andric 
18430b57cec5SDimitry Andric     while (IntermedVals.size() > 2) {
18440b57cec5SDimitry Andric       NewIntermedVals.clear();
18450b57cec5SDimitry Andric       for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
18460b57cec5SDimitry Andric         // This vector and the next vector are shuffled together (simply to
18470b57cec5SDimitry Andric         // append the one to the other).
18480b57cec5SDimitry Andric         SmallVector<int, 16> ShuffleVec(NumElems, -1);
18490b57cec5SDimitry Andric 
18500b57cec5SDimitry Andric         SmallVector<int, 16> FinalIndices;
18510b57cec5SDimitry Andric         FinalIndices.reserve(IntermedVals[i].second.size() +
18520b57cec5SDimitry Andric                              IntermedVals[i+1].second.size());
18530b57cec5SDimitry Andric 
18540b57cec5SDimitry Andric         int k = 0;
18550b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
18560b57cec5SDimitry Andric              ++j, ++k) {
18570b57cec5SDimitry Andric           ShuffleVec[k] = j;
18580b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i].second[j]);
18590b57cec5SDimitry Andric         }
18600b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
18610b57cec5SDimitry Andric              ++j, ++k) {
18620b57cec5SDimitry Andric           ShuffleVec[k] = NumElems + j;
18630b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i+1].second[j]);
18640b57cec5SDimitry Andric         }
18650b57cec5SDimitry Andric 
18660b57cec5SDimitry Andric         SDValue Shuffle;
18670b57cec5SDimitry Andric         if (Phase)
18680b57cec5SDimitry Andric           Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
18690b57cec5SDimitry Andric                                          IntermedVals[i+1].first,
18700b57cec5SDimitry Andric                                          ShuffleVec);
18710b57cec5SDimitry Andric         else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
18720b57cec5SDimitry Andric           return false;
18730b57cec5SDimitry Andric         NewIntermedVals.push_back(
18740b57cec5SDimitry Andric             std::make_pair(Shuffle, std::move(FinalIndices)));
18750b57cec5SDimitry Andric       }
18760b57cec5SDimitry Andric 
18770b57cec5SDimitry Andric       // If we had an odd number of defined values, then append the last
18780b57cec5SDimitry Andric       // element to the array of new vectors.
18790b57cec5SDimitry Andric       if ((IntermedVals.size() & 1) != 0)
18800b57cec5SDimitry Andric         NewIntermedVals.push_back(IntermedVals.back());
18810b57cec5SDimitry Andric 
18820b57cec5SDimitry Andric       IntermedVals.swap(NewIntermedVals);
18830b57cec5SDimitry Andric     }
18840b57cec5SDimitry Andric 
18850b57cec5SDimitry Andric     assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
18860b57cec5SDimitry Andric            "Invalid number of intermediate vectors");
18870b57cec5SDimitry Andric     SDValue Vec1 = IntermedVals[0].first;
18880b57cec5SDimitry Andric     SDValue Vec2;
18890b57cec5SDimitry Andric     if (IntermedVals.size() > 1)
18900b57cec5SDimitry Andric       Vec2 = IntermedVals[1].first;
18910b57cec5SDimitry Andric     else if (Phase)
18920b57cec5SDimitry Andric       Vec2 = DAG.getUNDEF(VT);
18930b57cec5SDimitry Andric 
18940b57cec5SDimitry Andric     SmallVector<int, 16> ShuffleVec(NumElems, -1);
18950b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
18960b57cec5SDimitry Andric       ShuffleVec[IntermedVals[0].second[i]] = i;
18970b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
18980b57cec5SDimitry Andric       ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
18990b57cec5SDimitry Andric 
19000b57cec5SDimitry Andric     if (Phase)
19010b57cec5SDimitry Andric       Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
19020b57cec5SDimitry Andric     else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
19030b57cec5SDimitry Andric       return false;
19040b57cec5SDimitry Andric   }
19050b57cec5SDimitry Andric 
19060b57cec5SDimitry Andric   return true;
19070b57cec5SDimitry Andric }
19080b57cec5SDimitry Andric 
19090b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't
19100b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type.
19110b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
19120b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
19130b57cec5SDimitry Andric   SDValue Value1, Value2;
19140b57cec5SDimitry Andric   SDLoc dl(Node);
19150b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
19160b57cec5SDimitry Andric   EVT OpVT = Node->getOperand(0).getValueType();
19170b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric   // If the only non-undef value is the low element, turn this into a
19200b57cec5SDimitry Andric   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
19210b57cec5SDimitry Andric   bool isOnlyLowElement = true;
19220b57cec5SDimitry Andric   bool MoreThanTwoValues = false;
19230b57cec5SDimitry Andric   bool isConstant = true;
19240b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
19250b57cec5SDimitry Andric     SDValue V = Node->getOperand(i);
19260b57cec5SDimitry Andric     if (V.isUndef())
19270b57cec5SDimitry Andric       continue;
19280b57cec5SDimitry Andric     if (i > 0)
19290b57cec5SDimitry Andric       isOnlyLowElement = false;
19300b57cec5SDimitry Andric     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
19310b57cec5SDimitry Andric       isConstant = false;
19320b57cec5SDimitry Andric 
19330b57cec5SDimitry Andric     if (!Value1.getNode()) {
19340b57cec5SDimitry Andric       Value1 = V;
19350b57cec5SDimitry Andric     } else if (!Value2.getNode()) {
19360b57cec5SDimitry Andric       if (V != Value1)
19370b57cec5SDimitry Andric         Value2 = V;
19380b57cec5SDimitry Andric     } else if (V != Value1 && V != Value2) {
19390b57cec5SDimitry Andric       MoreThanTwoValues = true;
19400b57cec5SDimitry Andric     }
19410b57cec5SDimitry Andric   }
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric   if (!Value1.getNode())
19440b57cec5SDimitry Andric     return DAG.getUNDEF(VT);
19450b57cec5SDimitry Andric 
19460b57cec5SDimitry Andric   if (isOnlyLowElement)
19470b57cec5SDimitry Andric     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
19480b57cec5SDimitry Andric 
19490b57cec5SDimitry Andric   // If all elements are constants, create a load from the constant pool.
19500b57cec5SDimitry Andric   if (isConstant) {
19510b57cec5SDimitry Andric     SmallVector<Constant*, 16> CV;
19520b57cec5SDimitry Andric     for (unsigned i = 0, e = NumElems; i != e; ++i) {
19530b57cec5SDimitry Andric       if (ConstantFPSDNode *V =
19540b57cec5SDimitry Andric           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
19550b57cec5SDimitry Andric         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
19560b57cec5SDimitry Andric       } else if (ConstantSDNode *V =
19570b57cec5SDimitry Andric                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
19580b57cec5SDimitry Andric         if (OpVT==EltVT)
19590b57cec5SDimitry Andric           CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
19600b57cec5SDimitry Andric         else {
19610b57cec5SDimitry Andric           // If OpVT and EltVT don't match, EltVT is not legal and the
19620b57cec5SDimitry Andric           // element values have been promoted/truncated earlier.  Undo this;
19630b57cec5SDimitry Andric           // we don't want a v16i8 to become a v16i32 for example.
19640b57cec5SDimitry Andric           const ConstantInt *CI = V->getConstantIntValue();
19650b57cec5SDimitry Andric           CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
19660b57cec5SDimitry Andric                                         CI->getZExtValue()));
19670b57cec5SDimitry Andric         }
19680b57cec5SDimitry Andric       } else {
19690b57cec5SDimitry Andric         assert(Node->getOperand(i).isUndef());
19700b57cec5SDimitry Andric         Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
19710b57cec5SDimitry Andric         CV.push_back(UndefValue::get(OpNTy));
19720b57cec5SDimitry Andric       }
19730b57cec5SDimitry Andric     }
19740b57cec5SDimitry Andric     Constant *CP = ConstantVector::get(CV);
19750b57cec5SDimitry Andric     SDValue CPIdx =
19760b57cec5SDimitry Andric         DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
19775ffd83dbSDimitry Andric     Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
19780b57cec5SDimitry Andric     return DAG.getLoad(
19790b57cec5SDimitry Andric         VT, dl, DAG.getEntryNode(), CPIdx,
19800b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
19810b57cec5SDimitry Andric         Alignment);
19820b57cec5SDimitry Andric   }
19830b57cec5SDimitry Andric 
19840b57cec5SDimitry Andric   SmallSet<SDValue, 16> DefinedValues;
19850b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
19860b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef())
19870b57cec5SDimitry Andric       continue;
19880b57cec5SDimitry Andric     DefinedValues.insert(Node->getOperand(i));
19890b57cec5SDimitry Andric   }
19900b57cec5SDimitry Andric 
19910b57cec5SDimitry Andric   if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
19920b57cec5SDimitry Andric     if (!MoreThanTwoValues) {
19930b57cec5SDimitry Andric       SmallVector<int, 8> ShuffleVec(NumElems, -1);
19940b57cec5SDimitry Andric       for (unsigned i = 0; i < NumElems; ++i) {
19950b57cec5SDimitry Andric         SDValue V = Node->getOperand(i);
19960b57cec5SDimitry Andric         if (V.isUndef())
19970b57cec5SDimitry Andric           continue;
19980b57cec5SDimitry Andric         ShuffleVec[i] = V == Value1 ? 0 : NumElems;
19990b57cec5SDimitry Andric       }
20000b57cec5SDimitry Andric       if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
20010b57cec5SDimitry Andric         // Get the splatted value into the low element of a vector register.
20020b57cec5SDimitry Andric         SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
20030b57cec5SDimitry Andric         SDValue Vec2;
20040b57cec5SDimitry Andric         if (Value2.getNode())
20050b57cec5SDimitry Andric           Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
20060b57cec5SDimitry Andric         else
20070b57cec5SDimitry Andric           Vec2 = DAG.getUNDEF(VT);
20080b57cec5SDimitry Andric 
20090b57cec5SDimitry Andric         // Return shuffle(LowValVec, undef, <0,0,0,0>)
20100b57cec5SDimitry Andric         return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
20110b57cec5SDimitry Andric       }
20120b57cec5SDimitry Andric     } else {
20130b57cec5SDimitry Andric       SDValue Res;
20140b57cec5SDimitry Andric       if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
20150b57cec5SDimitry Andric         return Res;
20160b57cec5SDimitry Andric     }
20170b57cec5SDimitry Andric   }
20180b57cec5SDimitry Andric 
20190b57cec5SDimitry Andric   // Otherwise, we can't handle this case efficiently.
20200b57cec5SDimitry Andric   return ExpandVectorBuildThroughStack(Node);
20210b57cec5SDimitry Andric }
20220b57cec5SDimitry Andric 
20238bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
20248bcb0991SDimitry Andric   SDLoc DL(Node);
20258bcb0991SDimitry Andric   EVT VT = Node->getValueType(0);
20268bcb0991SDimitry Andric   SDValue SplatVal = Node->getOperand(0);
20278bcb0991SDimitry Andric 
20288bcb0991SDimitry Andric   return DAG.getSplatBuildVector(VT, DL, SplatVal);
20298bcb0991SDimitry Andric }
20308bcb0991SDimitry Andric 
2031*06c3fb27SDimitry Andric // Expand a node into a call to a libcall, returning the value as the first
2032*06c3fb27SDimitry Andric // result and the chain as the second.  If the result value does not fit into a
2033*06c3fb27SDimitry Andric // register, return the lo part and set the hi part to the by-reg argument in
2034*06c3fb27SDimitry Andric // the first.  If it does fit into a single register, return the result and
2035*06c3fb27SDimitry Andric // leave the Hi part unset.
2036*06c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2037*06c3fb27SDimitry Andric                                             TargetLowering::ArgListTy &&Args,
20380b57cec5SDimitry Andric                                             bool isSigned) {
20390b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
20400b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
20430b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric   // By default, the input chain to this libcall is the entry node of the
20460b57cec5SDimitry Andric   // function. If the libcall is going to be emitted as a tail call then
20470b57cec5SDimitry Andric   // TLI.isUsedByReturnOnly will change it to the right chain if the return
20480b57cec5SDimitry Andric   // node which is being folded has a non-entry input chain.
20490b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
20500b57cec5SDimitry Andric 
20510b57cec5SDimitry Andric   // isTailCall may be true since the callee does not reference caller stack
20520b57cec5SDimitry Andric   // frame. Check if it's in the right position and that the return types match.
20530b57cec5SDimitry Andric   SDValue TCChain = InChain;
20540b57cec5SDimitry Andric   const Function &F = DAG.getMachineFunction().getFunction();
20550b57cec5SDimitry Andric   bool isTailCall =
20560b57cec5SDimitry Andric       TLI.isInTailCallPosition(DAG, Node, TCChain) &&
20570b57cec5SDimitry Andric       (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
20580b57cec5SDimitry Andric   if (isTailCall)
20590b57cec5SDimitry Andric     InChain = TCChain;
20600b57cec5SDimitry Andric 
20610b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
20620b57cec5SDimitry Andric   bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned);
20630b57cec5SDimitry Andric   CLI.setDebugLoc(SDLoc(Node))
20640b57cec5SDimitry Andric       .setChain(InChain)
20650b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
20660b57cec5SDimitry Andric                     std::move(Args))
20670b57cec5SDimitry Andric       .setTailCall(isTailCall)
20680b57cec5SDimitry Andric       .setSExtResult(signExtend)
20690b57cec5SDimitry Andric       .setZExtResult(!signExtend)
20700b57cec5SDimitry Andric       .setIsPostTypeLegalization(true);
20710b57cec5SDimitry Andric 
20720b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
20730b57cec5SDimitry Andric 
20740b57cec5SDimitry Andric   if (!CallInfo.second.getNode()) {
20758bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
20760b57cec5SDimitry Andric     // It's a tailcall, return the chain (which is the DAG root).
2077*06c3fb27SDimitry Andric     return {DAG.getRoot(), DAG.getRoot()};
20780b57cec5SDimitry Andric   }
20790b57cec5SDimitry Andric 
20808bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
2081*06c3fb27SDimitry Andric   return CallInfo;
2082*06c3fb27SDimitry Andric }
2083*06c3fb27SDimitry Andric 
2084*06c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
2085*06c3fb27SDimitry Andric                                             bool isSigned) {
2086*06c3fb27SDimitry Andric   TargetLowering::ArgListTy Args;
2087*06c3fb27SDimitry Andric   TargetLowering::ArgListEntry Entry;
2088*06c3fb27SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
2089*06c3fb27SDimitry Andric     EVT ArgVT = Op.getValueType();
2090*06c3fb27SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2091*06c3fb27SDimitry Andric     Entry.Node = Op;
2092*06c3fb27SDimitry Andric     Entry.Ty = ArgTy;
2093*06c3fb27SDimitry Andric     Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned);
2094*06c3fb27SDimitry Andric     Entry.IsZExt = !Entry.IsSExt;
2095*06c3fb27SDimitry Andric     Args.push_back(Entry);
2096*06c3fb27SDimitry Andric   }
2097*06c3fb27SDimitry Andric 
2098*06c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, std::move(Args), isSigned);
2099*06c3fb27SDimitry Andric }
2100*06c3fb27SDimitry Andric 
2101*06c3fb27SDimitry Andric void SelectionDAGLegalize::ExpandFrexpLibCall(
2102*06c3fb27SDimitry Andric     SDNode *Node, SmallVectorImpl<SDValue> &Results) {
2103*06c3fb27SDimitry Andric   SDLoc dl(Node);
2104*06c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
2105*06c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
2106*06c3fb27SDimitry Andric 
2107*06c3fb27SDimitry Andric   SDValue FPOp = Node->getOperand(0);
2108*06c3fb27SDimitry Andric 
2109*06c3fb27SDimitry Andric   EVT ArgVT = FPOp.getValueType();
2110*06c3fb27SDimitry Andric   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
2111*06c3fb27SDimitry Andric 
2112*06c3fb27SDimitry Andric   TargetLowering::ArgListEntry FPArgEntry;
2113*06c3fb27SDimitry Andric   FPArgEntry.Node = FPOp;
2114*06c3fb27SDimitry Andric   FPArgEntry.Ty = ArgTy;
2115*06c3fb27SDimitry Andric 
2116*06c3fb27SDimitry Andric   SDValue StackSlot = DAG.CreateStackTemporary(ExpVT);
2117*06c3fb27SDimitry Andric   TargetLowering::ArgListEntry PtrArgEntry;
2118*06c3fb27SDimitry Andric   PtrArgEntry.Node = StackSlot;
2119*06c3fb27SDimitry Andric   PtrArgEntry.Ty = PointerType::get(*DAG.getContext(),
2120*06c3fb27SDimitry Andric                                     DAG.getDataLayout().getAllocaAddrSpace());
2121*06c3fb27SDimitry Andric 
2122*06c3fb27SDimitry Andric   TargetLowering::ArgListTy Args = {FPArgEntry, PtrArgEntry};
2123*06c3fb27SDimitry Andric 
2124*06c3fb27SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFREXP(VT);
2125*06c3fb27SDimitry Andric   auto [Call, Chain] = ExpandLibCall(LC, Node, std::move(Args), false);
2126*06c3fb27SDimitry Andric 
2127*06c3fb27SDimitry Andric   // FIXME: Get type of int for libcall declaration and cast
2128*06c3fb27SDimitry Andric 
2129*06c3fb27SDimitry Andric   int FrameIdx = cast<FrameIndexSDNode>(StackSlot)->getIndex();
2130*06c3fb27SDimitry Andric   auto PtrInfo =
2131*06c3fb27SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
2132*06c3fb27SDimitry Andric 
2133*06c3fb27SDimitry Andric   SDValue LoadExp = DAG.getLoad(ExpVT, dl, Chain, StackSlot, PtrInfo);
2134*06c3fb27SDimitry Andric   SDValue OutputChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2135*06c3fb27SDimitry Andric                                     LoadExp.getValue(1), DAG.getRoot());
2136*06c3fb27SDimitry Andric   DAG.setRoot(OutputChain);
2137*06c3fb27SDimitry Andric 
2138*06c3fb27SDimitry Andric   Results.push_back(Call);
2139*06c3fb27SDimitry Andric   Results.push_back(LoadExp);
21400b57cec5SDimitry Andric }
21410b57cec5SDimitry Andric 
2142480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2143fe6060f1SDimitry Andric                                            RTLIB::Libcall LC,
2144480093f4SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2145fe6060f1SDimitry Andric   if (LC == RTLIB::UNKNOWN_LIBCALL)
2146fe6060f1SDimitry Andric     llvm_unreachable("Can't create an unknown libcall!");
2147480093f4SDimitry Andric 
2148480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2149480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
2150e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops(drop_begin(Node->ops()));
2151480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
2152480093f4SDimitry Andric     // FIXME: This doesn't support tail calls.
2153480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2154480093f4SDimitry Andric                                                       Ops, CallOptions,
2155480093f4SDimitry Andric                                                       SDLoc(Node),
2156480093f4SDimitry Andric                                                       Node->getOperand(0));
2157480093f4SDimitry Andric     Results.push_back(Tmp.first);
2158480093f4SDimitry Andric     Results.push_back(Tmp.second);
2159480093f4SDimitry Andric   } else {
2160*06c3fb27SDimitry Andric     SDValue Tmp = ExpandLibCall(LC, Node, false).first;
2161480093f4SDimitry Andric     Results.push_back(Tmp);
2162480093f4SDimitry Andric   }
21630b57cec5SDimitry Andric }
21640b57cec5SDimitry Andric 
2165fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type.
2166fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2167fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F32,
2168fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F64,
2169fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F80,
2170fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F128,
2171fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_PPCF128,
2172fe6060f1SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2173fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2174fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2175fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2176fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
2177fe6060f1SDimitry Andric }
2178fe6060f1SDimitry Andric 
2179bdd1243dSDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2180bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I8,
2181bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I16,
2182bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I32,
2183bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I64,
2184bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I128) {
21850b57cec5SDimitry Andric   RTLIB::Libcall LC;
21860b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2187bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
21880b57cec5SDimitry Andric   case MVT::i8:   LC = Call_I8; break;
21890b57cec5SDimitry Andric   case MVT::i16:  LC = Call_I16; break;
21900b57cec5SDimitry Andric   case MVT::i32:  LC = Call_I32; break;
21910b57cec5SDimitry Andric   case MVT::i64:  LC = Call_I64; break;
21920b57cec5SDimitry Andric   case MVT::i128: LC = Call_I128; break;
21930b57cec5SDimitry Andric   }
2194*06c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, isSigned).first;
21950b57cec5SDimitry Andric }
21960b57cec5SDimitry Andric 
21970b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance
21980b57cec5SDimitry Andric /// lround and its variant).
2199480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
22000b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F32,
22010b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F64,
22020b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F80,
22030b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F128,
2204480093f4SDimitry Andric                                             RTLIB::Libcall Call_PPCF128,
2205480093f4SDimitry Andric                                             SmallVectorImpl<SDValue> &Results) {
2206480093f4SDimitry Andric   EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2207fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(),
2208fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2209fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2210fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
22110b57cec5SDimitry Andric }
22120b57cec5SDimitry Andric 
22130b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs.
22140b57cec5SDimitry Andric void
22150b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
22160b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
22170b57cec5SDimitry Andric   unsigned Opcode = Node->getOpcode();
22180b57cec5SDimitry Andric   bool isSigned = Opcode == ISD::SDIVREM;
22190b57cec5SDimitry Andric 
22200b57cec5SDimitry Andric   RTLIB::Libcall LC;
22210b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2222bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22230b57cec5SDimitry Andric   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
22240b57cec5SDimitry Andric   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
22250b57cec5SDimitry Andric   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
22260b57cec5SDimitry Andric   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
22270b57cec5SDimitry Andric   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
22280b57cec5SDimitry Andric   }
22290b57cec5SDimitry Andric 
22300b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
22310b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
22320b57cec5SDimitry Andric   // dependence.
22330b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
22340b57cec5SDimitry Andric 
22350b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
22360b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
22370b57cec5SDimitry Andric 
22380b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
22390b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
22400b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
22410b57cec5SDimitry Andric     EVT ArgVT = Op.getValueType();
22420b57cec5SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
22430b57cec5SDimitry Andric     Entry.Node = Op;
22440b57cec5SDimitry Andric     Entry.Ty = ArgTy;
22450b57cec5SDimitry Andric     Entry.IsSExt = isSigned;
22460b57cec5SDimitry Andric     Entry.IsZExt = !isSigned;
22470b57cec5SDimitry Andric     Args.push_back(Entry);
22480b57cec5SDimitry Andric   }
22490b57cec5SDimitry Andric 
22500b57cec5SDimitry Andric   // Also pass the return address of the remainder.
22510b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
22520b57cec5SDimitry Andric   Entry.Node = FIPtr;
22530b57cec5SDimitry Andric   Entry.Ty = RetTy->getPointerTo();
22540b57cec5SDimitry Andric   Entry.IsSExt = isSigned;
22550b57cec5SDimitry Andric   Entry.IsZExt = !isSigned;
22560b57cec5SDimitry Andric   Args.push_back(Entry);
22570b57cec5SDimitry Andric 
22580b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
22590b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
22600b57cec5SDimitry Andric 
22610b57cec5SDimitry Andric   SDLoc dl(Node);
22620b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
22630b57cec5SDimitry Andric   CLI.setDebugLoc(dl)
22640b57cec5SDimitry Andric       .setChain(InChain)
22650b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
22660b57cec5SDimitry Andric                     std::move(Args))
22670b57cec5SDimitry Andric       .setSExtResult(isSigned)
22680b57cec5SDimitry Andric       .setZExtResult(!isSigned);
22690b57cec5SDimitry Andric 
22700b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
22710b57cec5SDimitry Andric 
22720b57cec5SDimitry Andric   // Remainder is loaded back from the stack frame.
22730b57cec5SDimitry Andric   SDValue Rem =
22740b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
22750b57cec5SDimitry Andric   Results.push_back(CallInfo.first);
22760b57cec5SDimitry Andric   Results.push_back(Rem);
22770b57cec5SDimitry Andric }
22780b57cec5SDimitry Andric 
22790b57cec5SDimitry Andric /// Return true if sincos libcall is available.
22800b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
22810b57cec5SDimitry Andric   RTLIB::Libcall LC;
22820b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
22830b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22840b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
22850b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
22860b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
22870b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
22880b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
22890b57cec5SDimitry Andric   }
22900b57cec5SDimitry Andric   return TLI.getLibcallName(LC) != nullptr;
22910b57cec5SDimitry Andric }
22920b57cec5SDimitry Andric 
22930b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed.
22940b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) {
22950b57cec5SDimitry Andric   unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
22960b57cec5SDimitry Andric     ? ISD::FCOS : ISD::FSIN;
22970b57cec5SDimitry Andric 
22980b57cec5SDimitry Andric   SDValue Op0 = Node->getOperand(0);
2299349cc55cSDimitry Andric   for (const SDNode *User : Op0.getNode()->uses()) {
23000b57cec5SDimitry Andric     if (User == Node)
23010b57cec5SDimitry Andric       continue;
23020b57cec5SDimitry Andric     // The other user might have been turned into sincos already.
23030b57cec5SDimitry Andric     if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
23040b57cec5SDimitry Andric       return true;
23050b57cec5SDimitry Andric   }
23060b57cec5SDimitry Andric   return false;
23070b57cec5SDimitry Andric }
23080b57cec5SDimitry Andric 
23090b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs.
23100b57cec5SDimitry Andric void
23110b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
23120b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
23130b57cec5SDimitry Andric   RTLIB::Libcall LC;
23140b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
23150b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
23160b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
23170b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
23180b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
23190b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
23200b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
23210b57cec5SDimitry Andric   }
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
23240b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
23250b57cec5SDimitry Andric   // dependence.
23260b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
23270b57cec5SDimitry Andric 
23280b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
23290b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
23300b57cec5SDimitry Andric 
23310b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
23320b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
23330b57cec5SDimitry Andric 
23340b57cec5SDimitry Andric   // Pass the argument.
23350b57cec5SDimitry Andric   Entry.Node = Node->getOperand(0);
23360b57cec5SDimitry Andric   Entry.Ty = RetTy;
23370b57cec5SDimitry Andric   Entry.IsSExt = false;
23380b57cec5SDimitry Andric   Entry.IsZExt = false;
23390b57cec5SDimitry Andric   Args.push_back(Entry);
23400b57cec5SDimitry Andric 
23410b57cec5SDimitry Andric   // Pass the return address of sin.
23420b57cec5SDimitry Andric   SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
23430b57cec5SDimitry Andric   Entry.Node = SinPtr;
23440b57cec5SDimitry Andric   Entry.Ty = RetTy->getPointerTo();
23450b57cec5SDimitry Andric   Entry.IsSExt = false;
23460b57cec5SDimitry Andric   Entry.IsZExt = false;
23470b57cec5SDimitry Andric   Args.push_back(Entry);
23480b57cec5SDimitry Andric 
23490b57cec5SDimitry Andric   // Also pass the return address of the cos.
23500b57cec5SDimitry Andric   SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
23510b57cec5SDimitry Andric   Entry.Node = CosPtr;
23520b57cec5SDimitry Andric   Entry.Ty = RetTy->getPointerTo();
23530b57cec5SDimitry Andric   Entry.IsSExt = false;
23540b57cec5SDimitry Andric   Entry.IsZExt = false;
23550b57cec5SDimitry Andric   Args.push_back(Entry);
23560b57cec5SDimitry Andric 
23570b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
23580b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
23590b57cec5SDimitry Andric 
23600b57cec5SDimitry Andric   SDLoc dl(Node);
23610b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
23620b57cec5SDimitry Andric   CLI.setDebugLoc(dl).setChain(InChain).setLibCallee(
23630b57cec5SDimitry Andric       TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee,
23640b57cec5SDimitry Andric       std::move(Args));
23650b57cec5SDimitry Andric 
23660b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
23670b57cec5SDimitry Andric 
23680b57cec5SDimitry Andric   Results.push_back(
23690b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo()));
23700b57cec5SDimitry Andric   Results.push_back(
23710b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo()));
23720b57cec5SDimitry Andric }
23730b57cec5SDimitry Andric 
2374*06c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
2375*06c3fb27SDimitry Andric   SDLoc dl(Node);
2376*06c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
2377*06c3fb27SDimitry Andric   SDValue X = Node->getOperand(0);
2378*06c3fb27SDimitry Andric   SDValue N = Node->getOperand(1);
2379*06c3fb27SDimitry Andric   EVT ExpVT = N.getValueType();
2380*06c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
2381*06c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
2382*06c3fb27SDimitry Andric     return SDValue();
2383*06c3fb27SDimitry Andric 
2384*06c3fb27SDimitry Andric   if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
2385*06c3fb27SDimitry Andric     return SDValue();
2386*06c3fb27SDimitry Andric 
2387*06c3fb27SDimitry Andric   SDNodeFlags NSW;
2388*06c3fb27SDimitry Andric   NSW.setNoSignedWrap(true);
2389*06c3fb27SDimitry Andric   SDNodeFlags NUW_NSW;
2390*06c3fb27SDimitry Andric   NUW_NSW.setNoUnsignedWrap(true);
2391*06c3fb27SDimitry Andric   NUW_NSW.setNoSignedWrap(true);
2392*06c3fb27SDimitry Andric 
2393*06c3fb27SDimitry Andric   EVT SetCCVT =
2394*06c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
2395*06c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
2396*06c3fb27SDimitry Andric 
2397*06c3fb27SDimitry Andric   const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
2398*06c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2399*06c3fb27SDimitry Andric   const int Precision = APFloat::semanticsPrecision(FltSem);
2400*06c3fb27SDimitry Andric 
2401*06c3fb27SDimitry Andric   const SDValue MaxExp = DAG.getConstant(MaxExpVal, dl, ExpVT);
2402*06c3fb27SDimitry Andric   const SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2403*06c3fb27SDimitry Andric 
2404*06c3fb27SDimitry Andric   const SDValue DoubleMaxExp = DAG.getConstant(2 * MaxExpVal, dl, ExpVT);
2405*06c3fb27SDimitry Andric 
2406*06c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
2407*06c3fb27SDimitry Andric   APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
2408*06c3fb27SDimitry Andric 
2409*06c3fb27SDimitry Andric   // Offset by precision to avoid denormal range.
2410*06c3fb27SDimitry Andric   APFloat ScaleDownK =
2411*06c3fb27SDimitry Andric       scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
2412*06c3fb27SDimitry Andric 
2413*06c3fb27SDimitry Andric   // TODO: Should really introduce control flow and use a block for the >
2414*06c3fb27SDimitry Andric   // MaxExp, < MinExp cases
2415*06c3fb27SDimitry Andric 
2416*06c3fb27SDimitry Andric   // First, handle exponents Exp > MaxExp and scale down.
2417*06c3fb27SDimitry Andric   SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
2418*06c3fb27SDimitry Andric 
2419*06c3fb27SDimitry Andric   SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
2420*06c3fb27SDimitry Andric   SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
2421*06c3fb27SDimitry Andric   SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
2422*06c3fb27SDimitry Andric   SDValue DecN1 =
2423*06c3fb27SDimitry Andric       DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
2424*06c3fb27SDimitry Andric 
2425*06c3fb27SDimitry Andric   SDValue ScaleUpTwice =
2426*06c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
2427*06c3fb27SDimitry Andric 
2428*06c3fb27SDimitry Andric   const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
2429*06c3fb27SDimitry Andric   SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
2430*06c3fb27SDimitry Andric   SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
2431*06c3fb27SDimitry Andric 
2432*06c3fb27SDimitry Andric   SDValue SelectN_Big =
2433*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
2434*06c3fb27SDimitry Andric   SDValue SelectX_Big =
2435*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
2436*06c3fb27SDimitry Andric 
2437*06c3fb27SDimitry Andric   // Now handle exponents Exp < MinExp
2438*06c3fb27SDimitry Andric   SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
2439*06c3fb27SDimitry Andric 
2440*06c3fb27SDimitry Andric   SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
2441*06c3fb27SDimitry Andric   SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
2442*06c3fb27SDimitry Andric 
2443*06c3fb27SDimitry Andric   SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
2444*06c3fb27SDimitry Andric 
2445*06c3fb27SDimitry Andric   SDValue ClampMinVal =
2446*06c3fb27SDimitry Andric       DAG.getConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
2447*06c3fb27SDimitry Andric   SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
2448*06c3fb27SDimitry Andric   SDValue IncN1 =
2449*06c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
2450*06c3fb27SDimitry Andric 
2451*06c3fb27SDimitry Andric   const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
2452*06c3fb27SDimitry Andric   SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
2453*06c3fb27SDimitry Andric   SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
2454*06c3fb27SDimitry Andric 
2455*06c3fb27SDimitry Andric   SDValue ScaleDownTwice = DAG.getSetCC(
2456*06c3fb27SDimitry Andric       dl, SetCCVT, N, DAG.getConstant(2 * MinExpVal + Precision, dl, ExpVT),
2457*06c3fb27SDimitry Andric       ISD::SETULT);
2458*06c3fb27SDimitry Andric 
2459*06c3fb27SDimitry Andric   SDValue SelectN_Small =
2460*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
2461*06c3fb27SDimitry Andric   SDValue SelectX_Small =
2462*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
2463*06c3fb27SDimitry Andric 
2464*06c3fb27SDimitry Andric   // Now combine the two out of range exponent handling cases with the base
2465*06c3fb27SDimitry Andric   // case.
2466*06c3fb27SDimitry Andric   SDValue NewX = DAG.getNode(
2467*06c3fb27SDimitry Andric       ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
2468*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
2469*06c3fb27SDimitry Andric 
2470*06c3fb27SDimitry Andric   SDValue NewN = DAG.getNode(
2471*06c3fb27SDimitry Andric       ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
2472*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
2473*06c3fb27SDimitry Andric 
2474*06c3fb27SDimitry Andric   SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
2475*06c3fb27SDimitry Andric 
2476*06c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
2477*06c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
2478*06c3fb27SDimitry Andric   SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
2479*06c3fb27SDimitry Andric 
2480*06c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
2481*06c3fb27SDimitry Andric                               ExponentShiftAmt, NUW_NSW);
2482*06c3fb27SDimitry Andric   SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
2483*06c3fb27SDimitry Andric   return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
2484*06c3fb27SDimitry Andric }
2485*06c3fb27SDimitry Andric 
2486*06c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
2487*06c3fb27SDimitry Andric   SDLoc dl(Node);
2488*06c3fb27SDimitry Andric   SDValue Val = Node->getOperand(0);
2489*06c3fb27SDimitry Andric   EVT VT = Val.getValueType();
2490*06c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
2491*06c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
2492*06c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
2493*06c3fb27SDimitry Andric     return SDValue();
2494*06c3fb27SDimitry Andric 
2495*06c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
2496*06c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
2497*06c3fb27SDimitry Andric   const unsigned Precision = APFloat::semanticsPrecision(FltSem);
2498*06c3fb27SDimitry Andric   const unsigned BitSize = VT.getScalarSizeInBits();
2499*06c3fb27SDimitry Andric 
2500*06c3fb27SDimitry Andric   // TODO: Could introduce control flow and skip over the denormal handling.
2501*06c3fb27SDimitry Andric 
2502*06c3fb27SDimitry Andric   // scale_up = fmul value, scalbn(1.0, precision + 1)
2503*06c3fb27SDimitry Andric   // extracted_exp = (bitcast value to uint) >> precision - 1
2504*06c3fb27SDimitry Andric   // biased_exp = extracted_exp + min_exp
2505*06c3fb27SDimitry Andric   // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
2506*06c3fb27SDimitry Andric   //
2507*06c3fb27SDimitry Andric   // is_denormal = val < smallest_normalized
2508*06c3fb27SDimitry Andric   // computed_fract = is_denormal ? scale_up : extracted_fract
2509*06c3fb27SDimitry Andric   // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
2510*06c3fb27SDimitry Andric   //
2511*06c3fb27SDimitry Andric   // result_0 =  (!isfinite(val) || iszero(val)) ? val : computed_fract
2512*06c3fb27SDimitry Andric   // result_1 =  (!isfinite(val) || iszero(val)) ? 0 : computed_exp
2513*06c3fb27SDimitry Andric 
2514*06c3fb27SDimitry Andric   SDValue NegSmallestNormalizedInt = DAG.getConstant(
2515*06c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
2516*06c3fb27SDimitry Andric       AsIntVT);
2517*06c3fb27SDimitry Andric 
2518*06c3fb27SDimitry Andric   SDValue SmallestNormalizedInt = DAG.getConstant(
2519*06c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
2520*06c3fb27SDimitry Andric       AsIntVT);
2521*06c3fb27SDimitry Andric 
2522*06c3fb27SDimitry Andric   // Masks out the exponent bits.
2523*06c3fb27SDimitry Andric   SDValue ExpMask =
2524*06c3fb27SDimitry Andric       DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
2525*06c3fb27SDimitry Andric 
2526*06c3fb27SDimitry Andric   // Mask out the exponent part of the value.
2527*06c3fb27SDimitry Andric   //
2528*06c3fb27SDimitry Andric   // e.g, for f32 FractSignMaskVal = 0x807fffff
2529*06c3fb27SDimitry Andric   APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
2530*06c3fb27SDimitry Andric   FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
2531*06c3fb27SDimitry Andric 
2532*06c3fb27SDimitry Andric   APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
2533*06c3fb27SDimitry Andric   SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
2534*06c3fb27SDimitry Andric 
2535*06c3fb27SDimitry Andric   SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
2536*06c3fb27SDimitry Andric 
2537*06c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
2538*06c3fb27SDimitry Andric   // Scale a possible denormal input.
2539*06c3fb27SDimitry Andric   // e.g., for f64, 0x1p+54
2540*06c3fb27SDimitry Andric   APFloat ScaleUpKVal =
2541*06c3fb27SDimitry Andric       scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
2542*06c3fb27SDimitry Andric 
2543*06c3fb27SDimitry Andric   SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
2544*06c3fb27SDimitry Andric   SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
2545*06c3fb27SDimitry Andric 
2546*06c3fb27SDimitry Andric   EVT SetCCVT =
2547*06c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
2548*06c3fb27SDimitry Andric 
2549*06c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
2550*06c3fb27SDimitry Andric 
2551*06c3fb27SDimitry Andric   SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
2552*06c3fb27SDimitry Andric 
2553*06c3fb27SDimitry Andric   SDValue AddNegSmallestNormal =
2554*06c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
2555*06c3fb27SDimitry Andric   SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
2556*06c3fb27SDimitry Andric                                       NegSmallestNormalizedInt, ISD::SETULE);
2557*06c3fb27SDimitry Andric 
2558*06c3fb27SDimitry Andric   SDValue IsDenormal =
2559*06c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
2560*06c3fb27SDimitry Andric 
2561*06c3fb27SDimitry Andric   SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
2562*06c3fb27SDimitry Andric   SDValue Zero = DAG.getConstant(0, dl, ExpVT);
2563*06c3fb27SDimitry Andric 
2564*06c3fb27SDimitry Andric   SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
2565*06c3fb27SDimitry Andric   SDValue ScaledSelect =
2566*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
2567*06c3fb27SDimitry Andric 
2568*06c3fb27SDimitry Andric   SDValue ExpMaskScaled =
2569*06c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
2570*06c3fb27SDimitry Andric 
2571*06c3fb27SDimitry Andric   SDValue ScaledValue =
2572*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
2573*06c3fb27SDimitry Andric 
2574*06c3fb27SDimitry Andric   // Extract the exponent bits.
2575*06c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
2576*06c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
2577*06c3fb27SDimitry Andric   SDValue ShiftedExp =
2578*06c3fb27SDimitry Andric       DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
2579*06c3fb27SDimitry Andric   SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
2580*06c3fb27SDimitry Andric 
2581*06c3fb27SDimitry Andric   SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
2582*06c3fb27SDimitry Andric   SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
2583*06c3fb27SDimitry Andric   SDValue DenormalExpBias =
2584*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
2585*06c3fb27SDimitry Andric 
2586*06c3fb27SDimitry Andric   SDValue MaskedFractAsInt =
2587*06c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
2588*06c3fb27SDimitry Andric   const APFloat Half(FltSem, "0.5");
2589*06c3fb27SDimitry Andric   SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
2590*06c3fb27SDimitry Andric   SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
2591*06c3fb27SDimitry Andric   SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
2592*06c3fb27SDimitry Andric 
2593*06c3fb27SDimitry Andric   SDValue ComputedExp =
2594*06c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
2595*06c3fb27SDimitry Andric 
2596*06c3fb27SDimitry Andric   SDValue Result0 =
2597*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
2598*06c3fb27SDimitry Andric 
2599*06c3fb27SDimitry Andric   SDValue Result1 =
2600*06c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
2601*06c3fb27SDimitry Andric 
2602*06c3fb27SDimitry Andric   return DAG.getMergeValues({Result0, Result1}, dl);
2603*06c3fb27SDimitry Andric }
2604*06c3fb27SDimitry Andric 
26050b57cec5SDimitry Andric /// This function is responsible for legalizing a
26060b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that
26070b57cec5SDimitry Andric /// we expand it.  At this point, we know that the result and operand types are
26080b57cec5SDimitry Andric /// legal for the target.
2609480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2610480093f4SDimitry Andric                                                    SDValue &Chain) {
2611480093f4SDimitry Andric   bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2612480093f4SDimitry Andric                    Node->getOpcode() == ISD::SINT_TO_FP);
2613480093f4SDimitry Andric   EVT DestVT = Node->getValueType(0);
2614480093f4SDimitry Andric   SDLoc dl(Node);
2615480093f4SDimitry Andric   unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2616480093f4SDimitry Andric   SDValue Op0 = Node->getOperand(OpNo);
26170b57cec5SDimitry Andric   EVT SrcVT = Op0.getValueType();
26180b57cec5SDimitry Andric 
26190b57cec5SDimitry Andric   // TODO: Should any fast-math-flags be set for the created nodes?
26200b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2621e8d8bef9SDimitry Andric   if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2622e8d8bef9SDimitry Andric       (DestVT.bitsLE(MVT::f64) ||
2623e8d8bef9SDimitry Andric        TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2624e8d8bef9SDimitry Andric                                                      : ISD::FP_EXTEND,
2625e8d8bef9SDimitry Andric                             DestVT))) {
26260b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
26270b57cec5SDimitry Andric                          "expansion\n");
26280b57cec5SDimitry Andric 
26290b57cec5SDimitry Andric     // Get the stack frame index of a 8 byte buffer.
26300b57cec5SDimitry Andric     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
26310b57cec5SDimitry Andric 
26325ffd83dbSDimitry Andric     SDValue Lo = Op0;
26330b57cec5SDimitry Andric     // if signed map to unsigned space
26340b57cec5SDimitry Andric     if (isSigned) {
26355ffd83dbSDimitry Andric       // Invert sign bit (signed to unsigned mapping).
26365ffd83dbSDimitry Andric       Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
26375ffd83dbSDimitry Andric                        DAG.getConstant(0x80000000u, dl, MVT::i32));
26380b57cec5SDimitry Andric     }
26395ffd83dbSDimitry Andric     // Initial hi portion of constructed double.
26405ffd83dbSDimitry Andric     SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
26415ffd83dbSDimitry Andric 
26425ffd83dbSDimitry Andric     // If this a big endian target, swap the lo and high data.
26435ffd83dbSDimitry Andric     if (DAG.getDataLayout().isBigEndian())
26445ffd83dbSDimitry Andric       std::swap(Lo, Hi);
26455ffd83dbSDimitry Andric 
26465ffd83dbSDimitry Andric     SDValue MemChain = DAG.getEntryNode();
26475ffd83dbSDimitry Andric 
26485ffd83dbSDimitry Andric     // Store the lo of the constructed double.
26495ffd83dbSDimitry Andric     SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
26500b57cec5SDimitry Andric                                   MachinePointerInfo());
26515ffd83dbSDimitry Andric     // Store the hi of the constructed double.
2652e8d8bef9SDimitry Andric     SDValue HiPtr = DAG.getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), dl);
26530b57cec5SDimitry Andric     SDValue Store2 =
26545ffd83dbSDimitry Andric         DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
26555ffd83dbSDimitry Andric     MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
26565ffd83dbSDimitry Andric 
26570b57cec5SDimitry Andric     // load the constructed double
26580b57cec5SDimitry Andric     SDValue Load =
26595ffd83dbSDimitry Andric         DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
26600b57cec5SDimitry Andric     // FP constant to bias correct the final result
2661*06c3fb27SDimitry Andric     SDValue Bias = DAG.getConstantFP(
2662*06c3fb27SDimitry Andric         isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
2663*06c3fb27SDimitry Andric                  : llvm::bit_cast<double>(0x4330000000000000ULL),
26640b57cec5SDimitry Andric         dl, MVT::f64);
2665480093f4SDimitry Andric     // Subtract the bias and get the final result.
2666480093f4SDimitry Andric     SDValue Sub;
2667480093f4SDimitry Andric     SDValue Result;
2668480093f4SDimitry Andric     if (Node->isStrictFPOpcode()) {
2669480093f4SDimitry Andric       Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2670480093f4SDimitry Andric                         {Node->getOperand(0), Load, Bias});
2671480093f4SDimitry Andric       Chain = Sub.getValue(1);
2672480093f4SDimitry Andric       if (DestVT != Sub.getValueType()) {
2673480093f4SDimitry Andric         std::pair<SDValue, SDValue> ResultPair;
2674480093f4SDimitry Andric         ResultPair =
2675480093f4SDimitry Andric             DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2676480093f4SDimitry Andric         Result = ResultPair.first;
2677480093f4SDimitry Andric         Chain = ResultPair.second;
2678480093f4SDimitry Andric       }
2679480093f4SDimitry Andric       else
2680480093f4SDimitry Andric         Result = Sub;
2681480093f4SDimitry Andric     } else {
2682480093f4SDimitry Andric       Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2683480093f4SDimitry Andric       Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2684480093f4SDimitry Andric     }
26850b57cec5SDimitry Andric     return Result;
26860b57cec5SDimitry Andric   }
2687e8d8bef9SDimitry Andric 
2688e8d8bef9SDimitry Andric   if (isSigned)
2689e8d8bef9SDimitry Andric     return SDValue();
26905ffd83dbSDimitry Andric 
26915ffd83dbSDimitry Andric   // TODO: Generalize this for use with other types.
2692e8d8bef9SDimitry Andric   if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2693e8d8bef9SDimitry Andric       (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2694e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
26955ffd83dbSDimitry Andric     // For unsigned conversions, convert them to signed conversions using the
26965ffd83dbSDimitry Andric     // algorithm from the x86_64 __floatundisf in compiler_rt. That method
26975ffd83dbSDimitry Andric     // should be valid for i32->f32 as well.
26985ffd83dbSDimitry Andric 
2699e8d8bef9SDimitry Andric     // More generally this transform should be valid if there are 3 more bits
2700e8d8bef9SDimitry Andric     // in the integer type than the significand. Rounding uses the first bit
2701e8d8bef9SDimitry Andric     // after the width of the significand and the OR of all bits after that. So
2702e8d8bef9SDimitry Andric     // we need to be able to OR the shifted out bit into one of the bits that
2703e8d8bef9SDimitry Andric     // participate in the OR.
2704e8d8bef9SDimitry Andric 
27055ffd83dbSDimitry Andric     // TODO: This really should be implemented using a branch rather than a
27065ffd83dbSDimitry Andric     // select.  We happen to get lucky and machinesink does the right
27075ffd83dbSDimitry Andric     // thing most of the time.  This would be a good candidate for a
27085ffd83dbSDimitry Andric     // pseudo-op, or, even better, for whole-function isel.
27095ffd83dbSDimitry Andric     EVT SetCCVT = getSetCCResultType(SrcVT);
27105ffd83dbSDimitry Andric 
27115ffd83dbSDimitry Andric     SDValue SignBitTest = DAG.getSetCC(
27125ffd83dbSDimitry Andric         dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
27135ffd83dbSDimitry Andric 
27145ffd83dbSDimitry Andric     EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout());
27155ffd83dbSDimitry Andric     SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
27165ffd83dbSDimitry Andric     SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
27175ffd83dbSDimitry Andric     SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
27185ffd83dbSDimitry Andric     SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
27195ffd83dbSDimitry Andric     SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
27205ffd83dbSDimitry Andric 
27215ffd83dbSDimitry Andric     SDValue Slow, Fast;
27225ffd83dbSDimitry Andric     if (Node->isStrictFPOpcode()) {
27235ffd83dbSDimitry Andric       // In strict mode, we must avoid spurious exceptions, and therefore
27245ffd83dbSDimitry Andric       // must make sure to only emit a single STRICT_SINT_TO_FP.
27255ffd83dbSDimitry Andric       SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
27265ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
27275ffd83dbSDimitry Andric                          { Node->getOperand(0), InCvt });
27285ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
27295ffd83dbSDimitry Andric                          { Fast.getValue(1), Fast, Fast });
27305ffd83dbSDimitry Andric       Chain = Slow.getValue(1);
27315ffd83dbSDimitry Andric       // The STRICT_SINT_TO_FP inherits the exception mode from the
27325ffd83dbSDimitry Andric       // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
27335ffd83dbSDimitry Andric       // never raise any exception.
27345ffd83dbSDimitry Andric       SDNodeFlags Flags;
27355ffd83dbSDimitry Andric       Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
27365ffd83dbSDimitry Andric       Fast->setFlags(Flags);
27375ffd83dbSDimitry Andric       Flags.setNoFPExcept(true);
27385ffd83dbSDimitry Andric       Slow->setFlags(Flags);
27395ffd83dbSDimitry Andric     } else {
27405ffd83dbSDimitry Andric       SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
27415ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
27425ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
27435ffd83dbSDimitry Andric     }
27445ffd83dbSDimitry Andric 
27455ffd83dbSDimitry Andric     return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
27465ffd83dbSDimitry Andric   }
27475ffd83dbSDimitry Andric 
2748e8d8bef9SDimitry Andric   // Don't expand it if there isn't cheap fadd.
2749e8d8bef9SDimitry Andric   if (!TLI.isOperationLegalOrCustom(
2750e8d8bef9SDimitry Andric           Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2751e8d8bef9SDimitry Andric     return SDValue();
2752e8d8bef9SDimitry Andric 
27535ffd83dbSDimitry Andric   // The following optimization is valid only if every value in SrcVT (when
27545ffd83dbSDimitry Andric   // treated as signed) is representable in DestVT.  Check that the mantissa
27555ffd83dbSDimitry Andric   // size of DestVT is >= than the number of bits in SrcVT -1.
27565ffd83dbSDimitry Andric   assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >=
27575ffd83dbSDimitry Andric              SrcVT.getSizeInBits() - 1 &&
27585ffd83dbSDimitry Andric          "Cannot perform lossless SINT_TO_FP!");
27590b57cec5SDimitry Andric 
2760480093f4SDimitry Andric   SDValue Tmp1;
2761480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2762480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2763480093f4SDimitry Andric                        { Node->getOperand(0), Op0 });
2764480093f4SDimitry Andric   } else
2765480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric   SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
27680b57cec5SDimitry Andric                                  DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
27690b57cec5SDimitry Andric   SDValue Zero = DAG.getIntPtrConstant(0, dl),
27700b57cec5SDimitry Andric           Four = DAG.getIntPtrConstant(4, dl);
27710b57cec5SDimitry Andric   SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
27720b57cec5SDimitry Andric                                     SignSet, Four, Zero);
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric   // If the sign bit of the integer is set, the large number will be treated
27750b57cec5SDimitry Andric   // as a negative number.  To counteract this, the dynamic code adds an
27760b57cec5SDimitry Andric   // offset depending on the data type.
27770b57cec5SDimitry Andric   uint64_t FF;
27780b57cec5SDimitry Andric   switch (SrcVT.getSimpleVT().SimpleTy) {
2779e8d8bef9SDimitry Andric   default:
2780e8d8bef9SDimitry Andric     return SDValue();
27810b57cec5SDimitry Andric   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
27820b57cec5SDimitry Andric   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
27830b57cec5SDimitry Andric   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
27840b57cec5SDimitry Andric   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
27850b57cec5SDimitry Andric   }
27860b57cec5SDimitry Andric   if (DAG.getDataLayout().isLittleEndian())
27870b57cec5SDimitry Andric     FF <<= 32;
27880b57cec5SDimitry Andric   Constant *FudgeFactor = ConstantInt::get(
27890b57cec5SDimitry Andric                                        Type::getInt64Ty(*DAG.getContext()), FF);
27900b57cec5SDimitry Andric 
27910b57cec5SDimitry Andric   SDValue CPIdx =
27920b57cec5SDimitry Andric       DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
27935ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
27940b57cec5SDimitry Andric   CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
27955ffd83dbSDimitry Andric   Alignment = commonAlignment(Alignment, 4);
27960b57cec5SDimitry Andric   SDValue FudgeInReg;
27970b57cec5SDimitry Andric   if (DestVT == MVT::f32)
27980b57cec5SDimitry Andric     FudgeInReg = DAG.getLoad(
27990b57cec5SDimitry Andric         MVT::f32, dl, DAG.getEntryNode(), CPIdx,
28000b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
28010b57cec5SDimitry Andric         Alignment);
28020b57cec5SDimitry Andric   else {
28030b57cec5SDimitry Andric     SDValue Load = DAG.getExtLoad(
28040b57cec5SDimitry Andric         ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
28050b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
28060b57cec5SDimitry Andric         Alignment);
28070b57cec5SDimitry Andric     HandleSDNode Handle(Load);
28080b57cec5SDimitry Andric     LegalizeOp(Load.getNode());
28090b57cec5SDimitry Andric     FudgeInReg = Handle.getValue();
28100b57cec5SDimitry Andric   }
28110b57cec5SDimitry Andric 
2812480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2813480093f4SDimitry Andric     SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2814480093f4SDimitry Andric                                  { Tmp1.getValue(1), Tmp1, FudgeInReg });
2815480093f4SDimitry Andric     Chain = Result.getValue(1);
2816480093f4SDimitry Andric     return Result;
2817480093f4SDimitry Andric   }
2818480093f4SDimitry Andric 
28190b57cec5SDimitry Andric   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
28200b57cec5SDimitry Andric }
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric /// This function is responsible for legalizing a
28230b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that
28240b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
28250b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
28260b57cec5SDimitry Andric /// operation that takes a larger input.
2827480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2828480093f4SDimitry Andric     SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) {
2829480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2830480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2831480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2832480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2833480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2834480093f4SDimitry Andric   unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2835480093f4SDimitry Andric   unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2836480093f4SDimitry Andric 
28370b57cec5SDimitry Andric   // First step, figure out the appropriate *INT_TO_FP operation to use.
28380b57cec5SDimitry Andric   EVT NewInTy = LegalOp.getValueType();
28390b57cec5SDimitry Andric 
28400b57cec5SDimitry Andric   unsigned OpToUse = 0;
28410b57cec5SDimitry Andric 
28420b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
28430b57cec5SDimitry Andric   while (true) {
28440b57cec5SDimitry Andric     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
28450b57cec5SDimitry Andric     assert(NewInTy.isInteger() && "Ran out of possibilities!");
28460b57cec5SDimitry Andric 
28470b57cec5SDimitry Andric     // If the target supports SINT_TO_FP of this type, use it.
2848480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2849480093f4SDimitry Andric       OpToUse = SIntOp;
28500b57cec5SDimitry Andric       break;
28510b57cec5SDimitry Andric     }
2852480093f4SDimitry Andric     if (IsSigned)
2853480093f4SDimitry Andric       continue;
28540b57cec5SDimitry Andric 
28550b57cec5SDimitry Andric     // If the target supports UINT_TO_FP of this type, use it.
2856480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2857480093f4SDimitry Andric       OpToUse = UIntOp;
28580b57cec5SDimitry Andric       break;
28590b57cec5SDimitry Andric     }
28600b57cec5SDimitry Andric 
28610b57cec5SDimitry Andric     // Otherwise, try a larger type.
28620b57cec5SDimitry Andric   }
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric   // Okay, we found the operation and type to use.  Zero extend our input to the
28650b57cec5SDimitry Andric   // desired type then run the operation on it.
2866480093f4SDimitry Andric   if (IsStrict) {
2867480093f4SDimitry Andric     SDValue Res =
2868480093f4SDimitry Andric         DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2869480093f4SDimitry Andric                     {N->getOperand(0),
2870480093f4SDimitry Andric                      DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2871480093f4SDimitry Andric                                  dl, NewInTy, LegalOp)});
2872480093f4SDimitry Andric     Results.push_back(Res);
2873480093f4SDimitry Andric     Results.push_back(Res.getValue(1));
2874480093f4SDimitry Andric     return;
2875480093f4SDimitry Andric   }
2876480093f4SDimitry Andric 
2877480093f4SDimitry Andric   Results.push_back(
2878480093f4SDimitry Andric       DAG.getNode(OpToUse, dl, DestVT,
2879480093f4SDimitry Andric                   DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2880480093f4SDimitry Andric                               dl, NewInTy, LegalOp)));
28810b57cec5SDimitry Andric }
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric /// This function is responsible for legalizing a
28840b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that
28850b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
28860b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
28870b57cec5SDimitry Andric /// operation that returns a larger result.
2888480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2889480093f4SDimitry Andric                                                  SmallVectorImpl<SDValue> &Results) {
2890480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2891480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2892480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2893480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2894480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
28950b57cec5SDimitry Andric   // First step, figure out the appropriate FP_TO*INT operation to use.
28960b57cec5SDimitry Andric   EVT NewOutTy = DestVT;
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   unsigned OpToUse = 0;
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
29010b57cec5SDimitry Andric   while (true) {
29020b57cec5SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
29030b57cec5SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
29040b57cec5SDimitry Andric 
29050b57cec5SDimitry Andric     // A larger signed type can hold all unsigned values of the requested type,
29060b57cec5SDimitry Andric     // so using FP_TO_SINT is valid
2907480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2908480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29090b57cec5SDimitry Andric       break;
29100b57cec5SDimitry Andric 
29110b57cec5SDimitry Andric     // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2912480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2913480093f4SDimitry Andric     if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29140b57cec5SDimitry Andric       break;
29150b57cec5SDimitry Andric 
29160b57cec5SDimitry Andric     // Otherwise, try a larger type.
29170b57cec5SDimitry Andric   }
29180b57cec5SDimitry Andric 
29190b57cec5SDimitry Andric   // Okay, we found the operation and type to use.
2920480093f4SDimitry Andric   SDValue Operation;
2921480093f4SDimitry Andric   if (IsStrict) {
2922480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2923480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2924480093f4SDimitry Andric   } else
2925480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
29260b57cec5SDimitry Andric 
29270b57cec5SDimitry Andric   // Truncate the result of the extended FP_TO_*INT operation to the desired
29280b57cec5SDimitry Andric   // size.
2929480093f4SDimitry Andric   SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2930480093f4SDimitry Andric   Results.push_back(Trunc);
2931480093f4SDimitry Andric   if (IsStrict)
2932480093f4SDimitry Andric     Results.push_back(Operation.getValue(1));
29330b57cec5SDimitry Andric }
29340b57cec5SDimitry Andric 
2935e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2936e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal
2937e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type.
2938e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2939e8d8bef9SDimitry Andric                                                         const SDLoc &dl) {
2940e8d8bef9SDimitry Andric   unsigned Opcode = Node->getOpcode();
2941e8d8bef9SDimitry Andric 
2942e8d8bef9SDimitry Andric   // Scan for the appropriate larger type to use.
2943e8d8bef9SDimitry Andric   EVT NewOutTy = Node->getValueType(0);
2944e8d8bef9SDimitry Andric   while (true) {
2945e8d8bef9SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2946e8d8bef9SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2947e8d8bef9SDimitry Andric 
2948e8d8bef9SDimitry Andric     if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2949e8d8bef9SDimitry Andric       break;
2950e8d8bef9SDimitry Andric   }
2951e8d8bef9SDimitry Andric 
2952e8d8bef9SDimitry Andric   // Saturation width is determined by second operand, so we don't have to
2953e8d8bef9SDimitry Andric   // perform any fixup and can directly truncate the result.
2954e8d8bef9SDimitry Andric   SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
2955e8d8bef9SDimitry Andric                                Node->getOperand(1));
2956e8d8bef9SDimitry Andric   return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2957e8d8bef9SDimitry Andric }
2958e8d8bef9SDimitry Andric 
2959e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation.
2960e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
2961e8d8bef9SDimitry Andric   EVT VT = Op.getValueType();
2962e8d8bef9SDimitry Andric   EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2963e8d8bef9SDimitry Andric   unsigned Sz = VT.getScalarSizeInBits();
2964e8d8bef9SDimitry Andric 
2965e8d8bef9SDimitry Andric   // If CTPOP is legal, use it. Otherwise use shifts and xor.
2966e8d8bef9SDimitry Andric   SDValue Result;
2967349cc55cSDimitry Andric   if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) {
2968e8d8bef9SDimitry Andric     Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
2969e8d8bef9SDimitry Andric   } else {
2970e8d8bef9SDimitry Andric     Result = Op;
2971e8d8bef9SDimitry Andric     for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
2972e8d8bef9SDimitry Andric       SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
2973e8d8bef9SDimitry Andric                                   DAG.getConstant(1ULL << (--i), dl, ShVT));
2974e8d8bef9SDimitry Andric       Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
2975e8d8bef9SDimitry Andric     }
2976e8d8bef9SDimitry Andric   }
2977e8d8bef9SDimitry Andric 
2978e8d8bef9SDimitry Andric   return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
2979e8d8bef9SDimitry Andric }
2980e8d8bef9SDimitry Andric 
29810b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
29820b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to expand node\n");
29830b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
29840b57cec5SDimitry Andric   SDLoc dl(Node);
29850b57cec5SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
29860b57cec5SDimitry Andric   bool NeedInvert;
29870b57cec5SDimitry Andric   switch (Node->getOpcode()) {
29880b57cec5SDimitry Andric   case ISD::ABS:
2989349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandABS(Node, DAG)))
29900b57cec5SDimitry Andric       Results.push_back(Tmp1);
29910b57cec5SDimitry Andric     break;
2992*06c3fb27SDimitry Andric   case ISD::ABDS:
2993*06c3fb27SDimitry Andric   case ISD::ABDU:
2994*06c3fb27SDimitry Andric     if ((Tmp1 = TLI.expandABD(Node, DAG)))
2995*06c3fb27SDimitry Andric       Results.push_back(Tmp1);
2996*06c3fb27SDimitry Andric     break;
29970b57cec5SDimitry Andric   case ISD::CTPOP:
2998349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
29990b57cec5SDimitry Andric       Results.push_back(Tmp1);
30000b57cec5SDimitry Andric     break;
30010b57cec5SDimitry Andric   case ISD::CTLZ:
30020b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
3003349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
30040b57cec5SDimitry Andric       Results.push_back(Tmp1);
30050b57cec5SDimitry Andric     break;
30060b57cec5SDimitry Andric   case ISD::CTTZ:
30070b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
3008349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
30090b57cec5SDimitry Andric       Results.push_back(Tmp1);
30100b57cec5SDimitry Andric     break;
30110b57cec5SDimitry Andric   case ISD::BITREVERSE:
3012fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3013fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30140b57cec5SDimitry Andric     break;
30150b57cec5SDimitry Andric   case ISD::BSWAP:
3016fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3017fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30180b57cec5SDimitry Andric     break;
3019e8d8bef9SDimitry Andric   case ISD::PARITY:
3020e8d8bef9SDimitry Andric     Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3021e8d8bef9SDimitry Andric     break;
30220b57cec5SDimitry Andric   case ISD::FRAMEADDR:
30230b57cec5SDimitry Andric   case ISD::RETURNADDR:
30240b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
30250b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
30260b57cec5SDimitry Andric     break;
30270b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA: {
30280b57cec5SDimitry Andric     SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
30290b57cec5SDimitry Andric                                         TLI.getPointerTy(DAG.getDataLayout()));
30300b57cec5SDimitry Andric     SDValue Offset = DAG.getNode(ISD::ADD, dl,
30310b57cec5SDimitry Andric                                  CfaArg.getValueType(),
30320b57cec5SDimitry Andric                                  DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
30330b57cec5SDimitry Andric                                              CfaArg.getValueType()),
30340b57cec5SDimitry Andric                                  CfaArg);
30350b57cec5SDimitry Andric     SDValue FA = DAG.getNode(
30360b57cec5SDimitry Andric         ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
30370b57cec5SDimitry Andric         DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
30380b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
30390b57cec5SDimitry Andric                                   FA, Offset));
30400b57cec5SDimitry Andric     break;
30410b57cec5SDimitry Andric   }
3042bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
30430b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
30445ffd83dbSDimitry Andric     Results.push_back(Node->getOperand(0));
30450b57cec5SDimitry Andric     break;
30460b57cec5SDimitry Andric   case ISD::EH_RETURN:
30470b57cec5SDimitry Andric   case ISD::EH_LABEL:
30480b57cec5SDimitry Andric   case ISD::PREFETCH:
30490b57cec5SDimitry Andric   case ISD::VAEND:
30500b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
30510b57cec5SDimitry Andric     // If the target didn't expand these, there's nothing to do, so just
30520b57cec5SDimitry Andric     // preserve the chain and be done.
30530b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30540b57cec5SDimitry Andric     break;
30550b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
30560b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
30570b57cec5SDimitry Andric     // chain.
30580b57cec5SDimitry Andric     Results.append(Node->getNumValues() - 1,
30590b57cec5SDimitry Andric                    DAG.getConstant(0, dl, Node->getValueType(0)));
30600b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30610b57cec5SDimitry Andric     break;
30620b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
30630b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
30640b57cec5SDimitry Andric     // chain.
30650b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, MVT::i32));
30660b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30670b57cec5SDimitry Andric     break;
30680b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD: {
30690b57cec5SDimitry Andric     // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
30700b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
30710b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
30720b57cec5SDimitry Andric     SDValue Swap = DAG.getAtomicCmpSwap(
30730b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
30740b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Zero, Zero,
30750b57cec5SDimitry Andric         cast<AtomicSDNode>(Node)->getMemOperand());
30760b57cec5SDimitry Andric     Results.push_back(Swap.getValue(0));
30770b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
30780b57cec5SDimitry Andric     break;
30790b57cec5SDimitry Andric   }
30800b57cec5SDimitry Andric   case ISD::ATOMIC_STORE: {
30810b57cec5SDimitry Andric     // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
30820b57cec5SDimitry Andric     SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
30830b57cec5SDimitry Andric                                  cast<AtomicSDNode>(Node)->getMemoryVT(),
30840b57cec5SDimitry Andric                                  Node->getOperand(0),
30850b57cec5SDimitry Andric                                  Node->getOperand(1), Node->getOperand(2),
30860b57cec5SDimitry Andric                                  cast<AtomicSDNode>(Node)->getMemOperand());
30870b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
30880b57cec5SDimitry Andric     break;
30890b57cec5SDimitry Andric   }
30900b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
30910b57cec5SDimitry Andric     // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
30920b57cec5SDimitry Andric     // splits out the success value as a comparison. Expanding the resulting
30930b57cec5SDimitry Andric     // ATOMIC_CMP_SWAP will produce a libcall.
30940b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
30950b57cec5SDimitry Andric     SDValue Res = DAG.getAtomicCmpSwap(
30960b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
30970b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
30980b57cec5SDimitry Andric         Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
30990b57cec5SDimitry Andric 
31000b57cec5SDimitry Andric     SDValue ExtRes = Res;
31010b57cec5SDimitry Andric     SDValue LHS = Res;
31020b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
31030b57cec5SDimitry Andric 
31040b57cec5SDimitry Andric     EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
31050b57cec5SDimitry Andric     EVT OuterType = Node->getValueType(0);
31060b57cec5SDimitry Andric     switch (TLI.getExtendForAtomicOps()) {
31070b57cec5SDimitry Andric     case ISD::SIGN_EXTEND:
31080b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
31090b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31100b57cec5SDimitry Andric       RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
31110b57cec5SDimitry Andric                         Node->getOperand(2), DAG.getValueType(AtomicType));
31120b57cec5SDimitry Andric       ExtRes = LHS;
31130b57cec5SDimitry Andric       break;
31140b57cec5SDimitry Andric     case ISD::ZERO_EXTEND:
31150b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
31160b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31170b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
31180b57cec5SDimitry Andric       ExtRes = LHS;
31190b57cec5SDimitry Andric       break;
31200b57cec5SDimitry Andric     case ISD::ANY_EXTEND:
31210b57cec5SDimitry Andric       LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
31220b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
31230b57cec5SDimitry Andric       break;
31240b57cec5SDimitry Andric     default:
31250b57cec5SDimitry Andric       llvm_unreachable("Invalid atomic op extension");
31260b57cec5SDimitry Andric     }
31270b57cec5SDimitry Andric 
31280b57cec5SDimitry Andric     SDValue Success =
31290b57cec5SDimitry Andric         DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
31300b57cec5SDimitry Andric 
31310b57cec5SDimitry Andric     Results.push_back(ExtRes.getValue(0));
31320b57cec5SDimitry Andric     Results.push_back(Success);
31330b57cec5SDimitry Andric     Results.push_back(Res.getValue(1));
31340b57cec5SDimitry Andric     break;
31350b57cec5SDimitry Andric   }
31360b57cec5SDimitry Andric   case ISD::DYNAMIC_STACKALLOC:
31370b57cec5SDimitry Andric     ExpandDYNAMIC_STACKALLOC(Node, Results);
31380b57cec5SDimitry Andric     break;
31390b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
31400b57cec5SDimitry Andric     for (unsigned i = 0; i < Node->getNumValues(); i++)
31410b57cec5SDimitry Andric       Results.push_back(Node->getOperand(i));
31420b57cec5SDimitry Andric     break;
31430b57cec5SDimitry Andric   case ISD::UNDEF: {
31440b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
31450b57cec5SDimitry Andric     if (VT.isInteger())
31460b57cec5SDimitry Andric       Results.push_back(DAG.getConstant(0, dl, VT));
31470b57cec5SDimitry Andric     else {
31480b57cec5SDimitry Andric       assert(VT.isFloatingPoint() && "Unknown value type!");
31490b57cec5SDimitry Andric       Results.push_back(DAG.getConstantFP(0, dl, VT));
31500b57cec5SDimitry Andric     }
31510b57cec5SDimitry Andric     break;
31520b57cec5SDimitry Andric   }
31530b57cec5SDimitry Andric   case ISD::STRICT_FP_ROUND:
3154480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3155480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3156480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3157480093f4SDimitry Andric       break;
3158480093f4SDimitry Andric     // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3159480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
31608bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
31618bcb0991SDimitry Andric                                        Node->getValueType(0))
31628bcb0991SDimitry Andric         == TargetLowering::Legal)
31638bcb0991SDimitry Andric       break;
3164480093f4SDimitry Andric     // We fall back to use stack operation when the FP_ROUND operation
3165480093f4SDimitry Andric     // isn't available.
3166e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3167e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl,
3168e8d8bef9SDimitry Andric                                  Node->getOperand(0)))) {
31690b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
31700b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
31710b57cec5SDimitry Andric       return true;
3172e8d8bef9SDimitry Andric     }
3173e8d8bef9SDimitry Andric     break;
31740b57cec5SDimitry Andric   case ISD::FP_ROUND:
31750b57cec5SDimitry Andric   case ISD::BITCAST:
3176e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3177e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl)))
31780b57cec5SDimitry Andric       Results.push_back(Tmp1);
31790b57cec5SDimitry Andric     break;
31800b57cec5SDimitry Andric   case ISD::STRICT_FP_EXTEND:
3181480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3182480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3183480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3184480093f4SDimitry Andric       break;
3185480093f4SDimitry Andric     // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3186480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
31878bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
31888bcb0991SDimitry Andric                                        Node->getValueType(0))
31898bcb0991SDimitry Andric         == TargetLowering::Legal)
31908bcb0991SDimitry Andric       break;
3191480093f4SDimitry Andric     // We fall back to use stack operation when the FP_EXTEND operation
3192480093f4SDimitry Andric     // isn't available.
3193e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(
3194e8d8bef9SDimitry Andric              Node->getOperand(1), Node->getOperand(1).getValueType(),
3195e8d8bef9SDimitry Andric              Node->getValueType(0), dl, Node->getOperand(0)))) {
31960b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
31970b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
31980b57cec5SDimitry Andric       return true;
3199e8d8bef9SDimitry Andric     }
3200e8d8bef9SDimitry Andric     break;
32010b57cec5SDimitry Andric   case ISD::FP_EXTEND:
3202e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(0),
32030b57cec5SDimitry Andric                                  Node->getOperand(0).getValueType(),
3204e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl)))
32050b57cec5SDimitry Andric       Results.push_back(Tmp1);
32060b57cec5SDimitry Andric     break;
320781ad6265SDimitry Andric   case ISD::BF16_TO_FP: {
320881ad6265SDimitry Andric     // Always expand bf16 to f32 casts, they lower to ext + shift.
3209bdd1243dSDimitry Andric     //
3210bdd1243dSDimitry Andric     // Note that the operand of this code can be bf16 or an integer type in case
3211bdd1243dSDimitry Andric     // bf16 is not supported on the target and was softened.
3212bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3213bdd1243dSDimitry Andric     if (Op.getValueType() == MVT::bf16) {
3214bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3215bdd1243dSDimitry Andric                        DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3216bdd1243dSDimitry Andric     } else {
3217bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3218bdd1243dSDimitry Andric     }
321981ad6265SDimitry Andric     Op = DAG.getNode(
322081ad6265SDimitry Andric         ISD::SHL, dl, MVT::i32, Op,
322181ad6265SDimitry Andric         DAG.getConstant(16, dl,
322281ad6265SDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
322381ad6265SDimitry Andric     Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3224bdd1243dSDimitry Andric     // Add fp_extend in case the output is bigger than f32.
3225bdd1243dSDimitry Andric     if (Node->getValueType(0) != MVT::f32)
3226bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3227bdd1243dSDimitry Andric     Results.push_back(Op);
3228bdd1243dSDimitry Andric     break;
3229bdd1243dSDimitry Andric   }
3230bdd1243dSDimitry Andric   case ISD::FP_TO_BF16: {
3231bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3232bdd1243dSDimitry Andric     if (Op.getValueType() != MVT::f32)
3233bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3234bdd1243dSDimitry Andric                        DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3235bdd1243dSDimitry Andric     Op = DAG.getNode(
3236bdd1243dSDimitry Andric         ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3237bdd1243dSDimitry Andric         DAG.getConstant(16, dl,
3238bdd1243dSDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3239bdd1243dSDimitry Andric     // The result of this node can be bf16 or an integer type in case bf16 is
3240bdd1243dSDimitry Andric     // not supported on the target and was softened to i16 for storage.
3241bdd1243dSDimitry Andric     if (Node->getValueType(0) == MVT::bf16) {
3242bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3243bdd1243dSDimitry Andric                        DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3244bdd1243dSDimitry Andric     } else {
3245bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3246bdd1243dSDimitry Andric     }
324781ad6265SDimitry Andric     Results.push_back(Op);
324881ad6265SDimitry Andric     break;
324981ad6265SDimitry Andric   }
32500b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
32510b57cec5SDimitry Andric     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
32520b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
32530b57cec5SDimitry Andric 
32540b57cec5SDimitry Andric     // An in-register sign-extend of a boolean is a negation:
32550b57cec5SDimitry Andric     // 'true' (1) sign-extended is -1.
32560b57cec5SDimitry Andric     // 'false' (0) sign-extended is 0.
32570b57cec5SDimitry Andric     // However, we must mask the high bits of the source operand because the
32580b57cec5SDimitry Andric     // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
32590b57cec5SDimitry Andric 
32600b57cec5SDimitry Andric     // TODO: Do this for vectors too?
326181ad6265SDimitry Andric     if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
32620b57cec5SDimitry Andric       SDValue One = DAG.getConstant(1, dl, VT);
32630b57cec5SDimitry Andric       SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
32640b57cec5SDimitry Andric       SDValue Zero = DAG.getConstant(0, dl, VT);
32650b57cec5SDimitry Andric       SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
32660b57cec5SDimitry Andric       Results.push_back(Neg);
32670b57cec5SDimitry Andric       break;
32680b57cec5SDimitry Andric     }
32690b57cec5SDimitry Andric 
32700b57cec5SDimitry Andric     // NOTE: we could fall back on load/store here too for targets without
32710b57cec5SDimitry Andric     // SRA.  However, it is doubtful that any exist.
32720b57cec5SDimitry Andric     EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
32730b57cec5SDimitry Andric     unsigned BitsDiff = VT.getScalarSizeInBits() -
32740b57cec5SDimitry Andric                         ExtraVT.getScalarSizeInBits();
32750b57cec5SDimitry Andric     SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
32760b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
32770b57cec5SDimitry Andric                        Node->getOperand(0), ShiftCst);
32780b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
32790b57cec5SDimitry Andric     Results.push_back(Tmp1);
32800b57cec5SDimitry Andric     break;
32810b57cec5SDimitry Andric   }
32820b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
3283480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
3284480093f4SDimitry Andric     if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
32850b57cec5SDimitry Andric       Results.push_back(Tmp1);
3286480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3287480093f4SDimitry Andric         Results.push_back(Tmp2);
32880b57cec5SDimitry Andric       break;
32890b57cec5SDimitry Andric     }
3290bdd1243dSDimitry Andric     [[fallthrough]];
32910b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
3292480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
3293e8d8bef9SDimitry Andric     if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
32940b57cec5SDimitry Andric       Results.push_back(Tmp1);
3295480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3296480093f4SDimitry Andric         Results.push_back(Tmp2);
3297e8d8bef9SDimitry Andric     }
32980b57cec5SDimitry Andric     break;
32990b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
33000b57cec5SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
33010b57cec5SDimitry Andric       Results.push_back(Tmp1);
33020b57cec5SDimitry Andric     break;
33038bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
33048bcb0991SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
33058bcb0991SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
33068bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
33078bcb0991SDimitry Andric       return true;
33088bcb0991SDimitry Andric     }
33098bcb0991SDimitry Andric     break;
33100b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
33118bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
33120b57cec5SDimitry Andric       Results.push_back(Tmp1);
33130b57cec5SDimitry Andric     break;
33148bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
33158bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
33168bcb0991SDimitry Andric       // Relink the chain.
33178bcb0991SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
33188bcb0991SDimitry Andric       // Replace the new UINT result.
33198bcb0991SDimitry Andric       ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
33208bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
33218bcb0991SDimitry Andric       return true;
33228bcb0991SDimitry Andric     }
33230b57cec5SDimitry Andric     break;
3324e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
3325e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
3326e8d8bef9SDimitry Andric     Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3327e8d8bef9SDimitry Andric     break;
33280b57cec5SDimitry Andric   case ISD::VAARG:
33290b57cec5SDimitry Andric     Results.push_back(DAG.expandVAArg(Node));
33300b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(1));
33310b57cec5SDimitry Andric     break;
33320b57cec5SDimitry Andric   case ISD::VACOPY:
33330b57cec5SDimitry Andric     Results.push_back(DAG.expandVACopy(Node));
33340b57cec5SDimitry Andric     break;
33350b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
33360b57cec5SDimitry Andric     if (Node->getOperand(0).getValueType().getVectorNumElements() == 1)
33370b57cec5SDimitry Andric       // This must be an access of the only element.  Return it.
33380b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
33390b57cec5SDimitry Andric                          Node->getOperand(0));
33400b57cec5SDimitry Andric     else
33410b57cec5SDimitry Andric       Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
33420b57cec5SDimitry Andric     Results.push_back(Tmp1);
33430b57cec5SDimitry Andric     break;
33440b57cec5SDimitry Andric   case ISD::EXTRACT_SUBVECTOR:
33450b57cec5SDimitry Andric     Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
33460b57cec5SDimitry Andric     break;
33470b57cec5SDimitry Andric   case ISD::INSERT_SUBVECTOR:
33480b57cec5SDimitry Andric     Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
33490b57cec5SDimitry Andric     break;
33500b57cec5SDimitry Andric   case ISD::CONCAT_VECTORS:
33510b57cec5SDimitry Andric     Results.push_back(ExpandVectorBuildThroughStack(Node));
33520b57cec5SDimitry Andric     break;
33530b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR:
33540b57cec5SDimitry Andric     Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
33550b57cec5SDimitry Andric     break;
33560b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT:
33570b57cec5SDimitry Andric     Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
33580b57cec5SDimitry Andric                                               Node->getOperand(1),
33590b57cec5SDimitry Andric                                               Node->getOperand(2), dl));
33600b57cec5SDimitry Andric     break;
33610b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
33620b57cec5SDimitry Andric     SmallVector<int, 32> NewMask;
33630b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
33640b57cec5SDimitry Andric 
33650b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
33660b57cec5SDimitry Andric     EVT EltVT = VT.getVectorElementType();
33670b57cec5SDimitry Andric     SDValue Op0 = Node->getOperand(0);
33680b57cec5SDimitry Andric     SDValue Op1 = Node->getOperand(1);
33690b57cec5SDimitry Andric     if (!TLI.isTypeLegal(EltVT)) {
33700b57cec5SDimitry Andric       EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
33710b57cec5SDimitry Andric 
33720b57cec5SDimitry Andric       // BUILD_VECTOR operands are allowed to be wider than the element type.
33730b57cec5SDimitry Andric       // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
33740b57cec5SDimitry Andric       // it.
33750b57cec5SDimitry Andric       if (NewEltVT.bitsLT(EltVT)) {
33760b57cec5SDimitry Andric         // Convert shuffle node.
33770b57cec5SDimitry Andric         // If original node was v4i64 and the new EltVT is i32,
33780b57cec5SDimitry Andric         // cast operands to v8i32 and re-build the mask.
33790b57cec5SDimitry Andric 
33800b57cec5SDimitry Andric         // Calculate new VT, the size of the new VT should be equal to original.
33810b57cec5SDimitry Andric         EVT NewVT =
33820b57cec5SDimitry Andric             EVT::getVectorVT(*DAG.getContext(), NewEltVT,
33830b57cec5SDimitry Andric                              VT.getSizeInBits() / NewEltVT.getSizeInBits());
33840b57cec5SDimitry Andric         assert(NewVT.bitsEq(VT));
33850b57cec5SDimitry Andric 
33860b57cec5SDimitry Andric         // cast operands to new VT
33870b57cec5SDimitry Andric         Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
33880b57cec5SDimitry Andric         Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric         // Convert the shuffle mask
33910b57cec5SDimitry Andric         unsigned int factor =
33920b57cec5SDimitry Andric                          NewVT.getVectorNumElements()/VT.getVectorNumElements();
33930b57cec5SDimitry Andric 
33940b57cec5SDimitry Andric         // EltVT gets smaller
33950b57cec5SDimitry Andric         assert(factor > 0);
33960b57cec5SDimitry Andric 
33970b57cec5SDimitry Andric         for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
33980b57cec5SDimitry Andric           if (Mask[i] < 0) {
33990b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
34000b57cec5SDimitry Andric               NewMask.push_back(Mask[i]);
34010b57cec5SDimitry Andric           }
34020b57cec5SDimitry Andric           else {
34030b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
34040b57cec5SDimitry Andric               NewMask.push_back(Mask[i]*factor+fi);
34050b57cec5SDimitry Andric           }
34060b57cec5SDimitry Andric         }
34070b57cec5SDimitry Andric         Mask = NewMask;
34080b57cec5SDimitry Andric         VT = NewVT;
34090b57cec5SDimitry Andric       }
34100b57cec5SDimitry Andric       EltVT = NewEltVT;
34110b57cec5SDimitry Andric     }
34120b57cec5SDimitry Andric     unsigned NumElems = VT.getVectorNumElements();
34130b57cec5SDimitry Andric     SmallVector<SDValue, 16> Ops;
34140b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElems; ++i) {
34150b57cec5SDimitry Andric       if (Mask[i] < 0) {
34160b57cec5SDimitry Andric         Ops.push_back(DAG.getUNDEF(EltVT));
34170b57cec5SDimitry Andric         continue;
34180b57cec5SDimitry Andric       }
34190b57cec5SDimitry Andric       unsigned Idx = Mask[i];
34200b57cec5SDimitry Andric       if (Idx < NumElems)
34215ffd83dbSDimitry Andric         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
34225ffd83dbSDimitry Andric                                   DAG.getVectorIdxConstant(Idx, dl)));
34230b57cec5SDimitry Andric       else
34245ffd83dbSDimitry Andric         Ops.push_back(
34255ffd83dbSDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
34265ffd83dbSDimitry Andric                         DAG.getVectorIdxConstant(Idx - NumElems, dl)));
34270b57cec5SDimitry Andric     }
34280b57cec5SDimitry Andric 
34290b57cec5SDimitry Andric     Tmp1 = DAG.getBuildVector(VT, dl, Ops);
34300b57cec5SDimitry Andric     // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
34310b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
34320b57cec5SDimitry Andric     Results.push_back(Tmp1);
34330b57cec5SDimitry Andric     break;
34340b57cec5SDimitry Andric   }
3435fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
3436fe6060f1SDimitry Andric     Results.push_back(TLI.expandVectorSplice(Node, DAG));
3437fe6060f1SDimitry Andric     break;
3438fe6060f1SDimitry Andric   }
34390b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT: {
34400b57cec5SDimitry Andric     EVT OpTy = Node->getOperand(0).getValueType();
3441bdd1243dSDimitry Andric     if (Node->getConstantOperandVal(1)) {
34420b57cec5SDimitry Andric       // 1 -> Hi
34430b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
34440b57cec5SDimitry Andric                          DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
34450b57cec5SDimitry Andric                                          TLI.getShiftAmountTy(
34460b57cec5SDimitry Andric                                              Node->getOperand(0).getValueType(),
34470b57cec5SDimitry Andric                                              DAG.getDataLayout())));
34480b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
34490b57cec5SDimitry Andric     } else {
34500b57cec5SDimitry Andric       // 0 -> Lo
34510b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
34520b57cec5SDimitry Andric                          Node->getOperand(0));
34530b57cec5SDimitry Andric     }
34540b57cec5SDimitry Andric     Results.push_back(Tmp1);
34550b57cec5SDimitry Andric     break;
34560b57cec5SDimitry Andric   }
34570b57cec5SDimitry Andric   case ISD::STACKSAVE:
34580b57cec5SDimitry Andric     // Expand to CopyFromReg if the target set
34590b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3460e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
34610b57cec5SDimitry Andric       Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
34620b57cec5SDimitry Andric                                            Node->getValueType(0)));
34630b57cec5SDimitry Andric       Results.push_back(Results[0].getValue(1));
34640b57cec5SDimitry Andric     } else {
34650b57cec5SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
34660b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
34670b57cec5SDimitry Andric     }
34680b57cec5SDimitry Andric     break;
34690b57cec5SDimitry Andric   case ISD::STACKRESTORE:
34700b57cec5SDimitry Andric     // Expand to CopyToReg if the target set
34710b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3472e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
34730b57cec5SDimitry Andric       Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
34740b57cec5SDimitry Andric                                          Node->getOperand(1)));
34750b57cec5SDimitry Andric     } else {
34760b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
34770b57cec5SDimitry Andric     }
34780b57cec5SDimitry Andric     break;
34790b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
34800b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
34810b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(0));
34820b57cec5SDimitry Andric     break;
34830b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
34840b57cec5SDimitry Andric     Results.push_back(ExpandFCOPYSIGN(Node));
34850b57cec5SDimitry Andric     break;
34860b57cec5SDimitry Andric   case ISD::FNEG:
3487e8d8bef9SDimitry Andric     Results.push_back(ExpandFNEG(Node));
34880b57cec5SDimitry Andric     break;
34890b57cec5SDimitry Andric   case ISD::FABS:
34900b57cec5SDimitry Andric     Results.push_back(ExpandFABS(Node));
34910b57cec5SDimitry Andric     break;
349281ad6265SDimitry Andric   case ISD::IS_FPCLASS: {
349381ad6265SDimitry Andric     auto CNode = cast<ConstantSDNode>(Node->getOperand(1));
349481ad6265SDimitry Andric     auto Test = static_cast<FPClassTest>(CNode->getZExtValue());
349581ad6265SDimitry Andric     if (SDValue Expanded =
349681ad6265SDimitry Andric             TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
349781ad6265SDimitry Andric                                  Test, Node->getFlags(), SDLoc(Node), DAG))
349881ad6265SDimitry Andric       Results.push_back(Expanded);
349981ad6265SDimitry Andric     break;
350081ad6265SDimitry Andric   }
35010b57cec5SDimitry Andric   case ISD::SMIN:
35020b57cec5SDimitry Andric   case ISD::SMAX:
35030b57cec5SDimitry Andric   case ISD::UMIN:
35040b57cec5SDimitry Andric   case ISD::UMAX: {
35050b57cec5SDimitry Andric     // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
35060b57cec5SDimitry Andric     ISD::CondCode Pred;
35070b57cec5SDimitry Andric     switch (Node->getOpcode()) {
35080b57cec5SDimitry Andric     default: llvm_unreachable("How did we get here?");
35090b57cec5SDimitry Andric     case ISD::SMAX: Pred = ISD::SETGT; break;
35100b57cec5SDimitry Andric     case ISD::SMIN: Pred = ISD::SETLT; break;
35110b57cec5SDimitry Andric     case ISD::UMAX: Pred = ISD::SETUGT; break;
35120b57cec5SDimitry Andric     case ISD::UMIN: Pred = ISD::SETULT; break;
35130b57cec5SDimitry Andric     }
35140b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
35150b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
35160b57cec5SDimitry Andric     Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
35170b57cec5SDimitry Andric     Results.push_back(Tmp1);
35180b57cec5SDimitry Andric     break;
35190b57cec5SDimitry Andric   }
35200b57cec5SDimitry Andric   case ISD::FMINNUM:
35210b57cec5SDimitry Andric   case ISD::FMAXNUM: {
35220b57cec5SDimitry Andric     if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
35230b57cec5SDimitry Andric       Results.push_back(Expanded);
35240b57cec5SDimitry Andric     break;
35250b57cec5SDimitry Andric   }
35260b57cec5SDimitry Andric   case ISD::FSIN:
35270b57cec5SDimitry Andric   case ISD::FCOS: {
35280b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
35290b57cec5SDimitry Andric     // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
35300b57cec5SDimitry Andric     // fcos which share the same operand and both are used.
35310b57cec5SDimitry Andric     if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
35320b57cec5SDimitry Andric          isSinCosLibcallAvailable(Node, TLI))
35330b57cec5SDimitry Andric         && useSinCos(Node)) {
35340b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
35350b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
35360b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::FCOS)
35370b57cec5SDimitry Andric         Tmp1 = Tmp1.getValue(1);
35380b57cec5SDimitry Andric       Results.push_back(Tmp1);
35390b57cec5SDimitry Andric     }
35400b57cec5SDimitry Andric     break;
35410b57cec5SDimitry Andric   }
3542*06c3fb27SDimitry Andric   case ISD::FLDEXP:
3543*06c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP: {
3544*06c3fb27SDimitry Andric     EVT VT = Node->getValueType(0);
3545*06c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getLDEXP(VT);
3546*06c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
3547*06c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
3548*06c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
3549*06c3fb27SDimitry Andric       break;
3550*06c3fb27SDimitry Andric 
3551*06c3fb27SDimitry Andric     if (SDValue Expanded = expandLdexp(Node)) {
3552*06c3fb27SDimitry Andric       Results.push_back(Expanded);
3553*06c3fb27SDimitry Andric       if (Node->getOpcode() == ISD::STRICT_FLDEXP)
3554*06c3fb27SDimitry Andric         Results.push_back(Expanded.getValue(1));
3555*06c3fb27SDimitry Andric     }
3556*06c3fb27SDimitry Andric 
3557*06c3fb27SDimitry Andric     break;
3558*06c3fb27SDimitry Andric   }
3559*06c3fb27SDimitry Andric   case ISD::FFREXP: {
3560*06c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
3561*06c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
3562*06c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
3563*06c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
3564*06c3fb27SDimitry Andric       break;
3565*06c3fb27SDimitry Andric 
3566*06c3fb27SDimitry Andric     if (SDValue Expanded = expandFrexp(Node)) {
3567*06c3fb27SDimitry Andric       Results.push_back(Expanded);
3568*06c3fb27SDimitry Andric       Results.push_back(Expanded.getValue(1));
3569*06c3fb27SDimitry Andric     }
3570*06c3fb27SDimitry Andric     break;
3571*06c3fb27SDimitry Andric   }
35720b57cec5SDimitry Andric   case ISD::FMAD:
35730b57cec5SDimitry Andric     llvm_unreachable("Illegal fmad should never be formed");
35740b57cec5SDimitry Andric 
35750b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
35760b57cec5SDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
35770b57cec5SDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
35780b57cec5SDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
35790b57cec5SDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
35800b57cec5SDimitry Andric       SDValue Res =
35810b57cec5SDimitry Andric           DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
35820b57cec5SDimitry Andric       Results.push_back(
35830b57cec5SDimitry Andric           DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
35840b57cec5SDimitry Andric     }
35850b57cec5SDimitry Andric     break;
35865ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP:
35875ffd83dbSDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
35885ffd83dbSDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
35895ffd83dbSDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
35905ffd83dbSDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
35915ffd83dbSDimitry Andric       SDValue Res =
35925ffd83dbSDimitry Andric           DAG.getNode(ISD::STRICT_FP16_TO_FP, dl, {MVT::f32, MVT::Other},
35935ffd83dbSDimitry Andric                       {Node->getOperand(0), Node->getOperand(1)});
35945ffd83dbSDimitry Andric       Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
35955ffd83dbSDimitry Andric                         {Node->getValueType(0), MVT::Other},
35965ffd83dbSDimitry Andric                         {Res.getValue(1), Res});
35975ffd83dbSDimitry Andric       Results.push_back(Res);
35985ffd83dbSDimitry Andric       Results.push_back(Res.getValue(1));
35995ffd83dbSDimitry Andric     }
36005ffd83dbSDimitry Andric     break;
36010b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
36020b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
36030b57cec5SDimitry Andric     if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
36040b57cec5SDimitry Andric       SDValue Op = Node->getOperand(0);
36050b57cec5SDimitry Andric       MVT SVT = Op.getSimpleValueType();
36060b57cec5SDimitry Andric       if ((SVT == MVT::f64 || SVT == MVT::f80) &&
36070b57cec5SDimitry Andric           TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
36080b57cec5SDimitry Andric         // Under fastmath, we can expand this node into a fround followed by
36090b57cec5SDimitry Andric         // a float-half conversion.
3610bdd1243dSDimitry Andric         SDValue FloatVal =
3611bdd1243dSDimitry Andric             DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3612bdd1243dSDimitry Andric                         DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
36130b57cec5SDimitry Andric         Results.push_back(
36140b57cec5SDimitry Andric             DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
36150b57cec5SDimitry Andric       }
36160b57cec5SDimitry Andric     }
36170b57cec5SDimitry Andric     break;
36180b57cec5SDimitry Andric   case ISD::ConstantFP: {
36190b57cec5SDimitry Andric     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
36200b57cec5SDimitry Andric     // Check to see if this FP immediate is already legal.
36210b57cec5SDimitry Andric     // If this is a legal constant, turn it into a TargetConstantFP node.
36220b57cec5SDimitry Andric     if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3623e8d8bef9SDimitry Andric                           DAG.shouldOptForSize()))
36240b57cec5SDimitry Andric       Results.push_back(ExpandConstantFP(CFP, true));
36250b57cec5SDimitry Andric     break;
36260b57cec5SDimitry Andric   }
36270b57cec5SDimitry Andric   case ISD::Constant: {
36280b57cec5SDimitry Andric     ConstantSDNode *CP = cast<ConstantSDNode>(Node);
36290b57cec5SDimitry Andric     Results.push_back(ExpandConstant(CP));
36300b57cec5SDimitry Andric     break;
36310b57cec5SDimitry Andric   }
36320b57cec5SDimitry Andric   case ISD::FSUB: {
36330b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36340b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
36350b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
36360b57cec5SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
36370b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
36380b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
36390b57cec5SDimitry Andric       Results.push_back(Tmp1);
36400b57cec5SDimitry Andric     }
36410b57cec5SDimitry Andric     break;
36420b57cec5SDimitry Andric   }
36430b57cec5SDimitry Andric   case ISD::SUB: {
36440b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36450b57cec5SDimitry Andric     assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
36460b57cec5SDimitry Andric            TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
36470b57cec5SDimitry Andric            "Don't know how to expand this subtraction!");
3648349cc55cSDimitry Andric     Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
36490b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
36500b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
36510b57cec5SDimitry Andric     break;
36520b57cec5SDimitry Andric   }
36530b57cec5SDimitry Andric   case ISD::UREM:
36545ffd83dbSDimitry Andric   case ISD::SREM:
36555ffd83dbSDimitry Andric     if (TLI.expandREM(Node, Tmp1, DAG))
36560b57cec5SDimitry Andric       Results.push_back(Tmp1);
36570b57cec5SDimitry Andric     break;
36580b57cec5SDimitry Andric   case ISD::UDIV:
36590b57cec5SDimitry Andric   case ISD::SDIV: {
36600b57cec5SDimitry Andric     bool isSigned = Node->getOpcode() == ISD::SDIV;
36610b57cec5SDimitry Andric     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
36620b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36630b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
36640b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
36650b57cec5SDimitry Andric       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
36660b57cec5SDimitry Andric                          Node->getOperand(1));
36670b57cec5SDimitry Andric       Results.push_back(Tmp1);
36680b57cec5SDimitry Andric     }
36690b57cec5SDimitry Andric     break;
36700b57cec5SDimitry Andric   }
36710b57cec5SDimitry Andric   case ISD::MULHU:
36720b57cec5SDimitry Andric   case ISD::MULHS: {
36730b57cec5SDimitry Andric     unsigned ExpandOpcode =
36740b57cec5SDimitry Andric         Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
36750b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36760b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
36770b57cec5SDimitry Andric 
36780b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
36790b57cec5SDimitry Andric                        Node->getOperand(1));
36800b57cec5SDimitry Andric     Results.push_back(Tmp1.getValue(1));
36810b57cec5SDimitry Andric     break;
36820b57cec5SDimitry Andric   }
36830b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
36840b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
36850b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
36860b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
36870b57cec5SDimitry Andric     MVT VT = LHS.getSimpleValueType();
36880b57cec5SDimitry Andric     unsigned MULHOpcode =
36890b57cec5SDimitry Andric         Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
36900b57cec5SDimitry Andric 
36910b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
36920b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
36930b57cec5SDimitry Andric       Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
36940b57cec5SDimitry Andric       break;
36950b57cec5SDimitry Andric     }
36960b57cec5SDimitry Andric 
36970b57cec5SDimitry Andric     SmallVector<SDValue, 4> Halves;
36980b57cec5SDimitry Andric     EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
36990b57cec5SDimitry Andric     assert(TLI.isTypeLegal(HalfType));
3700e8d8bef9SDimitry Andric     if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
37010b57cec5SDimitry Andric                            HalfType, DAG,
37020b57cec5SDimitry Andric                            TargetLowering::MulExpansionKind::Always)) {
37030b57cec5SDimitry Andric       for (unsigned i = 0; i < 2; ++i) {
37040b57cec5SDimitry Andric         SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
37050b57cec5SDimitry Andric         SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
37060b57cec5SDimitry Andric         SDValue Shift = DAG.getConstant(
37070b57cec5SDimitry Andric             HalfType.getScalarSizeInBits(), dl,
37080b57cec5SDimitry Andric             TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
37090b57cec5SDimitry Andric         Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
37100b57cec5SDimitry Andric         Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
37110b57cec5SDimitry Andric       }
37120b57cec5SDimitry Andric       break;
37130b57cec5SDimitry Andric     }
37140b57cec5SDimitry Andric     break;
37150b57cec5SDimitry Andric   }
37160b57cec5SDimitry Andric   case ISD::MUL: {
37170b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37180b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
37190b57cec5SDimitry Andric     // See if multiply or divide can be lowered using two-result operations.
37200b57cec5SDimitry Andric     // We just need the low half of the multiply; try both the signed
37210b57cec5SDimitry Andric     // and unsigned forms. If the target supports both SMUL_LOHI and
37220b57cec5SDimitry Andric     // UMUL_LOHI, form a preference by checking which forms of plain
37230b57cec5SDimitry Andric     // MULH it supports.
37240b57cec5SDimitry Andric     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
37250b57cec5SDimitry Andric     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
37260b57cec5SDimitry Andric     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
37270b57cec5SDimitry Andric     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
37280b57cec5SDimitry Andric     unsigned OpToUse = 0;
37290b57cec5SDimitry Andric     if (HasSMUL_LOHI && !HasMULHS) {
37300b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
37310b57cec5SDimitry Andric     } else if (HasUMUL_LOHI && !HasMULHU) {
37320b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
37330b57cec5SDimitry Andric     } else if (HasSMUL_LOHI) {
37340b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
37350b57cec5SDimitry Andric     } else if (HasUMUL_LOHI) {
37360b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
37370b57cec5SDimitry Andric     }
37380b57cec5SDimitry Andric     if (OpToUse) {
37390b57cec5SDimitry Andric       Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
37400b57cec5SDimitry Andric                                     Node->getOperand(1)));
37410b57cec5SDimitry Andric       break;
37420b57cec5SDimitry Andric     }
37430b57cec5SDimitry Andric 
37440b57cec5SDimitry Andric     SDValue Lo, Hi;
37450b57cec5SDimitry Andric     EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
37460b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
37470b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
37480b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
37490b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
37500b57cec5SDimitry Andric         TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
37510b57cec5SDimitry Andric                       TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
37520b57cec5SDimitry Andric       Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
37530b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
37540b57cec5SDimitry Andric       SDValue Shift =
37550b57cec5SDimitry Andric           DAG.getConstant(HalfType.getSizeInBits(), dl,
37560b57cec5SDimitry Andric                           TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
37570b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
37580b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
37590b57cec5SDimitry Andric     }
37600b57cec5SDimitry Andric     break;
37610b57cec5SDimitry Andric   }
37620b57cec5SDimitry Andric   case ISD::FSHL:
37630b57cec5SDimitry Andric   case ISD::FSHR:
37640eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
37650eae32dcSDimitry Andric       Results.push_back(Expanded);
37660b57cec5SDimitry Andric     break;
37670b57cec5SDimitry Andric   case ISD::ROTL:
37680b57cec5SDimitry Andric   case ISD::ROTR:
37690eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
37700eae32dcSDimitry Andric       Results.push_back(Expanded);
37710b57cec5SDimitry Andric     break;
37720b57cec5SDimitry Andric   case ISD::SADDSAT:
37730b57cec5SDimitry Andric   case ISD::UADDSAT:
37740b57cec5SDimitry Andric   case ISD::SSUBSAT:
37750b57cec5SDimitry Andric   case ISD::USUBSAT:
37760b57cec5SDimitry Andric     Results.push_back(TLI.expandAddSubSat(Node, DAG));
37770b57cec5SDimitry Andric     break;
3778e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
3779e8d8bef9SDimitry Andric   case ISD::USHLSAT:
3780e8d8bef9SDimitry Andric     Results.push_back(TLI.expandShlSat(Node, DAG));
3781e8d8bef9SDimitry Andric     break;
37820b57cec5SDimitry Andric   case ISD::SMULFIX:
37830b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
37840b57cec5SDimitry Andric   case ISD::UMULFIX:
37858bcb0991SDimitry Andric   case ISD::UMULFIXSAT:
37860b57cec5SDimitry Andric     Results.push_back(TLI.expandFixedPointMul(Node, DAG));
37870b57cec5SDimitry Andric     break;
3788480093f4SDimitry Andric   case ISD::SDIVFIX:
37895ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
3790480093f4SDimitry Andric   case ISD::UDIVFIX:
37915ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT:
3792480093f4SDimitry Andric     if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
3793480093f4SDimitry Andric                                             Node->getOperand(0),
3794480093f4SDimitry Andric                                             Node->getOperand(1),
3795480093f4SDimitry Andric                                             Node->getConstantOperandVal(2),
3796480093f4SDimitry Andric                                             DAG)) {
3797480093f4SDimitry Andric       Results.push_back(V);
3798480093f4SDimitry Andric       break;
3799480093f4SDimitry Andric     }
3800480093f4SDimitry Andric     // FIXME: We might want to retry here with a wider type if we fail, if that
3801480093f4SDimitry Andric     // type is legal.
3802480093f4SDimitry Andric     // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
3803480093f4SDimitry Andric     // <= 128 (which is the case for all of the default Embedded-C types),
3804480093f4SDimitry Andric     // we will only get here with types and scales that we could always expand
3805480093f4SDimitry Andric     // if we were allowed to generate libcalls to division functions of illegal
3806480093f4SDimitry Andric     // type. But we cannot do that.
3807480093f4SDimitry Andric     llvm_unreachable("Cannot expand DIVFIX!");
3808*06c3fb27SDimitry Andric   case ISD::UADDO_CARRY:
3809*06c3fb27SDimitry Andric   case ISD::USUBO_CARRY: {
38100b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
38110b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
38120b57cec5SDimitry Andric     SDValue Carry = Node->getOperand(2);
38130b57cec5SDimitry Andric 
3814*06c3fb27SDimitry Andric     bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
38150b57cec5SDimitry Andric 
38160b57cec5SDimitry Andric     // Initial add of the 2 operands.
38170b57cec5SDimitry Andric     unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
38180b57cec5SDimitry Andric     EVT VT = LHS.getValueType();
38190b57cec5SDimitry Andric     SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
38200b57cec5SDimitry Andric 
38210b57cec5SDimitry Andric     // Initial check for overflow.
38220b57cec5SDimitry Andric     EVT CarryType = Node->getValueType(1);
38230b57cec5SDimitry Andric     EVT SetCCType = getSetCCResultType(Node->getValueType(0));
38240b57cec5SDimitry Andric     ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
38250b57cec5SDimitry Andric     SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
38260b57cec5SDimitry Andric 
38270b57cec5SDimitry Andric     // Add of the sum and the carry.
38285ffd83dbSDimitry Andric     SDValue One = DAG.getConstant(1, dl, VT);
38290b57cec5SDimitry Andric     SDValue CarryExt =
38305ffd83dbSDimitry Andric         DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
38310b57cec5SDimitry Andric     SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
38320b57cec5SDimitry Andric 
38330b57cec5SDimitry Andric     // Second check for overflow. If we are adding, we can only overflow if the
38340b57cec5SDimitry Andric     // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
38350b57cec5SDimitry Andric     // If we are subtracting, we can only overflow if the initial sum is 0 and
38360b57cec5SDimitry Andric     // the carry is set, resulting in a new sum of all 1s.
38370b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, VT);
38380b57cec5SDimitry Andric     SDValue Overflow2 =
38390b57cec5SDimitry Andric         IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
38400b57cec5SDimitry Andric               : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
38410b57cec5SDimitry Andric     Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
38420b57cec5SDimitry Andric                             DAG.getZExtOrTrunc(Carry, dl, SetCCType));
38430b57cec5SDimitry Andric 
38440b57cec5SDimitry Andric     SDValue ResultCarry =
38450b57cec5SDimitry Andric         DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
38460b57cec5SDimitry Andric 
38470b57cec5SDimitry Andric     Results.push_back(Sum2);
38480b57cec5SDimitry Andric     Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
38490b57cec5SDimitry Andric     break;
38500b57cec5SDimitry Andric   }
38510b57cec5SDimitry Andric   case ISD::SADDO:
38520b57cec5SDimitry Andric   case ISD::SSUBO: {
38530b57cec5SDimitry Andric     SDValue Result, Overflow;
38540b57cec5SDimitry Andric     TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
38550b57cec5SDimitry Andric     Results.push_back(Result);
38560b57cec5SDimitry Andric     Results.push_back(Overflow);
38570b57cec5SDimitry Andric     break;
38580b57cec5SDimitry Andric   }
38590b57cec5SDimitry Andric   case ISD::UADDO:
38600b57cec5SDimitry Andric   case ISD::USUBO: {
38610b57cec5SDimitry Andric     SDValue Result, Overflow;
38620b57cec5SDimitry Andric     TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
38630b57cec5SDimitry Andric     Results.push_back(Result);
38640b57cec5SDimitry Andric     Results.push_back(Overflow);
38650b57cec5SDimitry Andric     break;
38660b57cec5SDimitry Andric   }
38670b57cec5SDimitry Andric   case ISD::UMULO:
38680b57cec5SDimitry Andric   case ISD::SMULO: {
38690b57cec5SDimitry Andric     SDValue Result, Overflow;
38700b57cec5SDimitry Andric     if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
38710b57cec5SDimitry Andric       Results.push_back(Result);
38720b57cec5SDimitry Andric       Results.push_back(Overflow);
38730b57cec5SDimitry Andric     }
38740b57cec5SDimitry Andric     break;
38750b57cec5SDimitry Andric   }
38760b57cec5SDimitry Andric   case ISD::BUILD_PAIR: {
38770b57cec5SDimitry Andric     EVT PairTy = Node->getValueType(0);
38780b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
38790b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
38800b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
38810b57cec5SDimitry Andric         ISD::SHL, dl, PairTy, Tmp2,
38820b57cec5SDimitry Andric         DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
38830b57cec5SDimitry Andric                         TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
38840b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
38850b57cec5SDimitry Andric     break;
38860b57cec5SDimitry Andric   }
38870b57cec5SDimitry Andric   case ISD::SELECT:
38880b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
38890b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
38900b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);
38910b57cec5SDimitry Andric     if (Tmp1.getOpcode() == ISD::SETCC) {
38920b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
38930b57cec5SDimitry Andric                              Tmp2, Tmp3,
38940b57cec5SDimitry Andric                              cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
38950b57cec5SDimitry Andric     } else {
38960b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1,
38970b57cec5SDimitry Andric                              DAG.getConstant(0, dl, Tmp1.getValueType()),
38980b57cec5SDimitry Andric                              Tmp2, Tmp3, ISD::SETNE);
38990b57cec5SDimitry Andric     }
39000b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
39010b57cec5SDimitry Andric     Results.push_back(Tmp1);
39020b57cec5SDimitry Andric     break;
39030b57cec5SDimitry Andric   case ISD::BR_JT: {
39040b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0);
39050b57cec5SDimitry Andric     SDValue Table = Node->getOperand(1);
39060b57cec5SDimitry Andric     SDValue Index = Node->getOperand(2);
39070b57cec5SDimitry Andric 
39080b57cec5SDimitry Andric     const DataLayout &TD = DAG.getDataLayout();
39090b57cec5SDimitry Andric     EVT PTy = TLI.getPointerTy(TD);
39100b57cec5SDimitry Andric 
39110b57cec5SDimitry Andric     unsigned EntrySize =
39120b57cec5SDimitry Andric       DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
39130b57cec5SDimitry Andric 
39140b57cec5SDimitry Andric     // For power-of-two jumptable entry sizes convert multiplication to a shift.
39150b57cec5SDimitry Andric     // This transformation needs to be done here since otherwise the MIPS
39160b57cec5SDimitry Andric     // backend will end up emitting a three instruction multiply sequence
39170b57cec5SDimitry Andric     // instead of a single shift and MSP430 will call a runtime function.
39180b57cec5SDimitry Andric     if (llvm::isPowerOf2_32(EntrySize))
39190b57cec5SDimitry Andric       Index = DAG.getNode(
39200b57cec5SDimitry Andric           ISD::SHL, dl, Index.getValueType(), Index,
39210b57cec5SDimitry Andric           DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
39220b57cec5SDimitry Andric     else
39230b57cec5SDimitry Andric       Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
39240b57cec5SDimitry Andric                           DAG.getConstant(EntrySize, dl, Index.getValueType()));
39250b57cec5SDimitry Andric     SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
39260b57cec5SDimitry Andric                                Index, Table);
39270b57cec5SDimitry Andric 
39280b57cec5SDimitry Andric     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
39290b57cec5SDimitry Andric     SDValue LD = DAG.getExtLoad(
39300b57cec5SDimitry Andric         ISD::SEXTLOAD, dl, PTy, Chain, Addr,
39310b57cec5SDimitry Andric         MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
39320b57cec5SDimitry Andric     Addr = LD;
39330b57cec5SDimitry Andric     if (TLI.isJumpTableRelative()) {
39340b57cec5SDimitry Andric       // For PIC, the sequence is:
39350b57cec5SDimitry Andric       // BRIND(load(Jumptable + index) + RelocBase)
39360b57cec5SDimitry Andric       // RelocBase can be JumpTable, GOT or some sort of global base.
39370b57cec5SDimitry Andric       Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
39380b57cec5SDimitry Andric                           TLI.getPICJumpTableRelocBase(Table, DAG));
39390b57cec5SDimitry Andric     }
39400b57cec5SDimitry Andric 
39410b57cec5SDimitry Andric     Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, DAG);
39420b57cec5SDimitry Andric     Results.push_back(Tmp1);
39430b57cec5SDimitry Andric     break;
39440b57cec5SDimitry Andric   }
39450b57cec5SDimitry Andric   case ISD::BRCOND:
39460b57cec5SDimitry Andric     // Expand brcond's setcc into its constituent parts and create a BR_CC
39470b57cec5SDimitry Andric     // Node.
39480b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
39490b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
39504824e7fdSDimitry Andric     if (Tmp2.getOpcode() == ISD::SETCC &&
39514824e7fdSDimitry Andric         TLI.isOperationLegalOrCustom(ISD::BR_CC,
39524824e7fdSDimitry Andric                                      Tmp2.getOperand(0).getValueType())) {
39534824e7fdSDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
39540b57cec5SDimitry Andric                          Tmp2.getOperand(0), Tmp2.getOperand(1),
39550b57cec5SDimitry Andric                          Node->getOperand(2));
39560b57cec5SDimitry Andric     } else {
39570b57cec5SDimitry Andric       // We test only the i1 bit.  Skip the AND if UNDEF or another AND.
39580b57cec5SDimitry Andric       if (Tmp2.isUndef() ||
3959*06c3fb27SDimitry Andric           (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
39600b57cec5SDimitry Andric         Tmp3 = Tmp2;
39610b57cec5SDimitry Andric       else
39620b57cec5SDimitry Andric         Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
39630b57cec5SDimitry Andric                            DAG.getConstant(1, dl, Tmp2.getValueType()));
39640b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
39650b57cec5SDimitry Andric                          DAG.getCondCode(ISD::SETNE), Tmp3,
39660b57cec5SDimitry Andric                          DAG.getConstant(0, dl, Tmp3.getValueType()),
39670b57cec5SDimitry Andric                          Node->getOperand(2));
39680b57cec5SDimitry Andric     }
39690b57cec5SDimitry Andric     Results.push_back(Tmp1);
39700b57cec5SDimitry Andric     break;
3971480093f4SDimitry Andric   case ISD::SETCC:
397281ad6265SDimitry Andric   case ISD::VP_SETCC:
3973480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
3974480093f4SDimitry Andric   case ISD::STRICT_FSETCCS: {
397581ad6265SDimitry Andric     bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
397681ad6265SDimitry Andric     bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
397781ad6265SDimitry Andric                     Node->getOpcode() == ISD::STRICT_FSETCCS;
3978480093f4SDimitry Andric     bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
3979480093f4SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
3980480093f4SDimitry Andric     unsigned Offset = IsStrict ? 1 : 0;
3981480093f4SDimitry Andric     Tmp1 = Node->getOperand(0 + Offset);
3982480093f4SDimitry Andric     Tmp2 = Node->getOperand(1 + Offset);
3983480093f4SDimitry Andric     Tmp3 = Node->getOperand(2 + Offset);
398481ad6265SDimitry Andric     SDValue Mask, EVL;
398581ad6265SDimitry Andric     if (IsVP) {
398681ad6265SDimitry Andric       Mask = Node->getOperand(3 + Offset);
398781ad6265SDimitry Andric       EVL = Node->getOperand(4 + Offset);
398881ad6265SDimitry Andric     }
398981ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
399081ad6265SDimitry Andric         DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
399181ad6265SDimitry Andric         Chain, IsSignaling);
39920b57cec5SDimitry Andric 
39930b57cec5SDimitry Andric     if (Legalized) {
39940b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
39950b57cec5SDimitry Andric       // condition code, create a new SETCC node.
399604eeddc0SDimitry Andric       if (Tmp3.getNode()) {
399704eeddc0SDimitry Andric         if (IsStrict) {
399804eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
399904eeddc0SDimitry Andric                              {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
400004eeddc0SDimitry Andric           Chain = Tmp1.getValue(1);
400181ad6265SDimitry Andric         } else if (IsVP) {
400281ad6265SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
400381ad6265SDimitry Andric                              {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
400404eeddc0SDimitry Andric         } else {
400504eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
400604eeddc0SDimitry Andric                              Tmp2, Tmp3, Node->getFlags());
400704eeddc0SDimitry Andric         }
400804eeddc0SDimitry Andric       }
40090b57cec5SDimitry Andric 
40100b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then wrap
40110b57cec5SDimitry Andric       // the existing SETCC in a NOT to restore the intended condition.
401281ad6265SDimitry Andric       if (NeedInvert) {
401381ad6265SDimitry Andric         if (!IsVP)
40140b57cec5SDimitry Andric           Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
401581ad6265SDimitry Andric         else
401681ad6265SDimitry Andric           Tmp1 =
401781ad6265SDimitry Andric               DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
401881ad6265SDimitry Andric       }
40190b57cec5SDimitry Andric 
40200b57cec5SDimitry Andric       Results.push_back(Tmp1);
4021480093f4SDimitry Andric       if (IsStrict)
4022480093f4SDimitry Andric         Results.push_back(Chain);
4023480093f4SDimitry Andric 
40240b57cec5SDimitry Andric       break;
40250b57cec5SDimitry Andric     }
40260b57cec5SDimitry Andric 
4027480093f4SDimitry Andric     // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4028480093f4SDimitry Andric     // understand if this code is useful for strict nodes.
4029480093f4SDimitry Andric     assert(!IsStrict && "Don't know how to expand for strict nodes.");
4030480093f4SDimitry Andric 
40310b57cec5SDimitry Andric     // Otherwise, SETCC for the given comparison type must be completely
40320b57cec5SDimitry Andric     // illegal; expand it into a SELECT_CC.
403381ad6265SDimitry Andric     // FIXME: This drops the mask/evl for VP_SETCC.
40340b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
403581ad6265SDimitry Andric     EVT Tmp1VT = Tmp1.getValueType();
40360b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
403781ad6265SDimitry Andric                        DAG.getBoolConstant(true, dl, VT, Tmp1VT),
403881ad6265SDimitry Andric                        DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3);
40390b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
40400b57cec5SDimitry Andric     Results.push_back(Tmp1);
40410b57cec5SDimitry Andric     break;
40420b57cec5SDimitry Andric   }
40430b57cec5SDimitry Andric   case ISD::SELECT_CC: {
4044480093f4SDimitry Andric     // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
40450b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);   // LHS
40460b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);   // RHS
40470b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);   // True
40480b57cec5SDimitry Andric     Tmp4 = Node->getOperand(3);   // False
40490b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
4050480093f4SDimitry Andric     SDValue Chain;
40510b57cec5SDimitry Andric     SDValue CC = Node->getOperand(4);
40520b57cec5SDimitry Andric     ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
40530b57cec5SDimitry Andric 
40540b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
40550b57cec5SDimitry Andric       // If the condition code is legal, then we need to expand this
40560b57cec5SDimitry Andric       // node using SETCC and SELECT.
40570b57cec5SDimitry Andric       EVT CmpVT = Tmp1.getValueType();
40580b57cec5SDimitry Andric       assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
40590b57cec5SDimitry Andric              "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
40600b57cec5SDimitry Andric              "expanded.");
40610b57cec5SDimitry Andric       EVT CCVT = getSetCCResultType(CmpVT);
40620b57cec5SDimitry Andric       SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
40630b57cec5SDimitry Andric       Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
40640b57cec5SDimitry Andric       break;
40650b57cec5SDimitry Andric     }
40660b57cec5SDimitry Andric 
40670b57cec5SDimitry Andric     // SELECT_CC is legal, so the condition code must not be.
40680b57cec5SDimitry Andric     bool Legalized = false;
40690b57cec5SDimitry Andric     // Try to legalize by inverting the condition.  This is for targets that
40700b57cec5SDimitry Andric     // might support an ordered version of a condition, but not the unordered
40710b57cec5SDimitry Andric     // version (or vice versa).
4072480093f4SDimitry Andric     ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
40730b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
40740b57cec5SDimitry Andric       // Use the new condition code and swap true and false
40750b57cec5SDimitry Andric       Legalized = true;
40760b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
40770b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
40780b57cec5SDimitry Andric     } else {
40790b57cec5SDimitry Andric       // If The inverse is not legal, then try to swap the arguments using
40800b57cec5SDimitry Andric       // the inverse condition code.
40810b57cec5SDimitry Andric       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
40820b57cec5SDimitry Andric       if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
40830b57cec5SDimitry Andric         // The swapped inverse condition is legal, so swap true and false,
40840b57cec5SDimitry Andric         // lhs and rhs.
40850b57cec5SDimitry Andric         Legalized = true;
40860b57cec5SDimitry Andric         Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
40870b57cec5SDimitry Andric         Tmp1->setFlags(Node->getFlags());
40880b57cec5SDimitry Andric       }
40890b57cec5SDimitry Andric     }
40900b57cec5SDimitry Andric 
40910b57cec5SDimitry Andric     if (!Legalized) {
4092fe6060f1SDimitry Andric       Legalized = TLI.LegalizeSetCCCondCode(
4093fe6060f1SDimitry Andric           DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
409481ad6265SDimitry Andric           /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
40950b57cec5SDimitry Andric 
40960b57cec5SDimitry Andric       assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
40970b57cec5SDimitry Andric 
40980b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then swap
40990b57cec5SDimitry Andric       // the True/False operands to match.
41000b57cec5SDimitry Andric       if (NeedInvert)
41010b57cec5SDimitry Andric         std::swap(Tmp3, Tmp4);
41020b57cec5SDimitry Andric 
41030b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
41040b57cec5SDimitry Andric       // condition code, create a new SELECT_CC node.
41050b57cec5SDimitry Andric       if (CC.getNode()) {
41060b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
41070b57cec5SDimitry Andric                            Tmp1, Tmp2, Tmp3, Tmp4, CC);
41080b57cec5SDimitry Andric       } else {
41090b57cec5SDimitry Andric         Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
41100b57cec5SDimitry Andric         CC = DAG.getCondCode(ISD::SETNE);
41110b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
41120b57cec5SDimitry Andric                            Tmp2, Tmp3, Tmp4, CC);
41130b57cec5SDimitry Andric       }
41140b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
41150b57cec5SDimitry Andric     }
41160b57cec5SDimitry Andric     Results.push_back(Tmp1);
41170b57cec5SDimitry Andric     break;
41180b57cec5SDimitry Andric   }
41190b57cec5SDimitry Andric   case ISD::BR_CC: {
4120480093f4SDimitry Andric     // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4121480093f4SDimitry Andric     SDValue Chain;
41220b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);              // Chain
41230b57cec5SDimitry Andric     Tmp2 = Node->getOperand(2);              // LHS
41240b57cec5SDimitry Andric     Tmp3 = Node->getOperand(3);              // RHS
41250b57cec5SDimitry Andric     Tmp4 = Node->getOperand(1);              // CC
41260b57cec5SDimitry Andric 
412781ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
412881ad6265SDimitry Andric         DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
412981ad6265SDimitry Andric         /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
41300b57cec5SDimitry Andric     (void)Legalized;
41310b57cec5SDimitry Andric     assert(Legalized && "Can't legalize BR_CC with legal condition!");
41320b57cec5SDimitry Andric 
41330b57cec5SDimitry Andric     // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
41340b57cec5SDimitry Andric     // node.
41350b57cec5SDimitry Andric     if (Tmp4.getNode()) {
4136e8d8bef9SDimitry Andric       assert(!NeedInvert && "Don't know how to invert BR_CC!");
4137e8d8bef9SDimitry Andric 
41380b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
41390b57cec5SDimitry Andric                          Tmp4, Tmp2, Tmp3, Node->getOperand(4));
41400b57cec5SDimitry Andric     } else {
41410b57cec5SDimitry Andric       Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4142e8d8bef9SDimitry Andric       Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
41430b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
41440b57cec5SDimitry Andric                          Tmp2, Tmp3, Node->getOperand(4));
41450b57cec5SDimitry Andric     }
41460b57cec5SDimitry Andric     Results.push_back(Tmp1);
41470b57cec5SDimitry Andric     break;
41480b57cec5SDimitry Andric   }
41490b57cec5SDimitry Andric   case ISD::BUILD_VECTOR:
41500b57cec5SDimitry Andric     Results.push_back(ExpandBUILD_VECTOR(Node));
41510b57cec5SDimitry Andric     break;
41528bcb0991SDimitry Andric   case ISD::SPLAT_VECTOR:
41538bcb0991SDimitry Andric     Results.push_back(ExpandSPLAT_VECTOR(Node));
41548bcb0991SDimitry Andric     break;
41550b57cec5SDimitry Andric   case ISD::SRA:
41560b57cec5SDimitry Andric   case ISD::SRL:
41570b57cec5SDimitry Andric   case ISD::SHL: {
41580b57cec5SDimitry Andric     // Scalarize vector SRA/SRL/SHL.
41590b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
41600b57cec5SDimitry Andric     assert(VT.isVector() && "Unable to legalize non-vector shift");
41610b57cec5SDimitry Andric     assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
41620b57cec5SDimitry Andric     unsigned NumElem = VT.getVectorNumElements();
41630b57cec5SDimitry Andric 
41640b57cec5SDimitry Andric     SmallVector<SDValue, 8> Scalars;
41650b57cec5SDimitry Andric     for (unsigned Idx = 0; Idx < NumElem; Idx++) {
41665ffd83dbSDimitry Andric       SDValue Ex =
41675ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
41685ffd83dbSDimitry Andric                       Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
41695ffd83dbSDimitry Andric       SDValue Sh =
41705ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
41715ffd83dbSDimitry Andric                       Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
41720b57cec5SDimitry Andric       Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
41730b57cec5SDimitry Andric                                     VT.getScalarType(), Ex, Sh));
41740b57cec5SDimitry Andric     }
41750b57cec5SDimitry Andric 
41760b57cec5SDimitry Andric     SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4177480093f4SDimitry Andric     Results.push_back(Result);
41780b57cec5SDimitry Andric     break;
41790b57cec5SDimitry Andric   }
41800b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
41810b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
41820b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
41830b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
41840b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
41850b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
41860b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
41870b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
41880b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
41890b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
41900b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
41910b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
41920b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
4193*06c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
4194*06c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
41950b57cec5SDimitry Andric     Results.push_back(TLI.expandVecReduce(Node, DAG));
41960b57cec5SDimitry Andric     break;
41970b57cec5SDimitry Andric   case ISD::GLOBAL_OFFSET_TABLE:
41980b57cec5SDimitry Andric   case ISD::GlobalAddress:
41990b57cec5SDimitry Andric   case ISD::GlobalTLSAddress:
42000b57cec5SDimitry Andric   case ISD::ExternalSymbol:
42010b57cec5SDimitry Andric   case ISD::ConstantPool:
42020b57cec5SDimitry Andric   case ISD::JumpTable:
42030b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
42040b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
42050b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
42060b57cec5SDimitry Andric     // FIXME: Custom lowering for these operations shouldn't return null!
4207480093f4SDimitry Andric     // Return true so that we don't call ConvertNodeToLibcall which also won't
4208480093f4SDimitry Andric     // do anything.
4209480093f4SDimitry Andric     return true;
42100b57cec5SDimitry Andric   }
42110b57cec5SDimitry Andric 
4212480093f4SDimitry Andric   if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
42138bcb0991SDimitry Andric     // FIXME: We were asked to expand a strict floating-point operation,
42148bcb0991SDimitry Andric     // but there is currently no expansion implemented that would preserve
42158bcb0991SDimitry Andric     // the "strict" properties.  For now, we just fall back to the non-strict
42168bcb0991SDimitry Andric     // version if that is legal on the target.  The actual mutation of the
42178bcb0991SDimitry Andric     // operation will happen in SelectionDAGISel::DoInstructionSelection.
42188bcb0991SDimitry Andric     switch (Node->getOpcode()) {
42198bcb0991SDimitry Andric     default:
42208bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
42218bcb0991SDimitry Andric                                          Node->getValueType(0))
42228bcb0991SDimitry Andric           == TargetLowering::Legal)
42238bcb0991SDimitry Andric         return true;
42248bcb0991SDimitry Andric       break;
4225e8d8bef9SDimitry Andric     case ISD::STRICT_FSUB: {
4226e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4227e8d8bef9SDimitry Andric               ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4228e8d8bef9SDimitry Andric         return true;
4229e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4230e8d8bef9SDimitry Andric               ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4231e8d8bef9SDimitry Andric         break;
4232e8d8bef9SDimitry Andric 
4233e8d8bef9SDimitry Andric       EVT VT = Node->getValueType(0);
4234e8d8bef9SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
4235e8d8bef9SDimitry Andric       SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4236e8d8bef9SDimitry Andric       SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4237e8d8bef9SDimitry Andric                                  {Node->getOperand(0), Node->getOperand(1), Neg},
4238e8d8bef9SDimitry Andric                          Flags);
4239e8d8bef9SDimitry Andric 
4240e8d8bef9SDimitry Andric       Results.push_back(Fadd);
4241e8d8bef9SDimitry Andric       Results.push_back(Fadd.getValue(1));
4242e8d8bef9SDimitry Andric       break;
4243e8d8bef9SDimitry Andric     }
4244e8d8bef9SDimitry Andric     case ISD::STRICT_SINT_TO_FP:
4245e8d8bef9SDimitry Andric     case ISD::STRICT_UINT_TO_FP:
42468bcb0991SDimitry Andric     case ISD::STRICT_LRINT:
42478bcb0991SDimitry Andric     case ISD::STRICT_LLRINT:
42488bcb0991SDimitry Andric     case ISD::STRICT_LROUND:
42498bcb0991SDimitry Andric     case ISD::STRICT_LLROUND:
42508bcb0991SDimitry Andric       // These are registered by the operand type instead of the value
42518bcb0991SDimitry Andric       // type. Reflect that here.
42528bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
42538bcb0991SDimitry Andric                                          Node->getOperand(1).getValueType())
42548bcb0991SDimitry Andric           == TargetLowering::Legal)
42558bcb0991SDimitry Andric         return true;
42568bcb0991SDimitry Andric       break;
42578bcb0991SDimitry Andric     }
42588bcb0991SDimitry Andric   }
42598bcb0991SDimitry Andric 
42600b57cec5SDimitry Andric   // Replace the original node with the legalized result.
42610b57cec5SDimitry Andric   if (Results.empty()) {
42620b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Cannot expand node\n");
42630b57cec5SDimitry Andric     return false;
42640b57cec5SDimitry Andric   }
42650b57cec5SDimitry Andric 
42660b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
42670b57cec5SDimitry Andric   ReplaceNode(Node, Results.data());
42680b57cec5SDimitry Andric   return true;
42690b57cec5SDimitry Andric }
42700b57cec5SDimitry Andric 
42710b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
42720b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
42730b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
42740b57cec5SDimitry Andric   SDLoc dl(Node);
42750b57cec5SDimitry Andric   // FIXME: Check flags on the node to see if we can use a finite call.
42760b57cec5SDimitry Andric   unsigned Opc = Node->getOpcode();
42770b57cec5SDimitry Andric   switch (Opc) {
42780b57cec5SDimitry Andric   case ISD::ATOMIC_FENCE: {
42790b57cec5SDimitry Andric     // If the target didn't lower this, lower it to '__sync_synchronize()' call
42800b57cec5SDimitry Andric     // FIXME: handle "fence singlethread" more efficiently.
42810b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
42820b57cec5SDimitry Andric 
42830b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
42840b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
42850b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
42860b57cec5SDimitry Andric         .setLibCallee(
42870b57cec5SDimitry Andric             CallingConv::C, Type::getVoidTy(*DAG.getContext()),
42880b57cec5SDimitry Andric             DAG.getExternalSymbol("__sync_synchronize",
42890b57cec5SDimitry Andric                                   TLI.getPointerTy(DAG.getDataLayout())),
42900b57cec5SDimitry Andric             std::move(Args));
42910b57cec5SDimitry Andric 
42920b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
42930b57cec5SDimitry Andric 
42940b57cec5SDimitry Andric     Results.push_back(CallResult.second);
42950b57cec5SDimitry Andric     break;
42960b57cec5SDimitry Andric   }
42970b57cec5SDimitry Andric   // By default, atomic intrinsics are marked Legal and lowered. Targets
42980b57cec5SDimitry Andric   // which don't support them directly, however, may want libcalls, in which
42990b57cec5SDimitry Andric   // case they mark them Expand, and we get here.
43000b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP:
43010b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_ADD:
43020b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_SUB:
43030b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_AND:
43040b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_CLR:
43050b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_OR:
43060b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_XOR:
43070b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_NAND:
43080b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MIN:
43090b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MAX:
43100b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMIN:
43110b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMAX:
43120b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP: {
43130b57cec5SDimitry Andric     MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4314fe6060f1SDimitry Andric     AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4315e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4316480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
4317480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4318e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops;
4319e8d8bef9SDimitry Andric     if (TLI.getLibcallName(LC)) {
4320e8d8bef9SDimitry Andric       // If outline atomic available, prepare its arguments and expand.
4321e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 2, Node->op_end());
4322e8d8bef9SDimitry Andric       Ops.push_back(Node->getOperand(1));
4323e8d8bef9SDimitry Andric 
4324e8d8bef9SDimitry Andric     } else {
4325e8d8bef9SDimitry Andric       LC = RTLIB::getSYNC(Opc, VT);
4326e8d8bef9SDimitry Andric       assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4327e8d8bef9SDimitry Andric              "Unexpected atomic op or value type!");
4328e8d8bef9SDimitry Andric       // Arguments for expansion to sync libcall
4329e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 1, Node->op_end());
4330e8d8bef9SDimitry Andric     }
4331480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4332480093f4SDimitry Andric                                                       Ops, CallOptions,
4333480093f4SDimitry Andric                                                       SDLoc(Node),
4334480093f4SDimitry Andric                                                       Node->getOperand(0));
43350b57cec5SDimitry Andric     Results.push_back(Tmp.first);
43360b57cec5SDimitry Andric     Results.push_back(Tmp.second);
43370b57cec5SDimitry Andric     break;
43380b57cec5SDimitry Andric   }
43390b57cec5SDimitry Andric   case ISD::TRAP: {
43400b57cec5SDimitry Andric     // If this operation is not supported, lower it to 'abort()' call
43410b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
43420b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
43430b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
43440b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
43450b57cec5SDimitry Andric         .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
43460b57cec5SDimitry Andric                       DAG.getExternalSymbol(
43470b57cec5SDimitry Andric                           "abort", TLI.getPointerTy(DAG.getDataLayout())),
43480b57cec5SDimitry Andric                       std::move(Args));
43490b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
43500b57cec5SDimitry Andric 
43510b57cec5SDimitry Andric     Results.push_back(CallResult.second);
43520b57cec5SDimitry Andric     break;
43530b57cec5SDimitry Andric   }
43540b57cec5SDimitry Andric   case ISD::FMINNUM:
43550b57cec5SDimitry Andric   case ISD::STRICT_FMINNUM:
4356480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
43570b57cec5SDimitry Andric                     RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4358480093f4SDimitry Andric                     RTLIB::FMIN_PPCF128, Results);
43590b57cec5SDimitry Andric     break;
4360*06c3fb27SDimitry Andric   // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
4361*06c3fb27SDimitry Andric   // libcall legalization for these nodes, but there is no default expasion for
4362*06c3fb27SDimitry Andric   // these nodes either (see PR63267 for example).
43630b57cec5SDimitry Andric   case ISD::FMAXNUM:
43640b57cec5SDimitry Andric   case ISD::STRICT_FMAXNUM:
4365480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
43660b57cec5SDimitry Andric                     RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4367480093f4SDimitry Andric                     RTLIB::FMAX_PPCF128, Results);
43680b57cec5SDimitry Andric     break;
43690b57cec5SDimitry Andric   case ISD::FSQRT:
43700b57cec5SDimitry Andric   case ISD::STRICT_FSQRT:
4371480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
43720b57cec5SDimitry Andric                     RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4373480093f4SDimitry Andric                     RTLIB::SQRT_PPCF128, Results);
43740b57cec5SDimitry Andric     break;
43750b57cec5SDimitry Andric   case ISD::FCBRT:
4376480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
43770b57cec5SDimitry Andric                     RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4378480093f4SDimitry Andric                     RTLIB::CBRT_PPCF128, Results);
43790b57cec5SDimitry Andric     break;
43800b57cec5SDimitry Andric   case ISD::FSIN:
43810b57cec5SDimitry Andric   case ISD::STRICT_FSIN:
4382480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
43830b57cec5SDimitry Andric                     RTLIB::SIN_F80, RTLIB::SIN_F128,
4384480093f4SDimitry Andric                     RTLIB::SIN_PPCF128, Results);
43850b57cec5SDimitry Andric     break;
43860b57cec5SDimitry Andric   case ISD::FCOS:
43870b57cec5SDimitry Andric   case ISD::STRICT_FCOS:
4388480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
43890b57cec5SDimitry Andric                     RTLIB::COS_F80, RTLIB::COS_F128,
4390480093f4SDimitry Andric                     RTLIB::COS_PPCF128, Results);
43910b57cec5SDimitry Andric     break;
43920b57cec5SDimitry Andric   case ISD::FSINCOS:
43930b57cec5SDimitry Andric     // Expand into sincos libcall.
43940b57cec5SDimitry Andric     ExpandSinCosLibCall(Node, Results);
43950b57cec5SDimitry Andric     break;
43960b57cec5SDimitry Andric   case ISD::FLOG:
43970b57cec5SDimitry Andric   case ISD::STRICT_FLOG:
43988c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
43998c27c554SDimitry Andric                     RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
44000b57cec5SDimitry Andric     break;
44010b57cec5SDimitry Andric   case ISD::FLOG2:
44020b57cec5SDimitry Andric   case ISD::STRICT_FLOG2:
44038c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
44048c27c554SDimitry Andric                     RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
44050b57cec5SDimitry Andric     break;
44060b57cec5SDimitry Andric   case ISD::FLOG10:
44070b57cec5SDimitry Andric   case ISD::STRICT_FLOG10:
44088c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
44098c27c554SDimitry Andric                     RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
44100b57cec5SDimitry Andric     break;
44110b57cec5SDimitry Andric   case ISD::FEXP:
44120b57cec5SDimitry Andric   case ISD::STRICT_FEXP:
44138c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
44148c27c554SDimitry Andric                     RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
44150b57cec5SDimitry Andric     break;
44160b57cec5SDimitry Andric   case ISD::FEXP2:
44170b57cec5SDimitry Andric   case ISD::STRICT_FEXP2:
44188c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
44198c27c554SDimitry Andric                     RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
44200b57cec5SDimitry Andric     break;
44210b57cec5SDimitry Andric   case ISD::FTRUNC:
44220b57cec5SDimitry Andric   case ISD::STRICT_FTRUNC:
4423480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
44240b57cec5SDimitry Andric                     RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4425480093f4SDimitry Andric                     RTLIB::TRUNC_PPCF128, Results);
44260b57cec5SDimitry Andric     break;
44270b57cec5SDimitry Andric   case ISD::FFLOOR:
44280b57cec5SDimitry Andric   case ISD::STRICT_FFLOOR:
4429480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
44300b57cec5SDimitry Andric                     RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4431480093f4SDimitry Andric                     RTLIB::FLOOR_PPCF128, Results);
44320b57cec5SDimitry Andric     break;
44330b57cec5SDimitry Andric   case ISD::FCEIL:
44340b57cec5SDimitry Andric   case ISD::STRICT_FCEIL:
4435480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
44360b57cec5SDimitry Andric                     RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4437480093f4SDimitry Andric                     RTLIB::CEIL_PPCF128, Results);
44380b57cec5SDimitry Andric     break;
44390b57cec5SDimitry Andric   case ISD::FRINT:
44400b57cec5SDimitry Andric   case ISD::STRICT_FRINT:
4441480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
44420b57cec5SDimitry Andric                     RTLIB::RINT_F80, RTLIB::RINT_F128,
4443480093f4SDimitry Andric                     RTLIB::RINT_PPCF128, Results);
44440b57cec5SDimitry Andric     break;
44450b57cec5SDimitry Andric   case ISD::FNEARBYINT:
44460b57cec5SDimitry Andric   case ISD::STRICT_FNEARBYINT:
4447480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
44480b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F64,
44490b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F80,
44500b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F128,
4451480093f4SDimitry Andric                     RTLIB::NEARBYINT_PPCF128, Results);
44520b57cec5SDimitry Andric     break;
44530b57cec5SDimitry Andric   case ISD::FROUND:
44540b57cec5SDimitry Andric   case ISD::STRICT_FROUND:
4455480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUND_F32,
44560b57cec5SDimitry Andric                     RTLIB::ROUND_F64,
44570b57cec5SDimitry Andric                     RTLIB::ROUND_F80,
44580b57cec5SDimitry Andric                     RTLIB::ROUND_F128,
4459480093f4SDimitry Andric                     RTLIB::ROUND_PPCF128, Results);
44600b57cec5SDimitry Andric     break;
44615ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
44625ffd83dbSDimitry Andric   case ISD::STRICT_FROUNDEVEN:
44635ffd83dbSDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
44645ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F64,
44655ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F80,
44665ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F128,
44675ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_PPCF128, Results);
44685ffd83dbSDimitry Andric     break;
4469*06c3fb27SDimitry Andric   case ISD::FLDEXP:
4470*06c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP:
4471*06c3fb27SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
4472*06c3fb27SDimitry Andric                     RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
4473*06c3fb27SDimitry Andric     break;
4474*06c3fb27SDimitry Andric   case ISD::FFREXP: {
4475*06c3fb27SDimitry Andric     ExpandFrexpLibCall(Node, Results);
4476*06c3fb27SDimitry Andric     break;
4477*06c3fb27SDimitry Andric   }
44780b57cec5SDimitry Andric   case ISD::FPOWI:
4479480093f4SDimitry Andric   case ISD::STRICT_FPOWI: {
4480fe6060f1SDimitry Andric     RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4481fe6060f1SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4482480093f4SDimitry Andric     if (!TLI.getLibcallName(LC)) {
4483480093f4SDimitry Andric       // Some targets don't have a powi libcall; use pow instead.
448481ad6265SDimitry Andric       if (Node->isStrictFPOpcode()) {
448581ad6265SDimitry Andric         SDValue Exponent =
448681ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
448781ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
448881ad6265SDimitry Andric                         {Node->getOperand(0), Node->getOperand(2)});
448981ad6265SDimitry Andric         SDValue FPOW =
449081ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
449181ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
449281ad6265SDimitry Andric                         {Exponent.getValue(1), Node->getOperand(1), Exponent});
449381ad6265SDimitry Andric         Results.push_back(FPOW);
449481ad6265SDimitry Andric         Results.push_back(FPOW.getValue(1));
449581ad6265SDimitry Andric       } else {
449681ad6265SDimitry Andric         SDValue Exponent =
449781ad6265SDimitry Andric             DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
4498480093f4SDimitry Andric                         Node->getOperand(1));
4499480093f4SDimitry Andric         Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
450081ad6265SDimitry Andric                                       Node->getValueType(0),
450181ad6265SDimitry Andric                                       Node->getOperand(0), Exponent));
450281ad6265SDimitry Andric       }
45030b57cec5SDimitry Andric       break;
4504480093f4SDimitry Andric     }
4505fe6060f1SDimitry Andric     unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
4506fe6060f1SDimitry Andric     bool ExponentHasSizeOfInt =
4507fe6060f1SDimitry Andric         DAG.getLibInfo().getIntSize() ==
4508fe6060f1SDimitry Andric         Node->getOperand(1 + Offset).getValueType().getSizeInBits();
4509fe6060f1SDimitry Andric     if (!ExponentHasSizeOfInt) {
4510fe6060f1SDimitry Andric       // If the exponent does not match with sizeof(int) a libcall to
4511fe6060f1SDimitry Andric       // RTLIB::POWI would use the wrong type for the argument.
4512fe6060f1SDimitry Andric       DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
4513fe6060f1SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4514fe6060f1SDimitry Andric       break;
4515fe6060f1SDimitry Andric     }
4516fe6060f1SDimitry Andric     ExpandFPLibCall(Node, LC, Results);
4517480093f4SDimitry Andric     break;
4518480093f4SDimitry Andric   }
45190b57cec5SDimitry Andric   case ISD::FPOW:
45200b57cec5SDimitry Andric   case ISD::STRICT_FPOW:
45218c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
45228c27c554SDimitry Andric                     RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
45230b57cec5SDimitry Andric     break;
45248bcb0991SDimitry Andric   case ISD::LROUND:
45258bcb0991SDimitry Andric   case ISD::STRICT_LROUND:
4526480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
45278bcb0991SDimitry Andric                        RTLIB::LROUND_F64, RTLIB::LROUND_F80,
45288bcb0991SDimitry Andric                        RTLIB::LROUND_F128,
4529480093f4SDimitry Andric                        RTLIB::LROUND_PPCF128, Results);
45308bcb0991SDimitry Andric     break;
45318bcb0991SDimitry Andric   case ISD::LLROUND:
45328bcb0991SDimitry Andric   case ISD::STRICT_LLROUND:
4533480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
45348bcb0991SDimitry Andric                        RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
45358bcb0991SDimitry Andric                        RTLIB::LLROUND_F128,
4536480093f4SDimitry Andric                        RTLIB::LLROUND_PPCF128, Results);
45378bcb0991SDimitry Andric     break;
45388bcb0991SDimitry Andric   case ISD::LRINT:
45398bcb0991SDimitry Andric   case ISD::STRICT_LRINT:
4540480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
45418bcb0991SDimitry Andric                        RTLIB::LRINT_F64, RTLIB::LRINT_F80,
45428bcb0991SDimitry Andric                        RTLIB::LRINT_F128,
4543480093f4SDimitry Andric                        RTLIB::LRINT_PPCF128, Results);
45448bcb0991SDimitry Andric     break;
45458bcb0991SDimitry Andric   case ISD::LLRINT:
45468bcb0991SDimitry Andric   case ISD::STRICT_LLRINT:
4547480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
45488bcb0991SDimitry Andric                        RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
45498bcb0991SDimitry Andric                        RTLIB::LLRINT_F128,
4550480093f4SDimitry Andric                        RTLIB::LLRINT_PPCF128, Results);
45518bcb0991SDimitry Andric     break;
45520b57cec5SDimitry Andric   case ISD::FDIV:
4553480093f4SDimitry Andric   case ISD::STRICT_FDIV:
4554480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
45550b57cec5SDimitry Andric                     RTLIB::DIV_F80, RTLIB::DIV_F128,
4556480093f4SDimitry Andric                     RTLIB::DIV_PPCF128, Results);
45570b57cec5SDimitry Andric     break;
45580b57cec5SDimitry Andric   case ISD::FREM:
45590b57cec5SDimitry Andric   case ISD::STRICT_FREM:
4560480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
45610b57cec5SDimitry Andric                     RTLIB::REM_F80, RTLIB::REM_F128,
4562480093f4SDimitry Andric                     RTLIB::REM_PPCF128, Results);
45630b57cec5SDimitry Andric     break;
45640b57cec5SDimitry Andric   case ISD::FMA:
45650b57cec5SDimitry Andric   case ISD::STRICT_FMA:
4566480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
45670b57cec5SDimitry Andric                     RTLIB::FMA_F80, RTLIB::FMA_F128,
4568480093f4SDimitry Andric                     RTLIB::FMA_PPCF128, Results);
45690b57cec5SDimitry Andric     break;
45700b57cec5SDimitry Andric   case ISD::FADD:
4571480093f4SDimitry Andric   case ISD::STRICT_FADD:
4572480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
45730b57cec5SDimitry Andric                     RTLIB::ADD_F80, RTLIB::ADD_F128,
4574480093f4SDimitry Andric                     RTLIB::ADD_PPCF128, Results);
45750b57cec5SDimitry Andric     break;
45760b57cec5SDimitry Andric   case ISD::FMUL:
4577480093f4SDimitry Andric   case ISD::STRICT_FMUL:
4578480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
45790b57cec5SDimitry Andric                     RTLIB::MUL_F80, RTLIB::MUL_F128,
4580480093f4SDimitry Andric                     RTLIB::MUL_PPCF128, Results);
45810b57cec5SDimitry Andric     break;
45820b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
45830b57cec5SDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
4584*06c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
45850b57cec5SDimitry Andric     }
45860b57cec5SDimitry Andric     break;
45875ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP: {
45885ffd83dbSDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
45895ffd83dbSDimitry Andric       TargetLowering::MakeLibCallOptions CallOptions;
45905ffd83dbSDimitry Andric       std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
45915ffd83dbSDimitry Andric           DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
45925ffd83dbSDimitry Andric           SDLoc(Node), Node->getOperand(0));
45935ffd83dbSDimitry Andric       Results.push_back(Tmp.first);
45945ffd83dbSDimitry Andric       Results.push_back(Tmp.second);
45955ffd83dbSDimitry Andric     }
45965ffd83dbSDimitry Andric     break;
45975ffd83dbSDimitry Andric   }
45980b57cec5SDimitry Andric   case ISD::FP_TO_FP16: {
45990b57cec5SDimitry Andric     RTLIB::Libcall LC =
46000b57cec5SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
46010b57cec5SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
4602*06c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
46030b57cec5SDimitry Andric     break;
46040b57cec5SDimitry Andric   }
460581ad6265SDimitry Andric   case ISD::FP_TO_BF16: {
460681ad6265SDimitry Andric     RTLIB::Libcall LC =
460781ad6265SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
460881ad6265SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
4609*06c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
461081ad6265SDimitry Andric     break;
461181ad6265SDimitry Andric   }
4612e8d8bef9SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
4613e8d8bef9SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
4614e8d8bef9SDimitry Andric   case ISD::SINT_TO_FP:
4615e8d8bef9SDimitry Andric   case ISD::UINT_TO_FP: {
4616e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
4617e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4618e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
4619e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
4620e8d8bef9SDimitry Andric     EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
4621e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4622e8d8bef9SDimitry Andric     EVT NVT = EVT();
4623e8d8bef9SDimitry Andric     SDLoc dl(Node);
4624e8d8bef9SDimitry Andric 
4625e8d8bef9SDimitry Andric     // Even if the input is legal, no libcall may exactly match, eg. we don't
4626e8d8bef9SDimitry Andric     // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
4627e8d8bef9SDimitry Andric     // eg: i13 -> fp. Then, look for an appropriate libcall.
4628e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4629e8d8bef9SDimitry Andric     for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
4630e8d8bef9SDimitry Andric          t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4631e8d8bef9SDimitry Andric          ++t) {
4632e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)t;
4633e8d8bef9SDimitry Andric       // The source needs to big enough to hold the operand.
4634e8d8bef9SDimitry Andric       if (NVT.bitsGE(SVT))
4635e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
4636e8d8bef9SDimitry Andric                     : RTLIB::getUINTTOFP(NVT, RVT);
4637e8d8bef9SDimitry Andric     }
4638e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4639e8d8bef9SDimitry Andric 
4640e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4641e8d8bef9SDimitry Andric     // Sign/zero extend the argument if the libcall takes a larger type.
4642e8d8bef9SDimitry Andric     SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
4643e8d8bef9SDimitry Andric                              NVT, Node->getOperand(IsStrict ? 1 : 0));
4644e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4645e8d8bef9SDimitry Andric     CallOptions.setSExt(Signed);
4646e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4647e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
4648e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4649e8d8bef9SDimitry Andric     if (IsStrict)
4650e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4651e8d8bef9SDimitry Andric     break;
4652e8d8bef9SDimitry Andric   }
4653e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT:
4654e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT:
4655e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
4656e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_UINT: {
4657e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
4658e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4659e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
4660e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
4661e8d8bef9SDimitry Andric 
4662e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4663e8d8bef9SDimitry Andric     EVT SVT = Op.getValueType();
4664e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4665e8d8bef9SDimitry Andric     EVT NVT = EVT();
4666e8d8bef9SDimitry Andric     SDLoc dl(Node);
4667e8d8bef9SDimitry Andric 
4668e8d8bef9SDimitry Andric     // Even if the result is legal, no libcall may exactly match, eg. we don't
4669e8d8bef9SDimitry Andric     // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
4670e8d8bef9SDimitry Andric     // eg: fp -> i32. Then, look for an appropriate libcall.
4671e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4672e8d8bef9SDimitry Andric     for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
4673e8d8bef9SDimitry Andric          IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4674e8d8bef9SDimitry Andric          ++IntVT) {
4675e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)IntVT;
4676e8d8bef9SDimitry Andric       // The type needs to big enough to hold the result.
4677e8d8bef9SDimitry Andric       if (NVT.bitsGE(RVT))
4678e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
4679e8d8bef9SDimitry Andric                     : RTLIB::getFPTOUINT(SVT, NVT);
4680e8d8bef9SDimitry Andric     }
4681e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4682e8d8bef9SDimitry Andric 
4683e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4684e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4685e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4686e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
4687e8d8bef9SDimitry Andric 
4688e8d8bef9SDimitry Andric     // Truncate the result if the libcall returns a larger type.
4689e8d8bef9SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
4690e8d8bef9SDimitry Andric     if (IsStrict)
4691e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4692e8d8bef9SDimitry Andric     break;
4693e8d8bef9SDimitry Andric   }
4694e8d8bef9SDimitry Andric 
4695e8d8bef9SDimitry Andric   case ISD::FP_ROUND:
4696e8d8bef9SDimitry Andric   case ISD::STRICT_FP_ROUND: {
4697e8d8bef9SDimitry Andric     // X = FP_ROUND(Y, TRUNC)
4698e8d8bef9SDimitry Andric     // TRUNC is a flag, which is always an integer that is zero or one.
4699e8d8bef9SDimitry Andric     // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
4700e8d8bef9SDimitry Andric     // is known to not change the value of Y.
4701e8d8bef9SDimitry Andric     // We can only expand it into libcall if the TRUNC is 0.
4702e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4703e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4704e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4705e8d8bef9SDimitry Andric     EVT VT = Node->getValueType(0);
4706349cc55cSDimitry Andric     assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
4707e8d8bef9SDimitry Andric            "Unable to expand as libcall if it is not normal rounding");
4708e8d8bef9SDimitry Andric 
4709e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
4710e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4711e8d8bef9SDimitry Andric 
4712e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4713e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4714e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
4715e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4716e8d8bef9SDimitry Andric     if (IsStrict)
4717e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4718e8d8bef9SDimitry Andric     break;
4719e8d8bef9SDimitry Andric   }
4720e8d8bef9SDimitry Andric   case ISD::FP_EXTEND: {
4721e8d8bef9SDimitry Andric     Results.push_back(
4722e8d8bef9SDimitry Andric         ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
4723e8d8bef9SDimitry Andric                                       Node->getValueType(0)),
4724*06c3fb27SDimitry Andric                       Node, false).first);
4725e8d8bef9SDimitry Andric     break;
4726e8d8bef9SDimitry Andric   }
4727e8d8bef9SDimitry Andric   case ISD::STRICT_FP_EXTEND:
47285ffd83dbSDimitry Andric   case ISD::STRICT_FP_TO_FP16: {
47295ffd83dbSDimitry Andric     RTLIB::Libcall LC =
4730e8d8bef9SDimitry Andric         Node->getOpcode() == ISD::STRICT_FP_TO_FP16
4731e8d8bef9SDimitry Andric             ? RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16)
4732e8d8bef9SDimitry Andric             : RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
4733e8d8bef9SDimitry Andric                               Node->getValueType(0));
4734e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4735e8d8bef9SDimitry Andric 
47365ffd83dbSDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
47375ffd83dbSDimitry Andric     std::pair<SDValue, SDValue> Tmp =
47385ffd83dbSDimitry Andric         TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
47395ffd83dbSDimitry Andric                         CallOptions, SDLoc(Node), Node->getOperand(0));
47405ffd83dbSDimitry Andric     Results.push_back(Tmp.first);
47415ffd83dbSDimitry Andric     Results.push_back(Tmp.second);
47425ffd83dbSDimitry Andric     break;
47435ffd83dbSDimitry Andric   }
47440b57cec5SDimitry Andric   case ISD::FSUB:
4745480093f4SDimitry Andric   case ISD::STRICT_FSUB:
4746480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
47470b57cec5SDimitry Andric                     RTLIB::SUB_F80, RTLIB::SUB_F128,
4748480093f4SDimitry Andric                     RTLIB::SUB_PPCF128, Results);
47490b57cec5SDimitry Andric     break;
47500b57cec5SDimitry Andric   case ISD::SREM:
4751bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4752bdd1243dSDimitry Andric                                        RTLIB::SREM_I8,
4753bdd1243dSDimitry Andric                                        RTLIB::SREM_I16, RTLIB::SREM_I32,
4754bdd1243dSDimitry Andric                                        RTLIB::SREM_I64, RTLIB::SREM_I128));
47550b57cec5SDimitry Andric     break;
47560b57cec5SDimitry Andric   case ISD::UREM:
4757bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4758bdd1243dSDimitry Andric                                        RTLIB::UREM_I8,
4759bdd1243dSDimitry Andric                                        RTLIB::UREM_I16, RTLIB::UREM_I32,
4760bdd1243dSDimitry Andric                                        RTLIB::UREM_I64, RTLIB::UREM_I128));
47610b57cec5SDimitry Andric     break;
47620b57cec5SDimitry Andric   case ISD::SDIV:
4763bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4764bdd1243dSDimitry Andric                                        RTLIB::SDIV_I8,
4765bdd1243dSDimitry Andric                                        RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4766bdd1243dSDimitry Andric                                        RTLIB::SDIV_I64, RTLIB::SDIV_I128));
47670b57cec5SDimitry Andric     break;
47680b57cec5SDimitry Andric   case ISD::UDIV:
4769bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4770bdd1243dSDimitry Andric                                        RTLIB::UDIV_I8,
4771bdd1243dSDimitry Andric                                        RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4772bdd1243dSDimitry Andric                                        RTLIB::UDIV_I64, RTLIB::UDIV_I128));
47730b57cec5SDimitry Andric     break;
47740b57cec5SDimitry Andric   case ISD::SDIVREM:
47750b57cec5SDimitry Andric   case ISD::UDIVREM:
47760b57cec5SDimitry Andric     // Expand into divrem libcall
47770b57cec5SDimitry Andric     ExpandDivRemLibCall(Node, Results);
47780b57cec5SDimitry Andric     break;
47790b57cec5SDimitry Andric   case ISD::MUL:
4780bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4781bdd1243dSDimitry Andric                                        RTLIB::MUL_I8,
4782bdd1243dSDimitry Andric                                        RTLIB::MUL_I16, RTLIB::MUL_I32,
4783bdd1243dSDimitry Andric                                        RTLIB::MUL_I64, RTLIB::MUL_I128));
47840b57cec5SDimitry Andric     break;
47850b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
47860b57cec5SDimitry Andric     switch (Node->getSimpleValueType(0).SimpleTy) {
47870b57cec5SDimitry Andric     default:
47880b57cec5SDimitry Andric       llvm_unreachable("LibCall explicitly requested, but not available");
47890b57cec5SDimitry Andric     case MVT::i32:
4790*06c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first);
47910b57cec5SDimitry Andric       break;
47920b57cec5SDimitry Andric     case MVT::i64:
4793*06c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first);
47940b57cec5SDimitry Andric       break;
47950b57cec5SDimitry Andric     case MVT::i128:
4796*06c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first);
47970b57cec5SDimitry Andric       break;
47980b57cec5SDimitry Andric     }
47990b57cec5SDimitry Andric     break;
4800*06c3fb27SDimitry Andric   case ISD::RESET_FPENV: {
4801*06c3fb27SDimitry Andric     // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
4802*06c3fb27SDimitry Andric     // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
4803*06c3fb27SDimitry Andric     SDValue Ptr = DAG.getIntPtrConstant(-1LL, dl);
4804*06c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
4805*06c3fb27SDimitry Andric     Results.push_back(
4806*06c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
4807*06c3fb27SDimitry Andric     break;
4808*06c3fb27SDimitry Andric   }
4809*06c3fb27SDimitry Andric   case ISD::GET_FPENV_MEM: {
4810*06c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
4811*06c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
4812*06c3fb27SDimitry Andric     Results.push_back(
4813*06c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
4814*06c3fb27SDimitry Andric     break;
4815*06c3fb27SDimitry Andric   }
4816*06c3fb27SDimitry Andric   case ISD::SET_FPENV_MEM: {
4817*06c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
4818*06c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
4819*06c3fb27SDimitry Andric     Results.push_back(
4820*06c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
4821*06c3fb27SDimitry Andric     break;
4822*06c3fb27SDimitry Andric   }
48230b57cec5SDimitry Andric   }
48240b57cec5SDimitry Andric 
48250b57cec5SDimitry Andric   // Replace the original node with the legalized result.
48260b57cec5SDimitry Andric   if (!Results.empty()) {
48270b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
48280b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
48290b57cec5SDimitry Andric   } else
48300b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
48310b57cec5SDimitry Andric }
48320b57cec5SDimitry Andric 
48330b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when
48340b57cec5SDimitry Andric // promoting equally sized vectors.
48350b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI,
48360b57cec5SDimitry Andric                                         MVT EltVT, MVT NewEltVT) {
48370b57cec5SDimitry Andric   unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
48380b57cec5SDimitry Andric   MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
48390b57cec5SDimitry Andric   assert(TLI.isTypeLegal(MidVT) && "unexpected");
48400b57cec5SDimitry Andric   return MidVT;
48410b57cec5SDimitry Andric }
48420b57cec5SDimitry Andric 
48430b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
48440b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to promote node\n");
48450b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
48460b57cec5SDimitry Andric   MVT OVT = Node->getSimpleValueType(0);
48470b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::UINT_TO_FP ||
48480b57cec5SDimitry Andric       Node->getOpcode() == ISD::SINT_TO_FP ||
48490b57cec5SDimitry Andric       Node->getOpcode() == ISD::SETCC ||
48500b57cec5SDimitry Andric       Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
48510b57cec5SDimitry Andric       Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
48520b57cec5SDimitry Andric     OVT = Node->getOperand(0).getSimpleValueType();
48530b57cec5SDimitry Andric   }
4854480093f4SDimitry Andric   if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
4855e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
4856e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCC ||
4857e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCCS)
4858480093f4SDimitry Andric     OVT = Node->getOperand(1).getSimpleValueType();
4859fe6060f1SDimitry Andric   if (Node->getOpcode() == ISD::BR_CC ||
4860fe6060f1SDimitry Andric       Node->getOpcode() == ISD::SELECT_CC)
48610b57cec5SDimitry Andric     OVT = Node->getOperand(2).getSimpleValueType();
48620b57cec5SDimitry Andric   MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
48630b57cec5SDimitry Andric   SDLoc dl(Node);
4864fe6060f1SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
48650b57cec5SDimitry Andric   switch (Node->getOpcode()) {
48660b57cec5SDimitry Andric   case ISD::CTTZ:
48670b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
48680b57cec5SDimitry Andric   case ISD::CTLZ:
48690b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
48700b57cec5SDimitry Andric   case ISD::CTPOP:
48715ffd83dbSDimitry Andric     // Zero extend the argument unless its cttz, then use any_extend.
48725ffd83dbSDimitry Andric     if (Node->getOpcode() == ISD::CTTZ ||
48735ffd83dbSDimitry Andric         Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
48745ffd83dbSDimitry Andric       Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
48755ffd83dbSDimitry Andric     else
48760b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
48775ffd83dbSDimitry Andric 
48780b57cec5SDimitry Andric     if (Node->getOpcode() == ISD::CTTZ) {
48790b57cec5SDimitry Andric       // The count is the same in the promoted type except if the original
48800b57cec5SDimitry Andric       // value was zero.  This can be handled by setting the bit just off
48810b57cec5SDimitry Andric       // the top of the original type.
48820b57cec5SDimitry Andric       auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
48830b57cec5SDimitry Andric                                         OVT.getSizeInBits());
48840b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
48850b57cec5SDimitry Andric                          DAG.getConstant(TopBit, dl, NVT));
48860b57cec5SDimitry Andric     }
48870b57cec5SDimitry Andric     // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
48880b57cec5SDimitry Andric     // already the correct result.
48890b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
48900b57cec5SDimitry Andric     if (Node->getOpcode() == ISD::CTLZ ||
48910b57cec5SDimitry Andric         Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
48920b57cec5SDimitry Andric       // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
48930b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
48940b57cec5SDimitry Andric                           DAG.getConstant(NVT.getSizeInBits() -
48950b57cec5SDimitry Andric                                           OVT.getSizeInBits(), dl, NVT));
48960b57cec5SDimitry Andric     }
48970b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
48980b57cec5SDimitry Andric     break;
48990b57cec5SDimitry Andric   case ISD::BITREVERSE:
49000b57cec5SDimitry Andric   case ISD::BSWAP: {
49010b57cec5SDimitry Andric     unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
49020b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
49030b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
49040b57cec5SDimitry Andric     Tmp1 = DAG.getNode(
49050b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
49060b57cec5SDimitry Andric         DAG.getConstant(DiffBits, dl,
49070b57cec5SDimitry Andric                         TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
49080b57cec5SDimitry Andric 
49090b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
49100b57cec5SDimitry Andric     break;
49110b57cec5SDimitry Andric   }
49120b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
4913480093f4SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
49140b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
4915480093f4SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
4916480093f4SDimitry Andric     PromoteLegalFP_TO_INT(Node, dl, Results);
49170b57cec5SDimitry Andric     break;
4918e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
4919e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
4920e8d8bef9SDimitry Andric     Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
4921e8d8bef9SDimitry Andric     break;
49220b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
4923480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
49240b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
4925480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
4926480093f4SDimitry Andric     PromoteLegalINT_TO_FP(Node, dl, Results);
49270b57cec5SDimitry Andric     break;
49280b57cec5SDimitry Andric   case ISD::VAARG: {
49290b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0); // Get the chain.
49300b57cec5SDimitry Andric     SDValue Ptr = Node->getOperand(1); // Get the pointer.
49310b57cec5SDimitry Andric 
49320b57cec5SDimitry Andric     unsigned TruncOp;
49330b57cec5SDimitry Andric     if (OVT.isVector()) {
49340b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
49350b57cec5SDimitry Andric     } else {
49360b57cec5SDimitry Andric       assert(OVT.isInteger()
49370b57cec5SDimitry Andric         && "VAARG promotion is supported only for vectors or integer types");
49380b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
49390b57cec5SDimitry Andric     }
49400b57cec5SDimitry Andric 
49410b57cec5SDimitry Andric     // Perform the larger operation, then convert back
49420b57cec5SDimitry Andric     Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
49430b57cec5SDimitry Andric              Node->getConstantOperandVal(3));
49440b57cec5SDimitry Andric     Chain = Tmp1.getValue(1);
49450b57cec5SDimitry Andric 
49460b57cec5SDimitry Andric     Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
49470b57cec5SDimitry Andric 
49480b57cec5SDimitry Andric     // Modified the chain result - switch anything that used the old chain to
49490b57cec5SDimitry Andric     // use the new one.
49500b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
49510b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
49520b57cec5SDimitry Andric     if (UpdatedNodes) {
49530b57cec5SDimitry Andric       UpdatedNodes->insert(Tmp2.getNode());
49540b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
49550b57cec5SDimitry Andric     }
49560b57cec5SDimitry Andric     ReplacedNode(Node);
49570b57cec5SDimitry Andric     break;
49580b57cec5SDimitry Andric   }
49590b57cec5SDimitry Andric   case ISD::MUL:
49600b57cec5SDimitry Andric   case ISD::SDIV:
49610b57cec5SDimitry Andric   case ISD::SREM:
49620b57cec5SDimitry Andric   case ISD::UDIV:
49630b57cec5SDimitry Andric   case ISD::UREM:
49640b57cec5SDimitry Andric   case ISD::AND:
49650b57cec5SDimitry Andric   case ISD::OR:
49660b57cec5SDimitry Andric   case ISD::XOR: {
49670b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
49680b57cec5SDimitry Andric     if (OVT.isVector()) {
49690b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
49700b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
49710b57cec5SDimitry Andric     } else {
49720b57cec5SDimitry Andric       assert(OVT.isInteger() && "Cannot promote logic operation");
49730b57cec5SDimitry Andric 
49740b57cec5SDimitry Andric       switch (Node->getOpcode()) {
49750b57cec5SDimitry Andric       default:
49760b57cec5SDimitry Andric         ExtOp = ISD::ANY_EXTEND;
49770b57cec5SDimitry Andric         break;
49780b57cec5SDimitry Andric       case ISD::SDIV:
49790b57cec5SDimitry Andric       case ISD::SREM:
49800b57cec5SDimitry Andric         ExtOp = ISD::SIGN_EXTEND;
49810b57cec5SDimitry Andric         break;
49820b57cec5SDimitry Andric       case ISD::UDIV:
49830b57cec5SDimitry Andric       case ISD::UREM:
49840b57cec5SDimitry Andric         ExtOp = ISD::ZERO_EXTEND;
49850b57cec5SDimitry Andric         break;
49860b57cec5SDimitry Andric       }
49870b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
49880b57cec5SDimitry Andric     }
49890b57cec5SDimitry Andric     // Promote each of the values to the new type.
49900b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
49910b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
49920b57cec5SDimitry Andric     // Perform the larger operation, then convert back
49930b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
49940b57cec5SDimitry Andric     Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
49950b57cec5SDimitry Andric     break;
49960b57cec5SDimitry Andric   }
49970b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
49980b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
49990b57cec5SDimitry Andric     // Promote to a multiply in a wider integer type.
50000b57cec5SDimitry Andric     unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
50010b57cec5SDimitry Andric                                                          : ISD::SIGN_EXTEND;
50020b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
50030b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
50040b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
50050b57cec5SDimitry Andric 
50060b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
50070b57cec5SDimitry Andric     unsigned OriginalSize = OVT.getScalarSizeInBits();
50080b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
50090b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
50100b57cec5SDimitry Andric         DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT)));
50110b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
50120b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
50130b57cec5SDimitry Andric     break;
50140b57cec5SDimitry Andric   }
50150b57cec5SDimitry Andric   case ISD::SELECT: {
50160b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
50170b57cec5SDimitry Andric     if (Node->getValueType(0).isVector() ||
50180b57cec5SDimitry Andric         Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
50190b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
50200b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
50210b57cec5SDimitry Andric     } else if (Node->getValueType(0).isInteger()) {
50220b57cec5SDimitry Andric       ExtOp   = ISD::ANY_EXTEND;
50230b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
50240b57cec5SDimitry Andric     } else {
50250b57cec5SDimitry Andric       ExtOp   = ISD::FP_EXTEND;
50260b57cec5SDimitry Andric       TruncOp = ISD::FP_ROUND;
50270b57cec5SDimitry Andric     }
50280b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
50290b57cec5SDimitry Andric     // Promote each of the values to the new type.
50300b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
50310b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
50320b57cec5SDimitry Andric     // Perform the larger operation, then round down.
50330b57cec5SDimitry Andric     Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
50340b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
50350b57cec5SDimitry Andric     if (TruncOp != ISD::FP_ROUND)
50360b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
50370b57cec5SDimitry Andric     else
50380b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
50390b57cec5SDimitry Andric                          DAG.getIntPtrConstant(0, dl));
50400b57cec5SDimitry Andric     Results.push_back(Tmp1);
50410b57cec5SDimitry Andric     break;
50420b57cec5SDimitry Andric   }
50430b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
50440b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
50450b57cec5SDimitry Andric 
50460b57cec5SDimitry Andric     // Cast the two input vectors.
50470b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
50480b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
50490b57cec5SDimitry Andric 
50500b57cec5SDimitry Andric     // Convert the shuffle mask to the right # elements.
50510b57cec5SDimitry Andric     Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
50520b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
50530b57cec5SDimitry Andric     Results.push_back(Tmp1);
50540b57cec5SDimitry Andric     break;
50550b57cec5SDimitry Andric   }
5056fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
5057fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5058fe6060f1SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5059fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2,
5060fe6060f1SDimitry Andric                        Node->getOperand(2));
5061fe6060f1SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5062fe6060f1SDimitry Andric     break;
5063fe6060f1SDimitry Andric   }
5064fe6060f1SDimitry Andric   case ISD::SELECT_CC: {
5065fe6060f1SDimitry Andric     SDValue Cond = Node->getOperand(4);
5066fe6060f1SDimitry Andric     ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5067fe6060f1SDimitry Andric     // Type of the comparison operands.
5068fe6060f1SDimitry Andric     MVT CVT = Node->getSimpleValueType(0);
5069fe6060f1SDimitry Andric     assert(CVT == OVT && "not handled");
5070fe6060f1SDimitry Andric 
5071fe6060f1SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
5072fe6060f1SDimitry Andric     if (NVT.isInteger()) {
5073fe6060f1SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5074fe6060f1SDimitry Andric     }
5075fe6060f1SDimitry Andric 
5076fe6060f1SDimitry Andric     // Promote the comparison operands, if needed.
5077fe6060f1SDimitry Andric     if (TLI.isCondCodeLegal(CCCode, CVT)) {
5078fe6060f1SDimitry Andric       Tmp1 = Node->getOperand(0);
5079fe6060f1SDimitry Andric       Tmp2 = Node->getOperand(1);
5080fe6060f1SDimitry Andric     } else {
5081fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5082fe6060f1SDimitry Andric       Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5083fe6060f1SDimitry Andric     }
5084fe6060f1SDimitry Andric     // Cast the true/false operands.
5085fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5086fe6060f1SDimitry Andric     Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5087fe6060f1SDimitry Andric 
5088fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5089fe6060f1SDimitry Andric                        Node->getFlags());
5090fe6060f1SDimitry Andric 
5091fe6060f1SDimitry Andric     // Cast the result back to the original type.
5092fe6060f1SDimitry Andric     if (ExtOp != ISD::FP_EXTEND)
5093fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5094fe6060f1SDimitry Andric     else
5095fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5096bdd1243dSDimitry Andric                          DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5097fe6060f1SDimitry Andric 
5098fe6060f1SDimitry Andric     Results.push_back(Tmp1);
5099fe6060f1SDimitry Andric     break;
5100fe6060f1SDimitry Andric   }
5101e8d8bef9SDimitry Andric   case ISD::SETCC:
5102e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCC:
5103e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCCS: {
51040b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
51050b57cec5SDimitry Andric     if (NVT.isInteger()) {
5106e8d8bef9SDimitry Andric       ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
51070b57cec5SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
51080b57cec5SDimitry Andric     }
5109e8d8bef9SDimitry Andric     if (Node->isStrictFPOpcode()) {
5110e8d8bef9SDimitry Andric       SDValue InChain = Node->getOperand(0);
5111e8d8bef9SDimitry Andric       std::tie(Tmp1, std::ignore) =
5112e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
5113e8d8bef9SDimitry Andric       std::tie(Tmp2, std::ignore) =
5114e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
5115e8d8bef9SDimitry Andric       SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
5116e8d8bef9SDimitry Andric       SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
5117e8d8bef9SDimitry Andric       SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
5118e8d8bef9SDimitry Andric       Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
5119e8d8bef9SDimitry Andric                                     {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
5120e8d8bef9SDimitry Andric                                     Node->getFlags()));
5121e8d8bef9SDimitry Andric       Results.push_back(Results.back().getValue(1));
5122e8d8bef9SDimitry Andric       break;
5123e8d8bef9SDimitry Andric     }
51240b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
51250b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
51260b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
51270b57cec5SDimitry Andric                                   Tmp2, Node->getOperand(2), Node->getFlags()));
51280b57cec5SDimitry Andric     break;
51290b57cec5SDimitry Andric   }
51300b57cec5SDimitry Andric   case ISD::BR_CC: {
51310b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
51320b57cec5SDimitry Andric     if (NVT.isInteger()) {
51330b57cec5SDimitry Andric       ISD::CondCode CCCode =
51340b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(1))->get();
51350b57cec5SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
51360b57cec5SDimitry Andric     }
51370b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
51380b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
51390b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
51400b57cec5SDimitry Andric                                   Node->getOperand(0), Node->getOperand(1),
51410b57cec5SDimitry Andric                                   Tmp1, Tmp2, Node->getOperand(4)));
51420b57cec5SDimitry Andric     break;
51430b57cec5SDimitry Andric   }
51440b57cec5SDimitry Andric   case ISD::FADD:
51450b57cec5SDimitry Andric   case ISD::FSUB:
51460b57cec5SDimitry Andric   case ISD::FMUL:
51470b57cec5SDimitry Andric   case ISD::FDIV:
51480b57cec5SDimitry Andric   case ISD::FREM:
51490b57cec5SDimitry Andric   case ISD::FMINNUM:
51500b57cec5SDimitry Andric   case ISD::FMAXNUM:
5151*06c3fb27SDimitry Andric   case ISD::FMINIMUM:
5152*06c3fb27SDimitry Andric   case ISD::FMAXIMUM:
51530b57cec5SDimitry Andric   case ISD::FPOW:
51540b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
51550b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
51560b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
51570b57cec5SDimitry Andric                        Node->getFlags());
5158bdd1243dSDimitry Andric     Results.push_back(
5159bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5160bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
51610b57cec5SDimitry Andric     break;
516281ad6265SDimitry Andric   case ISD::STRICT_FADD:
516381ad6265SDimitry Andric   case ISD::STRICT_FSUB:
516481ad6265SDimitry Andric   case ISD::STRICT_FMUL:
516581ad6265SDimitry Andric   case ISD::STRICT_FDIV:
516681ad6265SDimitry Andric   case ISD::STRICT_FMINNUM:
516781ad6265SDimitry Andric   case ISD::STRICT_FMAXNUM:
5168480093f4SDimitry Andric   case ISD::STRICT_FREM:
5169480093f4SDimitry Andric   case ISD::STRICT_FPOW:
5170480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5171480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5172480093f4SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5173480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
5174480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5175480093f4SDimitry Andric                        Tmp2.getValue(1));
5176480093f4SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5177480093f4SDimitry Andric                        {Tmp3, Tmp1, Tmp2});
5178480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5179480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)});
5180480093f4SDimitry Andric     Results.push_back(Tmp1);
5181480093f4SDimitry Andric     Results.push_back(Tmp1.getValue(1));
5182480093f4SDimitry Andric     break;
51830b57cec5SDimitry Andric   case ISD::FMA:
51840b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
51850b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
51860b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
51870b57cec5SDimitry Andric     Results.push_back(
51880b57cec5SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT,
51890b57cec5SDimitry Andric                     DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
5190bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
51910b57cec5SDimitry Andric     break;
519281ad6265SDimitry Andric   case ISD::STRICT_FMA:
519381ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
519481ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
519581ad6265SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
519681ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
519781ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
519881ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(3)});
519981ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
520081ad6265SDimitry Andric                        Tmp2.getValue(1), Tmp3.getValue(1));
520181ad6265SDimitry Andric     Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
520281ad6265SDimitry Andric                        {Tmp4, Tmp1, Tmp2, Tmp3});
520381ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
520481ad6265SDimitry Andric                        {Tmp4.getValue(1), Tmp4, DAG.getIntPtrConstant(0, dl)});
520581ad6265SDimitry Andric     Results.push_back(Tmp4);
520681ad6265SDimitry Andric     Results.push_back(Tmp4.getValue(1));
520781ad6265SDimitry Andric     break;
52080b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
5209*06c3fb27SDimitry Andric   case ISD::FLDEXP:
52100b57cec5SDimitry Andric   case ISD::FPOWI: {
52110b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
52120b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
52130b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
52140b57cec5SDimitry Andric 
52150b57cec5SDimitry Andric     // fcopysign doesn't change anything but the sign bit, so
52160b57cec5SDimitry Andric     //   (fp_round (fcopysign (fpext a), b))
52170b57cec5SDimitry Andric     // is as precise as
52180b57cec5SDimitry Andric     //   (fp_round (fpext a))
52190b57cec5SDimitry Andric     // which is a no-op. Mark it as a TRUNCating FP_ROUND.
52200b57cec5SDimitry Andric     const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
5221bdd1243dSDimitry Andric     Results.push_back(
5222bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5223bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
52240b57cec5SDimitry Andric     break;
52250b57cec5SDimitry Andric   }
522681ad6265SDimitry Andric   case ISD::STRICT_FPOWI:
522781ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
522881ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
522981ad6265SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
523081ad6265SDimitry Andric                        {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
523181ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
523281ad6265SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
523381ad6265SDimitry Andric     Results.push_back(Tmp3);
523481ad6265SDimitry Andric     Results.push_back(Tmp3.getValue(1));
523581ad6265SDimitry Andric     break;
5236*06c3fb27SDimitry Andric   case ISD::FFREXP: {
5237*06c3fb27SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
5238*06c3fb27SDimitry Andric     Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
5239*06c3fb27SDimitry Andric 
5240*06c3fb27SDimitry Andric     Results.push_back(
5241*06c3fb27SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5242*06c3fb27SDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5243*06c3fb27SDimitry Andric 
5244*06c3fb27SDimitry Andric     Results.push_back(Tmp2.getValue(1));
5245*06c3fb27SDimitry Andric     break;
5246*06c3fb27SDimitry Andric   }
52470b57cec5SDimitry Andric   case ISD::FFLOOR:
52480b57cec5SDimitry Andric   case ISD::FCEIL:
52490b57cec5SDimitry Andric   case ISD::FRINT:
52500b57cec5SDimitry Andric   case ISD::FNEARBYINT:
52510b57cec5SDimitry Andric   case ISD::FROUND:
52525ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
52530b57cec5SDimitry Andric   case ISD::FTRUNC:
52540b57cec5SDimitry Andric   case ISD::FNEG:
52550b57cec5SDimitry Andric   case ISD::FSQRT:
52560b57cec5SDimitry Andric   case ISD::FSIN:
52570b57cec5SDimitry Andric   case ISD::FCOS:
52580b57cec5SDimitry Andric   case ISD::FLOG:
52590b57cec5SDimitry Andric   case ISD::FLOG2:
52600b57cec5SDimitry Andric   case ISD::FLOG10:
52610b57cec5SDimitry Andric   case ISD::FABS:
52620b57cec5SDimitry Andric   case ISD::FEXP:
52630b57cec5SDimitry Andric   case ISD::FEXP2:
52640b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
52650b57cec5SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5266bdd1243dSDimitry Andric     Results.push_back(
5267bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5268bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
52690b57cec5SDimitry Andric     break;
5270480093f4SDimitry Andric   case ISD::STRICT_FFLOOR:
5271480093f4SDimitry Andric   case ISD::STRICT_FCEIL:
527281ad6265SDimitry Andric   case ISD::STRICT_FRINT:
527381ad6265SDimitry Andric   case ISD::STRICT_FNEARBYINT:
5274349cc55cSDimitry Andric   case ISD::STRICT_FROUND:
527581ad6265SDimitry Andric   case ISD::STRICT_FROUNDEVEN:
527681ad6265SDimitry Andric   case ISD::STRICT_FTRUNC:
527781ad6265SDimitry Andric   case ISD::STRICT_FSQRT:
5278480093f4SDimitry Andric   case ISD::STRICT_FSIN:
5279480093f4SDimitry Andric   case ISD::STRICT_FCOS:
5280480093f4SDimitry Andric   case ISD::STRICT_FLOG:
528181ad6265SDimitry Andric   case ISD::STRICT_FLOG2:
5282480093f4SDimitry Andric   case ISD::STRICT_FLOG10:
5283480093f4SDimitry Andric   case ISD::STRICT_FEXP:
528481ad6265SDimitry Andric   case ISD::STRICT_FEXP2:
5285480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5286480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5287480093f4SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5288480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1});
5289480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5290480093f4SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
5291480093f4SDimitry Andric     Results.push_back(Tmp3);
5292480093f4SDimitry Andric     Results.push_back(Tmp3.getValue(1));
5293480093f4SDimitry Andric     break;
52940b57cec5SDimitry Andric   case ISD::BUILD_VECTOR: {
52950b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
52960b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
52970b57cec5SDimitry Andric 
52980b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
52990b57cec5SDimitry Andric     //
53000b57cec5SDimitry Andric     // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
53010b57cec5SDimitry Andric     //  =>
53020b57cec5SDimitry Andric     //  v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
53030b57cec5SDimitry Andric 
53040b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
53050b57cec5SDimitry Andric            "Invalid promote type for build_vector");
53060b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
53070b57cec5SDimitry Andric 
53080b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
53090b57cec5SDimitry Andric 
53100b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
53110b57cec5SDimitry Andric     for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
53120b57cec5SDimitry Andric       SDValue Op = Node->getOperand(I);
53130b57cec5SDimitry Andric       NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
53140b57cec5SDimitry Andric     }
53150b57cec5SDimitry Andric 
53160b57cec5SDimitry Andric     SDLoc SL(Node);
53170b57cec5SDimitry Andric     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
53180b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
53190b57cec5SDimitry Andric     Results.push_back(CvtVec);
53200b57cec5SDimitry Andric     break;
53210b57cec5SDimitry Andric   }
53220b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT: {
53230b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
53240b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
53250b57cec5SDimitry Andric 
53260b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size.
53270b57cec5SDimitry Andric     //
53280b57cec5SDimitry Andric     // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
53290b57cec5SDimitry Andric     //  =>
53300b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
53310b57cec5SDimitry Andric     //
53320b57cec5SDimitry Andric     // i64 = bitcast
53330b57cec5SDimitry Andric     //   (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
53340b57cec5SDimitry Andric     //                       (i32 (extract_vector_elt castx, (2 * y + 1)))
53350b57cec5SDimitry Andric     //
53360b57cec5SDimitry Andric 
53370b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
53380b57cec5SDimitry Andric            "Invalid promote type for extract_vector_elt");
53390b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
53400b57cec5SDimitry Andric 
53410b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
53420b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
53430b57cec5SDimitry Andric 
53440b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(1);
53450b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
53460b57cec5SDimitry Andric     SDLoc SL(Node);
53470b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
53480b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
53490b57cec5SDimitry Andric 
53500b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
53510b57cec5SDimitry Andric 
53520b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
53530b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
53540b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
53550b57cec5SDimitry Andric       SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
53560b57cec5SDimitry Andric 
53570b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
53580b57cec5SDimitry Andric                                 CastVec, TmpIdx);
53590b57cec5SDimitry Andric       NewOps.push_back(Elt);
53600b57cec5SDimitry Andric     }
53610b57cec5SDimitry Andric 
53620b57cec5SDimitry Andric     SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
53630b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
53640b57cec5SDimitry Andric     break;
53650b57cec5SDimitry Andric   }
53660b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT: {
53670b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
53680b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
53690b57cec5SDimitry Andric 
53700b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
53710b57cec5SDimitry Andric     //
53720b57cec5SDimitry Andric     // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
53730b57cec5SDimitry Andric     //  =>
53740b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
53750b57cec5SDimitry Andric     //  v2i32:casty = bitcast y:i64
53760b57cec5SDimitry Andric     //
53770b57cec5SDimitry Andric     // v2i64 = bitcast
53780b57cec5SDimitry Andric     //   (v4i32 insert_vector_elt
53790b57cec5SDimitry Andric     //       (v4i32 insert_vector_elt v4i32:castx,
53800b57cec5SDimitry Andric     //                                (extract_vector_elt casty, 0), 2 * z),
53810b57cec5SDimitry Andric     //        (extract_vector_elt casty, 1), (2 * z + 1))
53820b57cec5SDimitry Andric 
53830b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
53840b57cec5SDimitry Andric            "Invalid promote type for insert_vector_elt");
53850b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
53860b57cec5SDimitry Andric 
53870b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
53880b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
53890b57cec5SDimitry Andric 
53900b57cec5SDimitry Andric     SDValue Val = Node->getOperand(1);
53910b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(2);
53920b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
53930b57cec5SDimitry Andric     SDLoc SL(Node);
53940b57cec5SDimitry Andric 
53950b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
53960b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
53970b57cec5SDimitry Andric 
53980b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
53990b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
54000b57cec5SDimitry Andric 
54010b57cec5SDimitry Andric     SDValue NewVec = CastVec;
54020b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
54030b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
54040b57cec5SDimitry Andric       SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
54050b57cec5SDimitry Andric 
54060b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
54070b57cec5SDimitry Andric                                 CastVal, IdxOffset);
54080b57cec5SDimitry Andric 
54090b57cec5SDimitry Andric       NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
54100b57cec5SDimitry Andric                            NewVec, Elt, InEltIdx);
54110b57cec5SDimitry Andric     }
54120b57cec5SDimitry Andric 
54130b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
54140b57cec5SDimitry Andric     break;
54150b57cec5SDimitry Andric   }
54160b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR: {
54170b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
54180b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
54190b57cec5SDimitry Andric 
54200b57cec5SDimitry Andric     // Handle bitcasts to different vector type with the same total bit size.
54210b57cec5SDimitry Andric     //
54220b57cec5SDimitry Andric     // e.g. v2i64 = scalar_to_vector x:i64
54230b57cec5SDimitry Andric     //   =>
54240b57cec5SDimitry Andric     //  concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
54250b57cec5SDimitry Andric     //
54260b57cec5SDimitry Andric 
54270b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
54280b57cec5SDimitry Andric     SDValue Val = Node->getOperand(0);
54290b57cec5SDimitry Andric     SDLoc SL(Node);
54300b57cec5SDimitry Andric 
54310b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
54320b57cec5SDimitry Andric     SDValue Undef = DAG.getUNDEF(MidVT);
54330b57cec5SDimitry Andric 
54340b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewElts;
54350b57cec5SDimitry Andric     NewElts.push_back(CastVal);
54360b57cec5SDimitry Andric     for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
54370b57cec5SDimitry Andric       NewElts.push_back(Undef);
54380b57cec5SDimitry Andric 
54390b57cec5SDimitry Andric     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
54400b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
54410b57cec5SDimitry Andric     Results.push_back(CvtVec);
54420b57cec5SDimitry Andric     break;
54430b57cec5SDimitry Andric   }
54440b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP: {
54450b57cec5SDimitry Andric     AtomicSDNode *AM = cast<AtomicSDNode>(Node);
54460b57cec5SDimitry Andric     SDLoc SL(Node);
54470b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
54480b57cec5SDimitry Andric     assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
54490b57cec5SDimitry Andric            "unexpected promotion type");
54500b57cec5SDimitry Andric     assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
54510b57cec5SDimitry Andric            "unexpected atomic_swap with illegal type");
54520b57cec5SDimitry Andric 
54530b57cec5SDimitry Andric     SDValue NewAtomic
54540b57cec5SDimitry Andric       = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT,
54550b57cec5SDimitry Andric                       DAG.getVTList(NVT, MVT::Other),
54560b57cec5SDimitry Andric                       { AM->getChain(), AM->getBasePtr(), CastVal },
54570b57cec5SDimitry Andric                       AM->getMemOperand());
54580b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
54590b57cec5SDimitry Andric     Results.push_back(NewAtomic.getValue(1));
54600b57cec5SDimitry Andric     break;
54610b57cec5SDimitry Andric   }
54620b57cec5SDimitry Andric   }
54630b57cec5SDimitry Andric 
54640b57cec5SDimitry Andric   // Replace the original node with the legalized result.
54650b57cec5SDimitry Andric   if (!Results.empty()) {
54660b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
54670b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
54680b57cec5SDimitry Andric   } else
54690b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not promote node\n");
54700b57cec5SDimitry Andric }
54710b57cec5SDimitry Andric 
54720b57cec5SDimitry Andric /// This is the entry point for the file.
54730b57cec5SDimitry Andric void SelectionDAG::Legalize() {
54740b57cec5SDimitry Andric   AssignTopologicalOrder();
54750b57cec5SDimitry Andric 
54760b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
54770b57cec5SDimitry Andric   // Use a delete listener to remove nodes which were deleted during
54780b57cec5SDimitry Andric   // legalization from LegalizeNodes. This is needed to handle the situation
54790b57cec5SDimitry Andric   // where a new node is allocated by the object pool to the same address of a
54800b57cec5SDimitry Andric   // previously deleted node.
54810b57cec5SDimitry Andric   DAGNodeDeletedListener DeleteListener(
54820b57cec5SDimitry Andric       *this,
54830b57cec5SDimitry Andric       [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
54840b57cec5SDimitry Andric 
54850b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
54860b57cec5SDimitry Andric 
54870b57cec5SDimitry Andric   // Visit all the nodes. We start in topological order, so that we see
54880b57cec5SDimitry Andric   // nodes with their original operands intact. Legalization can produce
54890b57cec5SDimitry Andric   // new nodes which may themselves need to be legalized. Iterate until all
54900b57cec5SDimitry Andric   // nodes have been legalized.
54910b57cec5SDimitry Andric   while (true) {
54920b57cec5SDimitry Andric     bool AnyLegalized = false;
54930b57cec5SDimitry Andric     for (auto NI = allnodes_end(); NI != allnodes_begin();) {
54940b57cec5SDimitry Andric       --NI;
54950b57cec5SDimitry Andric 
54960b57cec5SDimitry Andric       SDNode *N = &*NI;
54970b57cec5SDimitry Andric       if (N->use_empty() && N != getRoot().getNode()) {
54980b57cec5SDimitry Andric         ++NI;
54990b57cec5SDimitry Andric         DeleteNode(N);
55000b57cec5SDimitry Andric         continue;
55010b57cec5SDimitry Andric       }
55020b57cec5SDimitry Andric 
55030b57cec5SDimitry Andric       if (LegalizedNodes.insert(N).second) {
55040b57cec5SDimitry Andric         AnyLegalized = true;
55050b57cec5SDimitry Andric         Legalizer.LegalizeOp(N);
55060b57cec5SDimitry Andric 
55070b57cec5SDimitry Andric         if (N->use_empty() && N != getRoot().getNode()) {
55080b57cec5SDimitry Andric           ++NI;
55090b57cec5SDimitry Andric           DeleteNode(N);
55100b57cec5SDimitry Andric         }
55110b57cec5SDimitry Andric       }
55120b57cec5SDimitry Andric     }
55130b57cec5SDimitry Andric     if (!AnyLegalized)
55140b57cec5SDimitry Andric       break;
55150b57cec5SDimitry Andric 
55160b57cec5SDimitry Andric   }
55170b57cec5SDimitry Andric 
55180b57cec5SDimitry Andric   // Remove dead nodes now.
55190b57cec5SDimitry Andric   RemoveDeadNodes();
55200b57cec5SDimitry Andric }
55210b57cec5SDimitry Andric 
55220b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N,
55230b57cec5SDimitry Andric                               SmallSetVector<SDNode *, 16> &UpdatedNodes) {
55240b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
55250b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
55260b57cec5SDimitry Andric 
55270b57cec5SDimitry Andric   // Directly insert the node in question, and legalize it. This will recurse
55280b57cec5SDimitry Andric   // as needed through operands.
55290b57cec5SDimitry Andric   LegalizedNodes.insert(N);
55300b57cec5SDimitry Andric   Legalizer.LegalizeOp(N);
55310b57cec5SDimitry Andric 
55320b57cec5SDimitry Andric   return LegalizedNodes.count(N);
55330b57cec5SDimitry Andric }
5534