xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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"
21*5f757f3fSDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
228bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
2706c3fb27SDimitry Andric #include "llvm/CodeGen/MachineValueType.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h"
320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h"
330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h"
340b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h"
350b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
360b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
370b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
380b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
390b57cec5SDimitry Andric #include "llvm/IR/Function.h"
400b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
410b57cec5SDimitry Andric #include "llvm/IR/Type.h"
420b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
430b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
440b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
450b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
460b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
470b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
480b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
490b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
500b57cec5SDimitry Andric #include <cassert>
510b57cec5SDimitry Andric #include <cstdint>
520b57cec5SDimitry Andric #include <tuple>
530b57cec5SDimitry Andric #include <utility>
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric using namespace llvm;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric #define DEBUG_TYPE "legalizedag"
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric namespace {
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric /// Keeps track of state when getting the sign of a floating-point value as an
620b57cec5SDimitry Andric /// integer.
630b57cec5SDimitry Andric struct FloatSignAsInt {
640b57cec5SDimitry Andric   EVT FloatVT;
650b57cec5SDimitry Andric   SDValue Chain;
660b57cec5SDimitry Andric   SDValue FloatPtr;
670b57cec5SDimitry Andric   SDValue IntPtr;
680b57cec5SDimitry Andric   MachinePointerInfo IntPointerInfo;
690b57cec5SDimitry Andric   MachinePointerInfo FloatPointerInfo;
700b57cec5SDimitry Andric   SDValue IntValue;
710b57cec5SDimitry Andric   APInt SignMask;
720b57cec5SDimitry Andric   uint8_t SignBit;
730b57cec5SDimitry Andric };
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
760b57cec5SDimitry Andric /// This takes an arbitrary SelectionDAG as input and
770b57cec5SDimitry Andric /// hacks on it until the target machine can handle it.  This involves
780b57cec5SDimitry Andric /// eliminating value sizes the machine cannot handle (promoting small sizes to
790b57cec5SDimitry Andric /// large sizes or splitting up large values into small values) as well as
800b57cec5SDimitry Andric /// eliminating operations the machine cannot handle.
810b57cec5SDimitry Andric ///
820b57cec5SDimitry Andric /// This code also does a small amount of optimization and recognition of idioms
830b57cec5SDimitry Andric /// as part of its processing.  For example, if a target does not support a
840b57cec5SDimitry Andric /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
850b57cec5SDimitry Andric /// will attempt merge setcc and brc instructions into brcc's.
860b57cec5SDimitry Andric class SelectionDAGLegalize {
870b57cec5SDimitry Andric   const TargetMachine &TM;
880b57cec5SDimitry Andric   const TargetLowering &TLI;
890b57cec5SDimitry Andric   SelectionDAG &DAG;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   /// The set of nodes which have already been legalized. We hold a
920b57cec5SDimitry Andric   /// reference to it in order to update as necessary on node deletion.
930b57cec5SDimitry Andric   SmallPtrSetImpl<SDNode *> &LegalizedNodes;
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric   /// A set of all the nodes updated during legalization.
960b57cec5SDimitry Andric   SmallSetVector<SDNode *, 16> *UpdatedNodes;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   EVT getSetCCResultType(EVT VT) const {
990b57cec5SDimitry Andric     return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1000b57cec5SDimitry Andric   }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   // Libcall insertion helpers.
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric public:
1050b57cec5SDimitry Andric   SelectionDAGLegalize(SelectionDAG &DAG,
1060b57cec5SDimitry Andric                        SmallPtrSetImpl<SDNode *> &LegalizedNodes,
1070b57cec5SDimitry Andric                        SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
1080b57cec5SDimitry Andric       : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
1090b57cec5SDimitry Andric         LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   /// Legalizes the given operation.
1120b57cec5SDimitry Andric   void LegalizeOp(SDNode *Node);
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric private:
1150b57cec5SDimitry Andric   SDValue OptimizeFloatStore(StoreSDNode *ST);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   void LegalizeLoadOps(SDNode *Node);
1180b57cec5SDimitry Andric   void LegalizeStoreOps(SDNode *Node);
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   /// Some targets cannot handle a variable
1210b57cec5SDimitry Andric   /// insertion index for the INSERT_VECTOR_ELT instruction.  In this case, it
1220b57cec5SDimitry Andric   /// is necessary to spill the vector being inserted into to memory, perform
1230b57cec5SDimitry Andric   /// the insert there, and then read the result back.
1240b57cec5SDimitry Andric   SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx,
1250b57cec5SDimitry Andric                                          const SDLoc &dl);
1260b57cec5SDimitry Andric   SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx,
1270b57cec5SDimitry Andric                                   const SDLoc &dl);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   /// Return a vector shuffle operation which
1300b57cec5SDimitry Andric   /// performs the same shuffe in terms of order or result bytes, but on a type
1310b57cec5SDimitry Andric   /// whose vector element type is narrower than the original shuffle type.
1320b57cec5SDimitry Andric   /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
1330b57cec5SDimitry Andric   SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
1340b57cec5SDimitry Andric                                      SDValue N1, SDValue N2,
1350b57cec5SDimitry Andric                                      ArrayRef<int> Mask) const;
1360b57cec5SDimitry Andric 
13706c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
13806c3fb27SDimitry Andric                         TargetLowering::ArgListTy &&Args, bool isSigned);
13906c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
1400b57cec5SDimitry Andric 
14106c3fb27SDimitry Andric   void ExpandFrexpLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
142fe6060f1SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC,
143fe6060f1SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
144480093f4SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
1450b57cec5SDimitry Andric                        RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
1460b57cec5SDimitry Andric                        RTLIB::Libcall Call_F128,
147480093f4SDimitry Andric                        RTLIB::Libcall Call_PPCF128,
148480093f4SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
149bdd1243dSDimitry Andric   SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
150bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I8,
151bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I16,
152bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I32,
153bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I64,
154bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I128);
155480093f4SDimitry Andric   void ExpandArgFPLibCall(SDNode *Node,
1560b57cec5SDimitry Andric                           RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
1570b57cec5SDimitry Andric                           RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
158480093f4SDimitry Andric                           RTLIB::Libcall Call_PPCF128,
159480093f4SDimitry Andric                           SmallVectorImpl<SDValue> &Results);
1600b57cec5SDimitry Andric   void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1610b57cec5SDimitry Andric   void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1640b57cec5SDimitry Andric                            const SDLoc &dl);
1650b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1660b57cec5SDimitry Andric                            const SDLoc &dl, SDValue ChainIn);
1670b57cec5SDimitry Andric   SDValue ExpandBUILD_VECTOR(SDNode *Node);
1688bcb0991SDimitry Andric   SDValue ExpandSPLAT_VECTOR(SDNode *Node);
1690b57cec5SDimitry Andric   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
1700b57cec5SDimitry Andric   void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
1710b57cec5SDimitry Andric                                 SmallVectorImpl<SDValue> &Results);
1720b57cec5SDimitry Andric   void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
1730b57cec5SDimitry Andric                          SDValue Value) const;
1740b57cec5SDimitry Andric   SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
1750b57cec5SDimitry Andric                           SDValue NewIntValue) const;
1760b57cec5SDimitry Andric   SDValue ExpandFCOPYSIGN(SDNode *Node) const;
1770b57cec5SDimitry Andric   SDValue ExpandFABS(SDNode *Node) const;
178e8d8bef9SDimitry Andric   SDValue ExpandFNEG(SDNode *Node) const;
17906c3fb27SDimitry Andric   SDValue expandLdexp(SDNode *Node) const;
18006c3fb27SDimitry Andric   SDValue expandFrexp(SDNode *Node) const;
18106c3fb27SDimitry Andric 
182480093f4SDimitry Andric   SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain);
183480093f4SDimitry Andric   void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl,
184480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
185480093f4SDimitry Andric   void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
186480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
187e8d8bef9SDimitry Andric   SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl);
1880b57cec5SDimitry Andric 
189e8d8bef9SDimitry Andric   SDValue ExpandPARITY(SDValue Op, const SDLoc &dl);
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
1920b57cec5SDimitry Andric   SDValue ExpandInsertToVectorThroughStack(SDValue Op);
1930b57cec5SDimitry Andric   SDValue ExpandVectorBuildThroughStack(SDNode* Node);
1940b57cec5SDimitry Andric 
1950b57cec5SDimitry Andric   SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
1960b57cec5SDimitry Andric   SDValue ExpandConstant(ConstantSDNode *CP);
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
1990b57cec5SDimitry Andric   bool ExpandNode(SDNode *Node);
2000b57cec5SDimitry Andric   void ConvertNodeToLibcall(SDNode *Node);
2010b57cec5SDimitry Andric   void PromoteNode(SDNode *Node);
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric public:
2040b57cec5SDimitry Andric   // Node replacement helpers
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   void ReplacedNode(SDNode *N) {
2070b57cec5SDimitry Andric     LegalizedNodes.erase(N);
2080b57cec5SDimitry Andric     if (UpdatedNodes)
2090b57cec5SDimitry Andric       UpdatedNodes->insert(N);
2100b57cec5SDimitry Andric   }
2110b57cec5SDimitry Andric 
2120b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, SDNode *New) {
2130b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2140b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric     assert(Old->getNumValues() == New->getNumValues() &&
2170b57cec5SDimitry Andric            "Replacing one node with another that produces a different number "
2180b57cec5SDimitry Andric            "of values!");
2190b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2200b57cec5SDimitry Andric     if (UpdatedNodes)
2210b57cec5SDimitry Andric       UpdatedNodes->insert(New);
2220b57cec5SDimitry Andric     ReplacedNode(Old);
2230b57cec5SDimitry Andric   }
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric   void ReplaceNode(SDValue Old, SDValue New) {
2260b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2270b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2300b57cec5SDimitry Andric     if (UpdatedNodes)
2310b57cec5SDimitry Andric       UpdatedNodes->insert(New.getNode());
2320b57cec5SDimitry Andric     ReplacedNode(Old.getNode());
2330b57cec5SDimitry Andric   }
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, const SDValue *New) {
2360b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2390b57cec5SDimitry Andric     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
2400b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << (i == 0 ? "     with:      " : "      and:      ");
2410b57cec5SDimitry Andric                  New[i]->dump(&DAG));
2420b57cec5SDimitry Andric       if (UpdatedNodes)
2430b57cec5SDimitry Andric         UpdatedNodes->insert(New[i].getNode());
2440b57cec5SDimitry Andric     }
2450b57cec5SDimitry Andric     ReplacedNode(Old);
2460b57cec5SDimitry Andric   }
2478bcb0991SDimitry Andric 
2488bcb0991SDimitry Andric   void ReplaceNodeWithValue(SDValue Old, SDValue New) {
2498bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2508bcb0991SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2518bcb0991SDimitry Andric 
2528bcb0991SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Old, New);
2538bcb0991SDimitry Andric     if (UpdatedNodes)
2548bcb0991SDimitry Andric       UpdatedNodes->insert(New.getNode());
2558bcb0991SDimitry Andric     ReplacedNode(Old.getNode());
2568bcb0991SDimitry Andric   }
2570b57cec5SDimitry Andric };
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric } // end anonymous namespace
2600b57cec5SDimitry Andric 
2610b57cec5SDimitry Andric /// Return a vector shuffle operation which
2620b57cec5SDimitry Andric /// performs the same shuffle in terms of order or result bytes, but on a type
2630b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type.
2640b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
2650b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
2660b57cec5SDimitry Andric     EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
2670b57cec5SDimitry Andric     ArrayRef<int> Mask) const {
2680b57cec5SDimitry Andric   unsigned NumMaskElts = VT.getVectorNumElements();
2690b57cec5SDimitry Andric   unsigned NumDestElts = NVT.getVectorNumElements();
2700b57cec5SDimitry Andric   unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric   assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
2730b57cec5SDimitry Andric 
2740b57cec5SDimitry Andric   if (NumEltsGrowth == 1)
2750b57cec5SDimitry Andric     return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   SmallVector<int, 8> NewMask;
2780b57cec5SDimitry Andric   for (unsigned i = 0; i != NumMaskElts; ++i) {
2790b57cec5SDimitry Andric     int Idx = Mask[i];
2800b57cec5SDimitry Andric     for (unsigned j = 0; j != NumEltsGrowth; ++j) {
2810b57cec5SDimitry Andric       if (Idx < 0)
2820b57cec5SDimitry Andric         NewMask.push_back(-1);
2830b57cec5SDimitry Andric       else
2840b57cec5SDimitry Andric         NewMask.push_back(Idx * NumEltsGrowth + j);
2850b57cec5SDimitry Andric     }
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric   assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
2880b57cec5SDimitry Andric   assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
2890b57cec5SDimitry Andric   return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric /// Expands the ConstantFP node to an integer constant or
2930b57cec5SDimitry Andric /// a load from the constant pool.
2940b57cec5SDimitry Andric SDValue
2950b57cec5SDimitry Andric SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
2960b57cec5SDimitry Andric   bool Extend = false;
2970b57cec5SDimitry Andric   SDLoc dl(CFP);
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric   // If a FP immediate is precise when represented as a float and if the
3000b57cec5SDimitry Andric   // target can do an extending load from float to double, we put it into
3010b57cec5SDimitry Andric   // the constant pool as a float, even if it's is statically typed as a
3020b57cec5SDimitry Andric   // double.  This shrinks FP constants and canonicalizes them for targets where
3030b57cec5SDimitry Andric   // an FP extending load is the same cost as a normal load (such as on the x87
3040b57cec5SDimitry Andric   // fp stack or PPC FP unit).
3050b57cec5SDimitry Andric   EVT VT = CFP->getValueType(0);
3060b57cec5SDimitry Andric   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
3070b57cec5SDimitry Andric   if (!UseCP) {
3080b57cec5SDimitry Andric     assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
3090b57cec5SDimitry Andric     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
3100b57cec5SDimitry Andric                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
3110b57cec5SDimitry Andric   }
3120b57cec5SDimitry Andric 
3130b57cec5SDimitry Andric   APFloat APF = CFP->getValueAPF();
3140b57cec5SDimitry Andric   EVT OrigVT = VT;
3150b57cec5SDimitry Andric   EVT SVT = VT;
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // We don't want to shrink SNaNs. Converting the SNaN back to its real type
3180b57cec5SDimitry Andric   // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
3190b57cec5SDimitry Andric   if (!APF.isSignaling()) {
320bdd1243dSDimitry Andric     while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) {
3210b57cec5SDimitry Andric       SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
3220b57cec5SDimitry Andric       if (ConstantFPSDNode::isValueValidForType(SVT, APF) &&
3230b57cec5SDimitry Andric           // Only do this if the target has a native EXTLOAD instruction from
3240b57cec5SDimitry Andric           // smaller type.
3250b57cec5SDimitry Andric           TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
3260b57cec5SDimitry Andric           TLI.ShouldShrinkFPConstant(OrigVT)) {
3270b57cec5SDimitry Andric         Type *SType = SVT.getTypeForEVT(*DAG.getContext());
328*5f757f3fSDimitry Andric         LLVMC = cast<ConstantFP>(ConstantFoldCastOperand(
329*5f757f3fSDimitry Andric             Instruction::FPTrunc, LLVMC, SType, DAG.getDataLayout()));
3300b57cec5SDimitry Andric         VT = SVT;
3310b57cec5SDimitry Andric         Extend = true;
3320b57cec5SDimitry Andric       }
3330b57cec5SDimitry Andric     }
3340b57cec5SDimitry Andric   }
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   SDValue CPIdx =
3370b57cec5SDimitry Andric       DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
3385ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3390b57cec5SDimitry Andric   if (Extend) {
3400b57cec5SDimitry Andric     SDValue Result = DAG.getExtLoad(
3410b57cec5SDimitry Andric         ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
3420b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
3430b57cec5SDimitry Andric         Alignment);
3440b57cec5SDimitry Andric     return Result;
3450b57cec5SDimitry Andric   }
3460b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3470b57cec5SDimitry Andric       OrigVT, dl, DAG.getEntryNode(), CPIdx,
3480b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3490b57cec5SDimitry Andric   return Result;
3500b57cec5SDimitry Andric }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric /// Expands the Constant node to a load from the constant pool.
3530b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
3540b57cec5SDimitry Andric   SDLoc dl(CP);
3550b57cec5SDimitry Andric   EVT VT = CP->getValueType(0);
3560b57cec5SDimitry Andric   SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
3570b57cec5SDimitry Andric                                       TLI.getPointerTy(DAG.getDataLayout()));
3585ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3590b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3600b57cec5SDimitry Andric       VT, dl, DAG.getEntryNode(), CPIdx,
3610b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3620b57cec5SDimitry Andric   return Result;
3630b57cec5SDimitry Andric }
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric /// Some target cannot handle a variable insertion index for the
3660b57cec5SDimitry Andric /// INSERT_VECTOR_ELT instruction.  In this case, it
3670b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform
3680b57cec5SDimitry Andric /// the insert there, and then read the result back.
3690b57cec5SDimitry Andric SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec,
3700b57cec5SDimitry Andric                                                              SDValue Val,
3710b57cec5SDimitry Andric                                                              SDValue Idx,
3720b57cec5SDimitry Andric                                                              const SDLoc &dl) {
3730b57cec5SDimitry Andric   SDValue Tmp1 = Vec;
3740b57cec5SDimitry Andric   SDValue Tmp2 = Val;
3750b57cec5SDimitry Andric   SDValue Tmp3 = Idx;
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   // If the target doesn't support this, we have to spill the input vector
3780b57cec5SDimitry Andric   // to a temporary stack slot, update the element, then reload it.  This is
3790b57cec5SDimitry Andric   // badness.  We could also load the value into a vector register (either
3800b57cec5SDimitry Andric   // with a "move to register" or "extload into register" instruction, then
3810b57cec5SDimitry Andric   // permute it into place, if the idx is a constant and if the idx is
3820b57cec5SDimitry Andric   // supported by the target.
3830b57cec5SDimitry Andric   EVT VT    = Tmp1.getValueType();
3840b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
3850b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(VT);
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
3880b57cec5SDimitry Andric 
3890b57cec5SDimitry Andric   // Store the vector.
3900b57cec5SDimitry Andric   SDValue Ch = DAG.getStore(
3910b57cec5SDimitry Andric       DAG.getEntryNode(), dl, Tmp1, StackPtr,
3920b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric   SDValue StackPtr2 = TLI.getVectorElementPointer(DAG, StackPtr, VT, Tmp3);
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric   // Store the scalar value.
3975ffd83dbSDimitry Andric   Ch = DAG.getTruncStore(
3985ffd83dbSDimitry Andric       Ch, dl, Tmp2, StackPtr2,
3995ffd83dbSDimitry Andric       MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltVT);
4000b57cec5SDimitry Andric   // Load the updated vector.
4010b57cec5SDimitry Andric   return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack(
4020b57cec5SDimitry Andric                                                DAG.getMachineFunction(), SPFI));
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val,
4060b57cec5SDimitry Andric                                                       SDValue Idx,
4070b57cec5SDimitry Andric                                                       const SDLoc &dl) {
4080b57cec5SDimitry Andric   if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
4090b57cec5SDimitry Andric     // SCALAR_TO_VECTOR requires that the type of the value being inserted
4100b57cec5SDimitry Andric     // match the element type of the vector being created, except for
4110b57cec5SDimitry Andric     // integers in which case the inserted value can be over width.
4120b57cec5SDimitry Andric     EVT EltVT = Vec.getValueType().getVectorElementType();
4130b57cec5SDimitry Andric     if (Val.getValueType() == EltVT ||
4140b57cec5SDimitry Andric         (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
4150b57cec5SDimitry Andric       SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
4160b57cec5SDimitry Andric                                   Vec.getValueType(), Val);
4170b57cec5SDimitry Andric 
4180b57cec5SDimitry Andric       unsigned NumElts = Vec.getValueType().getVectorNumElements();
4190b57cec5SDimitry Andric       // We generate a shuffle of InVec and ScVec, so the shuffle mask
4200b57cec5SDimitry Andric       // should be 0,1,2,3,4,5... with the appropriate element replaced with
4210b57cec5SDimitry Andric       // elt 0 of the RHS.
4220b57cec5SDimitry Andric       SmallVector<int, 8> ShufOps;
4230b57cec5SDimitry Andric       for (unsigned i = 0; i != NumElts; ++i)
4240b57cec5SDimitry Andric         ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric       return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
4270b57cec5SDimitry Andric     }
4280b57cec5SDimitry Andric   }
4290b57cec5SDimitry Andric   return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl);
4300b57cec5SDimitry Andric }
4310b57cec5SDimitry Andric 
4320b57cec5SDimitry Andric SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
433480093f4SDimitry Andric   if (!ISD::isNormalStore(ST))
434480093f4SDimitry Andric     return SDValue();
435480093f4SDimitry Andric 
4360b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Optimizing float store operations\n");
4370b57cec5SDimitry Andric   // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4380b57cec5SDimitry Andric   // FIXME: move this to the DAG Combiner!  Note that we can't regress due
4390b57cec5SDimitry Andric   // to phase ordering between legalized code and the dag combiner.  This
4400b57cec5SDimitry Andric   // probably means that we need to integrate dag combiner and legalizer
4410b57cec5SDimitry Andric   // together.
4420b57cec5SDimitry Andric   // We generally can't do this one for long doubles.
4430b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
4440b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
445e8d8bef9SDimitry Andric   SDValue Value = ST->getValue();
4460b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
4470b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
4480b57cec5SDimitry Andric   SDLoc dl(ST);
449e8d8bef9SDimitry Andric 
450e8d8bef9SDimitry Andric   // Don't optimise TargetConstantFP
451e8d8bef9SDimitry Andric   if (Value.getOpcode() == ISD::TargetConstantFP)
452e8d8bef9SDimitry Andric     return SDValue();
453e8d8bef9SDimitry Andric 
454e8d8bef9SDimitry Andric   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
4550b57cec5SDimitry Andric     if (CFP->getValueType(0) == MVT::f32 &&
4560b57cec5SDimitry Andric         TLI.isTypeLegal(MVT::i32)) {
4570b57cec5SDimitry Andric       SDValue Con = DAG.getConstant(CFP->getValueAPF().
4580b57cec5SDimitry Andric                                       bitcastToAPInt().zextOrTrunc(32),
4590b57cec5SDimitry Andric                                     SDLoc(CFP), MVT::i32);
4605ffd83dbSDimitry Andric       return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4615ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4620b57cec5SDimitry Andric     }
4630b57cec5SDimitry Andric 
464*5f757f3fSDimitry Andric     if (CFP->getValueType(0) == MVT::f64 &&
465*5f757f3fSDimitry Andric         !TLI.isFPImmLegal(CFP->getValueAPF(), MVT::f64)) {
4660b57cec5SDimitry Andric       // If this target supports 64-bit registers, do a single 64-bit store.
4670b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i64)) {
4680b57cec5SDimitry Andric         SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
4690b57cec5SDimitry Andric                                       zextOrTrunc(64), SDLoc(CFP), MVT::i64);
4700b57cec5SDimitry Andric         return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4715ffd83dbSDimitry Andric                             ST->getOriginalAlign(), MMOFlags, AAInfo);
4720b57cec5SDimitry Andric       }
4730b57cec5SDimitry Andric 
4740b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
4750b57cec5SDimitry Andric         // Otherwise, if the target supports 32-bit registers, use 2 32-bit
4760b57cec5SDimitry Andric         // stores.  If the target supports neither 32- nor 64-bits, this
4770b57cec5SDimitry Andric         // xform is certainly not worth it.
4780b57cec5SDimitry Andric         const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
4790b57cec5SDimitry Andric         SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
4800b57cec5SDimitry Andric         SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
4810b57cec5SDimitry Andric         if (DAG.getDataLayout().isBigEndian())
4820b57cec5SDimitry Andric           std::swap(Lo, Hi);
4830b57cec5SDimitry Andric 
4845ffd83dbSDimitry Andric         Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(),
4855ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
486*5f757f3fSDimitry Andric         Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(4), dl);
4870b57cec5SDimitry Andric         Hi = DAG.getStore(Chain, dl, Hi, Ptr,
4880b57cec5SDimitry Andric                           ST->getPointerInfo().getWithOffset(4),
4895ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric         return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4920b57cec5SDimitry Andric       }
4930b57cec5SDimitry Andric     }
4940b57cec5SDimitry Andric   }
495e8d8bef9SDimitry Andric   return SDValue();
4960b57cec5SDimitry Andric }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
4990b57cec5SDimitry Andric   StoreSDNode *ST = cast<StoreSDNode>(Node);
5000b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
5010b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
5020b57cec5SDimitry Andric   SDLoc dl(Node);
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
5050b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
5060b57cec5SDimitry Andric 
5070b57cec5SDimitry Andric   if (!ST->isTruncatingStore()) {
5080b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing store operation\n");
5090b57cec5SDimitry Andric     if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
5100b57cec5SDimitry Andric       ReplaceNode(ST, OptStore);
5110b57cec5SDimitry Andric       return;
5120b57cec5SDimitry Andric     }
5130b57cec5SDimitry Andric 
5140b57cec5SDimitry Andric     SDValue Value = ST->getValue();
5150b57cec5SDimitry Andric     MVT VT = Value.getSimpleValueType();
5160b57cec5SDimitry Andric     switch (TLI.getOperationAction(ISD::STORE, VT)) {
5170b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
5180b57cec5SDimitry Andric     case TargetLowering::Legal: {
5190b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
5200b57cec5SDimitry Andric       // expand it.
5210b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
5220b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
5238bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
5240b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
5250b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n");
5260b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
5270b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
5280b57cec5SDimitry Andric       } else
5290b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Legal store\n");
5300b57cec5SDimitry Andric       break;
5310b57cec5SDimitry Andric     }
5320b57cec5SDimitry Andric     case TargetLowering::Custom: {
5330b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom lowering\n");
5340b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
5350b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
5360b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
5370b57cec5SDimitry Andric       return;
5380b57cec5SDimitry Andric     }
5390b57cec5SDimitry Andric     case TargetLowering::Promote: {
5400b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
5410b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
5420b57cec5SDimitry Andric              "Can only promote stores to same size type");
5430b57cec5SDimitry Andric       Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
5445ffd83dbSDimitry Andric       SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5455ffd83dbSDimitry Andric                                     ST->getOriginalAlign(), MMOFlags, AAInfo);
5460b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
5470b57cec5SDimitry Andric       break;
5480b57cec5SDimitry Andric     }
5490b57cec5SDimitry Andric     }
5500b57cec5SDimitry Andric     return;
5510b57cec5SDimitry Andric   }
5520b57cec5SDimitry Andric 
5530b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n");
5540b57cec5SDimitry Andric   SDValue Value = ST->getValue();
5550b57cec5SDimitry Andric   EVT StVT = ST->getMemoryVT();
556e8d8bef9SDimitry Andric   TypeSize StWidth = StVT.getSizeInBits();
557e8d8bef9SDimitry Andric   TypeSize StSize = StVT.getStoreSizeInBits();
5580b57cec5SDimitry Andric   auto &DL = DAG.getDataLayout();
5590b57cec5SDimitry Andric 
560e8d8bef9SDimitry Andric   if (StWidth != StSize) {
5610b57cec5SDimitry Andric     // Promote to a byte-sized store with upper bits zero if not
5620b57cec5SDimitry Andric     // storing an integral number of bytes.  For example, promote
5630b57cec5SDimitry Andric     // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
564bdd1243dSDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue());
5650b57cec5SDimitry Andric     Value = DAG.getZeroExtendInReg(Value, dl, StVT);
5660b57cec5SDimitry Andric     SDValue Result =
5670b57cec5SDimitry Andric         DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
5685ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
5690b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
570bdd1243dSDimitry Andric   } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) {
5710b57cec5SDimitry Andric     // If not storing a power-of-2 number of bits, expand as two stores.
5720b57cec5SDimitry Andric     assert(!StVT.isVector() && "Unsupported truncstore!");
573bdd1243dSDimitry Andric     unsigned StWidthBits = StWidth.getFixedValue();
574e8d8bef9SDimitry Andric     unsigned LogStWidth = Log2_32(StWidthBits);
5750b57cec5SDimitry Andric     assert(LogStWidth < 32);
5760b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogStWidth;
577e8d8bef9SDimitry Andric     assert(RoundWidth < StWidthBits);
578e8d8bef9SDimitry Andric     unsigned ExtraWidth = StWidthBits - RoundWidth;
5790b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
5800b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
5810b57cec5SDimitry Andric            "Store size not an integral number of bytes!");
5820b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
5830b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
5840b57cec5SDimitry Andric     SDValue Lo, Hi;
5850b57cec5SDimitry Andric     unsigned IncrementSize;
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
5880b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
5890b57cec5SDimitry Andric       // Store the bottom RoundWidth bits.
5900b57cec5SDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5915ffd83dbSDimitry Andric                              RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
5940b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
595*5f757f3fSDimitry Andric       Ptr =
596*5f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
5970b57cec5SDimitry Andric       Hi = DAG.getNode(
5980b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
5990b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
6000b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
6015ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
6025ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
6035ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
6040b57cec5SDimitry Andric     } else {
6050b57cec5SDimitry Andric       // Big endian - avoid unaligned stores.
6060b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
6070b57cec5SDimitry Andric       // Store the top RoundWidth bits.
6080b57cec5SDimitry Andric       Hi = DAG.getNode(
6090b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
6100b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
6110b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
6125ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT,
6135ffd83dbSDimitry Andric                              ST->getOriginalAlign(), MMOFlags, AAInfo);
6140b57cec5SDimitry Andric 
6150b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
6160b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
6170b57cec5SDimitry Andric       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
6180b57cec5SDimitry Andric                         DAG.getConstant(IncrementSize, dl,
6190b57cec5SDimitry Andric                                         Ptr.getValueType()));
6205ffd83dbSDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
6215ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
6225ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
6230b57cec5SDimitry Andric     }
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric     // The order of the stores doesn't matter.
6260b57cec5SDimitry Andric     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6270b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
6280b57cec5SDimitry Andric   } else {
6290b57cec5SDimitry Andric     switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
6300b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6310b57cec5SDimitry Andric     case TargetLowering::Legal: {
6320b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
6330b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
6340b57cec5SDimitry Andric       // expand it.
6358bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6360b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
6370b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
6380b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
6390b57cec5SDimitry Andric       }
6400b57cec5SDimitry Andric       break;
6410b57cec5SDimitry Andric     }
6420b57cec5SDimitry Andric     case TargetLowering::Custom: {
6430b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
6440b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
6450b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
6460b57cec5SDimitry Andric       return;
6470b57cec5SDimitry Andric     }
6480b57cec5SDimitry Andric     case TargetLowering::Expand:
6490b57cec5SDimitry Andric       assert(!StVT.isVector() &&
6500b57cec5SDimitry Andric              "Vector Stores are handled in LegalizeVectorOps");
6510b57cec5SDimitry Andric 
6520b57cec5SDimitry Andric       SDValue Result;
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric       // TRUNCSTORE:i16 i32 -> STORE i16
6550b57cec5SDimitry Andric       if (TLI.isTypeLegal(StVT)) {
6560b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
6570b57cec5SDimitry Andric         Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
6585ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6590b57cec5SDimitry Andric       } else {
6600b57cec5SDimitry Andric         // The in-memory type isn't legal. Truncate to the type it would promote
6610b57cec5SDimitry Andric         // to, and then do a truncstore.
6620b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl,
6630b57cec5SDimitry Andric                             TLI.getTypeToTransformTo(*DAG.getContext(), StVT),
6640b57cec5SDimitry Andric                             Value);
6655ffd83dbSDimitry Andric         Result =
6665ffd83dbSDimitry Andric             DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT,
6675ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6680b57cec5SDimitry Andric       }
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
6710b57cec5SDimitry Andric       break;
6720b57cec5SDimitry Andric     }
6730b57cec5SDimitry Andric   }
6740b57cec5SDimitry Andric }
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
6770b57cec5SDimitry Andric   LoadSDNode *LD = cast<LoadSDNode>(Node);
6780b57cec5SDimitry Andric   SDValue Chain = LD->getChain();  // The chain.
6790b57cec5SDimitry Andric   SDValue Ptr = LD->getBasePtr();  // The base pointer.
6800b57cec5SDimitry Andric   SDValue Value;                   // The value returned by the load op.
6810b57cec5SDimitry Andric   SDLoc dl(Node);
6820b57cec5SDimitry Andric 
6830b57cec5SDimitry Andric   ISD::LoadExtType ExtType = LD->getExtensionType();
6840b57cec5SDimitry Andric   if (ExtType == ISD::NON_EXTLOAD) {
6850b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n");
6860b57cec5SDimitry Andric     MVT VT = Node->getSimpleValueType(0);
6870b57cec5SDimitry Andric     SDValue RVal = SDValue(Node, 0);
6880b57cec5SDimitry Andric     SDValue RChain = SDValue(Node, 1);
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
6910b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6920b57cec5SDimitry Andric     case TargetLowering::Legal: {
6930b57cec5SDimitry Andric       EVT MemVT = LD->getMemoryVT();
6940b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
6950b57cec5SDimitry Andric       // If this is an unaligned load and the target doesn't support it,
6960b57cec5SDimitry Andric       // expand it.
6978bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6980b57cec5SDimitry Andric                                               *LD->getMemOperand())) {
6990b57cec5SDimitry Andric         std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
7000b57cec5SDimitry Andric       }
7010b57cec5SDimitry Andric       break;
7020b57cec5SDimitry Andric     }
7030b57cec5SDimitry Andric     case TargetLowering::Custom:
7040b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
7050b57cec5SDimitry Andric         RVal = Res;
7060b57cec5SDimitry Andric         RChain = Res.getValue(1);
7070b57cec5SDimitry Andric       }
7080b57cec5SDimitry Andric       break;
7090b57cec5SDimitry Andric 
7100b57cec5SDimitry Andric     case TargetLowering::Promote: {
7110b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
7120b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
7130b57cec5SDimitry Andric              "Can only promote loads to same size type");
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric       SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
7160b57cec5SDimitry Andric       RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
7170b57cec5SDimitry Andric       RChain = Res.getValue(1);
7180b57cec5SDimitry Andric       break;
7190b57cec5SDimitry Andric     }
7200b57cec5SDimitry Andric     }
7210b57cec5SDimitry Andric     if (RChain.getNode() != Node) {
7220b57cec5SDimitry Andric       assert(RVal.getNode() != Node && "Load must be completely replaced");
7230b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
7240b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
7250b57cec5SDimitry Andric       if (UpdatedNodes) {
7260b57cec5SDimitry Andric         UpdatedNodes->insert(RVal.getNode());
7270b57cec5SDimitry Andric         UpdatedNodes->insert(RChain.getNode());
7280b57cec5SDimitry Andric       }
7290b57cec5SDimitry Andric       ReplacedNode(Node);
7300b57cec5SDimitry Andric     }
7310b57cec5SDimitry Andric     return;
7320b57cec5SDimitry Andric   }
7330b57cec5SDimitry Andric 
7340b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n");
7350b57cec5SDimitry Andric   EVT SrcVT = LD->getMemoryVT();
736e8d8bef9SDimitry Andric   TypeSize SrcWidth = SrcVT.getSizeInBits();
7370b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
7380b57cec5SDimitry Andric   AAMDNodes AAInfo = LD->getAAInfo();
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric   if (SrcWidth != SrcVT.getStoreSizeInBits() &&
7410b57cec5SDimitry Andric       // Some targets pretend to have an i1 loading operation, and actually
7420b57cec5SDimitry Andric       // load an i8.  This trick is correct for ZEXTLOAD because the top 7
7430b57cec5SDimitry Andric       // bits are guaranteed to be zero; it helps the optimizers understand
7440b57cec5SDimitry Andric       // that these bits are zero.  It is also useful for EXTLOAD, since it
7450b57cec5SDimitry Andric       // tells the optimizers that those bits are undefined.  It would be
7460b57cec5SDimitry Andric       // nice to have an effective generic way of getting these benefits...
7470b57cec5SDimitry Andric       // Until such a way is found, don't insist on promoting i1 here.
7480b57cec5SDimitry Andric       (SrcVT != MVT::i1 ||
7490b57cec5SDimitry Andric        TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
7500b57cec5SDimitry Andric          TargetLowering::Promote)) {
7510b57cec5SDimitry Andric     // Promote to a byte-sized load if not loading an integral number of
7520b57cec5SDimitry Andric     // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
7530b57cec5SDimitry Andric     unsigned NewWidth = SrcVT.getStoreSizeInBits();
7540b57cec5SDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
7550b57cec5SDimitry Andric     SDValue Ch;
7560b57cec5SDimitry Andric 
7570b57cec5SDimitry Andric     // The extra bits are guaranteed to be zero, since we stored them that
7580b57cec5SDimitry Andric     // way.  A zext load from NVT thus automatically gives zext from SrcVT.
7590b57cec5SDimitry Andric 
7600b57cec5SDimitry Andric     ISD::LoadExtType NewExtType =
7610b57cec5SDimitry Andric       ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7620b57cec5SDimitry Andric 
7635ffd83dbSDimitry Andric     SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
7645ffd83dbSDimitry Andric                                     Chain, Ptr, LD->getPointerInfo(), NVT,
7655ffd83dbSDimitry Andric                                     LD->getOriginalAlign(), MMOFlags, AAInfo);
7660b57cec5SDimitry Andric 
7670b57cec5SDimitry Andric     Ch = Result.getValue(1); // The chain.
7680b57cec5SDimitry Andric 
7690b57cec5SDimitry Andric     if (ExtType == ISD::SEXTLOAD)
7700b57cec5SDimitry Andric       // Having the top bits zero doesn't help when sign extending.
7710b57cec5SDimitry Andric       Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
7720b57cec5SDimitry Andric                            Result.getValueType(),
7730b57cec5SDimitry Andric                            Result, DAG.getValueType(SrcVT));
7740b57cec5SDimitry Andric     else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
7750b57cec5SDimitry Andric       // All the top bits are guaranteed to be zero - inform the optimizers.
7760b57cec5SDimitry Andric       Result = DAG.getNode(ISD::AssertZext, dl,
7770b57cec5SDimitry Andric                            Result.getValueType(), Result,
7780b57cec5SDimitry Andric                            DAG.getValueType(SrcVT));
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric     Value = Result;
7810b57cec5SDimitry Andric     Chain = Ch;
782bdd1243dSDimitry Andric   } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) {
7830b57cec5SDimitry Andric     // If not loading a power-of-2 number of bits, expand as two loads.
7840b57cec5SDimitry Andric     assert(!SrcVT.isVector() && "Unsupported extload!");
785bdd1243dSDimitry Andric     unsigned SrcWidthBits = SrcWidth.getFixedValue();
786e8d8bef9SDimitry Andric     unsigned LogSrcWidth = Log2_32(SrcWidthBits);
7870b57cec5SDimitry Andric     assert(LogSrcWidth < 32);
7880b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogSrcWidth;
789e8d8bef9SDimitry Andric     assert(RoundWidth < SrcWidthBits);
790e8d8bef9SDimitry Andric     unsigned ExtraWidth = SrcWidthBits - RoundWidth;
7910b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
7920b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
7930b57cec5SDimitry Andric            "Load size not an integral number of bytes!");
7940b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
7950b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
7960b57cec5SDimitry Andric     SDValue Lo, Hi, Ch;
7970b57cec5SDimitry Andric     unsigned IncrementSize;
7980b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
7990b57cec5SDimitry Andric 
8000b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
8010b57cec5SDimitry Andric       // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
8020b57cec5SDimitry Andric       // Load the bottom RoundWidth bits.
8030b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
8045ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
8055ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
8080b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
809*5f757f3fSDimitry Andric       Ptr =
810*5f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
8110b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
8120b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
8135ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
8160b57cec5SDimitry Andric       // the other one.
8170b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
8180b57cec5SDimitry Andric                        Hi.getValue(1));
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric       // Move the top bits to the right place.
8210b57cec5SDimitry Andric       Hi = DAG.getNode(
8220b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8230b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
8240b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric       // Join the hi and lo parts.
8270b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8280b57cec5SDimitry Andric     } else {
8290b57cec5SDimitry Andric       // Big endian - avoid unaligned loads.
8300b57cec5SDimitry Andric       // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
8310b57cec5SDimitry Andric       // Load the top RoundWidth bits.
8320b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
8335ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
8345ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
8350b57cec5SDimitry Andric 
8360b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
8370b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
838*5f757f3fSDimitry Andric       Ptr =
839*5f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
8400b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
8410b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
8425ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
8450b57cec5SDimitry Andric       // the other one.
8460b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
8470b57cec5SDimitry Andric                        Hi.getValue(1));
8480b57cec5SDimitry Andric 
8490b57cec5SDimitry Andric       // Move the top bits to the right place.
8500b57cec5SDimitry Andric       Hi = DAG.getNode(
8510b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8520b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
8530b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8540b57cec5SDimitry Andric 
8550b57cec5SDimitry Andric       // Join the hi and lo parts.
8560b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8570b57cec5SDimitry Andric     }
8580b57cec5SDimitry Andric 
8590b57cec5SDimitry Andric     Chain = Ch;
8600b57cec5SDimitry Andric   } else {
8610b57cec5SDimitry Andric     bool isCustom = false;
8620b57cec5SDimitry Andric     switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
8630b57cec5SDimitry Andric                                  SrcVT.getSimpleVT())) {
8640b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
8650b57cec5SDimitry Andric     case TargetLowering::Custom:
8660b57cec5SDimitry Andric       isCustom = true;
867bdd1243dSDimitry Andric       [[fallthrough]];
8680b57cec5SDimitry Andric     case TargetLowering::Legal:
8690b57cec5SDimitry Andric       Value = SDValue(Node, 0);
8700b57cec5SDimitry Andric       Chain = SDValue(Node, 1);
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric       if (isCustom) {
8730b57cec5SDimitry Andric         if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
8740b57cec5SDimitry Andric           Value = Res;
8750b57cec5SDimitry Andric           Chain = Res.getValue(1);
8760b57cec5SDimitry Andric         }
8770b57cec5SDimitry Andric       } else {
8780b57cec5SDimitry Andric         // If this is an unaligned load and the target doesn't support it,
8790b57cec5SDimitry Andric         // expand it.
8800b57cec5SDimitry Andric         EVT MemVT = LD->getMemoryVT();
8810b57cec5SDimitry Andric         const DataLayout &DL = DAG.getDataLayout();
8820b57cec5SDimitry Andric         if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT,
8830b57cec5SDimitry Andric                                     *LD->getMemOperand())) {
8840b57cec5SDimitry Andric           std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
8850b57cec5SDimitry Andric         }
8860b57cec5SDimitry Andric       }
8870b57cec5SDimitry Andric       break;
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric     case TargetLowering::Expand: {
8900b57cec5SDimitry Andric       EVT DestVT = Node->getValueType(0);
8910b57cec5SDimitry Andric       if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
8920b57cec5SDimitry Andric         // If the source type is not legal, see if there is a legal extload to
8930b57cec5SDimitry Andric         // an intermediate type that we can then extend further.
8940b57cec5SDimitry Andric         EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
89506c3fb27SDimitry Andric         if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) &&
89606c3fb27SDimitry Andric             (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
89706c3fb27SDimitry Andric              TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) {
8980b57cec5SDimitry Andric           // If we are loading a legal type, this is a non-extload followed by a
8990b57cec5SDimitry Andric           // full extend.
9000b57cec5SDimitry Andric           ISD::LoadExtType MidExtType =
9010b57cec5SDimitry Andric               (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric           SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
9040b57cec5SDimitry Andric                                         SrcVT, LD->getMemOperand());
9050b57cec5SDimitry Andric           unsigned ExtendOp =
9060b57cec5SDimitry Andric               ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
9070b57cec5SDimitry Andric           Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
9080b57cec5SDimitry Andric           Chain = Load.getValue(1);
9090b57cec5SDimitry Andric           break;
9100b57cec5SDimitry Andric         }
9110b57cec5SDimitry Andric 
9120b57cec5SDimitry Andric         // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
9130b57cec5SDimitry Andric         // normal undefined upper bits behavior to allow using an in-reg extend
9140b57cec5SDimitry Andric         // with the illegal FP type, so load as an integer and do the
9150b57cec5SDimitry Andric         // from-integer conversion.
9160b57cec5SDimitry Andric         if (SrcVT.getScalarType() == MVT::f16) {
9170b57cec5SDimitry Andric           EVT ISrcVT = SrcVT.changeTypeToInteger();
9180b57cec5SDimitry Andric           EVT IDestVT = DestVT.changeTypeToInteger();
9198bcb0991SDimitry Andric           EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
9200b57cec5SDimitry Andric 
9218bcb0991SDimitry Andric           SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain,
9228bcb0991SDimitry Andric                                           Ptr, ISrcVT, LD->getMemOperand());
9230b57cec5SDimitry Andric           Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result);
9240b57cec5SDimitry Andric           Chain = Result.getValue(1);
9250b57cec5SDimitry Andric           break;
9260b57cec5SDimitry Andric         }
9270b57cec5SDimitry Andric       }
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric       assert(!SrcVT.isVector() &&
9300b57cec5SDimitry Andric              "Vector Loads are handled in LegalizeVectorOps");
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric       // FIXME: This does not work for vectors on most targets.  Sign-
9330b57cec5SDimitry Andric       // and zero-extend operations are currently folded into extending
9340b57cec5SDimitry Andric       // loads, whether they are legal or not, and then we end up here
9350b57cec5SDimitry Andric       // without any support for legalizing them.
9360b57cec5SDimitry Andric       assert(ExtType != ISD::EXTLOAD &&
9370b57cec5SDimitry Andric              "EXTLOAD should always be supported!");
9380b57cec5SDimitry Andric       // Turn the unsupported load into an EXTLOAD followed by an
9390b57cec5SDimitry Andric       // explicit zero/sign extend inreg.
9400b57cec5SDimitry Andric       SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
9410b57cec5SDimitry Andric                                       Node->getValueType(0),
9420b57cec5SDimitry Andric                                       Chain, Ptr, SrcVT,
9430b57cec5SDimitry Andric                                       LD->getMemOperand());
9440b57cec5SDimitry Andric       SDValue ValRes;
9450b57cec5SDimitry Andric       if (ExtType == ISD::SEXTLOAD)
9460b57cec5SDimitry Andric         ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
9470b57cec5SDimitry Andric                              Result.getValueType(),
9480b57cec5SDimitry Andric                              Result, DAG.getValueType(SrcVT));
9490b57cec5SDimitry Andric       else
9505ffd83dbSDimitry Andric         ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
9510b57cec5SDimitry Andric       Value = ValRes;
9520b57cec5SDimitry Andric       Chain = Result.getValue(1);
9530b57cec5SDimitry Andric       break;
9540b57cec5SDimitry Andric     }
9550b57cec5SDimitry Andric     }
9560b57cec5SDimitry Andric   }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   // Since loads produce two values, make sure to remember that we legalized
9590b57cec5SDimitry Andric   // both of them.
9600b57cec5SDimitry Andric   if (Chain.getNode() != Node) {
9610b57cec5SDimitry Andric     assert(Value.getNode() != Node && "Load must be completely replaced");
9620b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
9630b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
9640b57cec5SDimitry Andric     if (UpdatedNodes) {
9650b57cec5SDimitry Andric       UpdatedNodes->insert(Value.getNode());
9660b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
9670b57cec5SDimitry Andric     }
9680b57cec5SDimitry Andric     ReplacedNode(Node);
9690b57cec5SDimitry Andric   }
9700b57cec5SDimitry Andric }
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric /// Return a legal replacement for the given operation, with all legal operands.
9730b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
9740b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
9750b57cec5SDimitry Andric 
9760b57cec5SDimitry Andric   // Allow illegal target nodes and illegal registers.
9770b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::TargetConstant ||
9780b57cec5SDimitry Andric       Node->getOpcode() == ISD::Register)
9790b57cec5SDimitry Andric     return;
9800b57cec5SDimitry Andric 
9810b57cec5SDimitry Andric #ifndef NDEBUG
9820b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
9838bcb0991SDimitry Andric     assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
9848bcb0991SDimitry Andric              TargetLowering::TypeLegal &&
9850b57cec5SDimitry Andric            "Unexpected illegal type!");
9860b57cec5SDimitry Andric 
9870b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values())
9880b57cec5SDimitry Andric     assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
9890b57cec5SDimitry Andric               TargetLowering::TypeLegal ||
9900b57cec5SDimitry Andric             Op.getOpcode() == ISD::TargetConstant ||
9910b57cec5SDimitry Andric             Op.getOpcode() == ISD::Register) &&
9920b57cec5SDimitry Andric             "Unexpected illegal type!");
9930b57cec5SDimitry Andric #endif
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric   // Figure out the correct action; the way to query this varies by opcode
9960b57cec5SDimitry Andric   TargetLowering::LegalizeAction Action = TargetLowering::Legal;
9970b57cec5SDimitry Andric   bool SimpleFinishLegalizing = true;
9980b57cec5SDimitry Andric   switch (Node->getOpcode()) {
9990b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
10000b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
10010b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
10020b57cec5SDimitry Andric   case ISD::STACKSAVE:
10030b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
10040b57cec5SDimitry Andric     break;
10050b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
10060b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10070b57cec5SDimitry Andric                                     Node->getValueType(0));
10080b57cec5SDimitry Andric     break;
10090b57cec5SDimitry Andric   case ISD::VAARG:
10100b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10110b57cec5SDimitry Andric                                     Node->getValueType(0));
10120b57cec5SDimitry Andric     if (Action != TargetLowering::Promote)
10130b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
10140b57cec5SDimitry Andric     break;
101506c3fb27SDimitry Andric   case ISD::SET_FPENV:
1016*5f757f3fSDimitry Andric   case ISD::SET_FPMODE:
101706c3fb27SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
101806c3fb27SDimitry Andric                                     Node->getOperand(1).getValueType());
101906c3fb27SDimitry Andric     break;
10200b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
102181ad6265SDimitry Andric   case ISD::FP_TO_BF16:
10220b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
10230b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
10240b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
10250b57cec5SDimitry Andric   case ISD::LROUND:
10260b57cec5SDimitry Andric   case ISD::LLROUND:
10270b57cec5SDimitry Andric   case ISD::LRINT:
10280b57cec5SDimitry Andric   case ISD::LLRINT:
10290b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10300b57cec5SDimitry Andric                                     Node->getOperand(0).getValueType());
10310b57cec5SDimitry Andric     break;
10325ffd83dbSDimitry Andric   case ISD::STRICT_FP_TO_FP16:
1033480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
1034480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
1035480093f4SDimitry Andric   case ISD::STRICT_LRINT:
1036480093f4SDimitry Andric   case ISD::STRICT_LLRINT:
1037480093f4SDimitry Andric   case ISD::STRICT_LROUND:
1038480093f4SDimitry Andric   case ISD::STRICT_LLROUND:
1039480093f4SDimitry Andric     // These pseudo-ops are the same as the other STRICT_ ops except
1040480093f4SDimitry Andric     // they are registered with setOperationAction() using the input type
1041480093f4SDimitry Andric     // instead of the output type.
1042480093f4SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1043480093f4SDimitry Andric                                     Node->getOperand(1).getValueType());
1044480093f4SDimitry Andric     break;
10450b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
10460b57cec5SDimitry Andric     EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
10470b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
10480b57cec5SDimitry Andric     break;
10490b57cec5SDimitry Andric   }
10500b57cec5SDimitry Andric   case ISD::ATOMIC_STORE:
10510b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1052*5f757f3fSDimitry Andric                                     Node->getOperand(1).getValueType());
10530b57cec5SDimitry Andric     break;
10540b57cec5SDimitry Andric   case ISD::SELECT_CC:
1055480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
1056480093f4SDimitry Andric   case ISD::STRICT_FSETCCS:
10570b57cec5SDimitry Andric   case ISD::SETCC:
1058bdd1243dSDimitry Andric   case ISD::SETCCCARRY:
105981ad6265SDimitry Andric   case ISD::VP_SETCC:
10600b57cec5SDimitry Andric   case ISD::BR_CC: {
106181ad6265SDimitry Andric     unsigned Opc = Node->getOpcode();
106281ad6265SDimitry Andric     unsigned CCOperand = Opc == ISD::SELECT_CC                         ? 4
106381ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCC                   ? 3
106481ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCCS                  ? 3
1065bdd1243dSDimitry Andric                          : Opc == ISD::SETCCCARRY                      ? 3
106681ad6265SDimitry Andric                          : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2
106781ad6265SDimitry Andric                                                                        : 1;
106881ad6265SDimitry Andric     unsigned CompareOperand = Opc == ISD::BR_CC            ? 2
106981ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCC  ? 1
107081ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCCS ? 1
107181ad6265SDimitry Andric                                                            : 0;
10720b57cec5SDimitry Andric     MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
10730b57cec5SDimitry Andric     ISD::CondCode CCCode =
10740b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
10750b57cec5SDimitry Andric     Action = TLI.getCondCodeAction(CCCode, OpVT);
10760b57cec5SDimitry Andric     if (Action == TargetLowering::Legal) {
10770b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::SELECT_CC)
10780b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(),
10790b57cec5SDimitry Andric                                         Node->getValueType(0));
10800b57cec5SDimitry Andric       else
10810b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
10820b57cec5SDimitry Andric     }
10830b57cec5SDimitry Andric     break;
10840b57cec5SDimitry Andric   }
10850b57cec5SDimitry Andric   case ISD::LOAD:
10860b57cec5SDimitry Andric   case ISD::STORE:
10870b57cec5SDimitry Andric     // FIXME: Model these properly.  LOAD and STORE are complicated, and
10880b57cec5SDimitry Andric     // STORE expects the unlegalized operand in some cases.
10890b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10900b57cec5SDimitry Andric     break;
10910b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
10920b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
10930b57cec5SDimitry Andric     // FIXME: This shouldn't be necessary.  These nodes have special properties
10940b57cec5SDimitry Andric     // dealing with the recursive nature of legalization.  Removing this
10950b57cec5SDimitry Andric     // special case should be done as part of making LegalizeDAG non-recursive.
10960b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10970b57cec5SDimitry Andric     break;
10980b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT:
1099bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
11000b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
11010b57cec5SDimitry Andric   case ISD::EH_RETURN:
11020b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
11030b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA:
11040b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
11050b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
11060b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETUP_DISPATCH:
11070b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
11080b57cec5SDimitry Andric     // they should actually be expanded.
11090b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11100b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
11110b57cec5SDimitry Andric       Action = TargetLowering::Expand;
11120b57cec5SDimitry Andric     break;
11130b57cec5SDimitry Andric   case ISD::INIT_TRAMPOLINE:
11140b57cec5SDimitry Andric   case ISD::ADJUST_TRAMPOLINE:
11150b57cec5SDimitry Andric   case ISD::FRAMEADDR:
11160b57cec5SDimitry Andric   case ISD::RETURNADDR:
11170b57cec5SDimitry Andric   case ISD::ADDROFRETURNADDR:
11180b57cec5SDimitry Andric   case ISD::SPONENTRY:
11190b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
11200b57cec5SDimitry Andric     // they should actually be custom-lowered.
11210b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11220b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
11230b57cec5SDimitry Andric       Action = TargetLowering::Custom;
11240b57cec5SDimitry Andric     break;
11250b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
11260b57cec5SDimitry Andric     // READCYCLECOUNTER returns an i64, even if type legalization might have
11270b57cec5SDimitry Andric     // expanded that to several smaller types.
11280b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
11290b57cec5SDimitry Andric     break;
11300b57cec5SDimitry Andric   case ISD::READ_REGISTER:
11310b57cec5SDimitry Andric   case ISD::WRITE_REGISTER:
11320b57cec5SDimitry Andric     // Named register is legal in the DAG, but blocked by register name
11330b57cec5SDimitry Andric     // selection if not implemented by target (to chose the correct register)
11340b57cec5SDimitry Andric     // They'll be converted to Copy(To/From)Reg.
11350b57cec5SDimitry Andric     Action = TargetLowering::Legal;
11360b57cec5SDimitry Andric     break;
1137e8d8bef9SDimitry Andric   case ISD::UBSANTRAP:
1138e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1139e8d8bef9SDimitry Andric     if (Action == TargetLowering::Expand) {
1140e8d8bef9SDimitry Andric       // replace ISD::UBSANTRAP with ISD::TRAP
1141e8d8bef9SDimitry Andric       SDValue NewVal;
1142e8d8bef9SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1143e8d8bef9SDimitry Andric                            Node->getOperand(0));
1144e8d8bef9SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
1145e8d8bef9SDimitry Andric       LegalizeOp(NewVal.getNode());
1146e8d8bef9SDimitry Andric       return;
1147e8d8bef9SDimitry Andric     }
1148e8d8bef9SDimitry Andric     break;
11490b57cec5SDimitry Andric   case ISD::DEBUGTRAP:
11500b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11510b57cec5SDimitry Andric     if (Action == TargetLowering::Expand) {
11520b57cec5SDimitry Andric       // replace ISD::DEBUGTRAP with ISD::TRAP
11530b57cec5SDimitry Andric       SDValue NewVal;
11540b57cec5SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
11550b57cec5SDimitry Andric                            Node->getOperand(0));
11560b57cec5SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
11570b57cec5SDimitry Andric       LegalizeOp(NewVal.getNode());
11580b57cec5SDimitry Andric       return;
11590b57cec5SDimitry Andric     }
11600b57cec5SDimitry Andric     break;
11610b57cec5SDimitry Andric   case ISD::SADDSAT:
11620b57cec5SDimitry Andric   case ISD::UADDSAT:
11630b57cec5SDimitry Andric   case ISD::SSUBSAT:
1164e8d8bef9SDimitry Andric   case ISD::USUBSAT:
1165e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
1166e8d8bef9SDimitry Andric   case ISD::USHLSAT:
1167e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
1168e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
11690b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11700b57cec5SDimitry Andric     break;
11710b57cec5SDimitry Andric   case ISD::SMULFIX:
11720b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
11738bcb0991SDimitry Andric   case ISD::UMULFIX:
1174480093f4SDimitry Andric   case ISD::UMULFIXSAT:
1175480093f4SDimitry Andric   case ISD::SDIVFIX:
11765ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
11775ffd83dbSDimitry Andric   case ISD::UDIVFIX:
11785ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT: {
11790b57cec5SDimitry Andric     unsigned Scale = Node->getConstantOperandVal(2);
11800b57cec5SDimitry Andric     Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
11810b57cec5SDimitry Andric                                               Node->getValueType(0), Scale);
11820b57cec5SDimitry Andric     break;
11830b57cec5SDimitry Andric   }
11840b57cec5SDimitry Andric   case ISD::MSCATTER:
11850b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11860b57cec5SDimitry Andric                     cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
11870b57cec5SDimitry Andric     break;
11880b57cec5SDimitry Andric   case ISD::MSTORE:
11890b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11900b57cec5SDimitry Andric                     cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
11910b57cec5SDimitry Andric     break;
1192349cc55cSDimitry Andric   case ISD::VP_SCATTER:
1193349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1194349cc55cSDimitry Andric         Node->getOpcode(),
1195349cc55cSDimitry Andric         cast<VPScatterSDNode>(Node)->getValue().getValueType());
1196349cc55cSDimitry Andric     break;
1197349cc55cSDimitry Andric   case ISD::VP_STORE:
1198349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1199349cc55cSDimitry Andric         Node->getOpcode(),
1200349cc55cSDimitry Andric         cast<VPStoreSDNode>(Node)->getValue().getValueType());
1201349cc55cSDimitry Andric     break;
120281ad6265SDimitry Andric   case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
120381ad6265SDimitry Andric     Action = TLI.getOperationAction(
120481ad6265SDimitry Andric         Node->getOpcode(),
120581ad6265SDimitry Andric         cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
120681ad6265SDimitry Andric     break;
12070b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
12080b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
12090b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
12100b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
12110b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
12120b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
12130b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
12140b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
12150b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
12160b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
12170b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
12180b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
12190b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
122006c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
122106c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
122281ad6265SDimitry Andric   case ISD::IS_FPCLASS:
12230b57cec5SDimitry Andric     Action = TLI.getOperationAction(
12240b57cec5SDimitry Andric         Node->getOpcode(), Node->getOperand(0).getValueType());
12250b57cec5SDimitry Andric     break;
1226e8d8bef9SDimitry Andric   case ISD::VECREDUCE_SEQ_FADD:
1227349cc55cSDimitry Andric   case ISD::VECREDUCE_SEQ_FMUL:
1228349cc55cSDimitry Andric   case ISD::VP_REDUCE_FADD:
1229349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMUL:
1230349cc55cSDimitry Andric   case ISD::VP_REDUCE_ADD:
1231349cc55cSDimitry Andric   case ISD::VP_REDUCE_MUL:
1232349cc55cSDimitry Andric   case ISD::VP_REDUCE_AND:
1233349cc55cSDimitry Andric   case ISD::VP_REDUCE_OR:
1234349cc55cSDimitry Andric   case ISD::VP_REDUCE_XOR:
1235349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMAX:
1236349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMIN:
1237349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMAX:
1238349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMIN:
1239349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMAX:
1240349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMIN:
1241349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FADD:
1242349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FMUL:
1243e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(
1244e8d8bef9SDimitry Andric         Node->getOpcode(), Node->getOperand(1).getValueType());
1245e8d8bef9SDimitry Andric     break;
12460b57cec5SDimitry Andric   default:
12470b57cec5SDimitry Andric     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
124881ad6265SDimitry Andric       Action = TLI.getCustomOperationAction(*Node);
12490b57cec5SDimitry Andric     } else {
12500b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
12510b57cec5SDimitry Andric     }
12520b57cec5SDimitry Andric     break;
12530b57cec5SDimitry Andric   }
12540b57cec5SDimitry Andric 
12550b57cec5SDimitry Andric   if (SimpleFinishLegalizing) {
12560b57cec5SDimitry Andric     SDNode *NewNode = Node;
12570b57cec5SDimitry Andric     switch (Node->getOpcode()) {
12580b57cec5SDimitry Andric     default: break;
12590b57cec5SDimitry Andric     case ISD::SHL:
12600b57cec5SDimitry Andric     case ISD::SRL:
12610b57cec5SDimitry Andric     case ISD::SRA:
12620b57cec5SDimitry Andric     case ISD::ROTL:
12630b57cec5SDimitry Andric     case ISD::ROTR: {
12640b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12650b57cec5SDimitry Andric       // to the appropriate width.
12660b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12670b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12680b57cec5SDimitry Andric       if (!Op1.getValueType().isVector()) {
12690b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
12700b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12710b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12720b57cec5SDimitry Andric         // to update the parent node.
12730b57cec5SDimitry Andric         // Do not try to legalize SAO here! It will be automatically legalized
12740b57cec5SDimitry Andric         // in the next round.
12750b57cec5SDimitry Andric         if (SAO != Op1)
12760b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
12770b57cec5SDimitry Andric       }
12780b57cec5SDimitry Andric     }
12790b57cec5SDimitry Andric     break;
12800b57cec5SDimitry Andric     case ISD::FSHL:
12810b57cec5SDimitry Andric     case ISD::FSHR:
12820b57cec5SDimitry Andric     case ISD::SRL_PARTS:
12830b57cec5SDimitry Andric     case ISD::SRA_PARTS:
12840b57cec5SDimitry Andric     case ISD::SHL_PARTS: {
12850b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12860b57cec5SDimitry Andric       // to the appropriate width.
12870b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12880b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12890b57cec5SDimitry Andric       SDValue Op2 = Node->getOperand(2);
12900b57cec5SDimitry Andric       if (!Op2.getValueType().isVector()) {
12910b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
12920b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12930b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12940b57cec5SDimitry Andric         // to update the parent node.
12950b57cec5SDimitry Andric         if (SAO != Op2)
12960b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
12970b57cec5SDimitry Andric       }
12980b57cec5SDimitry Andric       break;
12990b57cec5SDimitry Andric     }
13000b57cec5SDimitry Andric     }
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric     if (NewNode != Node) {
13030b57cec5SDimitry Andric       ReplaceNode(Node, NewNode);
13040b57cec5SDimitry Andric       Node = NewNode;
13050b57cec5SDimitry Andric     }
13060b57cec5SDimitry Andric     switch (Action) {
13070b57cec5SDimitry Andric     case TargetLowering::Legal:
13080b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
13090b57cec5SDimitry Andric       return;
13100b57cec5SDimitry Andric     case TargetLowering::Custom:
13110b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
13120b57cec5SDimitry Andric       // FIXME: The handling for custom lowering with multiple results is
13130b57cec5SDimitry Andric       // a complete mess.
13140b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
13150b57cec5SDimitry Andric         if (!(Res.getNode() != Node || Res.getResNo() != 0))
13160b57cec5SDimitry Andric           return;
13170b57cec5SDimitry Andric 
13180b57cec5SDimitry Andric         if (Node->getNumValues() == 1) {
1319fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1320fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1321fe6060f1SDimitry Andric           assert((Res.getValueType() == Node->getValueType(0) ||
1322fe6060f1SDimitry Andric                   Node->getValueType(0) == MVT::Glue) &&
1323fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13240b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13250b57cec5SDimitry Andric           // We can just directly replace this node with the lowered value.
13260b57cec5SDimitry Andric           ReplaceNode(SDValue(Node, 0), Res);
13270b57cec5SDimitry Andric           return;
13280b57cec5SDimitry Andric         }
13290b57cec5SDimitry Andric 
13300b57cec5SDimitry Andric         SmallVector<SDValue, 8> ResultVals;
1331fe6060f1SDimitry Andric         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1332fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1333fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1334fe6060f1SDimitry Andric           assert((Res->getValueType(i) == Node->getValueType(i) ||
1335fe6060f1SDimitry Andric                   Node->getValueType(i) == MVT::Glue) &&
1336fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13370b57cec5SDimitry Andric           ResultVals.push_back(Res.getValue(i));
1338fe6060f1SDimitry Andric         }
13390b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13400b57cec5SDimitry Andric         ReplaceNode(Node, ResultVals.data());
13410b57cec5SDimitry Andric         return;
13420b57cec5SDimitry Andric       }
13430b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1344bdd1243dSDimitry Andric       [[fallthrough]];
13450b57cec5SDimitry Andric     case TargetLowering::Expand:
13460b57cec5SDimitry Andric       if (ExpandNode(Node))
13470b57cec5SDimitry Andric         return;
1348bdd1243dSDimitry Andric       [[fallthrough]];
13490b57cec5SDimitry Andric     case TargetLowering::LibCall:
13500b57cec5SDimitry Andric       ConvertNodeToLibcall(Node);
13510b57cec5SDimitry Andric       return;
13520b57cec5SDimitry Andric     case TargetLowering::Promote:
13530b57cec5SDimitry Andric       PromoteNode(Node);
13540b57cec5SDimitry Andric       return;
13550b57cec5SDimitry Andric     }
13560b57cec5SDimitry Andric   }
13570b57cec5SDimitry Andric 
13580b57cec5SDimitry Andric   switch (Node->getOpcode()) {
13590b57cec5SDimitry Andric   default:
13600b57cec5SDimitry Andric #ifndef NDEBUG
13610b57cec5SDimitry Andric     dbgs() << "NODE: ";
13620b57cec5SDimitry Andric     Node->dump( &DAG);
13630b57cec5SDimitry Andric     dbgs() << "\n";
13640b57cec5SDimitry Andric #endif
13650b57cec5SDimitry Andric     llvm_unreachable("Do not know how to legalize this operator!");
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
13680b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
13690b57cec5SDimitry Andric     break;
13700b57cec5SDimitry Andric   case ISD::LOAD:
13710b57cec5SDimitry Andric     return LegalizeLoadOps(Node);
13720b57cec5SDimitry Andric   case ISD::STORE:
13730b57cec5SDimitry Andric     return LegalizeStoreOps(Node);
13740b57cec5SDimitry Andric   }
13750b57cec5SDimitry Andric }
13760b57cec5SDimitry Andric 
13770b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
13780b57cec5SDimitry Andric   SDValue Vec = Op.getOperand(0);
13790b57cec5SDimitry Andric   SDValue Idx = Op.getOperand(1);
13800b57cec5SDimitry Andric   SDLoc dl(Op);
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   // Before we generate a new store to a temporary stack slot, see if there is
13830b57cec5SDimitry Andric   // already one that we can use. There often is because when we scalarize
13840b57cec5SDimitry Andric   // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
13850b57cec5SDimitry Andric   // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
13860b57cec5SDimitry Andric   // the vector. If all are expanded here, we don't want one store per vector
13870b57cec5SDimitry Andric   // element.
13880b57cec5SDimitry Andric 
13890b57cec5SDimitry Andric   // Caches for hasPredecessorHelper
13900b57cec5SDimitry Andric   SmallPtrSet<const SDNode *, 32> Visited;
13910b57cec5SDimitry Andric   SmallVector<const SDNode *, 16> Worklist;
13920b57cec5SDimitry Andric   Visited.insert(Op.getNode());
13930b57cec5SDimitry Andric   Worklist.push_back(Idx.getNode());
13940b57cec5SDimitry Andric   SDValue StackPtr, Ch;
1395349cc55cSDimitry Andric   for (SDNode *User : Vec.getNode()->uses()) {
13960b57cec5SDimitry Andric     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
13970b57cec5SDimitry Andric       if (ST->isIndexed() || ST->isTruncatingStore() ||
13980b57cec5SDimitry Andric           ST->getValue() != Vec)
13990b57cec5SDimitry Andric         continue;
14000b57cec5SDimitry Andric 
14010b57cec5SDimitry Andric       // Make sure that nothing else could have stored into the destination of
14020b57cec5SDimitry Andric       // this store.
14030b57cec5SDimitry Andric       if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
14040b57cec5SDimitry Andric         continue;
14050b57cec5SDimitry Andric 
14060b57cec5SDimitry Andric       // If the index is dependent on the store we will introduce a cycle when
14070b57cec5SDimitry Andric       // creating the load (the load uses the index, and by replacing the chain
14080b57cec5SDimitry Andric       // we will make the index dependent on the load). Also, the store might be
14090b57cec5SDimitry Andric       // dependent on the extractelement and introduce a cycle when creating
14100b57cec5SDimitry Andric       // the load.
14110b57cec5SDimitry Andric       if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
14120b57cec5SDimitry Andric           ST->hasPredecessor(Op.getNode()))
14130b57cec5SDimitry Andric         continue;
14140b57cec5SDimitry Andric 
14150b57cec5SDimitry Andric       StackPtr = ST->getBasePtr();
14160b57cec5SDimitry Andric       Ch = SDValue(ST, 0);
14170b57cec5SDimitry Andric       break;
14180b57cec5SDimitry Andric     }
14190b57cec5SDimitry Andric   }
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
14220b57cec5SDimitry Andric 
14230b57cec5SDimitry Andric   if (!Ch.getNode()) {
14240b57cec5SDimitry Andric     // Store the value to a temporary stack slot, then LOAD the returned part.
14250b57cec5SDimitry Andric     StackPtr = DAG.CreateStackTemporary(VecVT);
14260b57cec5SDimitry Andric     Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr,
14270b57cec5SDimitry Andric                       MachinePointerInfo());
14280b57cec5SDimitry Andric   }
14290b57cec5SDimitry Andric 
14300b57cec5SDimitry Andric   SDValue NewLoad;
1431fcaf7f86SDimitry Andric   Align ElementAlignment =
1432fcaf7f86SDimitry Andric       std::min(cast<StoreSDNode>(Ch)->getAlign(),
1433fcaf7f86SDimitry Andric                DAG.getDataLayout().getPrefTypeAlign(
1434fcaf7f86SDimitry Andric                    Op.getValueType().getTypeForEVT(*DAG.getContext())));
14350b57cec5SDimitry Andric 
1436fe6060f1SDimitry Andric   if (Op.getValueType().isVector()) {
1437fe6060f1SDimitry Andric     StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1438fe6060f1SDimitry Andric                                           Op.getValueType(), Idx);
1439fcaf7f86SDimitry Andric     NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1440fcaf7f86SDimitry Andric                           MachinePointerInfo(), ElementAlignment);
1441fe6060f1SDimitry Andric   } else {
1442fe6060f1SDimitry Andric     StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
14430b57cec5SDimitry Andric     NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1444fcaf7f86SDimitry Andric                              MachinePointerInfo(), VecVT.getVectorElementType(),
1445fcaf7f86SDimitry Andric                              ElementAlignment);
1446fe6060f1SDimitry Andric   }
14470b57cec5SDimitry Andric 
14480b57cec5SDimitry Andric   // Replace the chain going out of the store, by the one out of the load.
14490b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
14500b57cec5SDimitry Andric 
14510b57cec5SDimitry Andric   // We introduced a cycle though, so update the loads operands, making sure
14520b57cec5SDimitry Andric   // to use the original store's chain as an incoming chain.
14530b57cec5SDimitry Andric   SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
14540b57cec5SDimitry Andric                                           NewLoad->op_end());
14550b57cec5SDimitry Andric   NewLoadOperands[0] = Ch;
14560b57cec5SDimitry Andric   NewLoad =
14570b57cec5SDimitry Andric       SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
14580b57cec5SDimitry Andric   return NewLoad;
14590b57cec5SDimitry Andric }
14600b57cec5SDimitry Andric 
14610b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
14620b57cec5SDimitry Andric   assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric   SDValue Vec  = Op.getOperand(0);
14650b57cec5SDimitry Andric   SDValue Part = Op.getOperand(1);
14660b57cec5SDimitry Andric   SDValue Idx  = Op.getOperand(2);
14670b57cec5SDimitry Andric   SDLoc dl(Op);
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric   // Store the value to a temporary stack slot, then LOAD the returned part.
14700b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
1471fe6060f1SDimitry Andric   EVT SubVecVT = Part.getValueType();
14720b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
14730b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
14740b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
14750b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
14760b57cec5SDimitry Andric 
14770b57cec5SDimitry Andric   // First store the whole vector.
14780b57cec5SDimitry Andric   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric   // Then store the inserted part.
1481fe6060f1SDimitry Andric   SDValue SubStackPtr =
1482fe6060f1SDimitry Andric       TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx);
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric   // Store the subvector.
14855ffd83dbSDimitry Andric   Ch = DAG.getStore(
14865ffd83dbSDimitry Andric       Ch, dl, Part, SubStackPtr,
14875ffd83dbSDimitry Andric       MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
14880b57cec5SDimitry Andric 
14890b57cec5SDimitry Andric   // Finally, load the updated vector.
14900b57cec5SDimitry Andric   return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo);
14910b57cec5SDimitry Andric }
14920b57cec5SDimitry Andric 
14930b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
14945ffd83dbSDimitry Andric   assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
14955ffd83dbSDimitry Andric           Node->getOpcode() == ISD::CONCAT_VECTORS) &&
14965ffd83dbSDimitry Andric          "Unexpected opcode!");
14975ffd83dbSDimitry Andric 
14980b57cec5SDimitry Andric   // We can't handle this case efficiently.  Allocate a sufficiently
14995ffd83dbSDimitry Andric   // aligned object on the stack, store each operand into it, then load
15000b57cec5SDimitry Andric   // the result as a vector.
15010b57cec5SDimitry Andric   // Create the stack frame object.
15020b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
15035ffd83dbSDimitry Andric   EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
15045ffd83dbSDimitry Andric                                            : Node->getOperand(0).getValueType();
15050b57cec5SDimitry Andric   SDLoc dl(Node);
15060b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(VT);
15070b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
15080b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
15090b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
15100b57cec5SDimitry Andric 
15110b57cec5SDimitry Andric   // Emit a store of each element to the stack slot.
15120b57cec5SDimitry Andric   SmallVector<SDValue, 8> Stores;
15135ffd83dbSDimitry Andric   unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
15140b57cec5SDimitry Andric   assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1515e8d8bef9SDimitry Andric 
1516e8d8bef9SDimitry Andric   // If the destination vector element type of a BUILD_VECTOR is narrower than
1517e8d8bef9SDimitry Andric   // the source element type, only store the bits necessary.
1518e8d8bef9SDimitry Andric   bool Truncate = isa<BuildVectorSDNode>(Node) &&
1519e8d8bef9SDimitry Andric                   MemVT.bitsLT(Node->getOperand(0).getValueType());
1520e8d8bef9SDimitry Andric 
15210b57cec5SDimitry Andric   // Store (in the right endianness) the elements to memory.
15220b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
15230b57cec5SDimitry Andric     // Ignore undef elements.
15240b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef()) continue;
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric     unsigned Offset = TypeByteSize*i;
15270b57cec5SDimitry Andric 
1528*5f757f3fSDimitry Andric     SDValue Idx =
1529*5f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(FIPtr, TypeSize::getFixed(Offset), dl);
15300b57cec5SDimitry Andric 
1531e8d8bef9SDimitry Andric     if (Truncate)
15320b57cec5SDimitry Andric       Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
15330b57cec5SDimitry Andric                                          Node->getOperand(i), Idx,
15345ffd83dbSDimitry Andric                                          PtrInfo.getWithOffset(Offset), MemVT));
15355ffd83dbSDimitry Andric     else
15360b57cec5SDimitry Andric       Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
15370b57cec5SDimitry Andric                                     Idx, PtrInfo.getWithOffset(Offset)));
15380b57cec5SDimitry Andric   }
15390b57cec5SDimitry Andric 
15400b57cec5SDimitry Andric   SDValue StoreChain;
15410b57cec5SDimitry Andric   if (!Stores.empty())    // Not all undef elements?
15420b57cec5SDimitry Andric     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
15430b57cec5SDimitry Andric   else
15440b57cec5SDimitry Andric     StoreChain = DAG.getEntryNode();
15450b57cec5SDimitry Andric 
15460b57cec5SDimitry Andric   // Result is a load from the stack slot.
15470b57cec5SDimitry Andric   return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
15480b57cec5SDimitry Andric }
15490b57cec5SDimitry Andric 
15500b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part
15510b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of
15520b57cec5SDimitry Andric /// holding all bits of the floating-point value.
15530b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
15540b57cec5SDimitry Andric                                              const SDLoc &DL,
15550b57cec5SDimitry Andric                                              SDValue Value) const {
15560b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
1557e8d8bef9SDimitry Andric   unsigned NumBits = FloatVT.getScalarSizeInBits();
15580b57cec5SDimitry Andric   State.FloatVT = FloatVT;
15590b57cec5SDimitry Andric   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
15600b57cec5SDimitry Andric   // Convert to an integer of the same size.
15610b57cec5SDimitry Andric   if (TLI.isTypeLegal(IVT)) {
15620b57cec5SDimitry Andric     State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
15630b57cec5SDimitry Andric     State.SignMask = APInt::getSignMask(NumBits);
15640b57cec5SDimitry Andric     State.SignBit = NumBits - 1;
15650b57cec5SDimitry Andric     return;
15660b57cec5SDimitry Andric   }
15670b57cec5SDimitry Andric 
15680b57cec5SDimitry Andric   auto &DataLayout = DAG.getDataLayout();
15690b57cec5SDimitry Andric   // Store the float to memory, then load the sign part out as an integer.
157006c3fb27SDimitry Andric   MVT LoadTy = TLI.getRegisterType(MVT::i8);
15710b57cec5SDimitry Andric   // First create a temporary that is aligned for both the load and store.
15720b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
15730b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
15740b57cec5SDimitry Andric   // Then store the float to it.
15750b57cec5SDimitry Andric   State.FloatPtr = StackPtr;
15760b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
15770b57cec5SDimitry Andric   State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
15780b57cec5SDimitry Andric   State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
15790b57cec5SDimitry Andric                              State.FloatPointerInfo);
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric   SDValue IntPtr;
15820b57cec5SDimitry Andric   if (DataLayout.isBigEndian()) {
15830b57cec5SDimitry Andric     assert(FloatVT.isByteSized() && "Unsupported floating point type!");
15840b57cec5SDimitry Andric     // Load out a legal integer with the same sign bit as the float.
15850b57cec5SDimitry Andric     IntPtr = StackPtr;
15860b57cec5SDimitry Andric     State.IntPointerInfo = State.FloatPointerInfo;
15870b57cec5SDimitry Andric   } else {
15880b57cec5SDimitry Andric     // Advance the pointer so that the loaded byte will contain the sign bit.
1589e8d8bef9SDimitry Andric     unsigned ByteOffset = (NumBits / 8) - 1;
1590e8d8bef9SDimitry Andric     IntPtr =
1591*5f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL);
15920b57cec5SDimitry Andric     State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
15930b57cec5SDimitry Andric                                                              ByteOffset);
15940b57cec5SDimitry Andric   }
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric   State.IntPtr = IntPtr;
15970b57cec5SDimitry Andric   State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
15980b57cec5SDimitry Andric                                   State.IntPointerInfo, MVT::i8);
1599e8d8bef9SDimitry Andric   State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
16000b57cec5SDimitry Andric   State.SignBit = 7;
16010b57cec5SDimitry Andric }
16020b57cec5SDimitry Andric 
16030b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value
16040b57cec5SDimitry Andric /// and cast the result back to a floating-point type.
16050b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
16060b57cec5SDimitry Andric                                               const SDLoc &DL,
16070b57cec5SDimitry Andric                                               SDValue NewIntValue) const {
16080b57cec5SDimitry Andric   if (!State.Chain)
16090b57cec5SDimitry Andric     return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
16100b57cec5SDimitry Andric 
16110b57cec5SDimitry Andric   // Override the part containing the sign bit in the value stored on the stack.
16120b57cec5SDimitry Andric   SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
16130b57cec5SDimitry Andric                                     State.IntPointerInfo, MVT::i8);
16140b57cec5SDimitry Andric   return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
16150b57cec5SDimitry Andric                      State.FloatPointerInfo);
16160b57cec5SDimitry Andric }
16170b57cec5SDimitry Andric 
16180b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
16190b57cec5SDimitry Andric   SDLoc DL(Node);
16200b57cec5SDimitry Andric   SDValue Mag = Node->getOperand(0);
16210b57cec5SDimitry Andric   SDValue Sign = Node->getOperand(1);
16220b57cec5SDimitry Andric 
16230b57cec5SDimitry Andric   // Get sign bit into an integer value.
16240b57cec5SDimitry Andric   FloatSignAsInt SignAsInt;
16250b57cec5SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Sign);
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
16280b57cec5SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
16290b57cec5SDimitry Andric   SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
16300b57cec5SDimitry Andric                                 SignMask);
16310b57cec5SDimitry Andric 
16320b57cec5SDimitry Andric   // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
16330b57cec5SDimitry Andric   EVT FloatVT = Mag.getValueType();
16340b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
16350b57cec5SDimitry Andric       TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
16360b57cec5SDimitry Andric     SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
16370b57cec5SDimitry Andric     SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
16380b57cec5SDimitry Andric     SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
16390b57cec5SDimitry Andric                                 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
16400b57cec5SDimitry Andric     return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
16410b57cec5SDimitry Andric   }
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric   // Transform Mag value to integer, and clear the sign bit.
16440b57cec5SDimitry Andric   FloatSignAsInt MagAsInt;
16450b57cec5SDimitry Andric   getSignAsIntValue(MagAsInt, DL, Mag);
16460b57cec5SDimitry Andric   EVT MagVT = MagAsInt.IntValue.getValueType();
16470b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
16480b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
16490b57cec5SDimitry Andric                                     ClearSignMask);
16500b57cec5SDimitry Andric 
16510b57cec5SDimitry Andric   // Get the signbit at the right position for MagAsInt.
16520b57cec5SDimitry Andric   int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
16530b57cec5SDimitry Andric   EVT ShiftVT = IntVT;
1654e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() <
1655e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16560b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
16570b57cec5SDimitry Andric     ShiftVT = MagVT;
16580b57cec5SDimitry Andric   }
16590b57cec5SDimitry Andric   if (ShiftAmount > 0) {
16600b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
16610b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
16620b57cec5SDimitry Andric   } else if (ShiftAmount < 0) {
16630b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
16640b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
16650b57cec5SDimitry Andric   }
1666e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() >
1667e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16680b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
16690b57cec5SDimitry Andric   }
16700b57cec5SDimitry Andric 
16710b57cec5SDimitry Andric   // Store the part with the modified sign and convert back to float.
16720b57cec5SDimitry Andric   SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit);
16730b57cec5SDimitry Andric   return modifySignAsInt(MagAsInt, DL, CopiedSign);
16740b57cec5SDimitry Andric }
16750b57cec5SDimitry Andric 
1676e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1677e8d8bef9SDimitry Andric   // Get the sign bit as an integer.
1678e8d8bef9SDimitry Andric   SDLoc DL(Node);
1679e8d8bef9SDimitry Andric   FloatSignAsInt SignAsInt;
1680e8d8bef9SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1681e8d8bef9SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
1682e8d8bef9SDimitry Andric 
1683e8d8bef9SDimitry Andric   // Flip the sign.
1684e8d8bef9SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1685e8d8bef9SDimitry Andric   SDValue SignFlip =
1686e8d8bef9SDimitry Andric       DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1687e8d8bef9SDimitry Andric 
1688e8d8bef9SDimitry Andric   // Convert back to float.
1689e8d8bef9SDimitry Andric   return modifySignAsInt(SignAsInt, DL, SignFlip);
1690e8d8bef9SDimitry Andric }
1691e8d8bef9SDimitry Andric 
16920b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
16930b57cec5SDimitry Andric   SDLoc DL(Node);
16940b57cec5SDimitry Andric   SDValue Value = Node->getOperand(0);
16950b57cec5SDimitry Andric 
16960b57cec5SDimitry Andric   // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
16970b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
16980b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
16990b57cec5SDimitry Andric     SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
17000b57cec5SDimitry Andric     return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
17010b57cec5SDimitry Andric   }
17020b57cec5SDimitry Andric 
17030b57cec5SDimitry Andric   // Transform value to integer, clear the sign bit and transform back.
17040b57cec5SDimitry Andric   FloatSignAsInt ValueAsInt;
17050b57cec5SDimitry Andric   getSignAsIntValue(ValueAsInt, DL, Value);
17060b57cec5SDimitry Andric   EVT IntVT = ValueAsInt.IntValue.getValueType();
17070b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
17080b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
17090b57cec5SDimitry Andric                                     ClearSignMask);
17100b57cec5SDimitry Andric   return modifySignAsInt(ValueAsInt, DL, ClearedSign);
17110b57cec5SDimitry Andric }
17120b57cec5SDimitry Andric 
17130b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
17140b57cec5SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
1715e8d8bef9SDimitry Andric   Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
17160b57cec5SDimitry Andric   assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
17170b57cec5SDimitry Andric           " not tell us which reg is the stack pointer!");
17180b57cec5SDimitry Andric   SDLoc dl(Node);
17190b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
17200b57cec5SDimitry Andric   SDValue Tmp1 = SDValue(Node, 0);
17210b57cec5SDimitry Andric   SDValue Tmp2 = SDValue(Node, 1);
17220b57cec5SDimitry Andric   SDValue Tmp3 = Node->getOperand(2);
17230b57cec5SDimitry Andric   SDValue Chain = Tmp1.getOperand(0);
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric   // Chain the dynamic stack allocation so that it doesn't modify the stack
17260b57cec5SDimitry Andric   // pointer when other instructions are using the stack.
17270b57cec5SDimitry Andric   Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
17280b57cec5SDimitry Andric 
17290b57cec5SDimitry Andric   SDValue Size  = Tmp2.getOperand(1);
17300b57cec5SDimitry Andric   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
17310b57cec5SDimitry Andric   Chain = SP.getValue(1);
17325ffd83dbSDimitry Andric   Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
17335ffd83dbSDimitry Andric   const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
17345ffd83dbSDimitry Andric   unsigned Opc =
17355ffd83dbSDimitry Andric     TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ?
17365ffd83dbSDimitry Andric     ISD::ADD : ISD::SUB;
17375ffd83dbSDimitry Andric 
17385ffd83dbSDimitry Andric   Align StackAlign = TFL->getStackAlign();
17395ffd83dbSDimitry Andric   Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size);       // Value
17405ffd83dbSDimitry Andric   if (Alignment > StackAlign)
17410b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
17425ffd83dbSDimitry Andric                        DAG.getConstant(-Alignment.value(), dl, VT));
17430b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);     // Output chain
17440b57cec5SDimitry Andric 
1745bdd1243dSDimitry Andric   Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   Results.push_back(Tmp1);
17480b57cec5SDimitry Andric   Results.push_back(Tmp2);
17490b57cec5SDimitry Andric }
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric /// Emit a store/load combination to the stack.  This stores
17520b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
17530b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed.
17540b57cec5SDimitry Andric /// The resultant code need not be legal.
17550b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17560b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl) {
17570b57cec5SDimitry Andric   return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
17580b57cec5SDimitry Andric }
17590b57cec5SDimitry Andric 
17600b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17610b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl,
17620b57cec5SDimitry Andric                                                SDValue Chain) {
176381ad6265SDimitry Andric   EVT SrcVT = SrcOp.getValueType();
1764e8d8bef9SDimitry Andric   Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1765e8d8bef9SDimitry Andric   Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1766e8d8bef9SDimitry Andric 
1767e8d8bef9SDimitry Andric   // Don't convert with stack if the load/store is expensive.
176881ad6265SDimitry Andric   if ((SrcVT.bitsGT(SlotVT) &&
1769e8d8bef9SDimitry Andric        !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
177081ad6265SDimitry Andric       (SlotVT.bitsLT(DestVT) &&
1771e8d8bef9SDimitry Andric        !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1772e8d8bef9SDimitry Andric     return SDValue();
1773e8d8bef9SDimitry Andric 
17740b57cec5SDimitry Andric   // Create the stack frame object.
1775e8d8bef9SDimitry Andric   Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
17760b57cec5SDimitry Andric       SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1777e8d8bef9SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
17780b57cec5SDimitry Andric 
17790b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
17800b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
17810b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
17820b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
17830b57cec5SDimitry Andric 
17840b57cec5SDimitry Andric   // Emit a store to the stack slot.  Use a truncstore if the input value is
17850b57cec5SDimitry Andric   // later than DestVT.
17860b57cec5SDimitry Andric   SDValue Store;
17870b57cec5SDimitry Andric 
178881ad6265SDimitry Andric   if (SrcVT.bitsGT(SlotVT))
17890b57cec5SDimitry Andric     Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
17900b57cec5SDimitry Andric                               SlotVT, SrcAlign);
17910b57cec5SDimitry Andric   else {
179281ad6265SDimitry Andric     assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
179381ad6265SDimitry Andric     Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
17940b57cec5SDimitry Andric   }
17950b57cec5SDimitry Andric 
17960b57cec5SDimitry Andric   // Result is a load from the stack slot.
179781ad6265SDimitry Andric   if (SlotVT.bitsEq(DestVT))
17980b57cec5SDimitry Andric     return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
17990b57cec5SDimitry Andric 
180081ad6265SDimitry Andric   assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
18010b57cec5SDimitry Andric   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
18020b57cec5SDimitry Andric                         DestAlign);
18030b57cec5SDimitry Andric }
18040b57cec5SDimitry Andric 
18050b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
18060b57cec5SDimitry Andric   SDLoc dl(Node);
18070b57cec5SDimitry Andric   // Create a vector sized/aligned stack slot, store the value to element #0,
18080b57cec5SDimitry Andric   // then load the whole vector back out.
18090b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
18100b57cec5SDimitry Andric 
18110b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
18120b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
18130b57cec5SDimitry Andric 
18140b57cec5SDimitry Andric   SDValue Ch = DAG.getTruncStore(
18150b57cec5SDimitry Andric       DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
18160b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
18170b57cec5SDimitry Andric       Node->getValueType(0).getVectorElementType());
18180b57cec5SDimitry Andric   return DAG.getLoad(
18190b57cec5SDimitry Andric       Node->getValueType(0), dl, Ch, StackPtr,
18200b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
18210b57cec5SDimitry Andric }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric static bool
18240b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
18250b57cec5SDimitry Andric                      const TargetLowering &TLI, SDValue &Res) {
18260b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
18270b57cec5SDimitry Andric   SDLoc dl(Node);
18280b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric   // Try to group the scalars into pairs, shuffle the pairs together, then
18310b57cec5SDimitry Andric   // shuffle the pairs of pairs together, etc. until the vector has
18320b57cec5SDimitry Andric   // been built. This will work only if all of the necessary shuffle masks
18330b57cec5SDimitry Andric   // are legal.
18340b57cec5SDimitry Andric 
18350b57cec5SDimitry Andric   // We do this in two phases; first to check the legality of the shuffles,
18360b57cec5SDimitry Andric   // and next, assuming that all shuffles are legal, to create the new nodes.
18370b57cec5SDimitry Andric   for (int Phase = 0; Phase < 2; ++Phase) {
18380b57cec5SDimitry Andric     SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals,
18390b57cec5SDimitry Andric                                                               NewIntermedVals;
18400b57cec5SDimitry Andric     for (unsigned i = 0; i < NumElems; ++i) {
18410b57cec5SDimitry Andric       SDValue V = Node->getOperand(i);
18420b57cec5SDimitry Andric       if (V.isUndef())
18430b57cec5SDimitry Andric         continue;
18440b57cec5SDimitry Andric 
18450b57cec5SDimitry Andric       SDValue Vec;
18460b57cec5SDimitry Andric       if (Phase)
18470b57cec5SDimitry Andric         Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
18480b57cec5SDimitry Andric       IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
18490b57cec5SDimitry Andric     }
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric     while (IntermedVals.size() > 2) {
18520b57cec5SDimitry Andric       NewIntermedVals.clear();
18530b57cec5SDimitry Andric       for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
18540b57cec5SDimitry Andric         // This vector and the next vector are shuffled together (simply to
18550b57cec5SDimitry Andric         // append the one to the other).
18560b57cec5SDimitry Andric         SmallVector<int, 16> ShuffleVec(NumElems, -1);
18570b57cec5SDimitry Andric 
18580b57cec5SDimitry Andric         SmallVector<int, 16> FinalIndices;
18590b57cec5SDimitry Andric         FinalIndices.reserve(IntermedVals[i].second.size() +
18600b57cec5SDimitry Andric                              IntermedVals[i+1].second.size());
18610b57cec5SDimitry Andric 
18620b57cec5SDimitry Andric         int k = 0;
18630b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
18640b57cec5SDimitry Andric              ++j, ++k) {
18650b57cec5SDimitry Andric           ShuffleVec[k] = j;
18660b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i].second[j]);
18670b57cec5SDimitry Andric         }
18680b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
18690b57cec5SDimitry Andric              ++j, ++k) {
18700b57cec5SDimitry Andric           ShuffleVec[k] = NumElems + j;
18710b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i+1].second[j]);
18720b57cec5SDimitry Andric         }
18730b57cec5SDimitry Andric 
18740b57cec5SDimitry Andric         SDValue Shuffle;
18750b57cec5SDimitry Andric         if (Phase)
18760b57cec5SDimitry Andric           Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
18770b57cec5SDimitry Andric                                          IntermedVals[i+1].first,
18780b57cec5SDimitry Andric                                          ShuffleVec);
18790b57cec5SDimitry Andric         else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
18800b57cec5SDimitry Andric           return false;
18810b57cec5SDimitry Andric         NewIntermedVals.push_back(
18820b57cec5SDimitry Andric             std::make_pair(Shuffle, std::move(FinalIndices)));
18830b57cec5SDimitry Andric       }
18840b57cec5SDimitry Andric 
18850b57cec5SDimitry Andric       // If we had an odd number of defined values, then append the last
18860b57cec5SDimitry Andric       // element to the array of new vectors.
18870b57cec5SDimitry Andric       if ((IntermedVals.size() & 1) != 0)
18880b57cec5SDimitry Andric         NewIntermedVals.push_back(IntermedVals.back());
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric       IntermedVals.swap(NewIntermedVals);
18910b57cec5SDimitry Andric     }
18920b57cec5SDimitry Andric 
18930b57cec5SDimitry Andric     assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
18940b57cec5SDimitry Andric            "Invalid number of intermediate vectors");
18950b57cec5SDimitry Andric     SDValue Vec1 = IntermedVals[0].first;
18960b57cec5SDimitry Andric     SDValue Vec2;
18970b57cec5SDimitry Andric     if (IntermedVals.size() > 1)
18980b57cec5SDimitry Andric       Vec2 = IntermedVals[1].first;
18990b57cec5SDimitry Andric     else if (Phase)
19000b57cec5SDimitry Andric       Vec2 = DAG.getUNDEF(VT);
19010b57cec5SDimitry Andric 
19020b57cec5SDimitry Andric     SmallVector<int, 16> ShuffleVec(NumElems, -1);
19030b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
19040b57cec5SDimitry Andric       ShuffleVec[IntermedVals[0].second[i]] = i;
19050b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
19060b57cec5SDimitry Andric       ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
19070b57cec5SDimitry Andric 
19080b57cec5SDimitry Andric     if (Phase)
19090b57cec5SDimitry Andric       Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
19100b57cec5SDimitry Andric     else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
19110b57cec5SDimitry Andric       return false;
19120b57cec5SDimitry Andric   }
19130b57cec5SDimitry Andric 
19140b57cec5SDimitry Andric   return true;
19150b57cec5SDimitry Andric }
19160b57cec5SDimitry Andric 
19170b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't
19180b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type.
19190b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
19200b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
19210b57cec5SDimitry Andric   SDValue Value1, Value2;
19220b57cec5SDimitry Andric   SDLoc dl(Node);
19230b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
19240b57cec5SDimitry Andric   EVT OpVT = Node->getOperand(0).getValueType();
19250b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric   // If the only non-undef value is the low element, turn this into a
19280b57cec5SDimitry Andric   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
19290b57cec5SDimitry Andric   bool isOnlyLowElement = true;
19300b57cec5SDimitry Andric   bool MoreThanTwoValues = false;
19310b57cec5SDimitry Andric   bool isConstant = true;
19320b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
19330b57cec5SDimitry Andric     SDValue V = Node->getOperand(i);
19340b57cec5SDimitry Andric     if (V.isUndef())
19350b57cec5SDimitry Andric       continue;
19360b57cec5SDimitry Andric     if (i > 0)
19370b57cec5SDimitry Andric       isOnlyLowElement = false;
19380b57cec5SDimitry Andric     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
19390b57cec5SDimitry Andric       isConstant = false;
19400b57cec5SDimitry Andric 
19410b57cec5SDimitry Andric     if (!Value1.getNode()) {
19420b57cec5SDimitry Andric       Value1 = V;
19430b57cec5SDimitry Andric     } else if (!Value2.getNode()) {
19440b57cec5SDimitry Andric       if (V != Value1)
19450b57cec5SDimitry Andric         Value2 = V;
19460b57cec5SDimitry Andric     } else if (V != Value1 && V != Value2) {
19470b57cec5SDimitry Andric       MoreThanTwoValues = true;
19480b57cec5SDimitry Andric     }
19490b57cec5SDimitry Andric   }
19500b57cec5SDimitry Andric 
19510b57cec5SDimitry Andric   if (!Value1.getNode())
19520b57cec5SDimitry Andric     return DAG.getUNDEF(VT);
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric   if (isOnlyLowElement)
19550b57cec5SDimitry Andric     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   // If all elements are constants, create a load from the constant pool.
19580b57cec5SDimitry Andric   if (isConstant) {
19590b57cec5SDimitry Andric     SmallVector<Constant*, 16> CV;
19600b57cec5SDimitry Andric     for (unsigned i = 0, e = NumElems; i != e; ++i) {
19610b57cec5SDimitry Andric       if (ConstantFPSDNode *V =
19620b57cec5SDimitry Andric           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
19630b57cec5SDimitry Andric         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
19640b57cec5SDimitry Andric       } else if (ConstantSDNode *V =
19650b57cec5SDimitry Andric                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
19660b57cec5SDimitry Andric         if (OpVT==EltVT)
19670b57cec5SDimitry Andric           CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
19680b57cec5SDimitry Andric         else {
19690b57cec5SDimitry Andric           // If OpVT and EltVT don't match, EltVT is not legal and the
19700b57cec5SDimitry Andric           // element values have been promoted/truncated earlier.  Undo this;
19710b57cec5SDimitry Andric           // we don't want a v16i8 to become a v16i32 for example.
19720b57cec5SDimitry Andric           const ConstantInt *CI = V->getConstantIntValue();
19730b57cec5SDimitry Andric           CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
19740b57cec5SDimitry Andric                                         CI->getZExtValue()));
19750b57cec5SDimitry Andric         }
19760b57cec5SDimitry Andric       } else {
19770b57cec5SDimitry Andric         assert(Node->getOperand(i).isUndef());
19780b57cec5SDimitry Andric         Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
19790b57cec5SDimitry Andric         CV.push_back(UndefValue::get(OpNTy));
19800b57cec5SDimitry Andric       }
19810b57cec5SDimitry Andric     }
19820b57cec5SDimitry Andric     Constant *CP = ConstantVector::get(CV);
19830b57cec5SDimitry Andric     SDValue CPIdx =
19840b57cec5SDimitry Andric         DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
19855ffd83dbSDimitry Andric     Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
19860b57cec5SDimitry Andric     return DAG.getLoad(
19870b57cec5SDimitry Andric         VT, dl, DAG.getEntryNode(), CPIdx,
19880b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
19890b57cec5SDimitry Andric         Alignment);
19900b57cec5SDimitry Andric   }
19910b57cec5SDimitry Andric 
19920b57cec5SDimitry Andric   SmallSet<SDValue, 16> DefinedValues;
19930b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
19940b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef())
19950b57cec5SDimitry Andric       continue;
19960b57cec5SDimitry Andric     DefinedValues.insert(Node->getOperand(i));
19970b57cec5SDimitry Andric   }
19980b57cec5SDimitry Andric 
19990b57cec5SDimitry Andric   if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
20000b57cec5SDimitry Andric     if (!MoreThanTwoValues) {
20010b57cec5SDimitry Andric       SmallVector<int, 8> ShuffleVec(NumElems, -1);
20020b57cec5SDimitry Andric       for (unsigned i = 0; i < NumElems; ++i) {
20030b57cec5SDimitry Andric         SDValue V = Node->getOperand(i);
20040b57cec5SDimitry Andric         if (V.isUndef())
20050b57cec5SDimitry Andric           continue;
20060b57cec5SDimitry Andric         ShuffleVec[i] = V == Value1 ? 0 : NumElems;
20070b57cec5SDimitry Andric       }
20080b57cec5SDimitry Andric       if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
20090b57cec5SDimitry Andric         // Get the splatted value into the low element of a vector register.
20100b57cec5SDimitry Andric         SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
20110b57cec5SDimitry Andric         SDValue Vec2;
20120b57cec5SDimitry Andric         if (Value2.getNode())
20130b57cec5SDimitry Andric           Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
20140b57cec5SDimitry Andric         else
20150b57cec5SDimitry Andric           Vec2 = DAG.getUNDEF(VT);
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric         // Return shuffle(LowValVec, undef, <0,0,0,0>)
20180b57cec5SDimitry Andric         return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
20190b57cec5SDimitry Andric       }
20200b57cec5SDimitry Andric     } else {
20210b57cec5SDimitry Andric       SDValue Res;
20220b57cec5SDimitry Andric       if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
20230b57cec5SDimitry Andric         return Res;
20240b57cec5SDimitry Andric     }
20250b57cec5SDimitry Andric   }
20260b57cec5SDimitry Andric 
20270b57cec5SDimitry Andric   // Otherwise, we can't handle this case efficiently.
20280b57cec5SDimitry Andric   return ExpandVectorBuildThroughStack(Node);
20290b57cec5SDimitry Andric }
20300b57cec5SDimitry Andric 
20318bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
20328bcb0991SDimitry Andric   SDLoc DL(Node);
20338bcb0991SDimitry Andric   EVT VT = Node->getValueType(0);
20348bcb0991SDimitry Andric   SDValue SplatVal = Node->getOperand(0);
20358bcb0991SDimitry Andric 
20368bcb0991SDimitry Andric   return DAG.getSplatBuildVector(VT, DL, SplatVal);
20378bcb0991SDimitry Andric }
20388bcb0991SDimitry Andric 
203906c3fb27SDimitry Andric // Expand a node into a call to a libcall, returning the value as the first
204006c3fb27SDimitry Andric // result and the chain as the second.  If the result value does not fit into a
204106c3fb27SDimitry Andric // register, return the lo part and set the hi part to the by-reg argument in
204206c3fb27SDimitry Andric // the first.  If it does fit into a single register, return the result and
204306c3fb27SDimitry Andric // leave the Hi part unset.
204406c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
204506c3fb27SDimitry Andric                                             TargetLowering::ArgListTy &&Args,
20460b57cec5SDimitry Andric                                             bool isSigned) {
20470b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
20480b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
20490b57cec5SDimitry Andric 
20500b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
20510b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric   // By default, the input chain to this libcall is the entry node of the
20540b57cec5SDimitry Andric   // function. If the libcall is going to be emitted as a tail call then
20550b57cec5SDimitry Andric   // TLI.isUsedByReturnOnly will change it to the right chain if the return
20560b57cec5SDimitry Andric   // node which is being folded has a non-entry input chain.
20570b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
20580b57cec5SDimitry Andric 
20590b57cec5SDimitry Andric   // isTailCall may be true since the callee does not reference caller stack
20600b57cec5SDimitry Andric   // frame. Check if it's in the right position and that the return types match.
20610b57cec5SDimitry Andric   SDValue TCChain = InChain;
20620b57cec5SDimitry Andric   const Function &F = DAG.getMachineFunction().getFunction();
20630b57cec5SDimitry Andric   bool isTailCall =
20640b57cec5SDimitry Andric       TLI.isInTailCallPosition(DAG, Node, TCChain) &&
20650b57cec5SDimitry Andric       (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
20660b57cec5SDimitry Andric   if (isTailCall)
20670b57cec5SDimitry Andric     InChain = TCChain;
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
20700b57cec5SDimitry Andric   bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned);
20710b57cec5SDimitry Andric   CLI.setDebugLoc(SDLoc(Node))
20720b57cec5SDimitry Andric       .setChain(InChain)
20730b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
20740b57cec5SDimitry Andric                     std::move(Args))
20750b57cec5SDimitry Andric       .setTailCall(isTailCall)
20760b57cec5SDimitry Andric       .setSExtResult(signExtend)
20770b57cec5SDimitry Andric       .setZExtResult(!signExtend)
20780b57cec5SDimitry Andric       .setIsPostTypeLegalization(true);
20790b57cec5SDimitry Andric 
20800b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
20810b57cec5SDimitry Andric 
20820b57cec5SDimitry Andric   if (!CallInfo.second.getNode()) {
20838bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
20840b57cec5SDimitry Andric     // It's a tailcall, return the chain (which is the DAG root).
208506c3fb27SDimitry Andric     return {DAG.getRoot(), DAG.getRoot()};
20860b57cec5SDimitry Andric   }
20870b57cec5SDimitry Andric 
20888bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
208906c3fb27SDimitry Andric   return CallInfo;
209006c3fb27SDimitry Andric }
209106c3fb27SDimitry Andric 
209206c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
209306c3fb27SDimitry Andric                                             bool isSigned) {
209406c3fb27SDimitry Andric   TargetLowering::ArgListTy Args;
209506c3fb27SDimitry Andric   TargetLowering::ArgListEntry Entry;
209606c3fb27SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
209706c3fb27SDimitry Andric     EVT ArgVT = Op.getValueType();
209806c3fb27SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
209906c3fb27SDimitry Andric     Entry.Node = Op;
210006c3fb27SDimitry Andric     Entry.Ty = ArgTy;
210106c3fb27SDimitry Andric     Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned);
210206c3fb27SDimitry Andric     Entry.IsZExt = !Entry.IsSExt;
210306c3fb27SDimitry Andric     Args.push_back(Entry);
210406c3fb27SDimitry Andric   }
210506c3fb27SDimitry Andric 
210606c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, std::move(Args), isSigned);
210706c3fb27SDimitry Andric }
210806c3fb27SDimitry Andric 
210906c3fb27SDimitry Andric void SelectionDAGLegalize::ExpandFrexpLibCall(
211006c3fb27SDimitry Andric     SDNode *Node, SmallVectorImpl<SDValue> &Results) {
211106c3fb27SDimitry Andric   SDLoc dl(Node);
211206c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
211306c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
211406c3fb27SDimitry Andric 
211506c3fb27SDimitry Andric   SDValue FPOp = Node->getOperand(0);
211606c3fb27SDimitry Andric 
211706c3fb27SDimitry Andric   EVT ArgVT = FPOp.getValueType();
211806c3fb27SDimitry Andric   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
211906c3fb27SDimitry Andric 
212006c3fb27SDimitry Andric   TargetLowering::ArgListEntry FPArgEntry;
212106c3fb27SDimitry Andric   FPArgEntry.Node = FPOp;
212206c3fb27SDimitry Andric   FPArgEntry.Ty = ArgTy;
212306c3fb27SDimitry Andric 
212406c3fb27SDimitry Andric   SDValue StackSlot = DAG.CreateStackTemporary(ExpVT);
212506c3fb27SDimitry Andric   TargetLowering::ArgListEntry PtrArgEntry;
212606c3fb27SDimitry Andric   PtrArgEntry.Node = StackSlot;
212706c3fb27SDimitry Andric   PtrArgEntry.Ty = PointerType::get(*DAG.getContext(),
212806c3fb27SDimitry Andric                                     DAG.getDataLayout().getAllocaAddrSpace());
212906c3fb27SDimitry Andric 
213006c3fb27SDimitry Andric   TargetLowering::ArgListTy Args = {FPArgEntry, PtrArgEntry};
213106c3fb27SDimitry Andric 
213206c3fb27SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFREXP(VT);
213306c3fb27SDimitry Andric   auto [Call, Chain] = ExpandLibCall(LC, Node, std::move(Args), false);
213406c3fb27SDimitry Andric 
213506c3fb27SDimitry Andric   // FIXME: Get type of int for libcall declaration and cast
213606c3fb27SDimitry Andric 
213706c3fb27SDimitry Andric   int FrameIdx = cast<FrameIndexSDNode>(StackSlot)->getIndex();
213806c3fb27SDimitry Andric   auto PtrInfo =
213906c3fb27SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
214006c3fb27SDimitry Andric 
214106c3fb27SDimitry Andric   SDValue LoadExp = DAG.getLoad(ExpVT, dl, Chain, StackSlot, PtrInfo);
214206c3fb27SDimitry Andric   SDValue OutputChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
214306c3fb27SDimitry Andric                                     LoadExp.getValue(1), DAG.getRoot());
214406c3fb27SDimitry Andric   DAG.setRoot(OutputChain);
214506c3fb27SDimitry Andric 
214606c3fb27SDimitry Andric   Results.push_back(Call);
214706c3fb27SDimitry Andric   Results.push_back(LoadExp);
21480b57cec5SDimitry Andric }
21490b57cec5SDimitry Andric 
2150480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2151fe6060f1SDimitry Andric                                            RTLIB::Libcall LC,
2152480093f4SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2153fe6060f1SDimitry Andric   if (LC == RTLIB::UNKNOWN_LIBCALL)
2154fe6060f1SDimitry Andric     llvm_unreachable("Can't create an unknown libcall!");
2155480093f4SDimitry Andric 
2156480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2157480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
2158e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops(drop_begin(Node->ops()));
2159480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
2160480093f4SDimitry Andric     // FIXME: This doesn't support tail calls.
2161480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2162480093f4SDimitry Andric                                                       Ops, CallOptions,
2163480093f4SDimitry Andric                                                       SDLoc(Node),
2164480093f4SDimitry Andric                                                       Node->getOperand(0));
2165480093f4SDimitry Andric     Results.push_back(Tmp.first);
2166480093f4SDimitry Andric     Results.push_back(Tmp.second);
2167480093f4SDimitry Andric   } else {
216806c3fb27SDimitry Andric     SDValue Tmp = ExpandLibCall(LC, Node, false).first;
2169480093f4SDimitry Andric     Results.push_back(Tmp);
2170480093f4SDimitry Andric   }
21710b57cec5SDimitry Andric }
21720b57cec5SDimitry Andric 
2173fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type.
2174fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2175fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F32,
2176fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F64,
2177fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F80,
2178fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F128,
2179fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_PPCF128,
2180fe6060f1SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2181fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2182fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2183fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2184fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
2185fe6060f1SDimitry Andric }
2186fe6060f1SDimitry Andric 
2187bdd1243dSDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2188bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I8,
2189bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I16,
2190bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I32,
2191bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I64,
2192bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I128) {
21930b57cec5SDimitry Andric   RTLIB::Libcall LC;
21940b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2195bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
21960b57cec5SDimitry Andric   case MVT::i8:   LC = Call_I8; break;
21970b57cec5SDimitry Andric   case MVT::i16:  LC = Call_I16; break;
21980b57cec5SDimitry Andric   case MVT::i32:  LC = Call_I32; break;
21990b57cec5SDimitry Andric   case MVT::i64:  LC = Call_I64; break;
22000b57cec5SDimitry Andric   case MVT::i128: LC = Call_I128; break;
22010b57cec5SDimitry Andric   }
220206c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, isSigned).first;
22030b57cec5SDimitry Andric }
22040b57cec5SDimitry Andric 
22050b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance
22060b57cec5SDimitry Andric /// lround and its variant).
2207480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
22080b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F32,
22090b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F64,
22100b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F80,
22110b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F128,
2212480093f4SDimitry Andric                                             RTLIB::Libcall Call_PPCF128,
2213480093f4SDimitry Andric                                             SmallVectorImpl<SDValue> &Results) {
2214480093f4SDimitry Andric   EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2215fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(),
2216fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2217fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2218fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
22190b57cec5SDimitry Andric }
22200b57cec5SDimitry Andric 
22210b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs.
22220b57cec5SDimitry Andric void
22230b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
22240b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
22250b57cec5SDimitry Andric   unsigned Opcode = Node->getOpcode();
22260b57cec5SDimitry Andric   bool isSigned = Opcode == ISD::SDIVREM;
22270b57cec5SDimitry Andric 
22280b57cec5SDimitry Andric   RTLIB::Libcall LC;
22290b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2230bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22310b57cec5SDimitry Andric   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
22320b57cec5SDimitry Andric   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
22330b57cec5SDimitry Andric   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
22340b57cec5SDimitry Andric   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
22350b57cec5SDimitry Andric   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
22360b57cec5SDimitry Andric   }
22370b57cec5SDimitry Andric 
22380b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
22390b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
22400b57cec5SDimitry Andric   // dependence.
22410b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
22420b57cec5SDimitry Andric 
22430b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
22440b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
22450b57cec5SDimitry Andric 
22460b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
22470b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
22480b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
22490b57cec5SDimitry Andric     EVT ArgVT = Op.getValueType();
22500b57cec5SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
22510b57cec5SDimitry Andric     Entry.Node = Op;
22520b57cec5SDimitry Andric     Entry.Ty = ArgTy;
22530b57cec5SDimitry Andric     Entry.IsSExt = isSigned;
22540b57cec5SDimitry Andric     Entry.IsZExt = !isSigned;
22550b57cec5SDimitry Andric     Args.push_back(Entry);
22560b57cec5SDimitry Andric   }
22570b57cec5SDimitry Andric 
22580b57cec5SDimitry Andric   // Also pass the return address of the remainder.
22590b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
22600b57cec5SDimitry Andric   Entry.Node = FIPtr;
2261*5f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
22620b57cec5SDimitry Andric   Entry.IsSExt = isSigned;
22630b57cec5SDimitry Andric   Entry.IsZExt = !isSigned;
22640b57cec5SDimitry Andric   Args.push_back(Entry);
22650b57cec5SDimitry Andric 
22660b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
22670b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
22680b57cec5SDimitry Andric 
22690b57cec5SDimitry Andric   SDLoc dl(Node);
22700b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
22710b57cec5SDimitry Andric   CLI.setDebugLoc(dl)
22720b57cec5SDimitry Andric       .setChain(InChain)
22730b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
22740b57cec5SDimitry Andric                     std::move(Args))
22750b57cec5SDimitry Andric       .setSExtResult(isSigned)
22760b57cec5SDimitry Andric       .setZExtResult(!isSigned);
22770b57cec5SDimitry Andric 
22780b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
22790b57cec5SDimitry Andric 
22800b57cec5SDimitry Andric   // Remainder is loaded back from the stack frame.
22810b57cec5SDimitry Andric   SDValue Rem =
22820b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
22830b57cec5SDimitry Andric   Results.push_back(CallInfo.first);
22840b57cec5SDimitry Andric   Results.push_back(Rem);
22850b57cec5SDimitry Andric }
22860b57cec5SDimitry Andric 
22870b57cec5SDimitry Andric /// Return true if sincos libcall is available.
22880b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
22890b57cec5SDimitry Andric   RTLIB::Libcall LC;
22900b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
22910b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22920b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
22930b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
22940b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
22950b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
22960b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
22970b57cec5SDimitry Andric   }
22980b57cec5SDimitry Andric   return TLI.getLibcallName(LC) != nullptr;
22990b57cec5SDimitry Andric }
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed.
23020b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) {
23030b57cec5SDimitry Andric   unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
23040b57cec5SDimitry Andric     ? ISD::FCOS : ISD::FSIN;
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric   SDValue Op0 = Node->getOperand(0);
2307349cc55cSDimitry Andric   for (const SDNode *User : Op0.getNode()->uses()) {
23080b57cec5SDimitry Andric     if (User == Node)
23090b57cec5SDimitry Andric       continue;
23100b57cec5SDimitry Andric     // The other user might have been turned into sincos already.
23110b57cec5SDimitry Andric     if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
23120b57cec5SDimitry Andric       return true;
23130b57cec5SDimitry Andric   }
23140b57cec5SDimitry Andric   return false;
23150b57cec5SDimitry Andric }
23160b57cec5SDimitry Andric 
23170b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs.
23180b57cec5SDimitry Andric void
23190b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
23200b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
23210b57cec5SDimitry Andric   RTLIB::Libcall LC;
23220b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
23230b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
23240b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
23250b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
23260b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
23270b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
23280b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
23290b57cec5SDimitry Andric   }
23300b57cec5SDimitry Andric 
23310b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
23320b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
23330b57cec5SDimitry Andric   // dependence.
23340b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
23350b57cec5SDimitry Andric 
23360b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
23370b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
23380b57cec5SDimitry Andric 
23390b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
23400b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
23410b57cec5SDimitry Andric 
23420b57cec5SDimitry Andric   // Pass the argument.
23430b57cec5SDimitry Andric   Entry.Node = Node->getOperand(0);
23440b57cec5SDimitry Andric   Entry.Ty = RetTy;
23450b57cec5SDimitry Andric   Entry.IsSExt = false;
23460b57cec5SDimitry Andric   Entry.IsZExt = false;
23470b57cec5SDimitry Andric   Args.push_back(Entry);
23480b57cec5SDimitry Andric 
23490b57cec5SDimitry Andric   // Pass the return address of sin.
23500b57cec5SDimitry Andric   SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
23510b57cec5SDimitry Andric   Entry.Node = SinPtr;
2352*5f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
23530b57cec5SDimitry Andric   Entry.IsSExt = false;
23540b57cec5SDimitry Andric   Entry.IsZExt = false;
23550b57cec5SDimitry Andric   Args.push_back(Entry);
23560b57cec5SDimitry Andric 
23570b57cec5SDimitry Andric   // Also pass the return address of the cos.
23580b57cec5SDimitry Andric   SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
23590b57cec5SDimitry Andric   Entry.Node = CosPtr;
2360*5f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
23610b57cec5SDimitry Andric   Entry.IsSExt = false;
23620b57cec5SDimitry Andric   Entry.IsZExt = false;
23630b57cec5SDimitry Andric   Args.push_back(Entry);
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
23660b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
23670b57cec5SDimitry Andric 
23680b57cec5SDimitry Andric   SDLoc dl(Node);
23690b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
23700b57cec5SDimitry Andric   CLI.setDebugLoc(dl).setChain(InChain).setLibCallee(
23710b57cec5SDimitry Andric       TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee,
23720b57cec5SDimitry Andric       std::move(Args));
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
23750b57cec5SDimitry Andric 
23760b57cec5SDimitry Andric   Results.push_back(
23770b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo()));
23780b57cec5SDimitry Andric   Results.push_back(
23790b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo()));
23800b57cec5SDimitry Andric }
23810b57cec5SDimitry Andric 
238206c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
238306c3fb27SDimitry Andric   SDLoc dl(Node);
238406c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
238506c3fb27SDimitry Andric   SDValue X = Node->getOperand(0);
238606c3fb27SDimitry Andric   SDValue N = Node->getOperand(1);
238706c3fb27SDimitry Andric   EVT ExpVT = N.getValueType();
238806c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
238906c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
239006c3fb27SDimitry Andric     return SDValue();
239106c3fb27SDimitry Andric 
239206c3fb27SDimitry Andric   if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
239306c3fb27SDimitry Andric     return SDValue();
239406c3fb27SDimitry Andric 
239506c3fb27SDimitry Andric   SDNodeFlags NSW;
239606c3fb27SDimitry Andric   NSW.setNoSignedWrap(true);
239706c3fb27SDimitry Andric   SDNodeFlags NUW_NSW;
239806c3fb27SDimitry Andric   NUW_NSW.setNoUnsignedWrap(true);
239906c3fb27SDimitry Andric   NUW_NSW.setNoSignedWrap(true);
240006c3fb27SDimitry Andric 
240106c3fb27SDimitry Andric   EVT SetCCVT =
240206c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
240306c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
240406c3fb27SDimitry Andric 
240506c3fb27SDimitry Andric   const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
240606c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
240706c3fb27SDimitry Andric   const int Precision = APFloat::semanticsPrecision(FltSem);
240806c3fb27SDimitry Andric 
240906c3fb27SDimitry Andric   const SDValue MaxExp = DAG.getConstant(MaxExpVal, dl, ExpVT);
241006c3fb27SDimitry Andric   const SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
241106c3fb27SDimitry Andric 
241206c3fb27SDimitry Andric   const SDValue DoubleMaxExp = DAG.getConstant(2 * MaxExpVal, dl, ExpVT);
241306c3fb27SDimitry Andric 
241406c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
241506c3fb27SDimitry Andric   APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
241606c3fb27SDimitry Andric 
241706c3fb27SDimitry Andric   // Offset by precision to avoid denormal range.
241806c3fb27SDimitry Andric   APFloat ScaleDownK =
241906c3fb27SDimitry Andric       scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
242006c3fb27SDimitry Andric 
242106c3fb27SDimitry Andric   // TODO: Should really introduce control flow and use a block for the >
242206c3fb27SDimitry Andric   // MaxExp, < MinExp cases
242306c3fb27SDimitry Andric 
242406c3fb27SDimitry Andric   // First, handle exponents Exp > MaxExp and scale down.
242506c3fb27SDimitry Andric   SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
242606c3fb27SDimitry Andric 
242706c3fb27SDimitry Andric   SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
242806c3fb27SDimitry Andric   SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
242906c3fb27SDimitry Andric   SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
243006c3fb27SDimitry Andric   SDValue DecN1 =
243106c3fb27SDimitry Andric       DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
243206c3fb27SDimitry Andric 
243306c3fb27SDimitry Andric   SDValue ScaleUpTwice =
243406c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
243506c3fb27SDimitry Andric 
243606c3fb27SDimitry Andric   const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
243706c3fb27SDimitry Andric   SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
243806c3fb27SDimitry Andric   SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
243906c3fb27SDimitry Andric 
244006c3fb27SDimitry Andric   SDValue SelectN_Big =
244106c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
244206c3fb27SDimitry Andric   SDValue SelectX_Big =
244306c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
244406c3fb27SDimitry Andric 
244506c3fb27SDimitry Andric   // Now handle exponents Exp < MinExp
244606c3fb27SDimitry Andric   SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
244706c3fb27SDimitry Andric 
244806c3fb27SDimitry Andric   SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
244906c3fb27SDimitry Andric   SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
245006c3fb27SDimitry Andric 
245106c3fb27SDimitry Andric   SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
245206c3fb27SDimitry Andric 
245306c3fb27SDimitry Andric   SDValue ClampMinVal =
245406c3fb27SDimitry Andric       DAG.getConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
245506c3fb27SDimitry Andric   SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
245606c3fb27SDimitry Andric   SDValue IncN1 =
245706c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
245806c3fb27SDimitry Andric 
245906c3fb27SDimitry Andric   const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
246006c3fb27SDimitry Andric   SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
246106c3fb27SDimitry Andric   SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
246206c3fb27SDimitry Andric 
246306c3fb27SDimitry Andric   SDValue ScaleDownTwice = DAG.getSetCC(
246406c3fb27SDimitry Andric       dl, SetCCVT, N, DAG.getConstant(2 * MinExpVal + Precision, dl, ExpVT),
246506c3fb27SDimitry Andric       ISD::SETULT);
246606c3fb27SDimitry Andric 
246706c3fb27SDimitry Andric   SDValue SelectN_Small =
246806c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
246906c3fb27SDimitry Andric   SDValue SelectX_Small =
247006c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
247106c3fb27SDimitry Andric 
247206c3fb27SDimitry Andric   // Now combine the two out of range exponent handling cases with the base
247306c3fb27SDimitry Andric   // case.
247406c3fb27SDimitry Andric   SDValue NewX = DAG.getNode(
247506c3fb27SDimitry Andric       ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
247606c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
247706c3fb27SDimitry Andric 
247806c3fb27SDimitry Andric   SDValue NewN = DAG.getNode(
247906c3fb27SDimitry Andric       ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
248006c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
248106c3fb27SDimitry Andric 
248206c3fb27SDimitry Andric   SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
248306c3fb27SDimitry Andric 
248406c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
248506c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
248606c3fb27SDimitry Andric   SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
248706c3fb27SDimitry Andric 
248806c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
248906c3fb27SDimitry Andric                               ExponentShiftAmt, NUW_NSW);
249006c3fb27SDimitry Andric   SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
249106c3fb27SDimitry Andric   return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
249206c3fb27SDimitry Andric }
249306c3fb27SDimitry Andric 
249406c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
249506c3fb27SDimitry Andric   SDLoc dl(Node);
249606c3fb27SDimitry Andric   SDValue Val = Node->getOperand(0);
249706c3fb27SDimitry Andric   EVT VT = Val.getValueType();
249806c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
249906c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
250006c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
250106c3fb27SDimitry Andric     return SDValue();
250206c3fb27SDimitry Andric 
250306c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
250406c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
250506c3fb27SDimitry Andric   const unsigned Precision = APFloat::semanticsPrecision(FltSem);
250606c3fb27SDimitry Andric   const unsigned BitSize = VT.getScalarSizeInBits();
250706c3fb27SDimitry Andric 
250806c3fb27SDimitry Andric   // TODO: Could introduce control flow and skip over the denormal handling.
250906c3fb27SDimitry Andric 
251006c3fb27SDimitry Andric   // scale_up = fmul value, scalbn(1.0, precision + 1)
251106c3fb27SDimitry Andric   // extracted_exp = (bitcast value to uint) >> precision - 1
251206c3fb27SDimitry Andric   // biased_exp = extracted_exp + min_exp
251306c3fb27SDimitry Andric   // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
251406c3fb27SDimitry Andric   //
251506c3fb27SDimitry Andric   // is_denormal = val < smallest_normalized
251606c3fb27SDimitry Andric   // computed_fract = is_denormal ? scale_up : extracted_fract
251706c3fb27SDimitry Andric   // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
251806c3fb27SDimitry Andric   //
251906c3fb27SDimitry Andric   // result_0 =  (!isfinite(val) || iszero(val)) ? val : computed_fract
252006c3fb27SDimitry Andric   // result_1 =  (!isfinite(val) || iszero(val)) ? 0 : computed_exp
252106c3fb27SDimitry Andric 
252206c3fb27SDimitry Andric   SDValue NegSmallestNormalizedInt = DAG.getConstant(
252306c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
252406c3fb27SDimitry Andric       AsIntVT);
252506c3fb27SDimitry Andric 
252606c3fb27SDimitry Andric   SDValue SmallestNormalizedInt = DAG.getConstant(
252706c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
252806c3fb27SDimitry Andric       AsIntVT);
252906c3fb27SDimitry Andric 
253006c3fb27SDimitry Andric   // Masks out the exponent bits.
253106c3fb27SDimitry Andric   SDValue ExpMask =
253206c3fb27SDimitry Andric       DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
253306c3fb27SDimitry Andric 
253406c3fb27SDimitry Andric   // Mask out the exponent part of the value.
253506c3fb27SDimitry Andric   //
253606c3fb27SDimitry Andric   // e.g, for f32 FractSignMaskVal = 0x807fffff
253706c3fb27SDimitry Andric   APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
253806c3fb27SDimitry Andric   FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
253906c3fb27SDimitry Andric 
254006c3fb27SDimitry Andric   APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
254106c3fb27SDimitry Andric   SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
254206c3fb27SDimitry Andric 
254306c3fb27SDimitry Andric   SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
254406c3fb27SDimitry Andric 
254506c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
254606c3fb27SDimitry Andric   // Scale a possible denormal input.
254706c3fb27SDimitry Andric   // e.g., for f64, 0x1p+54
254806c3fb27SDimitry Andric   APFloat ScaleUpKVal =
254906c3fb27SDimitry Andric       scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
255006c3fb27SDimitry Andric 
255106c3fb27SDimitry Andric   SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
255206c3fb27SDimitry Andric   SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
255306c3fb27SDimitry Andric 
255406c3fb27SDimitry Andric   EVT SetCCVT =
255506c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
255606c3fb27SDimitry Andric 
255706c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
255806c3fb27SDimitry Andric 
255906c3fb27SDimitry Andric   SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
256006c3fb27SDimitry Andric 
256106c3fb27SDimitry Andric   SDValue AddNegSmallestNormal =
256206c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
256306c3fb27SDimitry Andric   SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
256406c3fb27SDimitry Andric                                       NegSmallestNormalizedInt, ISD::SETULE);
256506c3fb27SDimitry Andric 
256606c3fb27SDimitry Andric   SDValue IsDenormal =
256706c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
256806c3fb27SDimitry Andric 
256906c3fb27SDimitry Andric   SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
257006c3fb27SDimitry Andric   SDValue Zero = DAG.getConstant(0, dl, ExpVT);
257106c3fb27SDimitry Andric 
257206c3fb27SDimitry Andric   SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
257306c3fb27SDimitry Andric   SDValue ScaledSelect =
257406c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
257506c3fb27SDimitry Andric 
257606c3fb27SDimitry Andric   SDValue ExpMaskScaled =
257706c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
257806c3fb27SDimitry Andric 
257906c3fb27SDimitry Andric   SDValue ScaledValue =
258006c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
258106c3fb27SDimitry Andric 
258206c3fb27SDimitry Andric   // Extract the exponent bits.
258306c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
258406c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
258506c3fb27SDimitry Andric   SDValue ShiftedExp =
258606c3fb27SDimitry Andric       DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
258706c3fb27SDimitry Andric   SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
258806c3fb27SDimitry Andric 
258906c3fb27SDimitry Andric   SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
259006c3fb27SDimitry Andric   SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
259106c3fb27SDimitry Andric   SDValue DenormalExpBias =
259206c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
259306c3fb27SDimitry Andric 
259406c3fb27SDimitry Andric   SDValue MaskedFractAsInt =
259506c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
259606c3fb27SDimitry Andric   const APFloat Half(FltSem, "0.5");
259706c3fb27SDimitry Andric   SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
259806c3fb27SDimitry Andric   SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
259906c3fb27SDimitry Andric   SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
260006c3fb27SDimitry Andric 
260106c3fb27SDimitry Andric   SDValue ComputedExp =
260206c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
260306c3fb27SDimitry Andric 
260406c3fb27SDimitry Andric   SDValue Result0 =
260506c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
260606c3fb27SDimitry Andric 
260706c3fb27SDimitry Andric   SDValue Result1 =
260806c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
260906c3fb27SDimitry Andric 
261006c3fb27SDimitry Andric   return DAG.getMergeValues({Result0, Result1}, dl);
261106c3fb27SDimitry Andric }
261206c3fb27SDimitry Andric 
26130b57cec5SDimitry Andric /// This function is responsible for legalizing a
26140b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that
26150b57cec5SDimitry Andric /// we expand it.  At this point, we know that the result and operand types are
26160b57cec5SDimitry Andric /// legal for the target.
2617480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2618480093f4SDimitry Andric                                                    SDValue &Chain) {
2619480093f4SDimitry Andric   bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2620480093f4SDimitry Andric                    Node->getOpcode() == ISD::SINT_TO_FP);
2621480093f4SDimitry Andric   EVT DestVT = Node->getValueType(0);
2622480093f4SDimitry Andric   SDLoc dl(Node);
2623480093f4SDimitry Andric   unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2624480093f4SDimitry Andric   SDValue Op0 = Node->getOperand(OpNo);
26250b57cec5SDimitry Andric   EVT SrcVT = Op0.getValueType();
26260b57cec5SDimitry Andric 
26270b57cec5SDimitry Andric   // TODO: Should any fast-math-flags be set for the created nodes?
26280b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2629e8d8bef9SDimitry Andric   if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2630e8d8bef9SDimitry Andric       (DestVT.bitsLE(MVT::f64) ||
2631e8d8bef9SDimitry Andric        TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2632e8d8bef9SDimitry Andric                                                      : ISD::FP_EXTEND,
2633e8d8bef9SDimitry Andric                             DestVT))) {
26340b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
26350b57cec5SDimitry Andric                          "expansion\n");
26360b57cec5SDimitry Andric 
26370b57cec5SDimitry Andric     // Get the stack frame index of a 8 byte buffer.
26380b57cec5SDimitry Andric     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
26390b57cec5SDimitry Andric 
26405ffd83dbSDimitry Andric     SDValue Lo = Op0;
26410b57cec5SDimitry Andric     // if signed map to unsigned space
26420b57cec5SDimitry Andric     if (isSigned) {
26435ffd83dbSDimitry Andric       // Invert sign bit (signed to unsigned mapping).
26445ffd83dbSDimitry Andric       Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
26455ffd83dbSDimitry Andric                        DAG.getConstant(0x80000000u, dl, MVT::i32));
26460b57cec5SDimitry Andric     }
26475ffd83dbSDimitry Andric     // Initial hi portion of constructed double.
26485ffd83dbSDimitry Andric     SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
26495ffd83dbSDimitry Andric 
26505ffd83dbSDimitry Andric     // If this a big endian target, swap the lo and high data.
26515ffd83dbSDimitry Andric     if (DAG.getDataLayout().isBigEndian())
26525ffd83dbSDimitry Andric       std::swap(Lo, Hi);
26535ffd83dbSDimitry Andric 
26545ffd83dbSDimitry Andric     SDValue MemChain = DAG.getEntryNode();
26555ffd83dbSDimitry Andric 
26565ffd83dbSDimitry Andric     // Store the lo of the constructed double.
26575ffd83dbSDimitry Andric     SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
26580b57cec5SDimitry Andric                                   MachinePointerInfo());
26595ffd83dbSDimitry Andric     // Store the hi of the constructed double.
2660*5f757f3fSDimitry Andric     SDValue HiPtr =
2661*5f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
26620b57cec5SDimitry Andric     SDValue Store2 =
26635ffd83dbSDimitry Andric         DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
26645ffd83dbSDimitry Andric     MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
26655ffd83dbSDimitry Andric 
26660b57cec5SDimitry Andric     // load the constructed double
26670b57cec5SDimitry Andric     SDValue Load =
26685ffd83dbSDimitry Andric         DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
26690b57cec5SDimitry Andric     // FP constant to bias correct the final result
267006c3fb27SDimitry Andric     SDValue Bias = DAG.getConstantFP(
267106c3fb27SDimitry Andric         isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
267206c3fb27SDimitry Andric                  : llvm::bit_cast<double>(0x4330000000000000ULL),
26730b57cec5SDimitry Andric         dl, MVT::f64);
2674480093f4SDimitry Andric     // Subtract the bias and get the final result.
2675480093f4SDimitry Andric     SDValue Sub;
2676480093f4SDimitry Andric     SDValue Result;
2677480093f4SDimitry Andric     if (Node->isStrictFPOpcode()) {
2678480093f4SDimitry Andric       Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2679480093f4SDimitry Andric                         {Node->getOperand(0), Load, Bias});
2680480093f4SDimitry Andric       Chain = Sub.getValue(1);
2681480093f4SDimitry Andric       if (DestVT != Sub.getValueType()) {
2682480093f4SDimitry Andric         std::pair<SDValue, SDValue> ResultPair;
2683480093f4SDimitry Andric         ResultPair =
2684480093f4SDimitry Andric             DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2685480093f4SDimitry Andric         Result = ResultPair.first;
2686480093f4SDimitry Andric         Chain = ResultPair.second;
2687480093f4SDimitry Andric       }
2688480093f4SDimitry Andric       else
2689480093f4SDimitry Andric         Result = Sub;
2690480093f4SDimitry Andric     } else {
2691480093f4SDimitry Andric       Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2692480093f4SDimitry Andric       Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2693480093f4SDimitry Andric     }
26940b57cec5SDimitry Andric     return Result;
26950b57cec5SDimitry Andric   }
2696e8d8bef9SDimitry Andric 
2697e8d8bef9SDimitry Andric   if (isSigned)
2698e8d8bef9SDimitry Andric     return SDValue();
26995ffd83dbSDimitry Andric 
27005ffd83dbSDimitry Andric   // TODO: Generalize this for use with other types.
2701e8d8bef9SDimitry Andric   if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2702e8d8bef9SDimitry Andric       (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2703e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
27045ffd83dbSDimitry Andric     // For unsigned conversions, convert them to signed conversions using the
27055ffd83dbSDimitry Andric     // algorithm from the x86_64 __floatundisf in compiler_rt. That method
27065ffd83dbSDimitry Andric     // should be valid for i32->f32 as well.
27075ffd83dbSDimitry Andric 
2708e8d8bef9SDimitry Andric     // More generally this transform should be valid if there are 3 more bits
2709e8d8bef9SDimitry Andric     // in the integer type than the significand. Rounding uses the first bit
2710e8d8bef9SDimitry Andric     // after the width of the significand and the OR of all bits after that. So
2711e8d8bef9SDimitry Andric     // we need to be able to OR the shifted out bit into one of the bits that
2712e8d8bef9SDimitry Andric     // participate in the OR.
2713e8d8bef9SDimitry Andric 
27145ffd83dbSDimitry Andric     // TODO: This really should be implemented using a branch rather than a
27155ffd83dbSDimitry Andric     // select.  We happen to get lucky and machinesink does the right
27165ffd83dbSDimitry Andric     // thing most of the time.  This would be a good candidate for a
27175ffd83dbSDimitry Andric     // pseudo-op, or, even better, for whole-function isel.
27185ffd83dbSDimitry Andric     EVT SetCCVT = getSetCCResultType(SrcVT);
27195ffd83dbSDimitry Andric 
27205ffd83dbSDimitry Andric     SDValue SignBitTest = DAG.getSetCC(
27215ffd83dbSDimitry Andric         dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
27225ffd83dbSDimitry Andric 
27235ffd83dbSDimitry Andric     EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout());
27245ffd83dbSDimitry Andric     SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
27255ffd83dbSDimitry Andric     SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
27265ffd83dbSDimitry Andric     SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
27275ffd83dbSDimitry Andric     SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
27285ffd83dbSDimitry Andric     SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
27295ffd83dbSDimitry Andric 
27305ffd83dbSDimitry Andric     SDValue Slow, Fast;
27315ffd83dbSDimitry Andric     if (Node->isStrictFPOpcode()) {
27325ffd83dbSDimitry Andric       // In strict mode, we must avoid spurious exceptions, and therefore
27335ffd83dbSDimitry Andric       // must make sure to only emit a single STRICT_SINT_TO_FP.
27345ffd83dbSDimitry Andric       SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
27355ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
27365ffd83dbSDimitry Andric                          { Node->getOperand(0), InCvt });
27375ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
27385ffd83dbSDimitry Andric                          { Fast.getValue(1), Fast, Fast });
27395ffd83dbSDimitry Andric       Chain = Slow.getValue(1);
27405ffd83dbSDimitry Andric       // The STRICT_SINT_TO_FP inherits the exception mode from the
27415ffd83dbSDimitry Andric       // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
27425ffd83dbSDimitry Andric       // never raise any exception.
27435ffd83dbSDimitry Andric       SDNodeFlags Flags;
27445ffd83dbSDimitry Andric       Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
27455ffd83dbSDimitry Andric       Fast->setFlags(Flags);
27465ffd83dbSDimitry Andric       Flags.setNoFPExcept(true);
27475ffd83dbSDimitry Andric       Slow->setFlags(Flags);
27485ffd83dbSDimitry Andric     } else {
27495ffd83dbSDimitry Andric       SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
27505ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
27515ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
27525ffd83dbSDimitry Andric     }
27535ffd83dbSDimitry Andric 
27545ffd83dbSDimitry Andric     return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
27555ffd83dbSDimitry Andric   }
27565ffd83dbSDimitry Andric 
2757e8d8bef9SDimitry Andric   // Don't expand it if there isn't cheap fadd.
2758e8d8bef9SDimitry Andric   if (!TLI.isOperationLegalOrCustom(
2759e8d8bef9SDimitry Andric           Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2760e8d8bef9SDimitry Andric     return SDValue();
2761e8d8bef9SDimitry Andric 
27625ffd83dbSDimitry Andric   // The following optimization is valid only if every value in SrcVT (when
27635ffd83dbSDimitry Andric   // treated as signed) is representable in DestVT.  Check that the mantissa
27645ffd83dbSDimitry Andric   // size of DestVT is >= than the number of bits in SrcVT -1.
27655ffd83dbSDimitry Andric   assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >=
27665ffd83dbSDimitry Andric              SrcVT.getSizeInBits() - 1 &&
27675ffd83dbSDimitry Andric          "Cannot perform lossless SINT_TO_FP!");
27680b57cec5SDimitry Andric 
2769480093f4SDimitry Andric   SDValue Tmp1;
2770480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2771480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2772480093f4SDimitry Andric                        { Node->getOperand(0), Op0 });
2773480093f4SDimitry Andric   } else
2774480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric   SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
27770b57cec5SDimitry Andric                                  DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
27780b57cec5SDimitry Andric   SDValue Zero = DAG.getIntPtrConstant(0, dl),
27790b57cec5SDimitry Andric           Four = DAG.getIntPtrConstant(4, dl);
27800b57cec5SDimitry Andric   SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
27810b57cec5SDimitry Andric                                     SignSet, Four, Zero);
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric   // If the sign bit of the integer is set, the large number will be treated
27840b57cec5SDimitry Andric   // as a negative number.  To counteract this, the dynamic code adds an
27850b57cec5SDimitry Andric   // offset depending on the data type.
27860b57cec5SDimitry Andric   uint64_t FF;
27870b57cec5SDimitry Andric   switch (SrcVT.getSimpleVT().SimpleTy) {
2788e8d8bef9SDimitry Andric   default:
2789e8d8bef9SDimitry Andric     return SDValue();
27900b57cec5SDimitry Andric   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
27910b57cec5SDimitry Andric   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
27920b57cec5SDimitry Andric   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
27930b57cec5SDimitry Andric   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
27940b57cec5SDimitry Andric   }
27950b57cec5SDimitry Andric   if (DAG.getDataLayout().isLittleEndian())
27960b57cec5SDimitry Andric     FF <<= 32;
27970b57cec5SDimitry Andric   Constant *FudgeFactor = ConstantInt::get(
27980b57cec5SDimitry Andric                                        Type::getInt64Ty(*DAG.getContext()), FF);
27990b57cec5SDimitry Andric 
28000b57cec5SDimitry Andric   SDValue CPIdx =
28010b57cec5SDimitry Andric       DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
28025ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
28030b57cec5SDimitry Andric   CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
28045ffd83dbSDimitry Andric   Alignment = commonAlignment(Alignment, 4);
28050b57cec5SDimitry Andric   SDValue FudgeInReg;
28060b57cec5SDimitry Andric   if (DestVT == MVT::f32)
28070b57cec5SDimitry Andric     FudgeInReg = DAG.getLoad(
28080b57cec5SDimitry Andric         MVT::f32, dl, DAG.getEntryNode(), CPIdx,
28090b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
28100b57cec5SDimitry Andric         Alignment);
28110b57cec5SDimitry Andric   else {
28120b57cec5SDimitry Andric     SDValue Load = DAG.getExtLoad(
28130b57cec5SDimitry Andric         ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
28140b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
28150b57cec5SDimitry Andric         Alignment);
28160b57cec5SDimitry Andric     HandleSDNode Handle(Load);
28170b57cec5SDimitry Andric     LegalizeOp(Load.getNode());
28180b57cec5SDimitry Andric     FudgeInReg = Handle.getValue();
28190b57cec5SDimitry Andric   }
28200b57cec5SDimitry Andric 
2821480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2822480093f4SDimitry Andric     SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2823480093f4SDimitry Andric                                  { Tmp1.getValue(1), Tmp1, FudgeInReg });
2824480093f4SDimitry Andric     Chain = Result.getValue(1);
2825480093f4SDimitry Andric     return Result;
2826480093f4SDimitry Andric   }
2827480093f4SDimitry Andric 
28280b57cec5SDimitry Andric   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
28290b57cec5SDimitry Andric }
28300b57cec5SDimitry Andric 
28310b57cec5SDimitry Andric /// This function is responsible for legalizing a
28320b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that
28330b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
28340b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
28350b57cec5SDimitry Andric /// operation that takes a larger input.
2836480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2837480093f4SDimitry Andric     SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) {
2838480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2839480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2840480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2841480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2842480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2843480093f4SDimitry Andric   unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2844480093f4SDimitry Andric   unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2845480093f4SDimitry Andric 
28460b57cec5SDimitry Andric   // First step, figure out the appropriate *INT_TO_FP operation to use.
28470b57cec5SDimitry Andric   EVT NewInTy = LegalOp.getValueType();
28480b57cec5SDimitry Andric 
28490b57cec5SDimitry Andric   unsigned OpToUse = 0;
28500b57cec5SDimitry Andric 
28510b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
28520b57cec5SDimitry Andric   while (true) {
28530b57cec5SDimitry Andric     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
28540b57cec5SDimitry Andric     assert(NewInTy.isInteger() && "Ran out of possibilities!");
28550b57cec5SDimitry Andric 
28560b57cec5SDimitry Andric     // If the target supports SINT_TO_FP of this type, use it.
2857480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2858480093f4SDimitry Andric       OpToUse = SIntOp;
28590b57cec5SDimitry Andric       break;
28600b57cec5SDimitry Andric     }
2861480093f4SDimitry Andric     if (IsSigned)
2862480093f4SDimitry Andric       continue;
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric     // If the target supports UINT_TO_FP of this type, use it.
2865480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2866480093f4SDimitry Andric       OpToUse = UIntOp;
28670b57cec5SDimitry Andric       break;
28680b57cec5SDimitry Andric     }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric     // Otherwise, try a larger type.
28710b57cec5SDimitry Andric   }
28720b57cec5SDimitry Andric 
28730b57cec5SDimitry Andric   // Okay, we found the operation and type to use.  Zero extend our input to the
28740b57cec5SDimitry Andric   // desired type then run the operation on it.
2875480093f4SDimitry Andric   if (IsStrict) {
2876480093f4SDimitry Andric     SDValue Res =
2877480093f4SDimitry Andric         DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2878480093f4SDimitry Andric                     {N->getOperand(0),
2879480093f4SDimitry Andric                      DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2880480093f4SDimitry Andric                                  dl, NewInTy, LegalOp)});
2881480093f4SDimitry Andric     Results.push_back(Res);
2882480093f4SDimitry Andric     Results.push_back(Res.getValue(1));
2883480093f4SDimitry Andric     return;
2884480093f4SDimitry Andric   }
2885480093f4SDimitry Andric 
2886480093f4SDimitry Andric   Results.push_back(
2887480093f4SDimitry Andric       DAG.getNode(OpToUse, dl, DestVT,
2888480093f4SDimitry Andric                   DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2889480093f4SDimitry Andric                               dl, NewInTy, LegalOp)));
28900b57cec5SDimitry Andric }
28910b57cec5SDimitry Andric 
28920b57cec5SDimitry Andric /// This function is responsible for legalizing a
28930b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that
28940b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
28950b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
28960b57cec5SDimitry Andric /// operation that returns a larger result.
2897480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2898480093f4SDimitry Andric                                                  SmallVectorImpl<SDValue> &Results) {
2899480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2900480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2901480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2902480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2903480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
29040b57cec5SDimitry Andric   // First step, figure out the appropriate FP_TO*INT operation to use.
29050b57cec5SDimitry Andric   EVT NewOutTy = DestVT;
29060b57cec5SDimitry Andric 
29070b57cec5SDimitry Andric   unsigned OpToUse = 0;
29080b57cec5SDimitry Andric 
29090b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
29100b57cec5SDimitry Andric   while (true) {
29110b57cec5SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
29120b57cec5SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
29130b57cec5SDimitry Andric 
29140b57cec5SDimitry Andric     // A larger signed type can hold all unsigned values of the requested type,
29150b57cec5SDimitry Andric     // so using FP_TO_SINT is valid
2916480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2917480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29180b57cec5SDimitry Andric       break;
29190b57cec5SDimitry Andric 
29200b57cec5SDimitry Andric     // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2921480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2922480093f4SDimitry Andric     if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29230b57cec5SDimitry Andric       break;
29240b57cec5SDimitry Andric 
29250b57cec5SDimitry Andric     // Otherwise, try a larger type.
29260b57cec5SDimitry Andric   }
29270b57cec5SDimitry Andric 
29280b57cec5SDimitry Andric   // Okay, we found the operation and type to use.
2929480093f4SDimitry Andric   SDValue Operation;
2930480093f4SDimitry Andric   if (IsStrict) {
2931480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2932480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2933480093f4SDimitry Andric   } else
2934480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
29350b57cec5SDimitry Andric 
29360b57cec5SDimitry Andric   // Truncate the result of the extended FP_TO_*INT operation to the desired
29370b57cec5SDimitry Andric   // size.
2938480093f4SDimitry Andric   SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2939480093f4SDimitry Andric   Results.push_back(Trunc);
2940480093f4SDimitry Andric   if (IsStrict)
2941480093f4SDimitry Andric     Results.push_back(Operation.getValue(1));
29420b57cec5SDimitry Andric }
29430b57cec5SDimitry Andric 
2944e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2945e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal
2946e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type.
2947e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2948e8d8bef9SDimitry Andric                                                         const SDLoc &dl) {
2949e8d8bef9SDimitry Andric   unsigned Opcode = Node->getOpcode();
2950e8d8bef9SDimitry Andric 
2951e8d8bef9SDimitry Andric   // Scan for the appropriate larger type to use.
2952e8d8bef9SDimitry Andric   EVT NewOutTy = Node->getValueType(0);
2953e8d8bef9SDimitry Andric   while (true) {
2954e8d8bef9SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2955e8d8bef9SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2956e8d8bef9SDimitry Andric 
2957e8d8bef9SDimitry Andric     if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2958e8d8bef9SDimitry Andric       break;
2959e8d8bef9SDimitry Andric   }
2960e8d8bef9SDimitry Andric 
2961e8d8bef9SDimitry Andric   // Saturation width is determined by second operand, so we don't have to
2962e8d8bef9SDimitry Andric   // perform any fixup and can directly truncate the result.
2963e8d8bef9SDimitry Andric   SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
2964e8d8bef9SDimitry Andric                                Node->getOperand(1));
2965e8d8bef9SDimitry Andric   return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2966e8d8bef9SDimitry Andric }
2967e8d8bef9SDimitry Andric 
2968e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation.
2969e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
2970e8d8bef9SDimitry Andric   EVT VT = Op.getValueType();
2971e8d8bef9SDimitry Andric   EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2972e8d8bef9SDimitry Andric   unsigned Sz = VT.getScalarSizeInBits();
2973e8d8bef9SDimitry Andric 
2974e8d8bef9SDimitry Andric   // If CTPOP is legal, use it. Otherwise use shifts and xor.
2975e8d8bef9SDimitry Andric   SDValue Result;
2976349cc55cSDimitry Andric   if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) {
2977e8d8bef9SDimitry Andric     Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
2978e8d8bef9SDimitry Andric   } else {
2979e8d8bef9SDimitry Andric     Result = Op;
2980e8d8bef9SDimitry Andric     for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
2981e8d8bef9SDimitry Andric       SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
2982e8d8bef9SDimitry Andric                                   DAG.getConstant(1ULL << (--i), dl, ShVT));
2983e8d8bef9SDimitry Andric       Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
2984e8d8bef9SDimitry Andric     }
2985e8d8bef9SDimitry Andric   }
2986e8d8bef9SDimitry Andric 
2987e8d8bef9SDimitry Andric   return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
2988e8d8bef9SDimitry Andric }
2989e8d8bef9SDimitry Andric 
29900b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
29910b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to expand node\n");
29920b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
29930b57cec5SDimitry Andric   SDLoc dl(Node);
29940b57cec5SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
29950b57cec5SDimitry Andric   bool NeedInvert;
29960b57cec5SDimitry Andric   switch (Node->getOpcode()) {
29970b57cec5SDimitry Andric   case ISD::ABS:
2998349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandABS(Node, DAG)))
29990b57cec5SDimitry Andric       Results.push_back(Tmp1);
30000b57cec5SDimitry Andric     break;
300106c3fb27SDimitry Andric   case ISD::ABDS:
300206c3fb27SDimitry Andric   case ISD::ABDU:
300306c3fb27SDimitry Andric     if ((Tmp1 = TLI.expandABD(Node, DAG)))
300406c3fb27SDimitry Andric       Results.push_back(Tmp1);
300506c3fb27SDimitry Andric     break;
30060b57cec5SDimitry Andric   case ISD::CTPOP:
3007349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
30080b57cec5SDimitry Andric       Results.push_back(Tmp1);
30090b57cec5SDimitry Andric     break;
30100b57cec5SDimitry Andric   case ISD::CTLZ:
30110b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
3012349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
30130b57cec5SDimitry Andric       Results.push_back(Tmp1);
30140b57cec5SDimitry Andric     break;
30150b57cec5SDimitry Andric   case ISD::CTTZ:
30160b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
3017349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
30180b57cec5SDimitry Andric       Results.push_back(Tmp1);
30190b57cec5SDimitry Andric     break;
30200b57cec5SDimitry Andric   case ISD::BITREVERSE:
3021fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3022fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30230b57cec5SDimitry Andric     break;
30240b57cec5SDimitry Andric   case ISD::BSWAP:
3025fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3026fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30270b57cec5SDimitry Andric     break;
3028e8d8bef9SDimitry Andric   case ISD::PARITY:
3029e8d8bef9SDimitry Andric     Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3030e8d8bef9SDimitry Andric     break;
30310b57cec5SDimitry Andric   case ISD::FRAMEADDR:
30320b57cec5SDimitry Andric   case ISD::RETURNADDR:
30330b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
30340b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
30350b57cec5SDimitry Andric     break;
30360b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA: {
30370b57cec5SDimitry Andric     SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
30380b57cec5SDimitry Andric                                         TLI.getPointerTy(DAG.getDataLayout()));
30390b57cec5SDimitry Andric     SDValue Offset = DAG.getNode(ISD::ADD, dl,
30400b57cec5SDimitry Andric                                  CfaArg.getValueType(),
30410b57cec5SDimitry Andric                                  DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
30420b57cec5SDimitry Andric                                              CfaArg.getValueType()),
30430b57cec5SDimitry Andric                                  CfaArg);
30440b57cec5SDimitry Andric     SDValue FA = DAG.getNode(
30450b57cec5SDimitry Andric         ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
30460b57cec5SDimitry Andric         DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
30470b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
30480b57cec5SDimitry Andric                                   FA, Offset));
30490b57cec5SDimitry Andric     break;
30500b57cec5SDimitry Andric   }
3051bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
30520b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
30535ffd83dbSDimitry Andric     Results.push_back(Node->getOperand(0));
30540b57cec5SDimitry Andric     break;
30550b57cec5SDimitry Andric   case ISD::EH_RETURN:
30560b57cec5SDimitry Andric   case ISD::EH_LABEL:
30570b57cec5SDimitry Andric   case ISD::PREFETCH:
30580b57cec5SDimitry Andric   case ISD::VAEND:
30590b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
30600b57cec5SDimitry Andric     // If the target didn't expand these, there's nothing to do, so just
30610b57cec5SDimitry Andric     // preserve the chain and be done.
30620b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30630b57cec5SDimitry Andric     break;
30640b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
30650b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
30660b57cec5SDimitry Andric     // chain.
30670b57cec5SDimitry Andric     Results.append(Node->getNumValues() - 1,
30680b57cec5SDimitry Andric                    DAG.getConstant(0, dl, Node->getValueType(0)));
30690b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30700b57cec5SDimitry Andric     break;
30710b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
30720b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
30730b57cec5SDimitry Andric     // chain.
30740b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, MVT::i32));
30750b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
30760b57cec5SDimitry Andric     break;
30770b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD: {
30780b57cec5SDimitry Andric     // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
30790b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
30800b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
30810b57cec5SDimitry Andric     SDValue Swap = DAG.getAtomicCmpSwap(
30820b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
30830b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Zero, Zero,
30840b57cec5SDimitry Andric         cast<AtomicSDNode>(Node)->getMemOperand());
30850b57cec5SDimitry Andric     Results.push_back(Swap.getValue(0));
30860b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
30870b57cec5SDimitry Andric     break;
30880b57cec5SDimitry Andric   }
30890b57cec5SDimitry Andric   case ISD::ATOMIC_STORE: {
30900b57cec5SDimitry Andric     // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
3091*5f757f3fSDimitry Andric     SDValue Swap = DAG.getAtomic(
3092*5f757f3fSDimitry Andric         ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
3093*5f757f3fSDimitry Andric         Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
30940b57cec5SDimitry Andric         cast<AtomicSDNode>(Node)->getMemOperand());
30950b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
30960b57cec5SDimitry Andric     break;
30970b57cec5SDimitry Andric   }
30980b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
30990b57cec5SDimitry Andric     // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
31000b57cec5SDimitry Andric     // splits out the success value as a comparison. Expanding the resulting
31010b57cec5SDimitry Andric     // ATOMIC_CMP_SWAP will produce a libcall.
31020b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
31030b57cec5SDimitry Andric     SDValue Res = DAG.getAtomicCmpSwap(
31040b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
31050b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
31060b57cec5SDimitry Andric         Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
31070b57cec5SDimitry Andric 
31080b57cec5SDimitry Andric     SDValue ExtRes = Res;
31090b57cec5SDimitry Andric     SDValue LHS = Res;
31100b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
31110b57cec5SDimitry Andric 
31120b57cec5SDimitry Andric     EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
31130b57cec5SDimitry Andric     EVT OuterType = Node->getValueType(0);
31140b57cec5SDimitry Andric     switch (TLI.getExtendForAtomicOps()) {
31150b57cec5SDimitry Andric     case ISD::SIGN_EXTEND:
31160b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
31170b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31180b57cec5SDimitry Andric       RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
31190b57cec5SDimitry Andric                         Node->getOperand(2), DAG.getValueType(AtomicType));
31200b57cec5SDimitry Andric       ExtRes = LHS;
31210b57cec5SDimitry Andric       break;
31220b57cec5SDimitry Andric     case ISD::ZERO_EXTEND:
31230b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
31240b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31250b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
31260b57cec5SDimitry Andric       ExtRes = LHS;
31270b57cec5SDimitry Andric       break;
31280b57cec5SDimitry Andric     case ISD::ANY_EXTEND:
31290b57cec5SDimitry Andric       LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
31300b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
31310b57cec5SDimitry Andric       break;
31320b57cec5SDimitry Andric     default:
31330b57cec5SDimitry Andric       llvm_unreachable("Invalid atomic op extension");
31340b57cec5SDimitry Andric     }
31350b57cec5SDimitry Andric 
31360b57cec5SDimitry Andric     SDValue Success =
31370b57cec5SDimitry Andric         DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
31380b57cec5SDimitry Andric 
31390b57cec5SDimitry Andric     Results.push_back(ExtRes.getValue(0));
31400b57cec5SDimitry Andric     Results.push_back(Success);
31410b57cec5SDimitry Andric     Results.push_back(Res.getValue(1));
31420b57cec5SDimitry Andric     break;
31430b57cec5SDimitry Andric   }
3144*5f757f3fSDimitry Andric   case ISD::ATOMIC_LOAD_SUB: {
3145*5f757f3fSDimitry Andric     SDLoc DL(Node);
3146*5f757f3fSDimitry Andric     EVT VT = Node->getValueType(0);
3147*5f757f3fSDimitry Andric     SDValue RHS = Node->getOperand(2);
3148*5f757f3fSDimitry Andric     AtomicSDNode *AN = cast<AtomicSDNode>(Node);
3149*5f757f3fSDimitry Andric     if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
3150*5f757f3fSDimitry Andric         cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
3151*5f757f3fSDimitry Andric       RHS = RHS->getOperand(0);
3152*5f757f3fSDimitry Andric     SDValue NewRHS =
3153*5f757f3fSDimitry Andric         DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
3154*5f757f3fSDimitry Andric     SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(),
3155*5f757f3fSDimitry Andric                                 Node->getOperand(0), Node->getOperand(1),
3156*5f757f3fSDimitry Andric                                 NewRHS, AN->getMemOperand());
3157*5f757f3fSDimitry Andric     Results.push_back(Res);
3158*5f757f3fSDimitry Andric     Results.push_back(Res.getValue(1));
3159*5f757f3fSDimitry Andric     break;
3160*5f757f3fSDimitry Andric   }
31610b57cec5SDimitry Andric   case ISD::DYNAMIC_STACKALLOC:
31620b57cec5SDimitry Andric     ExpandDYNAMIC_STACKALLOC(Node, Results);
31630b57cec5SDimitry Andric     break;
31640b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
31650b57cec5SDimitry Andric     for (unsigned i = 0; i < Node->getNumValues(); i++)
31660b57cec5SDimitry Andric       Results.push_back(Node->getOperand(i));
31670b57cec5SDimitry Andric     break;
31680b57cec5SDimitry Andric   case ISD::UNDEF: {
31690b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
31700b57cec5SDimitry Andric     if (VT.isInteger())
31710b57cec5SDimitry Andric       Results.push_back(DAG.getConstant(0, dl, VT));
31720b57cec5SDimitry Andric     else {
31730b57cec5SDimitry Andric       assert(VT.isFloatingPoint() && "Unknown value type!");
31740b57cec5SDimitry Andric       Results.push_back(DAG.getConstantFP(0, dl, VT));
31750b57cec5SDimitry Andric     }
31760b57cec5SDimitry Andric     break;
31770b57cec5SDimitry Andric   }
31780b57cec5SDimitry Andric   case ISD::STRICT_FP_ROUND:
3179480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3180480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3181480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3182480093f4SDimitry Andric       break;
3183480093f4SDimitry Andric     // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3184480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
31858bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
31868bcb0991SDimitry Andric                                        Node->getValueType(0))
31878bcb0991SDimitry Andric         == TargetLowering::Legal)
31888bcb0991SDimitry Andric       break;
3189480093f4SDimitry Andric     // We fall back to use stack operation when the FP_ROUND operation
3190480093f4SDimitry Andric     // isn't available.
3191e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3192e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl,
3193e8d8bef9SDimitry Andric                                  Node->getOperand(0)))) {
31940b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
31950b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
31960b57cec5SDimitry Andric       return true;
3197e8d8bef9SDimitry Andric     }
3198e8d8bef9SDimitry Andric     break;
31990b57cec5SDimitry Andric   case ISD::FP_ROUND:
32000b57cec5SDimitry Andric   case ISD::BITCAST:
3201e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3202e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl)))
32030b57cec5SDimitry Andric       Results.push_back(Tmp1);
32040b57cec5SDimitry Andric     break;
32050b57cec5SDimitry Andric   case ISD::STRICT_FP_EXTEND:
3206480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3207480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3208480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3209480093f4SDimitry Andric       break;
3210480093f4SDimitry Andric     // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3211480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
32128bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
32138bcb0991SDimitry Andric                                        Node->getValueType(0))
32148bcb0991SDimitry Andric         == TargetLowering::Legal)
32158bcb0991SDimitry Andric       break;
3216480093f4SDimitry Andric     // We fall back to use stack operation when the FP_EXTEND operation
3217480093f4SDimitry Andric     // isn't available.
3218e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(
3219e8d8bef9SDimitry Andric              Node->getOperand(1), Node->getOperand(1).getValueType(),
3220e8d8bef9SDimitry Andric              Node->getValueType(0), dl, Node->getOperand(0)))) {
32210b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
32220b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
32230b57cec5SDimitry Andric       return true;
3224e8d8bef9SDimitry Andric     }
3225e8d8bef9SDimitry Andric     break;
32260b57cec5SDimitry Andric   case ISD::FP_EXTEND:
3227e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(0),
32280b57cec5SDimitry Andric                                  Node->getOperand(0).getValueType(),
3229e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl)))
32300b57cec5SDimitry Andric       Results.push_back(Tmp1);
32310b57cec5SDimitry Andric     break;
323281ad6265SDimitry Andric   case ISD::BF16_TO_FP: {
323381ad6265SDimitry Andric     // Always expand bf16 to f32 casts, they lower to ext + shift.
3234bdd1243dSDimitry Andric     //
3235bdd1243dSDimitry Andric     // Note that the operand of this code can be bf16 or an integer type in case
3236bdd1243dSDimitry Andric     // bf16 is not supported on the target and was softened.
3237bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3238bdd1243dSDimitry Andric     if (Op.getValueType() == MVT::bf16) {
3239bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3240bdd1243dSDimitry Andric                        DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3241bdd1243dSDimitry Andric     } else {
3242bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3243bdd1243dSDimitry Andric     }
324481ad6265SDimitry Andric     Op = DAG.getNode(
324581ad6265SDimitry Andric         ISD::SHL, dl, MVT::i32, Op,
324681ad6265SDimitry Andric         DAG.getConstant(16, dl,
324781ad6265SDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
324881ad6265SDimitry Andric     Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3249bdd1243dSDimitry Andric     // Add fp_extend in case the output is bigger than f32.
3250bdd1243dSDimitry Andric     if (Node->getValueType(0) != MVT::f32)
3251bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3252bdd1243dSDimitry Andric     Results.push_back(Op);
3253bdd1243dSDimitry Andric     break;
3254bdd1243dSDimitry Andric   }
3255bdd1243dSDimitry Andric   case ISD::FP_TO_BF16: {
3256bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3257bdd1243dSDimitry Andric     if (Op.getValueType() != MVT::f32)
3258bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3259bdd1243dSDimitry Andric                        DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3260bdd1243dSDimitry Andric     Op = DAG.getNode(
3261bdd1243dSDimitry Andric         ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3262bdd1243dSDimitry Andric         DAG.getConstant(16, dl,
3263bdd1243dSDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3264bdd1243dSDimitry Andric     // The result of this node can be bf16 or an integer type in case bf16 is
3265bdd1243dSDimitry Andric     // not supported on the target and was softened to i16 for storage.
3266bdd1243dSDimitry Andric     if (Node->getValueType(0) == MVT::bf16) {
3267bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3268bdd1243dSDimitry Andric                        DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3269bdd1243dSDimitry Andric     } else {
3270bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3271bdd1243dSDimitry Andric     }
327281ad6265SDimitry Andric     Results.push_back(Op);
327381ad6265SDimitry Andric     break;
327481ad6265SDimitry Andric   }
32750b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
32760b57cec5SDimitry Andric     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
32770b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
32780b57cec5SDimitry Andric 
32790b57cec5SDimitry Andric     // An in-register sign-extend of a boolean is a negation:
32800b57cec5SDimitry Andric     // 'true' (1) sign-extended is -1.
32810b57cec5SDimitry Andric     // 'false' (0) sign-extended is 0.
32820b57cec5SDimitry Andric     // However, we must mask the high bits of the source operand because the
32830b57cec5SDimitry Andric     // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
32840b57cec5SDimitry Andric 
32850b57cec5SDimitry Andric     // TODO: Do this for vectors too?
328681ad6265SDimitry Andric     if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
32870b57cec5SDimitry Andric       SDValue One = DAG.getConstant(1, dl, VT);
32880b57cec5SDimitry Andric       SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
32890b57cec5SDimitry Andric       SDValue Zero = DAG.getConstant(0, dl, VT);
32900b57cec5SDimitry Andric       SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
32910b57cec5SDimitry Andric       Results.push_back(Neg);
32920b57cec5SDimitry Andric       break;
32930b57cec5SDimitry Andric     }
32940b57cec5SDimitry Andric 
32950b57cec5SDimitry Andric     // NOTE: we could fall back on load/store here too for targets without
32960b57cec5SDimitry Andric     // SRA.  However, it is doubtful that any exist.
32970b57cec5SDimitry Andric     EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
32980b57cec5SDimitry Andric     unsigned BitsDiff = VT.getScalarSizeInBits() -
32990b57cec5SDimitry Andric                         ExtraVT.getScalarSizeInBits();
33000b57cec5SDimitry Andric     SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
33010b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
33020b57cec5SDimitry Andric                        Node->getOperand(0), ShiftCst);
33030b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
33040b57cec5SDimitry Andric     Results.push_back(Tmp1);
33050b57cec5SDimitry Andric     break;
33060b57cec5SDimitry Andric   }
33070b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
3308480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
3309480093f4SDimitry Andric     if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
33100b57cec5SDimitry Andric       Results.push_back(Tmp1);
3311480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3312480093f4SDimitry Andric         Results.push_back(Tmp2);
33130b57cec5SDimitry Andric       break;
33140b57cec5SDimitry Andric     }
3315bdd1243dSDimitry Andric     [[fallthrough]];
33160b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
3317480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
3318e8d8bef9SDimitry Andric     if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
33190b57cec5SDimitry Andric       Results.push_back(Tmp1);
3320480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3321480093f4SDimitry Andric         Results.push_back(Tmp2);
3322e8d8bef9SDimitry Andric     }
33230b57cec5SDimitry Andric     break;
33240b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
33250b57cec5SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
33260b57cec5SDimitry Andric       Results.push_back(Tmp1);
33270b57cec5SDimitry Andric     break;
33288bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
33298bcb0991SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
33308bcb0991SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
33318bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
33328bcb0991SDimitry Andric       return true;
33338bcb0991SDimitry Andric     }
33348bcb0991SDimitry Andric     break;
33350b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
33368bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
33370b57cec5SDimitry Andric       Results.push_back(Tmp1);
33380b57cec5SDimitry Andric     break;
33398bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
33408bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
33418bcb0991SDimitry Andric       // Relink the chain.
33428bcb0991SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
33438bcb0991SDimitry Andric       // Replace the new UINT result.
33448bcb0991SDimitry Andric       ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
33458bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
33468bcb0991SDimitry Andric       return true;
33478bcb0991SDimitry Andric     }
33480b57cec5SDimitry Andric     break;
3349e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
3350e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
3351e8d8bef9SDimitry Andric     Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3352e8d8bef9SDimitry Andric     break;
33530b57cec5SDimitry Andric   case ISD::VAARG:
33540b57cec5SDimitry Andric     Results.push_back(DAG.expandVAArg(Node));
33550b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(1));
33560b57cec5SDimitry Andric     break;
33570b57cec5SDimitry Andric   case ISD::VACOPY:
33580b57cec5SDimitry Andric     Results.push_back(DAG.expandVACopy(Node));
33590b57cec5SDimitry Andric     break;
33600b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
3361*5f757f3fSDimitry Andric     if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
33620b57cec5SDimitry Andric       // This must be an access of the only element.  Return it.
33630b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
33640b57cec5SDimitry Andric                          Node->getOperand(0));
33650b57cec5SDimitry Andric     else
33660b57cec5SDimitry Andric       Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
33670b57cec5SDimitry Andric     Results.push_back(Tmp1);
33680b57cec5SDimitry Andric     break;
33690b57cec5SDimitry Andric   case ISD::EXTRACT_SUBVECTOR:
33700b57cec5SDimitry Andric     Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
33710b57cec5SDimitry Andric     break;
33720b57cec5SDimitry Andric   case ISD::INSERT_SUBVECTOR:
33730b57cec5SDimitry Andric     Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
33740b57cec5SDimitry Andric     break;
33750b57cec5SDimitry Andric   case ISD::CONCAT_VECTORS:
33760b57cec5SDimitry Andric     Results.push_back(ExpandVectorBuildThroughStack(Node));
33770b57cec5SDimitry Andric     break;
33780b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR:
33790b57cec5SDimitry Andric     Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
33800b57cec5SDimitry Andric     break;
33810b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT:
33820b57cec5SDimitry Andric     Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0),
33830b57cec5SDimitry Andric                                               Node->getOperand(1),
33840b57cec5SDimitry Andric                                               Node->getOperand(2), dl));
33850b57cec5SDimitry Andric     break;
33860b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
33870b57cec5SDimitry Andric     SmallVector<int, 32> NewMask;
33880b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
33910b57cec5SDimitry Andric     EVT EltVT = VT.getVectorElementType();
33920b57cec5SDimitry Andric     SDValue Op0 = Node->getOperand(0);
33930b57cec5SDimitry Andric     SDValue Op1 = Node->getOperand(1);
33940b57cec5SDimitry Andric     if (!TLI.isTypeLegal(EltVT)) {
33950b57cec5SDimitry Andric       EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
33960b57cec5SDimitry Andric 
33970b57cec5SDimitry Andric       // BUILD_VECTOR operands are allowed to be wider than the element type.
33980b57cec5SDimitry Andric       // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
33990b57cec5SDimitry Andric       // it.
34000b57cec5SDimitry Andric       if (NewEltVT.bitsLT(EltVT)) {
34010b57cec5SDimitry Andric         // Convert shuffle node.
34020b57cec5SDimitry Andric         // If original node was v4i64 and the new EltVT is i32,
34030b57cec5SDimitry Andric         // cast operands to v8i32 and re-build the mask.
34040b57cec5SDimitry Andric 
34050b57cec5SDimitry Andric         // Calculate new VT, the size of the new VT should be equal to original.
34060b57cec5SDimitry Andric         EVT NewVT =
34070b57cec5SDimitry Andric             EVT::getVectorVT(*DAG.getContext(), NewEltVT,
34080b57cec5SDimitry Andric                              VT.getSizeInBits() / NewEltVT.getSizeInBits());
34090b57cec5SDimitry Andric         assert(NewVT.bitsEq(VT));
34100b57cec5SDimitry Andric 
34110b57cec5SDimitry Andric         // cast operands to new VT
34120b57cec5SDimitry Andric         Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
34130b57cec5SDimitry Andric         Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
34140b57cec5SDimitry Andric 
34150b57cec5SDimitry Andric         // Convert the shuffle mask
34160b57cec5SDimitry Andric         unsigned int factor =
34170b57cec5SDimitry Andric                          NewVT.getVectorNumElements()/VT.getVectorNumElements();
34180b57cec5SDimitry Andric 
34190b57cec5SDimitry Andric         // EltVT gets smaller
34200b57cec5SDimitry Andric         assert(factor > 0);
34210b57cec5SDimitry Andric 
34220b57cec5SDimitry Andric         for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
34230b57cec5SDimitry Andric           if (Mask[i] < 0) {
34240b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
34250b57cec5SDimitry Andric               NewMask.push_back(Mask[i]);
34260b57cec5SDimitry Andric           }
34270b57cec5SDimitry Andric           else {
34280b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
34290b57cec5SDimitry Andric               NewMask.push_back(Mask[i]*factor+fi);
34300b57cec5SDimitry Andric           }
34310b57cec5SDimitry Andric         }
34320b57cec5SDimitry Andric         Mask = NewMask;
34330b57cec5SDimitry Andric         VT = NewVT;
34340b57cec5SDimitry Andric       }
34350b57cec5SDimitry Andric       EltVT = NewEltVT;
34360b57cec5SDimitry Andric     }
34370b57cec5SDimitry Andric     unsigned NumElems = VT.getVectorNumElements();
34380b57cec5SDimitry Andric     SmallVector<SDValue, 16> Ops;
34390b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElems; ++i) {
34400b57cec5SDimitry Andric       if (Mask[i] < 0) {
34410b57cec5SDimitry Andric         Ops.push_back(DAG.getUNDEF(EltVT));
34420b57cec5SDimitry Andric         continue;
34430b57cec5SDimitry Andric       }
34440b57cec5SDimitry Andric       unsigned Idx = Mask[i];
34450b57cec5SDimitry Andric       if (Idx < NumElems)
34465ffd83dbSDimitry Andric         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
34475ffd83dbSDimitry Andric                                   DAG.getVectorIdxConstant(Idx, dl)));
34480b57cec5SDimitry Andric       else
34495ffd83dbSDimitry Andric         Ops.push_back(
34505ffd83dbSDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
34515ffd83dbSDimitry Andric                         DAG.getVectorIdxConstant(Idx - NumElems, dl)));
34520b57cec5SDimitry Andric     }
34530b57cec5SDimitry Andric 
34540b57cec5SDimitry Andric     Tmp1 = DAG.getBuildVector(VT, dl, Ops);
34550b57cec5SDimitry Andric     // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
34560b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
34570b57cec5SDimitry Andric     Results.push_back(Tmp1);
34580b57cec5SDimitry Andric     break;
34590b57cec5SDimitry Andric   }
3460fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
3461fe6060f1SDimitry Andric     Results.push_back(TLI.expandVectorSplice(Node, DAG));
3462fe6060f1SDimitry Andric     break;
3463fe6060f1SDimitry Andric   }
34640b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT: {
34650b57cec5SDimitry Andric     EVT OpTy = Node->getOperand(0).getValueType();
3466bdd1243dSDimitry Andric     if (Node->getConstantOperandVal(1)) {
34670b57cec5SDimitry Andric       // 1 -> Hi
34680b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
34690b57cec5SDimitry Andric                          DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
34700b57cec5SDimitry Andric                                          TLI.getShiftAmountTy(
34710b57cec5SDimitry Andric                                              Node->getOperand(0).getValueType(),
34720b57cec5SDimitry Andric                                              DAG.getDataLayout())));
34730b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
34740b57cec5SDimitry Andric     } else {
34750b57cec5SDimitry Andric       // 0 -> Lo
34760b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
34770b57cec5SDimitry Andric                          Node->getOperand(0));
34780b57cec5SDimitry Andric     }
34790b57cec5SDimitry Andric     Results.push_back(Tmp1);
34800b57cec5SDimitry Andric     break;
34810b57cec5SDimitry Andric   }
34820b57cec5SDimitry Andric   case ISD::STACKSAVE:
34830b57cec5SDimitry Andric     // Expand to CopyFromReg if the target set
34840b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3485e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
34860b57cec5SDimitry Andric       Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
34870b57cec5SDimitry Andric                                            Node->getValueType(0)));
34880b57cec5SDimitry Andric       Results.push_back(Results[0].getValue(1));
34890b57cec5SDimitry Andric     } else {
34900b57cec5SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
34910b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
34920b57cec5SDimitry Andric     }
34930b57cec5SDimitry Andric     break;
34940b57cec5SDimitry Andric   case ISD::STACKRESTORE:
34950b57cec5SDimitry Andric     // Expand to CopyToReg if the target set
34960b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3497e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
34980b57cec5SDimitry Andric       Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
34990b57cec5SDimitry Andric                                          Node->getOperand(1)));
35000b57cec5SDimitry Andric     } else {
35010b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
35020b57cec5SDimitry Andric     }
35030b57cec5SDimitry Andric     break;
35040b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
35050b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
35060b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(0));
35070b57cec5SDimitry Andric     break;
35080b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
35090b57cec5SDimitry Andric     Results.push_back(ExpandFCOPYSIGN(Node));
35100b57cec5SDimitry Andric     break;
35110b57cec5SDimitry Andric   case ISD::FNEG:
3512e8d8bef9SDimitry Andric     Results.push_back(ExpandFNEG(Node));
35130b57cec5SDimitry Andric     break;
35140b57cec5SDimitry Andric   case ISD::FABS:
35150b57cec5SDimitry Andric     Results.push_back(ExpandFABS(Node));
35160b57cec5SDimitry Andric     break;
351781ad6265SDimitry Andric   case ISD::IS_FPCLASS: {
351881ad6265SDimitry Andric     auto CNode = cast<ConstantSDNode>(Node->getOperand(1));
351981ad6265SDimitry Andric     auto Test = static_cast<FPClassTest>(CNode->getZExtValue());
352081ad6265SDimitry Andric     if (SDValue Expanded =
352181ad6265SDimitry Andric             TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
352281ad6265SDimitry Andric                                  Test, Node->getFlags(), SDLoc(Node), DAG))
352381ad6265SDimitry Andric       Results.push_back(Expanded);
352481ad6265SDimitry Andric     break;
352581ad6265SDimitry Andric   }
35260b57cec5SDimitry Andric   case ISD::SMIN:
35270b57cec5SDimitry Andric   case ISD::SMAX:
35280b57cec5SDimitry Andric   case ISD::UMIN:
35290b57cec5SDimitry Andric   case ISD::UMAX: {
35300b57cec5SDimitry Andric     // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
35310b57cec5SDimitry Andric     ISD::CondCode Pred;
35320b57cec5SDimitry Andric     switch (Node->getOpcode()) {
35330b57cec5SDimitry Andric     default: llvm_unreachable("How did we get here?");
35340b57cec5SDimitry Andric     case ISD::SMAX: Pred = ISD::SETGT; break;
35350b57cec5SDimitry Andric     case ISD::SMIN: Pred = ISD::SETLT; break;
35360b57cec5SDimitry Andric     case ISD::UMAX: Pred = ISD::SETUGT; break;
35370b57cec5SDimitry Andric     case ISD::UMIN: Pred = ISD::SETULT; break;
35380b57cec5SDimitry Andric     }
35390b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
35400b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
35410b57cec5SDimitry Andric     Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
35420b57cec5SDimitry Andric     Results.push_back(Tmp1);
35430b57cec5SDimitry Andric     break;
35440b57cec5SDimitry Andric   }
35450b57cec5SDimitry Andric   case ISD::FMINNUM:
35460b57cec5SDimitry Andric   case ISD::FMAXNUM: {
35470b57cec5SDimitry Andric     if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
35480b57cec5SDimitry Andric       Results.push_back(Expanded);
35490b57cec5SDimitry Andric     break;
35500b57cec5SDimitry Andric   }
35510b57cec5SDimitry Andric   case ISD::FSIN:
35520b57cec5SDimitry Andric   case ISD::FCOS: {
35530b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
35540b57cec5SDimitry Andric     // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
35550b57cec5SDimitry Andric     // fcos which share the same operand and both are used.
35560b57cec5SDimitry Andric     if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
35570b57cec5SDimitry Andric          isSinCosLibcallAvailable(Node, TLI))
35580b57cec5SDimitry Andric         && useSinCos(Node)) {
35590b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
35600b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
35610b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::FCOS)
35620b57cec5SDimitry Andric         Tmp1 = Tmp1.getValue(1);
35630b57cec5SDimitry Andric       Results.push_back(Tmp1);
35640b57cec5SDimitry Andric     }
35650b57cec5SDimitry Andric     break;
35660b57cec5SDimitry Andric   }
356706c3fb27SDimitry Andric   case ISD::FLDEXP:
356806c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP: {
356906c3fb27SDimitry Andric     EVT VT = Node->getValueType(0);
357006c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getLDEXP(VT);
357106c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
357206c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
357306c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
357406c3fb27SDimitry Andric       break;
357506c3fb27SDimitry Andric 
357606c3fb27SDimitry Andric     if (SDValue Expanded = expandLdexp(Node)) {
357706c3fb27SDimitry Andric       Results.push_back(Expanded);
357806c3fb27SDimitry Andric       if (Node->getOpcode() == ISD::STRICT_FLDEXP)
357906c3fb27SDimitry Andric         Results.push_back(Expanded.getValue(1));
358006c3fb27SDimitry Andric     }
358106c3fb27SDimitry Andric 
358206c3fb27SDimitry Andric     break;
358306c3fb27SDimitry Andric   }
358406c3fb27SDimitry Andric   case ISD::FFREXP: {
358506c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
358606c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
358706c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
358806c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
358906c3fb27SDimitry Andric       break;
359006c3fb27SDimitry Andric 
359106c3fb27SDimitry Andric     if (SDValue Expanded = expandFrexp(Node)) {
359206c3fb27SDimitry Andric       Results.push_back(Expanded);
359306c3fb27SDimitry Andric       Results.push_back(Expanded.getValue(1));
359406c3fb27SDimitry Andric     }
359506c3fb27SDimitry Andric     break;
359606c3fb27SDimitry Andric   }
35970b57cec5SDimitry Andric   case ISD::FMAD:
35980b57cec5SDimitry Andric     llvm_unreachable("Illegal fmad should never be formed");
35990b57cec5SDimitry Andric 
36000b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
36010b57cec5SDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
36020b57cec5SDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
36030b57cec5SDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
36040b57cec5SDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
36050b57cec5SDimitry Andric       SDValue Res =
36060b57cec5SDimitry Andric           DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
36070b57cec5SDimitry Andric       Results.push_back(
36080b57cec5SDimitry Andric           DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
36090b57cec5SDimitry Andric     }
36100b57cec5SDimitry Andric     break;
36115ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP:
36125ffd83dbSDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
36135ffd83dbSDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
36145ffd83dbSDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
36155ffd83dbSDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
36165ffd83dbSDimitry Andric       SDValue Res =
36175ffd83dbSDimitry Andric           DAG.getNode(ISD::STRICT_FP16_TO_FP, dl, {MVT::f32, MVT::Other},
36185ffd83dbSDimitry Andric                       {Node->getOperand(0), Node->getOperand(1)});
36195ffd83dbSDimitry Andric       Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
36205ffd83dbSDimitry Andric                         {Node->getValueType(0), MVT::Other},
36215ffd83dbSDimitry Andric                         {Res.getValue(1), Res});
36225ffd83dbSDimitry Andric       Results.push_back(Res);
36235ffd83dbSDimitry Andric       Results.push_back(Res.getValue(1));
36245ffd83dbSDimitry Andric     }
36255ffd83dbSDimitry Andric     break;
36260b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
36270b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
36280b57cec5SDimitry Andric     if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
36290b57cec5SDimitry Andric       SDValue Op = Node->getOperand(0);
36300b57cec5SDimitry Andric       MVT SVT = Op.getSimpleValueType();
36310b57cec5SDimitry Andric       if ((SVT == MVT::f64 || SVT == MVT::f80) &&
36320b57cec5SDimitry Andric           TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
36330b57cec5SDimitry Andric         // Under fastmath, we can expand this node into a fround followed by
36340b57cec5SDimitry Andric         // a float-half conversion.
3635bdd1243dSDimitry Andric         SDValue FloatVal =
3636bdd1243dSDimitry Andric             DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3637bdd1243dSDimitry Andric                         DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
36380b57cec5SDimitry Andric         Results.push_back(
36390b57cec5SDimitry Andric             DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
36400b57cec5SDimitry Andric       }
36410b57cec5SDimitry Andric     }
36420b57cec5SDimitry Andric     break;
36430b57cec5SDimitry Andric   case ISD::ConstantFP: {
36440b57cec5SDimitry Andric     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
36450b57cec5SDimitry Andric     // Check to see if this FP immediate is already legal.
36460b57cec5SDimitry Andric     // If this is a legal constant, turn it into a TargetConstantFP node.
36470b57cec5SDimitry Andric     if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3648e8d8bef9SDimitry Andric                           DAG.shouldOptForSize()))
36490b57cec5SDimitry Andric       Results.push_back(ExpandConstantFP(CFP, true));
36500b57cec5SDimitry Andric     break;
36510b57cec5SDimitry Andric   }
36520b57cec5SDimitry Andric   case ISD::Constant: {
36530b57cec5SDimitry Andric     ConstantSDNode *CP = cast<ConstantSDNode>(Node);
36540b57cec5SDimitry Andric     Results.push_back(ExpandConstant(CP));
36550b57cec5SDimitry Andric     break;
36560b57cec5SDimitry Andric   }
36570b57cec5SDimitry Andric   case ISD::FSUB: {
36580b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36590b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
36600b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
36610b57cec5SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
36620b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
36630b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
36640b57cec5SDimitry Andric       Results.push_back(Tmp1);
36650b57cec5SDimitry Andric     }
36660b57cec5SDimitry Andric     break;
36670b57cec5SDimitry Andric   }
36680b57cec5SDimitry Andric   case ISD::SUB: {
36690b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36700b57cec5SDimitry Andric     assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
36710b57cec5SDimitry Andric            TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
36720b57cec5SDimitry Andric            "Don't know how to expand this subtraction!");
3673349cc55cSDimitry Andric     Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
36740b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
36750b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
36760b57cec5SDimitry Andric     break;
36770b57cec5SDimitry Andric   }
36780b57cec5SDimitry Andric   case ISD::UREM:
36795ffd83dbSDimitry Andric   case ISD::SREM:
36805ffd83dbSDimitry Andric     if (TLI.expandREM(Node, Tmp1, DAG))
36810b57cec5SDimitry Andric       Results.push_back(Tmp1);
36820b57cec5SDimitry Andric     break;
36830b57cec5SDimitry Andric   case ISD::UDIV:
36840b57cec5SDimitry Andric   case ISD::SDIV: {
36850b57cec5SDimitry Andric     bool isSigned = Node->getOpcode() == ISD::SDIV;
36860b57cec5SDimitry Andric     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
36870b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36880b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
36890b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
36900b57cec5SDimitry Andric       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
36910b57cec5SDimitry Andric                          Node->getOperand(1));
36920b57cec5SDimitry Andric       Results.push_back(Tmp1);
36930b57cec5SDimitry Andric     }
36940b57cec5SDimitry Andric     break;
36950b57cec5SDimitry Andric   }
36960b57cec5SDimitry Andric   case ISD::MULHU:
36970b57cec5SDimitry Andric   case ISD::MULHS: {
36980b57cec5SDimitry Andric     unsigned ExpandOpcode =
36990b57cec5SDimitry Andric         Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
37000b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37010b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
37020b57cec5SDimitry Andric 
37030b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
37040b57cec5SDimitry Andric                        Node->getOperand(1));
37050b57cec5SDimitry Andric     Results.push_back(Tmp1.getValue(1));
37060b57cec5SDimitry Andric     break;
37070b57cec5SDimitry Andric   }
37080b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
37090b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
37100b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
37110b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
37120b57cec5SDimitry Andric     MVT VT = LHS.getSimpleValueType();
37130b57cec5SDimitry Andric     unsigned MULHOpcode =
37140b57cec5SDimitry Andric         Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
37150b57cec5SDimitry Andric 
37160b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
37170b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
37180b57cec5SDimitry Andric       Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
37190b57cec5SDimitry Andric       break;
37200b57cec5SDimitry Andric     }
37210b57cec5SDimitry Andric 
37220b57cec5SDimitry Andric     SmallVector<SDValue, 4> Halves;
37230b57cec5SDimitry Andric     EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
37240b57cec5SDimitry Andric     assert(TLI.isTypeLegal(HalfType));
3725e8d8bef9SDimitry Andric     if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
37260b57cec5SDimitry Andric                            HalfType, DAG,
37270b57cec5SDimitry Andric                            TargetLowering::MulExpansionKind::Always)) {
37280b57cec5SDimitry Andric       for (unsigned i = 0; i < 2; ++i) {
37290b57cec5SDimitry Andric         SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
37300b57cec5SDimitry Andric         SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
37310b57cec5SDimitry Andric         SDValue Shift = DAG.getConstant(
37320b57cec5SDimitry Andric             HalfType.getScalarSizeInBits(), dl,
37330b57cec5SDimitry Andric             TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
37340b57cec5SDimitry Andric         Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
37350b57cec5SDimitry Andric         Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
37360b57cec5SDimitry Andric       }
37370b57cec5SDimitry Andric       break;
37380b57cec5SDimitry Andric     }
37390b57cec5SDimitry Andric     break;
37400b57cec5SDimitry Andric   }
37410b57cec5SDimitry Andric   case ISD::MUL: {
37420b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37430b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
37440b57cec5SDimitry Andric     // See if multiply or divide can be lowered using two-result operations.
37450b57cec5SDimitry Andric     // We just need the low half of the multiply; try both the signed
37460b57cec5SDimitry Andric     // and unsigned forms. If the target supports both SMUL_LOHI and
37470b57cec5SDimitry Andric     // UMUL_LOHI, form a preference by checking which forms of plain
37480b57cec5SDimitry Andric     // MULH it supports.
37490b57cec5SDimitry Andric     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
37500b57cec5SDimitry Andric     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
37510b57cec5SDimitry Andric     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
37520b57cec5SDimitry Andric     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
37530b57cec5SDimitry Andric     unsigned OpToUse = 0;
37540b57cec5SDimitry Andric     if (HasSMUL_LOHI && !HasMULHS) {
37550b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
37560b57cec5SDimitry Andric     } else if (HasUMUL_LOHI && !HasMULHU) {
37570b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
37580b57cec5SDimitry Andric     } else if (HasSMUL_LOHI) {
37590b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
37600b57cec5SDimitry Andric     } else if (HasUMUL_LOHI) {
37610b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
37620b57cec5SDimitry Andric     }
37630b57cec5SDimitry Andric     if (OpToUse) {
37640b57cec5SDimitry Andric       Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
37650b57cec5SDimitry Andric                                     Node->getOperand(1)));
37660b57cec5SDimitry Andric       break;
37670b57cec5SDimitry Andric     }
37680b57cec5SDimitry Andric 
37690b57cec5SDimitry Andric     SDValue Lo, Hi;
37700b57cec5SDimitry Andric     EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
37710b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
37720b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
37730b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
37740b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
37750b57cec5SDimitry Andric         TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
37760b57cec5SDimitry Andric                       TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
37770b57cec5SDimitry Andric       Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
37780b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
37790b57cec5SDimitry Andric       SDValue Shift =
37800b57cec5SDimitry Andric           DAG.getConstant(HalfType.getSizeInBits(), dl,
37810b57cec5SDimitry Andric                           TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
37820b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
37830b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
37840b57cec5SDimitry Andric     }
37850b57cec5SDimitry Andric     break;
37860b57cec5SDimitry Andric   }
37870b57cec5SDimitry Andric   case ISD::FSHL:
37880b57cec5SDimitry Andric   case ISD::FSHR:
37890eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
37900eae32dcSDimitry Andric       Results.push_back(Expanded);
37910b57cec5SDimitry Andric     break;
37920b57cec5SDimitry Andric   case ISD::ROTL:
37930b57cec5SDimitry Andric   case ISD::ROTR:
37940eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
37950eae32dcSDimitry Andric       Results.push_back(Expanded);
37960b57cec5SDimitry Andric     break;
37970b57cec5SDimitry Andric   case ISD::SADDSAT:
37980b57cec5SDimitry Andric   case ISD::UADDSAT:
37990b57cec5SDimitry Andric   case ISD::SSUBSAT:
38000b57cec5SDimitry Andric   case ISD::USUBSAT:
38010b57cec5SDimitry Andric     Results.push_back(TLI.expandAddSubSat(Node, DAG));
38020b57cec5SDimitry Andric     break;
3803e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
3804e8d8bef9SDimitry Andric   case ISD::USHLSAT:
3805e8d8bef9SDimitry Andric     Results.push_back(TLI.expandShlSat(Node, DAG));
3806e8d8bef9SDimitry Andric     break;
38070b57cec5SDimitry Andric   case ISD::SMULFIX:
38080b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
38090b57cec5SDimitry Andric   case ISD::UMULFIX:
38108bcb0991SDimitry Andric   case ISD::UMULFIXSAT:
38110b57cec5SDimitry Andric     Results.push_back(TLI.expandFixedPointMul(Node, DAG));
38120b57cec5SDimitry Andric     break;
3813480093f4SDimitry Andric   case ISD::SDIVFIX:
38145ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
3815480093f4SDimitry Andric   case ISD::UDIVFIX:
38165ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT:
3817480093f4SDimitry Andric     if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
3818480093f4SDimitry Andric                                             Node->getOperand(0),
3819480093f4SDimitry Andric                                             Node->getOperand(1),
3820480093f4SDimitry Andric                                             Node->getConstantOperandVal(2),
3821480093f4SDimitry Andric                                             DAG)) {
3822480093f4SDimitry Andric       Results.push_back(V);
3823480093f4SDimitry Andric       break;
3824480093f4SDimitry Andric     }
3825480093f4SDimitry Andric     // FIXME: We might want to retry here with a wider type if we fail, if that
3826480093f4SDimitry Andric     // type is legal.
3827480093f4SDimitry Andric     // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
3828480093f4SDimitry Andric     // <= 128 (which is the case for all of the default Embedded-C types),
3829480093f4SDimitry Andric     // we will only get here with types and scales that we could always expand
3830480093f4SDimitry Andric     // if we were allowed to generate libcalls to division functions of illegal
3831480093f4SDimitry Andric     // type. But we cannot do that.
3832480093f4SDimitry Andric     llvm_unreachable("Cannot expand DIVFIX!");
383306c3fb27SDimitry Andric   case ISD::UADDO_CARRY:
383406c3fb27SDimitry Andric   case ISD::USUBO_CARRY: {
38350b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
38360b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
38370b57cec5SDimitry Andric     SDValue Carry = Node->getOperand(2);
38380b57cec5SDimitry Andric 
383906c3fb27SDimitry Andric     bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
38400b57cec5SDimitry Andric 
38410b57cec5SDimitry Andric     // Initial add of the 2 operands.
38420b57cec5SDimitry Andric     unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
38430b57cec5SDimitry Andric     EVT VT = LHS.getValueType();
38440b57cec5SDimitry Andric     SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
38450b57cec5SDimitry Andric 
38460b57cec5SDimitry Andric     // Initial check for overflow.
38470b57cec5SDimitry Andric     EVT CarryType = Node->getValueType(1);
38480b57cec5SDimitry Andric     EVT SetCCType = getSetCCResultType(Node->getValueType(0));
38490b57cec5SDimitry Andric     ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
38500b57cec5SDimitry Andric     SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
38510b57cec5SDimitry Andric 
38520b57cec5SDimitry Andric     // Add of the sum and the carry.
38535ffd83dbSDimitry Andric     SDValue One = DAG.getConstant(1, dl, VT);
38540b57cec5SDimitry Andric     SDValue CarryExt =
38555ffd83dbSDimitry Andric         DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
38560b57cec5SDimitry Andric     SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
38570b57cec5SDimitry Andric 
38580b57cec5SDimitry Andric     // Second check for overflow. If we are adding, we can only overflow if the
38590b57cec5SDimitry Andric     // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
38600b57cec5SDimitry Andric     // If we are subtracting, we can only overflow if the initial sum is 0 and
38610b57cec5SDimitry Andric     // the carry is set, resulting in a new sum of all 1s.
38620b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, VT);
38630b57cec5SDimitry Andric     SDValue Overflow2 =
38640b57cec5SDimitry Andric         IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
38650b57cec5SDimitry Andric               : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
38660b57cec5SDimitry Andric     Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
38670b57cec5SDimitry Andric                             DAG.getZExtOrTrunc(Carry, dl, SetCCType));
38680b57cec5SDimitry Andric 
38690b57cec5SDimitry Andric     SDValue ResultCarry =
38700b57cec5SDimitry Andric         DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
38710b57cec5SDimitry Andric 
38720b57cec5SDimitry Andric     Results.push_back(Sum2);
38730b57cec5SDimitry Andric     Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
38740b57cec5SDimitry Andric     break;
38750b57cec5SDimitry Andric   }
38760b57cec5SDimitry Andric   case ISD::SADDO:
38770b57cec5SDimitry Andric   case ISD::SSUBO: {
38780b57cec5SDimitry Andric     SDValue Result, Overflow;
38790b57cec5SDimitry Andric     TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
38800b57cec5SDimitry Andric     Results.push_back(Result);
38810b57cec5SDimitry Andric     Results.push_back(Overflow);
38820b57cec5SDimitry Andric     break;
38830b57cec5SDimitry Andric   }
38840b57cec5SDimitry Andric   case ISD::UADDO:
38850b57cec5SDimitry Andric   case ISD::USUBO: {
38860b57cec5SDimitry Andric     SDValue Result, Overflow;
38870b57cec5SDimitry Andric     TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
38880b57cec5SDimitry Andric     Results.push_back(Result);
38890b57cec5SDimitry Andric     Results.push_back(Overflow);
38900b57cec5SDimitry Andric     break;
38910b57cec5SDimitry Andric   }
38920b57cec5SDimitry Andric   case ISD::UMULO:
38930b57cec5SDimitry Andric   case ISD::SMULO: {
38940b57cec5SDimitry Andric     SDValue Result, Overflow;
38950b57cec5SDimitry Andric     if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
38960b57cec5SDimitry Andric       Results.push_back(Result);
38970b57cec5SDimitry Andric       Results.push_back(Overflow);
38980b57cec5SDimitry Andric     }
38990b57cec5SDimitry Andric     break;
39000b57cec5SDimitry Andric   }
39010b57cec5SDimitry Andric   case ISD::BUILD_PAIR: {
39020b57cec5SDimitry Andric     EVT PairTy = Node->getValueType(0);
39030b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
39040b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
39050b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
39060b57cec5SDimitry Andric         ISD::SHL, dl, PairTy, Tmp2,
39070b57cec5SDimitry Andric         DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
39080b57cec5SDimitry Andric                         TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
39090b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
39100b57cec5SDimitry Andric     break;
39110b57cec5SDimitry Andric   }
39120b57cec5SDimitry Andric   case ISD::SELECT:
39130b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
39140b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
39150b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);
39160b57cec5SDimitry Andric     if (Tmp1.getOpcode() == ISD::SETCC) {
39170b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
39180b57cec5SDimitry Andric                              Tmp2, Tmp3,
39190b57cec5SDimitry Andric                              cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
39200b57cec5SDimitry Andric     } else {
39210b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1,
39220b57cec5SDimitry Andric                              DAG.getConstant(0, dl, Tmp1.getValueType()),
39230b57cec5SDimitry Andric                              Tmp2, Tmp3, ISD::SETNE);
39240b57cec5SDimitry Andric     }
39250b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
39260b57cec5SDimitry Andric     Results.push_back(Tmp1);
39270b57cec5SDimitry Andric     break;
39280b57cec5SDimitry Andric   case ISD::BR_JT: {
39290b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0);
39300b57cec5SDimitry Andric     SDValue Table = Node->getOperand(1);
39310b57cec5SDimitry Andric     SDValue Index = Node->getOperand(2);
3932*5f757f3fSDimitry Andric     int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
39330b57cec5SDimitry Andric 
39340b57cec5SDimitry Andric     const DataLayout &TD = DAG.getDataLayout();
39350b57cec5SDimitry Andric     EVT PTy = TLI.getPointerTy(TD);
39360b57cec5SDimitry Andric 
39370b57cec5SDimitry Andric     unsigned EntrySize =
39380b57cec5SDimitry Andric       DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
39390b57cec5SDimitry Andric 
39400b57cec5SDimitry Andric     // For power-of-two jumptable entry sizes convert multiplication to a shift.
39410b57cec5SDimitry Andric     // This transformation needs to be done here since otherwise the MIPS
39420b57cec5SDimitry Andric     // backend will end up emitting a three instruction multiply sequence
39430b57cec5SDimitry Andric     // instead of a single shift and MSP430 will call a runtime function.
39440b57cec5SDimitry Andric     if (llvm::isPowerOf2_32(EntrySize))
39450b57cec5SDimitry Andric       Index = DAG.getNode(
39460b57cec5SDimitry Andric           ISD::SHL, dl, Index.getValueType(), Index,
39470b57cec5SDimitry Andric           DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
39480b57cec5SDimitry Andric     else
39490b57cec5SDimitry Andric       Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
39500b57cec5SDimitry Andric                           DAG.getConstant(EntrySize, dl, Index.getValueType()));
39510b57cec5SDimitry Andric     SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
39520b57cec5SDimitry Andric                                Index, Table);
39530b57cec5SDimitry Andric 
39540b57cec5SDimitry Andric     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
39550b57cec5SDimitry Andric     SDValue LD = DAG.getExtLoad(
39560b57cec5SDimitry Andric         ISD::SEXTLOAD, dl, PTy, Chain, Addr,
39570b57cec5SDimitry Andric         MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
39580b57cec5SDimitry Andric     Addr = LD;
39590b57cec5SDimitry Andric     if (TLI.isJumpTableRelative()) {
39600b57cec5SDimitry Andric       // For PIC, the sequence is:
39610b57cec5SDimitry Andric       // BRIND(load(Jumptable + index) + RelocBase)
39620b57cec5SDimitry Andric       // RelocBase can be JumpTable, GOT or some sort of global base.
39630b57cec5SDimitry Andric       Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
39640b57cec5SDimitry Andric                           TLI.getPICJumpTableRelocBase(Table, DAG));
39650b57cec5SDimitry Andric     }
39660b57cec5SDimitry Andric 
3967*5f757f3fSDimitry Andric     Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
39680b57cec5SDimitry Andric     Results.push_back(Tmp1);
39690b57cec5SDimitry Andric     break;
39700b57cec5SDimitry Andric   }
39710b57cec5SDimitry Andric   case ISD::BRCOND:
39720b57cec5SDimitry Andric     // Expand brcond's setcc into its constituent parts and create a BR_CC
39730b57cec5SDimitry Andric     // Node.
39740b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
39750b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
39764824e7fdSDimitry Andric     if (Tmp2.getOpcode() == ISD::SETCC &&
39774824e7fdSDimitry Andric         TLI.isOperationLegalOrCustom(ISD::BR_CC,
39784824e7fdSDimitry Andric                                      Tmp2.getOperand(0).getValueType())) {
39794824e7fdSDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
39800b57cec5SDimitry Andric                          Tmp2.getOperand(0), Tmp2.getOperand(1),
39810b57cec5SDimitry Andric                          Node->getOperand(2));
39820b57cec5SDimitry Andric     } else {
39830b57cec5SDimitry Andric       // We test only the i1 bit.  Skip the AND if UNDEF or another AND.
39840b57cec5SDimitry Andric       if (Tmp2.isUndef() ||
398506c3fb27SDimitry Andric           (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
39860b57cec5SDimitry Andric         Tmp3 = Tmp2;
39870b57cec5SDimitry Andric       else
39880b57cec5SDimitry Andric         Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
39890b57cec5SDimitry Andric                            DAG.getConstant(1, dl, Tmp2.getValueType()));
39900b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
39910b57cec5SDimitry Andric                          DAG.getCondCode(ISD::SETNE), Tmp3,
39920b57cec5SDimitry Andric                          DAG.getConstant(0, dl, Tmp3.getValueType()),
39930b57cec5SDimitry Andric                          Node->getOperand(2));
39940b57cec5SDimitry Andric     }
39950b57cec5SDimitry Andric     Results.push_back(Tmp1);
39960b57cec5SDimitry Andric     break;
3997480093f4SDimitry Andric   case ISD::SETCC:
399881ad6265SDimitry Andric   case ISD::VP_SETCC:
3999480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
4000480093f4SDimitry Andric   case ISD::STRICT_FSETCCS: {
400181ad6265SDimitry Andric     bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
400281ad6265SDimitry Andric     bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
400381ad6265SDimitry Andric                     Node->getOpcode() == ISD::STRICT_FSETCCS;
4004480093f4SDimitry Andric     bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4005480093f4SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4006480093f4SDimitry Andric     unsigned Offset = IsStrict ? 1 : 0;
4007480093f4SDimitry Andric     Tmp1 = Node->getOperand(0 + Offset);
4008480093f4SDimitry Andric     Tmp2 = Node->getOperand(1 + Offset);
4009480093f4SDimitry Andric     Tmp3 = Node->getOperand(2 + Offset);
401081ad6265SDimitry Andric     SDValue Mask, EVL;
401181ad6265SDimitry Andric     if (IsVP) {
401281ad6265SDimitry Andric       Mask = Node->getOperand(3 + Offset);
401381ad6265SDimitry Andric       EVL = Node->getOperand(4 + Offset);
401481ad6265SDimitry Andric     }
401581ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
401681ad6265SDimitry Andric         DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
401781ad6265SDimitry Andric         Chain, IsSignaling);
40180b57cec5SDimitry Andric 
40190b57cec5SDimitry Andric     if (Legalized) {
40200b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
40210b57cec5SDimitry Andric       // condition code, create a new SETCC node.
402204eeddc0SDimitry Andric       if (Tmp3.getNode()) {
402304eeddc0SDimitry Andric         if (IsStrict) {
402404eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
402504eeddc0SDimitry Andric                              {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
402604eeddc0SDimitry Andric           Chain = Tmp1.getValue(1);
402781ad6265SDimitry Andric         } else if (IsVP) {
402881ad6265SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
402981ad6265SDimitry Andric                              {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
403004eeddc0SDimitry Andric         } else {
403104eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
403204eeddc0SDimitry Andric                              Tmp2, Tmp3, Node->getFlags());
403304eeddc0SDimitry Andric         }
403404eeddc0SDimitry Andric       }
40350b57cec5SDimitry Andric 
40360b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then wrap
40370b57cec5SDimitry Andric       // the existing SETCC in a NOT to restore the intended condition.
403881ad6265SDimitry Andric       if (NeedInvert) {
403981ad6265SDimitry Andric         if (!IsVP)
40400b57cec5SDimitry Andric           Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
404181ad6265SDimitry Andric         else
404281ad6265SDimitry Andric           Tmp1 =
404381ad6265SDimitry Andric               DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
404481ad6265SDimitry Andric       }
40450b57cec5SDimitry Andric 
40460b57cec5SDimitry Andric       Results.push_back(Tmp1);
4047480093f4SDimitry Andric       if (IsStrict)
4048480093f4SDimitry Andric         Results.push_back(Chain);
4049480093f4SDimitry Andric 
40500b57cec5SDimitry Andric       break;
40510b57cec5SDimitry Andric     }
40520b57cec5SDimitry Andric 
4053480093f4SDimitry Andric     // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4054480093f4SDimitry Andric     // understand if this code is useful for strict nodes.
4055480093f4SDimitry Andric     assert(!IsStrict && "Don't know how to expand for strict nodes.");
4056480093f4SDimitry Andric 
40570b57cec5SDimitry Andric     // Otherwise, SETCC for the given comparison type must be completely
40580b57cec5SDimitry Andric     // illegal; expand it into a SELECT_CC.
405981ad6265SDimitry Andric     // FIXME: This drops the mask/evl for VP_SETCC.
40600b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
406181ad6265SDimitry Andric     EVT Tmp1VT = Tmp1.getValueType();
40620b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
406381ad6265SDimitry Andric                        DAG.getBoolConstant(true, dl, VT, Tmp1VT),
406481ad6265SDimitry Andric                        DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3);
40650b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
40660b57cec5SDimitry Andric     Results.push_back(Tmp1);
40670b57cec5SDimitry Andric     break;
40680b57cec5SDimitry Andric   }
40690b57cec5SDimitry Andric   case ISD::SELECT_CC: {
4070480093f4SDimitry Andric     // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
40710b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);   // LHS
40720b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);   // RHS
40730b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);   // True
40740b57cec5SDimitry Andric     Tmp4 = Node->getOperand(3);   // False
40750b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
4076480093f4SDimitry Andric     SDValue Chain;
40770b57cec5SDimitry Andric     SDValue CC = Node->getOperand(4);
40780b57cec5SDimitry Andric     ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
40790b57cec5SDimitry Andric 
40800b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
40810b57cec5SDimitry Andric       // If the condition code is legal, then we need to expand this
40820b57cec5SDimitry Andric       // node using SETCC and SELECT.
40830b57cec5SDimitry Andric       EVT CmpVT = Tmp1.getValueType();
40840b57cec5SDimitry Andric       assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
40850b57cec5SDimitry Andric              "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
40860b57cec5SDimitry Andric              "expanded.");
40870b57cec5SDimitry Andric       EVT CCVT = getSetCCResultType(CmpVT);
40880b57cec5SDimitry Andric       SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
40890b57cec5SDimitry Andric       Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4));
40900b57cec5SDimitry Andric       break;
40910b57cec5SDimitry Andric     }
40920b57cec5SDimitry Andric 
40930b57cec5SDimitry Andric     // SELECT_CC is legal, so the condition code must not be.
40940b57cec5SDimitry Andric     bool Legalized = false;
40950b57cec5SDimitry Andric     // Try to legalize by inverting the condition.  This is for targets that
40960b57cec5SDimitry Andric     // might support an ordered version of a condition, but not the unordered
40970b57cec5SDimitry Andric     // version (or vice versa).
4098480093f4SDimitry Andric     ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
40990b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
41000b57cec5SDimitry Andric       // Use the new condition code and swap true and false
41010b57cec5SDimitry Andric       Legalized = true;
41020b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
41030b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
41040b57cec5SDimitry Andric     } else {
41050b57cec5SDimitry Andric       // If The inverse is not legal, then try to swap the arguments using
41060b57cec5SDimitry Andric       // the inverse condition code.
41070b57cec5SDimitry Andric       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
41080b57cec5SDimitry Andric       if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
41090b57cec5SDimitry Andric         // The swapped inverse condition is legal, so swap true and false,
41100b57cec5SDimitry Andric         // lhs and rhs.
41110b57cec5SDimitry Andric         Legalized = true;
41120b57cec5SDimitry Andric         Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
41130b57cec5SDimitry Andric         Tmp1->setFlags(Node->getFlags());
41140b57cec5SDimitry Andric       }
41150b57cec5SDimitry Andric     }
41160b57cec5SDimitry Andric 
41170b57cec5SDimitry Andric     if (!Legalized) {
4118fe6060f1SDimitry Andric       Legalized = TLI.LegalizeSetCCCondCode(
4119fe6060f1SDimitry Andric           DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
412081ad6265SDimitry Andric           /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
41210b57cec5SDimitry Andric 
41220b57cec5SDimitry Andric       assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
41230b57cec5SDimitry Andric 
41240b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then swap
41250b57cec5SDimitry Andric       // the True/False operands to match.
41260b57cec5SDimitry Andric       if (NeedInvert)
41270b57cec5SDimitry Andric         std::swap(Tmp3, Tmp4);
41280b57cec5SDimitry Andric 
41290b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
41300b57cec5SDimitry Andric       // condition code, create a new SELECT_CC node.
41310b57cec5SDimitry Andric       if (CC.getNode()) {
41320b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
41330b57cec5SDimitry Andric                            Tmp1, Tmp2, Tmp3, Tmp4, CC);
41340b57cec5SDimitry Andric       } else {
41350b57cec5SDimitry Andric         Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
41360b57cec5SDimitry Andric         CC = DAG.getCondCode(ISD::SETNE);
41370b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
41380b57cec5SDimitry Andric                            Tmp2, Tmp3, Tmp4, CC);
41390b57cec5SDimitry Andric       }
41400b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
41410b57cec5SDimitry Andric     }
41420b57cec5SDimitry Andric     Results.push_back(Tmp1);
41430b57cec5SDimitry Andric     break;
41440b57cec5SDimitry Andric   }
41450b57cec5SDimitry Andric   case ISD::BR_CC: {
4146480093f4SDimitry Andric     // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4147480093f4SDimitry Andric     SDValue Chain;
41480b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);              // Chain
41490b57cec5SDimitry Andric     Tmp2 = Node->getOperand(2);              // LHS
41500b57cec5SDimitry Andric     Tmp3 = Node->getOperand(3);              // RHS
41510b57cec5SDimitry Andric     Tmp4 = Node->getOperand(1);              // CC
41520b57cec5SDimitry Andric 
415381ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
415481ad6265SDimitry Andric         DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
415581ad6265SDimitry Andric         /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
41560b57cec5SDimitry Andric     (void)Legalized;
41570b57cec5SDimitry Andric     assert(Legalized && "Can't legalize BR_CC with legal condition!");
41580b57cec5SDimitry Andric 
41590b57cec5SDimitry Andric     // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
41600b57cec5SDimitry Andric     // node.
41610b57cec5SDimitry Andric     if (Tmp4.getNode()) {
4162e8d8bef9SDimitry Andric       assert(!NeedInvert && "Don't know how to invert BR_CC!");
4163e8d8bef9SDimitry Andric 
41640b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
41650b57cec5SDimitry Andric                          Tmp4, Tmp2, Tmp3, Node->getOperand(4));
41660b57cec5SDimitry Andric     } else {
41670b57cec5SDimitry Andric       Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4168e8d8bef9SDimitry Andric       Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
41690b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
41700b57cec5SDimitry Andric                          Tmp2, Tmp3, Node->getOperand(4));
41710b57cec5SDimitry Andric     }
41720b57cec5SDimitry Andric     Results.push_back(Tmp1);
41730b57cec5SDimitry Andric     break;
41740b57cec5SDimitry Andric   }
41750b57cec5SDimitry Andric   case ISD::BUILD_VECTOR:
41760b57cec5SDimitry Andric     Results.push_back(ExpandBUILD_VECTOR(Node));
41770b57cec5SDimitry Andric     break;
41788bcb0991SDimitry Andric   case ISD::SPLAT_VECTOR:
41798bcb0991SDimitry Andric     Results.push_back(ExpandSPLAT_VECTOR(Node));
41808bcb0991SDimitry Andric     break;
41810b57cec5SDimitry Andric   case ISD::SRA:
41820b57cec5SDimitry Andric   case ISD::SRL:
41830b57cec5SDimitry Andric   case ISD::SHL: {
41840b57cec5SDimitry Andric     // Scalarize vector SRA/SRL/SHL.
41850b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
41860b57cec5SDimitry Andric     assert(VT.isVector() && "Unable to legalize non-vector shift");
41870b57cec5SDimitry Andric     assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
41880b57cec5SDimitry Andric     unsigned NumElem = VT.getVectorNumElements();
41890b57cec5SDimitry Andric 
41900b57cec5SDimitry Andric     SmallVector<SDValue, 8> Scalars;
41910b57cec5SDimitry Andric     for (unsigned Idx = 0; Idx < NumElem; Idx++) {
41925ffd83dbSDimitry Andric       SDValue Ex =
41935ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
41945ffd83dbSDimitry Andric                       Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
41955ffd83dbSDimitry Andric       SDValue Sh =
41965ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
41975ffd83dbSDimitry Andric                       Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
41980b57cec5SDimitry Andric       Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
41990b57cec5SDimitry Andric                                     VT.getScalarType(), Ex, Sh));
42000b57cec5SDimitry Andric     }
42010b57cec5SDimitry Andric 
42020b57cec5SDimitry Andric     SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4203480093f4SDimitry Andric     Results.push_back(Result);
42040b57cec5SDimitry Andric     break;
42050b57cec5SDimitry Andric   }
42060b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
42070b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
42080b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
42090b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
42100b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
42110b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
42120b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
42130b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
42140b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
42150b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
42160b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
42170b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
42180b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
421906c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
422006c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
42210b57cec5SDimitry Andric     Results.push_back(TLI.expandVecReduce(Node, DAG));
42220b57cec5SDimitry Andric     break;
42230b57cec5SDimitry Andric   case ISD::GLOBAL_OFFSET_TABLE:
42240b57cec5SDimitry Andric   case ISD::GlobalAddress:
42250b57cec5SDimitry Andric   case ISD::GlobalTLSAddress:
42260b57cec5SDimitry Andric   case ISD::ExternalSymbol:
42270b57cec5SDimitry Andric   case ISD::ConstantPool:
42280b57cec5SDimitry Andric   case ISD::JumpTable:
42290b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
42300b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
42310b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
42320b57cec5SDimitry Andric     // FIXME: Custom lowering for these operations shouldn't return null!
4233480093f4SDimitry Andric     // Return true so that we don't call ConvertNodeToLibcall which also won't
4234480093f4SDimitry Andric     // do anything.
4235480093f4SDimitry Andric     return true;
42360b57cec5SDimitry Andric   }
42370b57cec5SDimitry Andric 
4238480093f4SDimitry Andric   if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
42398bcb0991SDimitry Andric     // FIXME: We were asked to expand a strict floating-point operation,
42408bcb0991SDimitry Andric     // but there is currently no expansion implemented that would preserve
42418bcb0991SDimitry Andric     // the "strict" properties.  For now, we just fall back to the non-strict
42428bcb0991SDimitry Andric     // version if that is legal on the target.  The actual mutation of the
42438bcb0991SDimitry Andric     // operation will happen in SelectionDAGISel::DoInstructionSelection.
42448bcb0991SDimitry Andric     switch (Node->getOpcode()) {
42458bcb0991SDimitry Andric     default:
42468bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
42478bcb0991SDimitry Andric                                          Node->getValueType(0))
42488bcb0991SDimitry Andric           == TargetLowering::Legal)
42498bcb0991SDimitry Andric         return true;
42508bcb0991SDimitry Andric       break;
4251e8d8bef9SDimitry Andric     case ISD::STRICT_FSUB: {
4252e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4253e8d8bef9SDimitry Andric               ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4254e8d8bef9SDimitry Andric         return true;
4255e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4256e8d8bef9SDimitry Andric               ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4257e8d8bef9SDimitry Andric         break;
4258e8d8bef9SDimitry Andric 
4259e8d8bef9SDimitry Andric       EVT VT = Node->getValueType(0);
4260e8d8bef9SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
4261e8d8bef9SDimitry Andric       SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4262e8d8bef9SDimitry Andric       SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4263e8d8bef9SDimitry Andric                                  {Node->getOperand(0), Node->getOperand(1), Neg},
4264e8d8bef9SDimitry Andric                          Flags);
4265e8d8bef9SDimitry Andric 
4266e8d8bef9SDimitry Andric       Results.push_back(Fadd);
4267e8d8bef9SDimitry Andric       Results.push_back(Fadd.getValue(1));
4268e8d8bef9SDimitry Andric       break;
4269e8d8bef9SDimitry Andric     }
4270e8d8bef9SDimitry Andric     case ISD::STRICT_SINT_TO_FP:
4271e8d8bef9SDimitry Andric     case ISD::STRICT_UINT_TO_FP:
42728bcb0991SDimitry Andric     case ISD::STRICT_LRINT:
42738bcb0991SDimitry Andric     case ISD::STRICT_LLRINT:
42748bcb0991SDimitry Andric     case ISD::STRICT_LROUND:
42758bcb0991SDimitry Andric     case ISD::STRICT_LLROUND:
42768bcb0991SDimitry Andric       // These are registered by the operand type instead of the value
42778bcb0991SDimitry Andric       // type. Reflect that here.
42788bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
42798bcb0991SDimitry Andric                                          Node->getOperand(1).getValueType())
42808bcb0991SDimitry Andric           == TargetLowering::Legal)
42818bcb0991SDimitry Andric         return true;
42828bcb0991SDimitry Andric       break;
42838bcb0991SDimitry Andric     }
42848bcb0991SDimitry Andric   }
42858bcb0991SDimitry Andric 
42860b57cec5SDimitry Andric   // Replace the original node with the legalized result.
42870b57cec5SDimitry Andric   if (Results.empty()) {
42880b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Cannot expand node\n");
42890b57cec5SDimitry Andric     return false;
42900b57cec5SDimitry Andric   }
42910b57cec5SDimitry Andric 
42920b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
42930b57cec5SDimitry Andric   ReplaceNode(Node, Results.data());
42940b57cec5SDimitry Andric   return true;
42950b57cec5SDimitry Andric }
42960b57cec5SDimitry Andric 
42970b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
42980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
42990b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
43000b57cec5SDimitry Andric   SDLoc dl(Node);
43010b57cec5SDimitry Andric   // FIXME: Check flags on the node to see if we can use a finite call.
43020b57cec5SDimitry Andric   unsigned Opc = Node->getOpcode();
43030b57cec5SDimitry Andric   switch (Opc) {
43040b57cec5SDimitry Andric   case ISD::ATOMIC_FENCE: {
43050b57cec5SDimitry Andric     // If the target didn't lower this, lower it to '__sync_synchronize()' call
43060b57cec5SDimitry Andric     // FIXME: handle "fence singlethread" more efficiently.
43070b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
43080b57cec5SDimitry Andric 
43090b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
43100b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
43110b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
43120b57cec5SDimitry Andric         .setLibCallee(
43130b57cec5SDimitry Andric             CallingConv::C, Type::getVoidTy(*DAG.getContext()),
43140b57cec5SDimitry Andric             DAG.getExternalSymbol("__sync_synchronize",
43150b57cec5SDimitry Andric                                   TLI.getPointerTy(DAG.getDataLayout())),
43160b57cec5SDimitry Andric             std::move(Args));
43170b57cec5SDimitry Andric 
43180b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
43190b57cec5SDimitry Andric 
43200b57cec5SDimitry Andric     Results.push_back(CallResult.second);
43210b57cec5SDimitry Andric     break;
43220b57cec5SDimitry Andric   }
43230b57cec5SDimitry Andric   // By default, atomic intrinsics are marked Legal and lowered. Targets
43240b57cec5SDimitry Andric   // which don't support them directly, however, may want libcalls, in which
43250b57cec5SDimitry Andric   // case they mark them Expand, and we get here.
43260b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP:
43270b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_ADD:
43280b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_SUB:
43290b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_AND:
43300b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_CLR:
43310b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_OR:
43320b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_XOR:
43330b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_NAND:
43340b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MIN:
43350b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MAX:
43360b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMIN:
43370b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMAX:
43380b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP: {
43390b57cec5SDimitry Andric     MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4340fe6060f1SDimitry Andric     AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4341e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4342480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
4343480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4344e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops;
4345e8d8bef9SDimitry Andric     if (TLI.getLibcallName(LC)) {
4346e8d8bef9SDimitry Andric       // If outline atomic available, prepare its arguments and expand.
4347e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 2, Node->op_end());
4348e8d8bef9SDimitry Andric       Ops.push_back(Node->getOperand(1));
4349e8d8bef9SDimitry Andric 
4350e8d8bef9SDimitry Andric     } else {
4351e8d8bef9SDimitry Andric       LC = RTLIB::getSYNC(Opc, VT);
4352e8d8bef9SDimitry Andric       assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4353e8d8bef9SDimitry Andric              "Unexpected atomic op or value type!");
4354e8d8bef9SDimitry Andric       // Arguments for expansion to sync libcall
4355e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 1, Node->op_end());
4356e8d8bef9SDimitry Andric     }
4357480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4358480093f4SDimitry Andric                                                       Ops, CallOptions,
4359480093f4SDimitry Andric                                                       SDLoc(Node),
4360480093f4SDimitry Andric                                                       Node->getOperand(0));
43610b57cec5SDimitry Andric     Results.push_back(Tmp.first);
43620b57cec5SDimitry Andric     Results.push_back(Tmp.second);
43630b57cec5SDimitry Andric     break;
43640b57cec5SDimitry Andric   }
43650b57cec5SDimitry Andric   case ISD::TRAP: {
43660b57cec5SDimitry Andric     // If this operation is not supported, lower it to 'abort()' call
43670b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
43680b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
43690b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
43700b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
43710b57cec5SDimitry Andric         .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
43720b57cec5SDimitry Andric                       DAG.getExternalSymbol(
43730b57cec5SDimitry Andric                           "abort", TLI.getPointerTy(DAG.getDataLayout())),
43740b57cec5SDimitry Andric                       std::move(Args));
43750b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
43760b57cec5SDimitry Andric 
43770b57cec5SDimitry Andric     Results.push_back(CallResult.second);
43780b57cec5SDimitry Andric     break;
43790b57cec5SDimitry Andric   }
43800b57cec5SDimitry Andric   case ISD::FMINNUM:
43810b57cec5SDimitry Andric   case ISD::STRICT_FMINNUM:
4382480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
43830b57cec5SDimitry Andric                     RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4384480093f4SDimitry Andric                     RTLIB::FMIN_PPCF128, Results);
43850b57cec5SDimitry Andric     break;
438606c3fb27SDimitry Andric   // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
438706c3fb27SDimitry Andric   // libcall legalization for these nodes, but there is no default expasion for
438806c3fb27SDimitry Andric   // these nodes either (see PR63267 for example).
43890b57cec5SDimitry Andric   case ISD::FMAXNUM:
43900b57cec5SDimitry Andric   case ISD::STRICT_FMAXNUM:
4391480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
43920b57cec5SDimitry Andric                     RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4393480093f4SDimitry Andric                     RTLIB::FMAX_PPCF128, Results);
43940b57cec5SDimitry Andric     break;
43950b57cec5SDimitry Andric   case ISD::FSQRT:
43960b57cec5SDimitry Andric   case ISD::STRICT_FSQRT:
4397480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
43980b57cec5SDimitry Andric                     RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4399480093f4SDimitry Andric                     RTLIB::SQRT_PPCF128, Results);
44000b57cec5SDimitry Andric     break;
44010b57cec5SDimitry Andric   case ISD::FCBRT:
4402480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
44030b57cec5SDimitry Andric                     RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4404480093f4SDimitry Andric                     RTLIB::CBRT_PPCF128, Results);
44050b57cec5SDimitry Andric     break;
44060b57cec5SDimitry Andric   case ISD::FSIN:
44070b57cec5SDimitry Andric   case ISD::STRICT_FSIN:
4408480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
44090b57cec5SDimitry Andric                     RTLIB::SIN_F80, RTLIB::SIN_F128,
4410480093f4SDimitry Andric                     RTLIB::SIN_PPCF128, Results);
44110b57cec5SDimitry Andric     break;
44120b57cec5SDimitry Andric   case ISD::FCOS:
44130b57cec5SDimitry Andric   case ISD::STRICT_FCOS:
4414480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
44150b57cec5SDimitry Andric                     RTLIB::COS_F80, RTLIB::COS_F128,
4416480093f4SDimitry Andric                     RTLIB::COS_PPCF128, Results);
44170b57cec5SDimitry Andric     break;
44180b57cec5SDimitry Andric   case ISD::FSINCOS:
44190b57cec5SDimitry Andric     // Expand into sincos libcall.
44200b57cec5SDimitry Andric     ExpandSinCosLibCall(Node, Results);
44210b57cec5SDimitry Andric     break;
44220b57cec5SDimitry Andric   case ISD::FLOG:
44230b57cec5SDimitry Andric   case ISD::STRICT_FLOG:
44248c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
44258c27c554SDimitry Andric                     RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
44260b57cec5SDimitry Andric     break;
44270b57cec5SDimitry Andric   case ISD::FLOG2:
44280b57cec5SDimitry Andric   case ISD::STRICT_FLOG2:
44298c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
44308c27c554SDimitry Andric                     RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
44310b57cec5SDimitry Andric     break;
44320b57cec5SDimitry Andric   case ISD::FLOG10:
44330b57cec5SDimitry Andric   case ISD::STRICT_FLOG10:
44348c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
44358c27c554SDimitry Andric                     RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
44360b57cec5SDimitry Andric     break;
44370b57cec5SDimitry Andric   case ISD::FEXP:
44380b57cec5SDimitry Andric   case ISD::STRICT_FEXP:
44398c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
44408c27c554SDimitry Andric                     RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
44410b57cec5SDimitry Andric     break;
44420b57cec5SDimitry Andric   case ISD::FEXP2:
44430b57cec5SDimitry Andric   case ISD::STRICT_FEXP2:
44448c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
44458c27c554SDimitry Andric                     RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
44460b57cec5SDimitry Andric     break;
4447*5f757f3fSDimitry Andric   case ISD::FEXP10:
4448*5f757f3fSDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
4449*5f757f3fSDimitry Andric                     RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
4450*5f757f3fSDimitry Andric     break;
44510b57cec5SDimitry Andric   case ISD::FTRUNC:
44520b57cec5SDimitry Andric   case ISD::STRICT_FTRUNC:
4453480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
44540b57cec5SDimitry Andric                     RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4455480093f4SDimitry Andric                     RTLIB::TRUNC_PPCF128, Results);
44560b57cec5SDimitry Andric     break;
44570b57cec5SDimitry Andric   case ISD::FFLOOR:
44580b57cec5SDimitry Andric   case ISD::STRICT_FFLOOR:
4459480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
44600b57cec5SDimitry Andric                     RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4461480093f4SDimitry Andric                     RTLIB::FLOOR_PPCF128, Results);
44620b57cec5SDimitry Andric     break;
44630b57cec5SDimitry Andric   case ISD::FCEIL:
44640b57cec5SDimitry Andric   case ISD::STRICT_FCEIL:
4465480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
44660b57cec5SDimitry Andric                     RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4467480093f4SDimitry Andric                     RTLIB::CEIL_PPCF128, Results);
44680b57cec5SDimitry Andric     break;
44690b57cec5SDimitry Andric   case ISD::FRINT:
44700b57cec5SDimitry Andric   case ISD::STRICT_FRINT:
4471480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
44720b57cec5SDimitry Andric                     RTLIB::RINT_F80, RTLIB::RINT_F128,
4473480093f4SDimitry Andric                     RTLIB::RINT_PPCF128, Results);
44740b57cec5SDimitry Andric     break;
44750b57cec5SDimitry Andric   case ISD::FNEARBYINT:
44760b57cec5SDimitry Andric   case ISD::STRICT_FNEARBYINT:
4477480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
44780b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F64,
44790b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F80,
44800b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F128,
4481480093f4SDimitry Andric                     RTLIB::NEARBYINT_PPCF128, Results);
44820b57cec5SDimitry Andric     break;
44830b57cec5SDimitry Andric   case ISD::FROUND:
44840b57cec5SDimitry Andric   case ISD::STRICT_FROUND:
4485480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUND_F32,
44860b57cec5SDimitry Andric                     RTLIB::ROUND_F64,
44870b57cec5SDimitry Andric                     RTLIB::ROUND_F80,
44880b57cec5SDimitry Andric                     RTLIB::ROUND_F128,
4489480093f4SDimitry Andric                     RTLIB::ROUND_PPCF128, Results);
44900b57cec5SDimitry Andric     break;
44915ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
44925ffd83dbSDimitry Andric   case ISD::STRICT_FROUNDEVEN:
44935ffd83dbSDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
44945ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F64,
44955ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F80,
44965ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F128,
44975ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_PPCF128, Results);
44985ffd83dbSDimitry Andric     break;
449906c3fb27SDimitry Andric   case ISD::FLDEXP:
450006c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP:
450106c3fb27SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
450206c3fb27SDimitry Andric                     RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
450306c3fb27SDimitry Andric     break;
450406c3fb27SDimitry Andric   case ISD::FFREXP: {
450506c3fb27SDimitry Andric     ExpandFrexpLibCall(Node, Results);
450606c3fb27SDimitry Andric     break;
450706c3fb27SDimitry Andric   }
45080b57cec5SDimitry Andric   case ISD::FPOWI:
4509480093f4SDimitry Andric   case ISD::STRICT_FPOWI: {
4510fe6060f1SDimitry Andric     RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4511fe6060f1SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4512480093f4SDimitry Andric     if (!TLI.getLibcallName(LC)) {
4513480093f4SDimitry Andric       // Some targets don't have a powi libcall; use pow instead.
451481ad6265SDimitry Andric       if (Node->isStrictFPOpcode()) {
451581ad6265SDimitry Andric         SDValue Exponent =
451681ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
451781ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
451881ad6265SDimitry Andric                         {Node->getOperand(0), Node->getOperand(2)});
451981ad6265SDimitry Andric         SDValue FPOW =
452081ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
452181ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
452281ad6265SDimitry Andric                         {Exponent.getValue(1), Node->getOperand(1), Exponent});
452381ad6265SDimitry Andric         Results.push_back(FPOW);
452481ad6265SDimitry Andric         Results.push_back(FPOW.getValue(1));
452581ad6265SDimitry Andric       } else {
452681ad6265SDimitry Andric         SDValue Exponent =
452781ad6265SDimitry Andric             DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
4528480093f4SDimitry Andric                         Node->getOperand(1));
4529480093f4SDimitry Andric         Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
453081ad6265SDimitry Andric                                       Node->getValueType(0),
453181ad6265SDimitry Andric                                       Node->getOperand(0), Exponent));
453281ad6265SDimitry Andric       }
45330b57cec5SDimitry Andric       break;
4534480093f4SDimitry Andric     }
4535fe6060f1SDimitry Andric     unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
4536fe6060f1SDimitry Andric     bool ExponentHasSizeOfInt =
4537fe6060f1SDimitry Andric         DAG.getLibInfo().getIntSize() ==
4538fe6060f1SDimitry Andric         Node->getOperand(1 + Offset).getValueType().getSizeInBits();
4539fe6060f1SDimitry Andric     if (!ExponentHasSizeOfInt) {
4540fe6060f1SDimitry Andric       // If the exponent does not match with sizeof(int) a libcall to
4541fe6060f1SDimitry Andric       // RTLIB::POWI would use the wrong type for the argument.
4542fe6060f1SDimitry Andric       DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
4543fe6060f1SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4544fe6060f1SDimitry Andric       break;
4545fe6060f1SDimitry Andric     }
4546fe6060f1SDimitry Andric     ExpandFPLibCall(Node, LC, Results);
4547480093f4SDimitry Andric     break;
4548480093f4SDimitry Andric   }
45490b57cec5SDimitry Andric   case ISD::FPOW:
45500b57cec5SDimitry Andric   case ISD::STRICT_FPOW:
45518c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
45528c27c554SDimitry Andric                     RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
45530b57cec5SDimitry Andric     break;
45548bcb0991SDimitry Andric   case ISD::LROUND:
45558bcb0991SDimitry Andric   case ISD::STRICT_LROUND:
4556480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
45578bcb0991SDimitry Andric                        RTLIB::LROUND_F64, RTLIB::LROUND_F80,
45588bcb0991SDimitry Andric                        RTLIB::LROUND_F128,
4559480093f4SDimitry Andric                        RTLIB::LROUND_PPCF128, Results);
45608bcb0991SDimitry Andric     break;
45618bcb0991SDimitry Andric   case ISD::LLROUND:
45628bcb0991SDimitry Andric   case ISD::STRICT_LLROUND:
4563480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
45648bcb0991SDimitry Andric                        RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
45658bcb0991SDimitry Andric                        RTLIB::LLROUND_F128,
4566480093f4SDimitry Andric                        RTLIB::LLROUND_PPCF128, Results);
45678bcb0991SDimitry Andric     break;
45688bcb0991SDimitry Andric   case ISD::LRINT:
45698bcb0991SDimitry Andric   case ISD::STRICT_LRINT:
4570480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
45718bcb0991SDimitry Andric                        RTLIB::LRINT_F64, RTLIB::LRINT_F80,
45728bcb0991SDimitry Andric                        RTLIB::LRINT_F128,
4573480093f4SDimitry Andric                        RTLIB::LRINT_PPCF128, Results);
45748bcb0991SDimitry Andric     break;
45758bcb0991SDimitry Andric   case ISD::LLRINT:
45768bcb0991SDimitry Andric   case ISD::STRICT_LLRINT:
4577480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
45788bcb0991SDimitry Andric                        RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
45798bcb0991SDimitry Andric                        RTLIB::LLRINT_F128,
4580480093f4SDimitry Andric                        RTLIB::LLRINT_PPCF128, Results);
45818bcb0991SDimitry Andric     break;
45820b57cec5SDimitry Andric   case ISD::FDIV:
4583480093f4SDimitry Andric   case ISD::STRICT_FDIV:
4584480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
45850b57cec5SDimitry Andric                     RTLIB::DIV_F80, RTLIB::DIV_F128,
4586480093f4SDimitry Andric                     RTLIB::DIV_PPCF128, Results);
45870b57cec5SDimitry Andric     break;
45880b57cec5SDimitry Andric   case ISD::FREM:
45890b57cec5SDimitry Andric   case ISD::STRICT_FREM:
4590480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
45910b57cec5SDimitry Andric                     RTLIB::REM_F80, RTLIB::REM_F128,
4592480093f4SDimitry Andric                     RTLIB::REM_PPCF128, Results);
45930b57cec5SDimitry Andric     break;
45940b57cec5SDimitry Andric   case ISD::FMA:
45950b57cec5SDimitry Andric   case ISD::STRICT_FMA:
4596480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
45970b57cec5SDimitry Andric                     RTLIB::FMA_F80, RTLIB::FMA_F128,
4598480093f4SDimitry Andric                     RTLIB::FMA_PPCF128, Results);
45990b57cec5SDimitry Andric     break;
46000b57cec5SDimitry Andric   case ISD::FADD:
4601480093f4SDimitry Andric   case ISD::STRICT_FADD:
4602480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
46030b57cec5SDimitry Andric                     RTLIB::ADD_F80, RTLIB::ADD_F128,
4604480093f4SDimitry Andric                     RTLIB::ADD_PPCF128, Results);
46050b57cec5SDimitry Andric     break;
46060b57cec5SDimitry Andric   case ISD::FMUL:
4607480093f4SDimitry Andric   case ISD::STRICT_FMUL:
4608480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
46090b57cec5SDimitry Andric                     RTLIB::MUL_F80, RTLIB::MUL_F128,
4610480093f4SDimitry Andric                     RTLIB::MUL_PPCF128, Results);
46110b57cec5SDimitry Andric     break;
46120b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
46130b57cec5SDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
461406c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
46150b57cec5SDimitry Andric     }
46160b57cec5SDimitry Andric     break;
46175ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP: {
46185ffd83dbSDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
46195ffd83dbSDimitry Andric       TargetLowering::MakeLibCallOptions CallOptions;
46205ffd83dbSDimitry Andric       std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
46215ffd83dbSDimitry Andric           DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
46225ffd83dbSDimitry Andric           SDLoc(Node), Node->getOperand(0));
46235ffd83dbSDimitry Andric       Results.push_back(Tmp.first);
46245ffd83dbSDimitry Andric       Results.push_back(Tmp.second);
46255ffd83dbSDimitry Andric     }
46265ffd83dbSDimitry Andric     break;
46275ffd83dbSDimitry Andric   }
46280b57cec5SDimitry Andric   case ISD::FP_TO_FP16: {
46290b57cec5SDimitry Andric     RTLIB::Libcall LC =
46300b57cec5SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
46310b57cec5SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
463206c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
46330b57cec5SDimitry Andric     break;
46340b57cec5SDimitry Andric   }
463581ad6265SDimitry Andric   case ISD::FP_TO_BF16: {
463681ad6265SDimitry Andric     RTLIB::Libcall LC =
463781ad6265SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
463881ad6265SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
463906c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
464081ad6265SDimitry Andric     break;
464181ad6265SDimitry Andric   }
4642e8d8bef9SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
4643e8d8bef9SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
4644e8d8bef9SDimitry Andric   case ISD::SINT_TO_FP:
4645e8d8bef9SDimitry Andric   case ISD::UINT_TO_FP: {
4646e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
4647e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4648e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
4649e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
4650e8d8bef9SDimitry Andric     EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
4651e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4652e8d8bef9SDimitry Andric     EVT NVT = EVT();
4653e8d8bef9SDimitry Andric     SDLoc dl(Node);
4654e8d8bef9SDimitry Andric 
4655e8d8bef9SDimitry Andric     // Even if the input is legal, no libcall may exactly match, eg. we don't
4656e8d8bef9SDimitry Andric     // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
4657e8d8bef9SDimitry Andric     // eg: i13 -> fp. Then, look for an appropriate libcall.
4658e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4659e8d8bef9SDimitry Andric     for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
4660e8d8bef9SDimitry Andric          t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4661e8d8bef9SDimitry Andric          ++t) {
4662e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)t;
4663e8d8bef9SDimitry Andric       // The source needs to big enough to hold the operand.
4664e8d8bef9SDimitry Andric       if (NVT.bitsGE(SVT))
4665e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
4666e8d8bef9SDimitry Andric                     : RTLIB::getUINTTOFP(NVT, RVT);
4667e8d8bef9SDimitry Andric     }
4668e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4669e8d8bef9SDimitry Andric 
4670e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4671e8d8bef9SDimitry Andric     // Sign/zero extend the argument if the libcall takes a larger type.
4672e8d8bef9SDimitry Andric     SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
4673e8d8bef9SDimitry Andric                              NVT, Node->getOperand(IsStrict ? 1 : 0));
4674e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4675e8d8bef9SDimitry Andric     CallOptions.setSExt(Signed);
4676e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4677e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
4678e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4679e8d8bef9SDimitry Andric     if (IsStrict)
4680e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4681e8d8bef9SDimitry Andric     break;
4682e8d8bef9SDimitry Andric   }
4683e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT:
4684e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT:
4685e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
4686e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_UINT: {
4687e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
4688e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4689e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
4690e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
4691e8d8bef9SDimitry Andric 
4692e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4693e8d8bef9SDimitry Andric     EVT SVT = Op.getValueType();
4694e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4695e8d8bef9SDimitry Andric     EVT NVT = EVT();
4696e8d8bef9SDimitry Andric     SDLoc dl(Node);
4697e8d8bef9SDimitry Andric 
4698e8d8bef9SDimitry Andric     // Even if the result is legal, no libcall may exactly match, eg. we don't
4699e8d8bef9SDimitry Andric     // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
4700e8d8bef9SDimitry Andric     // eg: fp -> i32. Then, look for an appropriate libcall.
4701e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4702e8d8bef9SDimitry Andric     for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
4703e8d8bef9SDimitry Andric          IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4704e8d8bef9SDimitry Andric          ++IntVT) {
4705e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)IntVT;
4706e8d8bef9SDimitry Andric       // The type needs to big enough to hold the result.
4707e8d8bef9SDimitry Andric       if (NVT.bitsGE(RVT))
4708e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
4709e8d8bef9SDimitry Andric                     : RTLIB::getFPTOUINT(SVT, NVT);
4710e8d8bef9SDimitry Andric     }
4711e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4712e8d8bef9SDimitry Andric 
4713e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4714e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4715e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4716e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
4717e8d8bef9SDimitry Andric 
4718e8d8bef9SDimitry Andric     // Truncate the result if the libcall returns a larger type.
4719e8d8bef9SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
4720e8d8bef9SDimitry Andric     if (IsStrict)
4721e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4722e8d8bef9SDimitry Andric     break;
4723e8d8bef9SDimitry Andric   }
4724e8d8bef9SDimitry Andric 
4725e8d8bef9SDimitry Andric   case ISD::FP_ROUND:
4726e8d8bef9SDimitry Andric   case ISD::STRICT_FP_ROUND: {
4727e8d8bef9SDimitry Andric     // X = FP_ROUND(Y, TRUNC)
4728e8d8bef9SDimitry Andric     // TRUNC is a flag, which is always an integer that is zero or one.
4729e8d8bef9SDimitry Andric     // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
4730e8d8bef9SDimitry Andric     // is known to not change the value of Y.
4731e8d8bef9SDimitry Andric     // We can only expand it into libcall if the TRUNC is 0.
4732e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4733e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4734e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4735e8d8bef9SDimitry Andric     EVT VT = Node->getValueType(0);
4736349cc55cSDimitry Andric     assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
4737e8d8bef9SDimitry Andric            "Unable to expand as libcall if it is not normal rounding");
4738e8d8bef9SDimitry Andric 
4739e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
4740e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4741e8d8bef9SDimitry Andric 
4742e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4743e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4744e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
4745e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4746e8d8bef9SDimitry Andric     if (IsStrict)
4747e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4748e8d8bef9SDimitry Andric     break;
4749e8d8bef9SDimitry Andric   }
4750e8d8bef9SDimitry Andric   case ISD::FP_EXTEND: {
4751e8d8bef9SDimitry Andric     Results.push_back(
4752e8d8bef9SDimitry Andric         ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
4753e8d8bef9SDimitry Andric                                       Node->getValueType(0)),
475406c3fb27SDimitry Andric                       Node, false).first);
4755e8d8bef9SDimitry Andric     break;
4756e8d8bef9SDimitry Andric   }
4757e8d8bef9SDimitry Andric   case ISD::STRICT_FP_EXTEND:
47585ffd83dbSDimitry Andric   case ISD::STRICT_FP_TO_FP16: {
47595ffd83dbSDimitry Andric     RTLIB::Libcall LC =
4760e8d8bef9SDimitry Andric         Node->getOpcode() == ISD::STRICT_FP_TO_FP16
4761e8d8bef9SDimitry Andric             ? RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16)
4762e8d8bef9SDimitry Andric             : RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
4763e8d8bef9SDimitry Andric                               Node->getValueType(0));
4764e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4765e8d8bef9SDimitry Andric 
47665ffd83dbSDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
47675ffd83dbSDimitry Andric     std::pair<SDValue, SDValue> Tmp =
47685ffd83dbSDimitry Andric         TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
47695ffd83dbSDimitry Andric                         CallOptions, SDLoc(Node), Node->getOperand(0));
47705ffd83dbSDimitry Andric     Results.push_back(Tmp.first);
47715ffd83dbSDimitry Andric     Results.push_back(Tmp.second);
47725ffd83dbSDimitry Andric     break;
47735ffd83dbSDimitry Andric   }
47740b57cec5SDimitry Andric   case ISD::FSUB:
4775480093f4SDimitry Andric   case ISD::STRICT_FSUB:
4776480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
47770b57cec5SDimitry Andric                     RTLIB::SUB_F80, RTLIB::SUB_F128,
4778480093f4SDimitry Andric                     RTLIB::SUB_PPCF128, Results);
47790b57cec5SDimitry Andric     break;
47800b57cec5SDimitry Andric   case ISD::SREM:
4781bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4782bdd1243dSDimitry Andric                                        RTLIB::SREM_I8,
4783bdd1243dSDimitry Andric                                        RTLIB::SREM_I16, RTLIB::SREM_I32,
4784bdd1243dSDimitry Andric                                        RTLIB::SREM_I64, RTLIB::SREM_I128));
47850b57cec5SDimitry Andric     break;
47860b57cec5SDimitry Andric   case ISD::UREM:
4787bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4788bdd1243dSDimitry Andric                                        RTLIB::UREM_I8,
4789bdd1243dSDimitry Andric                                        RTLIB::UREM_I16, RTLIB::UREM_I32,
4790bdd1243dSDimitry Andric                                        RTLIB::UREM_I64, RTLIB::UREM_I128));
47910b57cec5SDimitry Andric     break;
47920b57cec5SDimitry Andric   case ISD::SDIV:
4793bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4794bdd1243dSDimitry Andric                                        RTLIB::SDIV_I8,
4795bdd1243dSDimitry Andric                                        RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4796bdd1243dSDimitry Andric                                        RTLIB::SDIV_I64, RTLIB::SDIV_I128));
47970b57cec5SDimitry Andric     break;
47980b57cec5SDimitry Andric   case ISD::UDIV:
4799bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4800bdd1243dSDimitry Andric                                        RTLIB::UDIV_I8,
4801bdd1243dSDimitry Andric                                        RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4802bdd1243dSDimitry Andric                                        RTLIB::UDIV_I64, RTLIB::UDIV_I128));
48030b57cec5SDimitry Andric     break;
48040b57cec5SDimitry Andric   case ISD::SDIVREM:
48050b57cec5SDimitry Andric   case ISD::UDIVREM:
48060b57cec5SDimitry Andric     // Expand into divrem libcall
48070b57cec5SDimitry Andric     ExpandDivRemLibCall(Node, Results);
48080b57cec5SDimitry Andric     break;
48090b57cec5SDimitry Andric   case ISD::MUL:
4810bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4811bdd1243dSDimitry Andric                                        RTLIB::MUL_I8,
4812bdd1243dSDimitry Andric                                        RTLIB::MUL_I16, RTLIB::MUL_I32,
4813bdd1243dSDimitry Andric                                        RTLIB::MUL_I64, RTLIB::MUL_I128));
48140b57cec5SDimitry Andric     break;
48150b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
48160b57cec5SDimitry Andric     switch (Node->getSimpleValueType(0).SimpleTy) {
48170b57cec5SDimitry Andric     default:
48180b57cec5SDimitry Andric       llvm_unreachable("LibCall explicitly requested, but not available");
48190b57cec5SDimitry Andric     case MVT::i32:
482006c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first);
48210b57cec5SDimitry Andric       break;
48220b57cec5SDimitry Andric     case MVT::i64:
482306c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first);
48240b57cec5SDimitry Andric       break;
48250b57cec5SDimitry Andric     case MVT::i128:
482606c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first);
48270b57cec5SDimitry Andric       break;
48280b57cec5SDimitry Andric     }
48290b57cec5SDimitry Andric     break;
483006c3fb27SDimitry Andric   case ISD::RESET_FPENV: {
483106c3fb27SDimitry Andric     // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
483206c3fb27SDimitry Andric     // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
483306c3fb27SDimitry Andric     SDValue Ptr = DAG.getIntPtrConstant(-1LL, dl);
483406c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
483506c3fb27SDimitry Andric     Results.push_back(
483606c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
483706c3fb27SDimitry Andric     break;
483806c3fb27SDimitry Andric   }
483906c3fb27SDimitry Andric   case ISD::GET_FPENV_MEM: {
484006c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
484106c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
484206c3fb27SDimitry Andric     Results.push_back(
484306c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
484406c3fb27SDimitry Andric     break;
484506c3fb27SDimitry Andric   }
484606c3fb27SDimitry Andric   case ISD::SET_FPENV_MEM: {
484706c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
484806c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
484906c3fb27SDimitry Andric     Results.push_back(
485006c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
485106c3fb27SDimitry Andric     break;
485206c3fb27SDimitry Andric   }
4853*5f757f3fSDimitry Andric   case ISD::GET_FPMODE: {
4854*5f757f3fSDimitry Andric     // Call fegetmode, which saves control modes into a stack slot. Then load
4855*5f757f3fSDimitry Andric     // the value to return from the stack.
4856*5f757f3fSDimitry Andric     EVT ModeVT = Node->getValueType(0);
4857*5f757f3fSDimitry Andric     SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
4858*5f757f3fSDimitry Andric     int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
4859*5f757f3fSDimitry Andric     SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr,
4860*5f757f3fSDimitry Andric                                               Node->getOperand(0), dl);
4861*5f757f3fSDimitry Andric     SDValue LdInst = DAG.getLoad(
4862*5f757f3fSDimitry Andric         ModeVT, dl, Chain, StackPtr,
4863*5f757f3fSDimitry Andric         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
4864*5f757f3fSDimitry Andric     Results.push_back(LdInst);
4865*5f757f3fSDimitry Andric     Results.push_back(LdInst.getValue(1));
4866*5f757f3fSDimitry Andric     break;
4867*5f757f3fSDimitry Andric   }
4868*5f757f3fSDimitry Andric   case ISD::SET_FPMODE: {
4869*5f757f3fSDimitry Andric     // Move control modes to stack slot and then call fesetmode with the pointer
4870*5f757f3fSDimitry Andric     // to the slot as argument.
4871*5f757f3fSDimitry Andric     SDValue Mode = Node->getOperand(1);
4872*5f757f3fSDimitry Andric     EVT ModeVT = Mode.getValueType();
4873*5f757f3fSDimitry Andric     SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
4874*5f757f3fSDimitry Andric     int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
4875*5f757f3fSDimitry Andric     SDValue StInst = DAG.getStore(
4876*5f757f3fSDimitry Andric         Node->getOperand(0), dl, Mode, StackPtr,
4877*5f757f3fSDimitry Andric         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
4878*5f757f3fSDimitry Andric     Results.push_back(
4879*5f757f3fSDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl));
4880*5f757f3fSDimitry Andric     break;
4881*5f757f3fSDimitry Andric   }
4882*5f757f3fSDimitry Andric   case ISD::RESET_FPMODE: {
4883*5f757f3fSDimitry Andric     // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets
4884*5f757f3fSDimitry Andric     // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the
4885*5f757f3fSDimitry Andric     // target must provide custom lowering.
4886*5f757f3fSDimitry Andric     const DataLayout &DL = DAG.getDataLayout();
4887*5f757f3fSDimitry Andric     EVT PtrTy = TLI.getPointerTy(DL);
4888*5f757f3fSDimitry Andric     SDValue Mode = DAG.getConstant(-1LL, dl, PtrTy);
4889*5f757f3fSDimitry Andric     Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode,
4890*5f757f3fSDimitry Andric                                                 Node->getOperand(0), dl));
4891*5f757f3fSDimitry Andric     break;
4892*5f757f3fSDimitry Andric   }
48930b57cec5SDimitry Andric   }
48940b57cec5SDimitry Andric 
48950b57cec5SDimitry Andric   // Replace the original node with the legalized result.
48960b57cec5SDimitry Andric   if (!Results.empty()) {
48970b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
48980b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
48990b57cec5SDimitry Andric   } else
49000b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
49010b57cec5SDimitry Andric }
49020b57cec5SDimitry Andric 
49030b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when
49040b57cec5SDimitry Andric // promoting equally sized vectors.
49050b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI,
49060b57cec5SDimitry Andric                                         MVT EltVT, MVT NewEltVT) {
49070b57cec5SDimitry Andric   unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
49080b57cec5SDimitry Andric   MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
49090b57cec5SDimitry Andric   assert(TLI.isTypeLegal(MidVT) && "unexpected");
49100b57cec5SDimitry Andric   return MidVT;
49110b57cec5SDimitry Andric }
49120b57cec5SDimitry Andric 
49130b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
49140b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to promote node\n");
49150b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
49160b57cec5SDimitry Andric   MVT OVT = Node->getSimpleValueType(0);
49170b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::UINT_TO_FP ||
49180b57cec5SDimitry Andric       Node->getOpcode() == ISD::SINT_TO_FP ||
49190b57cec5SDimitry Andric       Node->getOpcode() == ISD::SETCC ||
49200b57cec5SDimitry Andric       Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
49210b57cec5SDimitry Andric       Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
49220b57cec5SDimitry Andric     OVT = Node->getOperand(0).getSimpleValueType();
49230b57cec5SDimitry Andric   }
4924480093f4SDimitry Andric   if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
4925e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
4926e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCC ||
4927e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCCS)
4928480093f4SDimitry Andric     OVT = Node->getOperand(1).getSimpleValueType();
4929fe6060f1SDimitry Andric   if (Node->getOpcode() == ISD::BR_CC ||
4930fe6060f1SDimitry Andric       Node->getOpcode() == ISD::SELECT_CC)
49310b57cec5SDimitry Andric     OVT = Node->getOperand(2).getSimpleValueType();
49320b57cec5SDimitry Andric   MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
49330b57cec5SDimitry Andric   SDLoc dl(Node);
4934fe6060f1SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
49350b57cec5SDimitry Andric   switch (Node->getOpcode()) {
49360b57cec5SDimitry Andric   case ISD::CTTZ:
49370b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
49380b57cec5SDimitry Andric   case ISD::CTLZ:
49390b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
49400b57cec5SDimitry Andric   case ISD::CTPOP:
49415ffd83dbSDimitry Andric     // Zero extend the argument unless its cttz, then use any_extend.
49425ffd83dbSDimitry Andric     if (Node->getOpcode() == ISD::CTTZ ||
49435ffd83dbSDimitry Andric         Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
49445ffd83dbSDimitry Andric       Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
49455ffd83dbSDimitry Andric     else
49460b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
49475ffd83dbSDimitry Andric 
49480b57cec5SDimitry Andric     if (Node->getOpcode() == ISD::CTTZ) {
49490b57cec5SDimitry Andric       // The count is the same in the promoted type except if the original
49500b57cec5SDimitry Andric       // value was zero.  This can be handled by setting the bit just off
49510b57cec5SDimitry Andric       // the top of the original type.
49520b57cec5SDimitry Andric       auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
49530b57cec5SDimitry Andric                                         OVT.getSizeInBits());
49540b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
49550b57cec5SDimitry Andric                          DAG.getConstant(TopBit, dl, NVT));
49560b57cec5SDimitry Andric     }
49570b57cec5SDimitry Andric     // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
49580b57cec5SDimitry Andric     // already the correct result.
49590b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
49600b57cec5SDimitry Andric     if (Node->getOpcode() == ISD::CTLZ ||
49610b57cec5SDimitry Andric         Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) {
49620b57cec5SDimitry Andric       // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
49630b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
49640b57cec5SDimitry Andric                           DAG.getConstant(NVT.getSizeInBits() -
49650b57cec5SDimitry Andric                                           OVT.getSizeInBits(), dl, NVT));
49660b57cec5SDimitry Andric     }
49670b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
49680b57cec5SDimitry Andric     break;
49690b57cec5SDimitry Andric   case ISD::BITREVERSE:
49700b57cec5SDimitry Andric   case ISD::BSWAP: {
49710b57cec5SDimitry Andric     unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
49720b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
49730b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
49740b57cec5SDimitry Andric     Tmp1 = DAG.getNode(
49750b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
49760b57cec5SDimitry Andric         DAG.getConstant(DiffBits, dl,
49770b57cec5SDimitry Andric                         TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
49780b57cec5SDimitry Andric 
49790b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
49800b57cec5SDimitry Andric     break;
49810b57cec5SDimitry Andric   }
49820b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
4983480093f4SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
49840b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
4985480093f4SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
4986480093f4SDimitry Andric     PromoteLegalFP_TO_INT(Node, dl, Results);
49870b57cec5SDimitry Andric     break;
4988e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
4989e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
4990e8d8bef9SDimitry Andric     Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
4991e8d8bef9SDimitry Andric     break;
49920b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
4993480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
49940b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
4995480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
4996480093f4SDimitry Andric     PromoteLegalINT_TO_FP(Node, dl, Results);
49970b57cec5SDimitry Andric     break;
49980b57cec5SDimitry Andric   case ISD::VAARG: {
49990b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0); // Get the chain.
50000b57cec5SDimitry Andric     SDValue Ptr = Node->getOperand(1); // Get the pointer.
50010b57cec5SDimitry Andric 
50020b57cec5SDimitry Andric     unsigned TruncOp;
50030b57cec5SDimitry Andric     if (OVT.isVector()) {
50040b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
50050b57cec5SDimitry Andric     } else {
50060b57cec5SDimitry Andric       assert(OVT.isInteger()
50070b57cec5SDimitry Andric         && "VAARG promotion is supported only for vectors or integer types");
50080b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
50090b57cec5SDimitry Andric     }
50100b57cec5SDimitry Andric 
50110b57cec5SDimitry Andric     // Perform the larger operation, then convert back
50120b57cec5SDimitry Andric     Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
50130b57cec5SDimitry Andric              Node->getConstantOperandVal(3));
50140b57cec5SDimitry Andric     Chain = Tmp1.getValue(1);
50150b57cec5SDimitry Andric 
50160b57cec5SDimitry Andric     Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
50170b57cec5SDimitry Andric 
50180b57cec5SDimitry Andric     // Modified the chain result - switch anything that used the old chain to
50190b57cec5SDimitry Andric     // use the new one.
50200b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
50210b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
50220b57cec5SDimitry Andric     if (UpdatedNodes) {
50230b57cec5SDimitry Andric       UpdatedNodes->insert(Tmp2.getNode());
50240b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
50250b57cec5SDimitry Andric     }
50260b57cec5SDimitry Andric     ReplacedNode(Node);
50270b57cec5SDimitry Andric     break;
50280b57cec5SDimitry Andric   }
50290b57cec5SDimitry Andric   case ISD::MUL:
50300b57cec5SDimitry Andric   case ISD::SDIV:
50310b57cec5SDimitry Andric   case ISD::SREM:
50320b57cec5SDimitry Andric   case ISD::UDIV:
50330b57cec5SDimitry Andric   case ISD::UREM:
5034*5f757f3fSDimitry Andric   case ISD::SMIN:
5035*5f757f3fSDimitry Andric   case ISD::SMAX:
5036*5f757f3fSDimitry Andric   case ISD::UMIN:
5037*5f757f3fSDimitry Andric   case ISD::UMAX:
50380b57cec5SDimitry Andric   case ISD::AND:
50390b57cec5SDimitry Andric   case ISD::OR:
50400b57cec5SDimitry Andric   case ISD::XOR: {
50410b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
50420b57cec5SDimitry Andric     if (OVT.isVector()) {
50430b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
50440b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
50450b57cec5SDimitry Andric     } else {
50460b57cec5SDimitry Andric       assert(OVT.isInteger() && "Cannot promote logic operation");
50470b57cec5SDimitry Andric 
50480b57cec5SDimitry Andric       switch (Node->getOpcode()) {
50490b57cec5SDimitry Andric       default:
50500b57cec5SDimitry Andric         ExtOp = ISD::ANY_EXTEND;
50510b57cec5SDimitry Andric         break;
50520b57cec5SDimitry Andric       case ISD::SDIV:
50530b57cec5SDimitry Andric       case ISD::SREM:
5054*5f757f3fSDimitry Andric       case ISD::SMIN:
5055*5f757f3fSDimitry Andric       case ISD::SMAX:
50560b57cec5SDimitry Andric         ExtOp = ISD::SIGN_EXTEND;
50570b57cec5SDimitry Andric         break;
50580b57cec5SDimitry Andric       case ISD::UDIV:
50590b57cec5SDimitry Andric       case ISD::UREM:
50600b57cec5SDimitry Andric         ExtOp = ISD::ZERO_EXTEND;
50610b57cec5SDimitry Andric         break;
5062*5f757f3fSDimitry Andric       case ISD::UMIN:
5063*5f757f3fSDimitry Andric       case ISD::UMAX:
5064*5f757f3fSDimitry Andric         if (TLI.isSExtCheaperThanZExt(OVT, NVT))
5065*5f757f3fSDimitry Andric           ExtOp = ISD::SIGN_EXTEND;
5066*5f757f3fSDimitry Andric         else
5067*5f757f3fSDimitry Andric           ExtOp = ISD::ZERO_EXTEND;
5068*5f757f3fSDimitry Andric         break;
50690b57cec5SDimitry Andric       }
50700b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
50710b57cec5SDimitry Andric     }
50720b57cec5SDimitry Andric     // Promote each of the values to the new type.
50730b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
50740b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
50750b57cec5SDimitry Andric     // Perform the larger operation, then convert back
50760b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
50770b57cec5SDimitry Andric     Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
50780b57cec5SDimitry Andric     break;
50790b57cec5SDimitry Andric   }
50800b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
50810b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
50820b57cec5SDimitry Andric     // Promote to a multiply in a wider integer type.
50830b57cec5SDimitry Andric     unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
50840b57cec5SDimitry Andric                                                          : ISD::SIGN_EXTEND;
50850b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
50860b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
50870b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
50880b57cec5SDimitry Andric 
50890b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
50900b57cec5SDimitry Andric     unsigned OriginalSize = OVT.getScalarSizeInBits();
50910b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
50920b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
50930b57cec5SDimitry Andric         DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT)));
50940b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
50950b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
50960b57cec5SDimitry Andric     break;
50970b57cec5SDimitry Andric   }
50980b57cec5SDimitry Andric   case ISD::SELECT: {
50990b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
51000b57cec5SDimitry Andric     if (Node->getValueType(0).isVector() ||
51010b57cec5SDimitry Andric         Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
51020b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
51030b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
51040b57cec5SDimitry Andric     } else if (Node->getValueType(0).isInteger()) {
51050b57cec5SDimitry Andric       ExtOp   = ISD::ANY_EXTEND;
51060b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
51070b57cec5SDimitry Andric     } else {
51080b57cec5SDimitry Andric       ExtOp   = ISD::FP_EXTEND;
51090b57cec5SDimitry Andric       TruncOp = ISD::FP_ROUND;
51100b57cec5SDimitry Andric     }
51110b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
51120b57cec5SDimitry Andric     // Promote each of the values to the new type.
51130b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
51140b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
51150b57cec5SDimitry Andric     // Perform the larger operation, then round down.
51160b57cec5SDimitry Andric     Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
51170b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
51180b57cec5SDimitry Andric     if (TruncOp != ISD::FP_ROUND)
51190b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
51200b57cec5SDimitry Andric     else
51210b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
51220b57cec5SDimitry Andric                          DAG.getIntPtrConstant(0, dl));
51230b57cec5SDimitry Andric     Results.push_back(Tmp1);
51240b57cec5SDimitry Andric     break;
51250b57cec5SDimitry Andric   }
51260b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
51270b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
51280b57cec5SDimitry Andric 
51290b57cec5SDimitry Andric     // Cast the two input vectors.
51300b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
51310b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
51320b57cec5SDimitry Andric 
51330b57cec5SDimitry Andric     // Convert the shuffle mask to the right # elements.
51340b57cec5SDimitry Andric     Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
51350b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
51360b57cec5SDimitry Andric     Results.push_back(Tmp1);
51370b57cec5SDimitry Andric     break;
51380b57cec5SDimitry Andric   }
5139fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
5140fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5141fe6060f1SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5142fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2,
5143fe6060f1SDimitry Andric                        Node->getOperand(2));
5144fe6060f1SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5145fe6060f1SDimitry Andric     break;
5146fe6060f1SDimitry Andric   }
5147fe6060f1SDimitry Andric   case ISD::SELECT_CC: {
5148fe6060f1SDimitry Andric     SDValue Cond = Node->getOperand(4);
5149fe6060f1SDimitry Andric     ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5150fe6060f1SDimitry Andric     // Type of the comparison operands.
5151fe6060f1SDimitry Andric     MVT CVT = Node->getSimpleValueType(0);
5152fe6060f1SDimitry Andric     assert(CVT == OVT && "not handled");
5153fe6060f1SDimitry Andric 
5154fe6060f1SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
5155fe6060f1SDimitry Andric     if (NVT.isInteger()) {
5156fe6060f1SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5157fe6060f1SDimitry Andric     }
5158fe6060f1SDimitry Andric 
5159fe6060f1SDimitry Andric     // Promote the comparison operands, if needed.
5160fe6060f1SDimitry Andric     if (TLI.isCondCodeLegal(CCCode, CVT)) {
5161fe6060f1SDimitry Andric       Tmp1 = Node->getOperand(0);
5162fe6060f1SDimitry Andric       Tmp2 = Node->getOperand(1);
5163fe6060f1SDimitry Andric     } else {
5164fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5165fe6060f1SDimitry Andric       Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5166fe6060f1SDimitry Andric     }
5167fe6060f1SDimitry Andric     // Cast the true/false operands.
5168fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5169fe6060f1SDimitry Andric     Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5170fe6060f1SDimitry Andric 
5171fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5172fe6060f1SDimitry Andric                        Node->getFlags());
5173fe6060f1SDimitry Andric 
5174fe6060f1SDimitry Andric     // Cast the result back to the original type.
5175fe6060f1SDimitry Andric     if (ExtOp != ISD::FP_EXTEND)
5176fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5177fe6060f1SDimitry Andric     else
5178fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5179bdd1243dSDimitry Andric                          DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5180fe6060f1SDimitry Andric 
5181fe6060f1SDimitry Andric     Results.push_back(Tmp1);
5182fe6060f1SDimitry Andric     break;
5183fe6060f1SDimitry Andric   }
5184e8d8bef9SDimitry Andric   case ISD::SETCC:
5185e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCC:
5186e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCCS: {
51870b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
51880b57cec5SDimitry Andric     if (NVT.isInteger()) {
5189e8d8bef9SDimitry Andric       ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
5190*5f757f3fSDimitry Andric       if (isSignedIntSetCC(CCCode) ||
5191*5f757f3fSDimitry Andric           TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT))
5192*5f757f3fSDimitry Andric         ExtOp = ISD::SIGN_EXTEND;
5193*5f757f3fSDimitry Andric       else
5194*5f757f3fSDimitry Andric         ExtOp = ISD::ZERO_EXTEND;
51950b57cec5SDimitry Andric     }
5196e8d8bef9SDimitry Andric     if (Node->isStrictFPOpcode()) {
5197e8d8bef9SDimitry Andric       SDValue InChain = Node->getOperand(0);
5198e8d8bef9SDimitry Andric       std::tie(Tmp1, std::ignore) =
5199e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
5200e8d8bef9SDimitry Andric       std::tie(Tmp2, std::ignore) =
5201e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
5202e8d8bef9SDimitry Andric       SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
5203e8d8bef9SDimitry Andric       SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
5204e8d8bef9SDimitry Andric       SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
5205e8d8bef9SDimitry Andric       Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
5206e8d8bef9SDimitry Andric                                     {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
5207e8d8bef9SDimitry Andric                                     Node->getFlags()));
5208e8d8bef9SDimitry Andric       Results.push_back(Results.back().getValue(1));
5209e8d8bef9SDimitry Andric       break;
5210e8d8bef9SDimitry Andric     }
52110b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
52120b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
52130b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
52140b57cec5SDimitry Andric                                   Tmp2, Node->getOperand(2), Node->getFlags()));
52150b57cec5SDimitry Andric     break;
52160b57cec5SDimitry Andric   }
52170b57cec5SDimitry Andric   case ISD::BR_CC: {
52180b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
52190b57cec5SDimitry Andric     if (NVT.isInteger()) {
52200b57cec5SDimitry Andric       ISD::CondCode CCCode =
52210b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(1))->get();
52220b57cec5SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
52230b57cec5SDimitry Andric     }
52240b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
52250b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
52260b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
52270b57cec5SDimitry Andric                                   Node->getOperand(0), Node->getOperand(1),
52280b57cec5SDimitry Andric                                   Tmp1, Tmp2, Node->getOperand(4)));
52290b57cec5SDimitry Andric     break;
52300b57cec5SDimitry Andric   }
52310b57cec5SDimitry Andric   case ISD::FADD:
52320b57cec5SDimitry Andric   case ISD::FSUB:
52330b57cec5SDimitry Andric   case ISD::FMUL:
52340b57cec5SDimitry Andric   case ISD::FDIV:
52350b57cec5SDimitry Andric   case ISD::FREM:
52360b57cec5SDimitry Andric   case ISD::FMINNUM:
52370b57cec5SDimitry Andric   case ISD::FMAXNUM:
523806c3fb27SDimitry Andric   case ISD::FMINIMUM:
523906c3fb27SDimitry Andric   case ISD::FMAXIMUM:
52400b57cec5SDimitry Andric   case ISD::FPOW:
52410b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
52420b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
52430b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
52440b57cec5SDimitry Andric                        Node->getFlags());
5245bdd1243dSDimitry Andric     Results.push_back(
5246bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5247bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
52480b57cec5SDimitry Andric     break;
524981ad6265SDimitry Andric   case ISD::STRICT_FADD:
525081ad6265SDimitry Andric   case ISD::STRICT_FSUB:
525181ad6265SDimitry Andric   case ISD::STRICT_FMUL:
525281ad6265SDimitry Andric   case ISD::STRICT_FDIV:
525381ad6265SDimitry Andric   case ISD::STRICT_FMINNUM:
525481ad6265SDimitry Andric   case ISD::STRICT_FMAXNUM:
5255480093f4SDimitry Andric   case ISD::STRICT_FREM:
5256480093f4SDimitry Andric   case ISD::STRICT_FPOW:
5257480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5258480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5259480093f4SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5260480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
5261480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5262480093f4SDimitry Andric                        Tmp2.getValue(1));
5263480093f4SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5264480093f4SDimitry Andric                        {Tmp3, Tmp1, Tmp2});
5265480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5266480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)});
5267480093f4SDimitry Andric     Results.push_back(Tmp1);
5268480093f4SDimitry Andric     Results.push_back(Tmp1.getValue(1));
5269480093f4SDimitry Andric     break;
52700b57cec5SDimitry Andric   case ISD::FMA:
52710b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
52720b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
52730b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
52740b57cec5SDimitry Andric     Results.push_back(
52750b57cec5SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT,
52760b57cec5SDimitry Andric                     DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
5277bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
52780b57cec5SDimitry Andric     break;
527981ad6265SDimitry Andric   case ISD::STRICT_FMA:
528081ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
528181ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
528281ad6265SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
528381ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
528481ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
528581ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(3)});
528681ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
528781ad6265SDimitry Andric                        Tmp2.getValue(1), Tmp3.getValue(1));
528881ad6265SDimitry Andric     Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
528981ad6265SDimitry Andric                        {Tmp4, Tmp1, Tmp2, Tmp3});
529081ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
529181ad6265SDimitry Andric                        {Tmp4.getValue(1), Tmp4, DAG.getIntPtrConstant(0, dl)});
529281ad6265SDimitry Andric     Results.push_back(Tmp4);
529381ad6265SDimitry Andric     Results.push_back(Tmp4.getValue(1));
529481ad6265SDimitry Andric     break;
52950b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
529606c3fb27SDimitry Andric   case ISD::FLDEXP:
52970b57cec5SDimitry Andric   case ISD::FPOWI: {
52980b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
52990b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
53000b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
53010b57cec5SDimitry Andric 
53020b57cec5SDimitry Andric     // fcopysign doesn't change anything but the sign bit, so
53030b57cec5SDimitry Andric     //   (fp_round (fcopysign (fpext a), b))
53040b57cec5SDimitry Andric     // is as precise as
53050b57cec5SDimitry Andric     //   (fp_round (fpext a))
53060b57cec5SDimitry Andric     // which is a no-op. Mark it as a TRUNCating FP_ROUND.
53070b57cec5SDimitry Andric     const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
5308bdd1243dSDimitry Andric     Results.push_back(
5309bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5310bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
53110b57cec5SDimitry Andric     break;
53120b57cec5SDimitry Andric   }
531381ad6265SDimitry Andric   case ISD::STRICT_FPOWI:
531481ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
531581ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
531681ad6265SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
531781ad6265SDimitry Andric                        {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
531881ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
531981ad6265SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
532081ad6265SDimitry Andric     Results.push_back(Tmp3);
532181ad6265SDimitry Andric     Results.push_back(Tmp3.getValue(1));
532281ad6265SDimitry Andric     break;
532306c3fb27SDimitry Andric   case ISD::FFREXP: {
532406c3fb27SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
532506c3fb27SDimitry Andric     Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
532606c3fb27SDimitry Andric 
532706c3fb27SDimitry Andric     Results.push_back(
532806c3fb27SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
532906c3fb27SDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
533006c3fb27SDimitry Andric 
533106c3fb27SDimitry Andric     Results.push_back(Tmp2.getValue(1));
533206c3fb27SDimitry Andric     break;
533306c3fb27SDimitry Andric   }
53340b57cec5SDimitry Andric   case ISD::FFLOOR:
53350b57cec5SDimitry Andric   case ISD::FCEIL:
53360b57cec5SDimitry Andric   case ISD::FRINT:
53370b57cec5SDimitry Andric   case ISD::FNEARBYINT:
53380b57cec5SDimitry Andric   case ISD::FROUND:
53395ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
53400b57cec5SDimitry Andric   case ISD::FTRUNC:
53410b57cec5SDimitry Andric   case ISD::FNEG:
53420b57cec5SDimitry Andric   case ISD::FSQRT:
53430b57cec5SDimitry Andric   case ISD::FSIN:
53440b57cec5SDimitry Andric   case ISD::FCOS:
53450b57cec5SDimitry Andric   case ISD::FLOG:
53460b57cec5SDimitry Andric   case ISD::FLOG2:
53470b57cec5SDimitry Andric   case ISD::FLOG10:
53480b57cec5SDimitry Andric   case ISD::FABS:
53490b57cec5SDimitry Andric   case ISD::FEXP:
53500b57cec5SDimitry Andric   case ISD::FEXP2:
5351*5f757f3fSDimitry Andric   case ISD::FEXP10:
53520b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
53530b57cec5SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5354bdd1243dSDimitry Andric     Results.push_back(
5355bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5356bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
53570b57cec5SDimitry Andric     break;
5358480093f4SDimitry Andric   case ISD::STRICT_FFLOOR:
5359480093f4SDimitry Andric   case ISD::STRICT_FCEIL:
536081ad6265SDimitry Andric   case ISD::STRICT_FRINT:
536181ad6265SDimitry Andric   case ISD::STRICT_FNEARBYINT:
5362349cc55cSDimitry Andric   case ISD::STRICT_FROUND:
536381ad6265SDimitry Andric   case ISD::STRICT_FROUNDEVEN:
536481ad6265SDimitry Andric   case ISD::STRICT_FTRUNC:
536581ad6265SDimitry Andric   case ISD::STRICT_FSQRT:
5366480093f4SDimitry Andric   case ISD::STRICT_FSIN:
5367480093f4SDimitry Andric   case ISD::STRICT_FCOS:
5368480093f4SDimitry Andric   case ISD::STRICT_FLOG:
536981ad6265SDimitry Andric   case ISD::STRICT_FLOG2:
5370480093f4SDimitry Andric   case ISD::STRICT_FLOG10:
5371480093f4SDimitry Andric   case ISD::STRICT_FEXP:
537281ad6265SDimitry Andric   case ISD::STRICT_FEXP2:
5373480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5374480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5375480093f4SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5376480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1});
5377480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5378480093f4SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
5379480093f4SDimitry Andric     Results.push_back(Tmp3);
5380480093f4SDimitry Andric     Results.push_back(Tmp3.getValue(1));
5381480093f4SDimitry Andric     break;
53820b57cec5SDimitry Andric   case ISD::BUILD_VECTOR: {
53830b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
53840b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
53850b57cec5SDimitry Andric 
53860b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
53870b57cec5SDimitry Andric     //
53880b57cec5SDimitry Andric     // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
53890b57cec5SDimitry Andric     //  =>
53900b57cec5SDimitry Andric     //  v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
53910b57cec5SDimitry Andric 
53920b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
53930b57cec5SDimitry Andric            "Invalid promote type for build_vector");
53940b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
53950b57cec5SDimitry Andric 
53960b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
53970b57cec5SDimitry Andric 
53980b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
53990b57cec5SDimitry Andric     for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
54000b57cec5SDimitry Andric       SDValue Op = Node->getOperand(I);
54010b57cec5SDimitry Andric       NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
54020b57cec5SDimitry Andric     }
54030b57cec5SDimitry Andric 
54040b57cec5SDimitry Andric     SDLoc SL(Node);
54050b57cec5SDimitry Andric     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps);
54060b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
54070b57cec5SDimitry Andric     Results.push_back(CvtVec);
54080b57cec5SDimitry Andric     break;
54090b57cec5SDimitry Andric   }
54100b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT: {
54110b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
54120b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
54130b57cec5SDimitry Andric 
54140b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size.
54150b57cec5SDimitry Andric     //
54160b57cec5SDimitry Andric     // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
54170b57cec5SDimitry Andric     //  =>
54180b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
54190b57cec5SDimitry Andric     //
54200b57cec5SDimitry Andric     // i64 = bitcast
54210b57cec5SDimitry Andric     //   (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
54220b57cec5SDimitry Andric     //                       (i32 (extract_vector_elt castx, (2 * y + 1)))
54230b57cec5SDimitry Andric     //
54240b57cec5SDimitry Andric 
54250b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
54260b57cec5SDimitry Andric            "Invalid promote type for extract_vector_elt");
54270b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
54280b57cec5SDimitry Andric 
54290b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
54300b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
54310b57cec5SDimitry Andric 
54320b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(1);
54330b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
54340b57cec5SDimitry Andric     SDLoc SL(Node);
54350b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
54360b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
54370b57cec5SDimitry Andric 
54380b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
54390b57cec5SDimitry Andric 
54400b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
54410b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
54420b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
54430b57cec5SDimitry Andric       SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
54440b57cec5SDimitry Andric 
54450b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
54460b57cec5SDimitry Andric                                 CastVec, TmpIdx);
54470b57cec5SDimitry Andric       NewOps.push_back(Elt);
54480b57cec5SDimitry Andric     }
54490b57cec5SDimitry Andric 
54500b57cec5SDimitry Andric     SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
54510b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
54520b57cec5SDimitry Andric     break;
54530b57cec5SDimitry Andric   }
54540b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT: {
54550b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
54560b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
54570b57cec5SDimitry Andric 
54580b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
54590b57cec5SDimitry Andric     //
54600b57cec5SDimitry Andric     // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
54610b57cec5SDimitry Andric     //  =>
54620b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
54630b57cec5SDimitry Andric     //  v2i32:casty = bitcast y:i64
54640b57cec5SDimitry Andric     //
54650b57cec5SDimitry Andric     // v2i64 = bitcast
54660b57cec5SDimitry Andric     //   (v4i32 insert_vector_elt
54670b57cec5SDimitry Andric     //       (v4i32 insert_vector_elt v4i32:castx,
54680b57cec5SDimitry Andric     //                                (extract_vector_elt casty, 0), 2 * z),
54690b57cec5SDimitry Andric     //        (extract_vector_elt casty, 1), (2 * z + 1))
54700b57cec5SDimitry Andric 
54710b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
54720b57cec5SDimitry Andric            "Invalid promote type for insert_vector_elt");
54730b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
54740b57cec5SDimitry Andric 
54750b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
54760b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
54770b57cec5SDimitry Andric 
54780b57cec5SDimitry Andric     SDValue Val = Node->getOperand(1);
54790b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(2);
54800b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
54810b57cec5SDimitry Andric     SDLoc SL(Node);
54820b57cec5SDimitry Andric 
54830b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
54840b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
54850b57cec5SDimitry Andric 
54860b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
54870b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
54880b57cec5SDimitry Andric 
54890b57cec5SDimitry Andric     SDValue NewVec = CastVec;
54900b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
54910b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
54920b57cec5SDimitry Andric       SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
54930b57cec5SDimitry Andric 
54940b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
54950b57cec5SDimitry Andric                                 CastVal, IdxOffset);
54960b57cec5SDimitry Andric 
54970b57cec5SDimitry Andric       NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
54980b57cec5SDimitry Andric                            NewVec, Elt, InEltIdx);
54990b57cec5SDimitry Andric     }
55000b57cec5SDimitry Andric 
55010b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
55020b57cec5SDimitry Andric     break;
55030b57cec5SDimitry Andric   }
55040b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR: {
55050b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
55060b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
55070b57cec5SDimitry Andric 
55080b57cec5SDimitry Andric     // Handle bitcasts to different vector type with the same total bit size.
55090b57cec5SDimitry Andric     //
55100b57cec5SDimitry Andric     // e.g. v2i64 = scalar_to_vector x:i64
55110b57cec5SDimitry Andric     //   =>
55120b57cec5SDimitry Andric     //  concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
55130b57cec5SDimitry Andric     //
55140b57cec5SDimitry Andric 
55150b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
55160b57cec5SDimitry Andric     SDValue Val = Node->getOperand(0);
55170b57cec5SDimitry Andric     SDLoc SL(Node);
55180b57cec5SDimitry Andric 
55190b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
55200b57cec5SDimitry Andric     SDValue Undef = DAG.getUNDEF(MidVT);
55210b57cec5SDimitry Andric 
55220b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewElts;
55230b57cec5SDimitry Andric     NewElts.push_back(CastVal);
55240b57cec5SDimitry Andric     for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
55250b57cec5SDimitry Andric       NewElts.push_back(Undef);
55260b57cec5SDimitry Andric 
55270b57cec5SDimitry Andric     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
55280b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
55290b57cec5SDimitry Andric     Results.push_back(CvtVec);
55300b57cec5SDimitry Andric     break;
55310b57cec5SDimitry Andric   }
55320b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP: {
55330b57cec5SDimitry Andric     AtomicSDNode *AM = cast<AtomicSDNode>(Node);
55340b57cec5SDimitry Andric     SDLoc SL(Node);
55350b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
55360b57cec5SDimitry Andric     assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
55370b57cec5SDimitry Andric            "unexpected promotion type");
55380b57cec5SDimitry Andric     assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
55390b57cec5SDimitry Andric            "unexpected atomic_swap with illegal type");
55400b57cec5SDimitry Andric 
55410b57cec5SDimitry Andric     SDValue NewAtomic
55420b57cec5SDimitry Andric       = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT,
55430b57cec5SDimitry Andric                       DAG.getVTList(NVT, MVT::Other),
55440b57cec5SDimitry Andric                       { AM->getChain(), AM->getBasePtr(), CastVal },
55450b57cec5SDimitry Andric                       AM->getMemOperand());
55460b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
55470b57cec5SDimitry Andric     Results.push_back(NewAtomic.getValue(1));
55480b57cec5SDimitry Andric     break;
55490b57cec5SDimitry Andric   }
5550*5f757f3fSDimitry Andric   case ISD::SPLAT_VECTOR: {
5551*5f757f3fSDimitry Andric     SDValue Scalar = Node->getOperand(0);
5552*5f757f3fSDimitry Andric     MVT ScalarType = Scalar.getSimpleValueType();
5553*5f757f3fSDimitry Andric     MVT NewScalarType = NVT.getVectorElementType();
5554*5f757f3fSDimitry Andric     if (ScalarType.isInteger()) {
5555*5f757f3fSDimitry Andric       Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar);
5556*5f757f3fSDimitry Andric       Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5557*5f757f3fSDimitry Andric       Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
5558*5f757f3fSDimitry Andric       break;
5559*5f757f3fSDimitry Andric     }
5560*5f757f3fSDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar);
5561*5f757f3fSDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5562*5f757f3fSDimitry Andric     Results.push_back(
5563*5f757f3fSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5564*5f757f3fSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
5565*5f757f3fSDimitry Andric     break;
5566*5f757f3fSDimitry Andric   }
55670b57cec5SDimitry Andric   }
55680b57cec5SDimitry Andric 
55690b57cec5SDimitry Andric   // Replace the original node with the legalized result.
55700b57cec5SDimitry Andric   if (!Results.empty()) {
55710b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
55720b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
55730b57cec5SDimitry Andric   } else
55740b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not promote node\n");
55750b57cec5SDimitry Andric }
55760b57cec5SDimitry Andric 
55770b57cec5SDimitry Andric /// This is the entry point for the file.
55780b57cec5SDimitry Andric void SelectionDAG::Legalize() {
55790b57cec5SDimitry Andric   AssignTopologicalOrder();
55800b57cec5SDimitry Andric 
55810b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
55820b57cec5SDimitry Andric   // Use a delete listener to remove nodes which were deleted during
55830b57cec5SDimitry Andric   // legalization from LegalizeNodes. This is needed to handle the situation
55840b57cec5SDimitry Andric   // where a new node is allocated by the object pool to the same address of a
55850b57cec5SDimitry Andric   // previously deleted node.
55860b57cec5SDimitry Andric   DAGNodeDeletedListener DeleteListener(
55870b57cec5SDimitry Andric       *this,
55880b57cec5SDimitry Andric       [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
55890b57cec5SDimitry Andric 
55900b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
55910b57cec5SDimitry Andric 
55920b57cec5SDimitry Andric   // Visit all the nodes. We start in topological order, so that we see
55930b57cec5SDimitry Andric   // nodes with their original operands intact. Legalization can produce
55940b57cec5SDimitry Andric   // new nodes which may themselves need to be legalized. Iterate until all
55950b57cec5SDimitry Andric   // nodes have been legalized.
55960b57cec5SDimitry Andric   while (true) {
55970b57cec5SDimitry Andric     bool AnyLegalized = false;
55980b57cec5SDimitry Andric     for (auto NI = allnodes_end(); NI != allnodes_begin();) {
55990b57cec5SDimitry Andric       --NI;
56000b57cec5SDimitry Andric 
56010b57cec5SDimitry Andric       SDNode *N = &*NI;
56020b57cec5SDimitry Andric       if (N->use_empty() && N != getRoot().getNode()) {
56030b57cec5SDimitry Andric         ++NI;
56040b57cec5SDimitry Andric         DeleteNode(N);
56050b57cec5SDimitry Andric         continue;
56060b57cec5SDimitry Andric       }
56070b57cec5SDimitry Andric 
56080b57cec5SDimitry Andric       if (LegalizedNodes.insert(N).second) {
56090b57cec5SDimitry Andric         AnyLegalized = true;
56100b57cec5SDimitry Andric         Legalizer.LegalizeOp(N);
56110b57cec5SDimitry Andric 
56120b57cec5SDimitry Andric         if (N->use_empty() && N != getRoot().getNode()) {
56130b57cec5SDimitry Andric           ++NI;
56140b57cec5SDimitry Andric           DeleteNode(N);
56150b57cec5SDimitry Andric         }
56160b57cec5SDimitry Andric       }
56170b57cec5SDimitry Andric     }
56180b57cec5SDimitry Andric     if (!AnyLegalized)
56190b57cec5SDimitry Andric       break;
56200b57cec5SDimitry Andric 
56210b57cec5SDimitry Andric   }
56220b57cec5SDimitry Andric 
56230b57cec5SDimitry Andric   // Remove dead nodes now.
56240b57cec5SDimitry Andric   RemoveDeadNodes();
56250b57cec5SDimitry Andric }
56260b57cec5SDimitry Andric 
56270b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N,
56280b57cec5SDimitry Andric                               SmallSetVector<SDNode *, 16> &UpdatedNodes) {
56290b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
56300b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
56310b57cec5SDimitry Andric 
56320b57cec5SDimitry Andric   // Directly insert the node in question, and legalize it. This will recurse
56330b57cec5SDimitry Andric   // as needed through operands.
56340b57cec5SDimitry Andric   LegalizedNodes.insert(N);
56350b57cec5SDimitry Andric   Legalizer.LegalizeOp(N);
56360b57cec5SDimitry Andric 
56370b57cec5SDimitry Andric   return LegalizedNodes.count(N);
56380b57cec5SDimitry Andric }
5639