xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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"
215f757f3fSDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
228bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
230b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h"
24*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h"
28*0fca6ea1SDimitry Andric #include "llvm/CodeGen/RuntimeLibcallUtil.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"
35*0fca6ea1SDimitry Andric #include "llvm/CodeGenTypes/MachineValueType.h"
360b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
370b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
380b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
390b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
400b57cec5SDimitry Andric #include "llvm/IR/Function.h"
410b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
420b57cec5SDimitry Andric #include "llvm/IR/Type.h"
430b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
440b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
450b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
460b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
470b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
480b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
490b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
500b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h"
510b57cec5SDimitry Andric #include <cassert>
520b57cec5SDimitry Andric #include <cstdint>
530b57cec5SDimitry Andric #include <tuple>
540b57cec5SDimitry Andric #include <utility>
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric using namespace llvm;
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric #define DEBUG_TYPE "legalizedag"
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric namespace {
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric /// Keeps track of state when getting the sign of a floating-point value as an
630b57cec5SDimitry Andric /// integer.
640b57cec5SDimitry Andric struct FloatSignAsInt {
650b57cec5SDimitry Andric   EVT FloatVT;
660b57cec5SDimitry Andric   SDValue Chain;
670b57cec5SDimitry Andric   SDValue FloatPtr;
680b57cec5SDimitry Andric   SDValue IntPtr;
690b57cec5SDimitry Andric   MachinePointerInfo IntPointerInfo;
700b57cec5SDimitry Andric   MachinePointerInfo FloatPointerInfo;
710b57cec5SDimitry Andric   SDValue IntValue;
720b57cec5SDimitry Andric   APInt SignMask;
730b57cec5SDimitry Andric   uint8_t SignBit;
740b57cec5SDimitry Andric };
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
770b57cec5SDimitry Andric /// This takes an arbitrary SelectionDAG as input and
780b57cec5SDimitry Andric /// hacks on it until the target machine can handle it.  This involves
790b57cec5SDimitry Andric /// eliminating value sizes the machine cannot handle (promoting small sizes to
800b57cec5SDimitry Andric /// large sizes or splitting up large values into small values) as well as
810b57cec5SDimitry Andric /// eliminating operations the machine cannot handle.
820b57cec5SDimitry Andric ///
830b57cec5SDimitry Andric /// This code also does a small amount of optimization and recognition of idioms
840b57cec5SDimitry Andric /// as part of its processing.  For example, if a target does not support a
850b57cec5SDimitry Andric /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
860b57cec5SDimitry Andric /// will attempt merge setcc and brc instructions into brcc's.
870b57cec5SDimitry Andric class SelectionDAGLegalize {
880b57cec5SDimitry Andric   const TargetMachine &TM;
890b57cec5SDimitry Andric   const TargetLowering &TLI;
900b57cec5SDimitry Andric   SelectionDAG &DAG;
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric   /// The set of nodes which have already been legalized. We hold a
930b57cec5SDimitry Andric   /// reference to it in order to update as necessary on node deletion.
940b57cec5SDimitry Andric   SmallPtrSetImpl<SDNode *> &LegalizedNodes;
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   /// A set of all the nodes updated during legalization.
970b57cec5SDimitry Andric   SmallSetVector<SDNode *, 16> *UpdatedNodes;
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   EVT getSetCCResultType(EVT VT) const {
1000b57cec5SDimitry Andric     return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1010b57cec5SDimitry Andric   }
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric   // Libcall insertion helpers.
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric public:
1060b57cec5SDimitry Andric   SelectionDAGLegalize(SelectionDAG &DAG,
1070b57cec5SDimitry Andric                        SmallPtrSetImpl<SDNode *> &LegalizedNodes,
1080b57cec5SDimitry Andric                        SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr)
1090b57cec5SDimitry Andric       : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG),
1100b57cec5SDimitry Andric         LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {}
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   /// Legalizes the given operation.
1130b57cec5SDimitry Andric   void LegalizeOp(SDNode *Node);
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric private:
1160b57cec5SDimitry Andric   SDValue OptimizeFloatStore(StoreSDNode *ST);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric   void LegalizeLoadOps(SDNode *Node);
1190b57cec5SDimitry Andric   void LegalizeStoreOps(SDNode *Node);
1200b57cec5SDimitry Andric 
121*0fca6ea1SDimitry Andric   SDValue ExpandINSERT_VECTOR_ELT(SDValue Op);
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   /// Return a vector shuffle operation which
1240b57cec5SDimitry Andric   /// performs the same shuffe in terms of order or result bytes, but on a type
1250b57cec5SDimitry Andric   /// whose vector element type is narrower than the original shuffle type.
1260b57cec5SDimitry Andric   /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
1270b57cec5SDimitry Andric   SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl,
1280b57cec5SDimitry Andric                                      SDValue N1, SDValue N2,
1290b57cec5SDimitry Andric                                      ArrayRef<int> Mask) const;
1300b57cec5SDimitry Andric 
13106c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
13206c3fb27SDimitry Andric                         TargetLowering::ArgListTy &&Args, bool isSigned);
13306c3fb27SDimitry Andric   std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned);
1340b57cec5SDimitry Andric 
13506c3fb27SDimitry Andric   void ExpandFrexpLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
136fe6060f1SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC,
137fe6060f1SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
138480093f4SDimitry Andric   void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32,
1390b57cec5SDimitry Andric                        RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80,
1400b57cec5SDimitry Andric                        RTLIB::Libcall Call_F128,
141480093f4SDimitry Andric                        RTLIB::Libcall Call_PPCF128,
142480093f4SDimitry Andric                        SmallVectorImpl<SDValue> &Results);
143bdd1243dSDimitry Andric   SDValue ExpandIntLibCall(SDNode *Node, bool isSigned,
144bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I8,
145bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I16,
146bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I32,
147bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I64,
148bdd1243dSDimitry Andric                            RTLIB::Libcall Call_I128);
149480093f4SDimitry Andric   void ExpandArgFPLibCall(SDNode *Node,
1500b57cec5SDimitry Andric                           RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64,
1510b57cec5SDimitry Andric                           RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128,
152480093f4SDimitry Andric                           RTLIB::Libcall Call_PPCF128,
153480093f4SDimitry Andric                           SmallVectorImpl<SDValue> &Results);
1540b57cec5SDimitry Andric   void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1550b57cec5SDimitry Andric   void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results);
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1580b57cec5SDimitry Andric                            const SDLoc &dl);
1590b57cec5SDimitry Andric   SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT,
1600b57cec5SDimitry Andric                            const SDLoc &dl, SDValue ChainIn);
1610b57cec5SDimitry Andric   SDValue ExpandBUILD_VECTOR(SDNode *Node);
1628bcb0991SDimitry Andric   SDValue ExpandSPLAT_VECTOR(SDNode *Node);
1630b57cec5SDimitry Andric   SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
1640b57cec5SDimitry Andric   void ExpandDYNAMIC_STACKALLOC(SDNode *Node,
1650b57cec5SDimitry Andric                                 SmallVectorImpl<SDValue> &Results);
1660b57cec5SDimitry Andric   void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL,
1670b57cec5SDimitry Andric                          SDValue Value) const;
1680b57cec5SDimitry Andric   SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL,
1690b57cec5SDimitry Andric                           SDValue NewIntValue) const;
1700b57cec5SDimitry Andric   SDValue ExpandFCOPYSIGN(SDNode *Node) const;
1710b57cec5SDimitry Andric   SDValue ExpandFABS(SDNode *Node) const;
172e8d8bef9SDimitry Andric   SDValue ExpandFNEG(SDNode *Node) const;
17306c3fb27SDimitry Andric   SDValue expandLdexp(SDNode *Node) const;
17406c3fb27SDimitry Andric   SDValue expandFrexp(SDNode *Node) const;
17506c3fb27SDimitry Andric 
176480093f4SDimitry Andric   SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain);
177480093f4SDimitry Andric   void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl,
178480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
179480093f4SDimitry Andric   void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
180480093f4SDimitry Andric                              SmallVectorImpl<SDValue> &Results);
181e8d8bef9SDimitry Andric   SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl);
1820b57cec5SDimitry Andric 
183*0fca6ea1SDimitry Andric   /// Implements vector reduce operation promotion.
184*0fca6ea1SDimitry Andric   ///
185*0fca6ea1SDimitry Andric   /// All vector operands are promoted to a vector type with larger element
186*0fca6ea1SDimitry Andric   /// type, and the start value is promoted to a larger scalar type. Then the
187*0fca6ea1SDimitry Andric   /// result is truncated back to the original scalar type.
188*0fca6ea1SDimitry Andric   SDValue PromoteReduction(SDNode *Node);
189*0fca6ea1SDimitry Andric 
190e8d8bef9SDimitry Andric   SDValue ExpandPARITY(SDValue Op, const SDLoc &dl);
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric   SDValue ExpandExtractFromVectorThroughStack(SDValue Op);
1930b57cec5SDimitry Andric   SDValue ExpandInsertToVectorThroughStack(SDValue Op);
1940b57cec5SDimitry Andric   SDValue ExpandVectorBuildThroughStack(SDNode* Node);
1950b57cec5SDimitry Andric 
1960b57cec5SDimitry Andric   SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP);
1970b57cec5SDimitry Andric   SDValue ExpandConstant(ConstantSDNode *CP);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric   // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall
2000b57cec5SDimitry Andric   bool ExpandNode(SDNode *Node);
2010b57cec5SDimitry Andric   void ConvertNodeToLibcall(SDNode *Node);
2020b57cec5SDimitry Andric   void PromoteNode(SDNode *Node);
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric public:
2050b57cec5SDimitry Andric   // Node replacement helpers
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric   void ReplacedNode(SDNode *N) {
2080b57cec5SDimitry Andric     LegalizedNodes.erase(N);
2090b57cec5SDimitry Andric     if (UpdatedNodes)
2100b57cec5SDimitry Andric       UpdatedNodes->insert(N);
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, SDNode *New) {
2140b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2150b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     assert(Old->getNumValues() == New->getNumValues() &&
2180b57cec5SDimitry Andric            "Replacing one node with another that produces a different number "
2190b57cec5SDimitry Andric            "of values!");
2200b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2210b57cec5SDimitry Andric     if (UpdatedNodes)
2220b57cec5SDimitry Andric       UpdatedNodes->insert(New);
2230b57cec5SDimitry Andric     ReplacedNode(Old);
2240b57cec5SDimitry Andric   }
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   void ReplaceNode(SDValue Old, SDValue New) {
2270b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2280b57cec5SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2310b57cec5SDimitry Andric     if (UpdatedNodes)
2320b57cec5SDimitry Andric       UpdatedNodes->insert(New.getNode());
2330b57cec5SDimitry Andric     ReplacedNode(Old.getNode());
2340b57cec5SDimitry Andric   }
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric   void ReplaceNode(SDNode *Old, const SDValue *New) {
2370b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG));
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     DAG.ReplaceAllUsesWith(Old, New);
2400b57cec5SDimitry Andric     for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) {
2410b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << (i == 0 ? "     with:      " : "      and:      ");
2420b57cec5SDimitry Andric                  New[i]->dump(&DAG));
2430b57cec5SDimitry Andric       if (UpdatedNodes)
2440b57cec5SDimitry Andric         UpdatedNodes->insert(New[i].getNode());
2450b57cec5SDimitry Andric     }
2460b57cec5SDimitry Andric     ReplacedNode(Old);
2470b57cec5SDimitry Andric   }
2488bcb0991SDimitry Andric 
2498bcb0991SDimitry Andric   void ReplaceNodeWithValue(SDValue Old, SDValue New) {
2508bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG);
2518bcb0991SDimitry Andric                dbgs() << "     with:      "; New->dump(&DAG));
2528bcb0991SDimitry Andric 
2538bcb0991SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Old, New);
2548bcb0991SDimitry Andric     if (UpdatedNodes)
2558bcb0991SDimitry Andric       UpdatedNodes->insert(New.getNode());
2568bcb0991SDimitry Andric     ReplacedNode(Old.getNode());
2578bcb0991SDimitry Andric   }
2580b57cec5SDimitry Andric };
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric } // end anonymous namespace
2610b57cec5SDimitry Andric 
262*0fca6ea1SDimitry Andric // Helper function that generates an MMO that considers the alignment of the
263*0fca6ea1SDimitry Andric // stack, and the size of the stack object
264*0fca6ea1SDimitry Andric static MachineMemOperand *getStackAlignedMMO(SDValue StackPtr,
265*0fca6ea1SDimitry Andric                                              MachineFunction &MF,
266*0fca6ea1SDimitry Andric                                              bool isObjectScalable) {
267*0fca6ea1SDimitry Andric   auto &MFI = MF.getFrameInfo();
268*0fca6ea1SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr)->getIndex();
269*0fca6ea1SDimitry Andric   MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FI);
270*0fca6ea1SDimitry Andric   LocationSize ObjectSize = isObjectScalable
271*0fca6ea1SDimitry Andric                                 ? LocationSize::beforeOrAfterPointer()
272*0fca6ea1SDimitry Andric                                 : LocationSize::precise(MFI.getObjectSize(FI));
273*0fca6ea1SDimitry Andric   return MF.getMachineMemOperand(PtrInfo, MachineMemOperand::MOStore,
274*0fca6ea1SDimitry Andric                                  ObjectSize, MFI.getObjectAlign(FI));
275*0fca6ea1SDimitry Andric }
276*0fca6ea1SDimitry Andric 
2770b57cec5SDimitry Andric /// Return a vector shuffle operation which
2780b57cec5SDimitry Andric /// performs the same shuffle in terms of order or result bytes, but on a type
2790b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type.
2800b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3>
2810b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType(
2820b57cec5SDimitry Andric     EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2,
2830b57cec5SDimitry Andric     ArrayRef<int> Mask) const {
2840b57cec5SDimitry Andric   unsigned NumMaskElts = VT.getVectorNumElements();
2850b57cec5SDimitry Andric   unsigned NumDestElts = NVT.getVectorNumElements();
2860b57cec5SDimitry Andric   unsigned NumEltsGrowth = NumDestElts / NumMaskElts;
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric   if (NumEltsGrowth == 1)
2910b57cec5SDimitry Andric     return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   SmallVector<int, 8> NewMask;
2940b57cec5SDimitry Andric   for (unsigned i = 0; i != NumMaskElts; ++i) {
2950b57cec5SDimitry Andric     int Idx = Mask[i];
2960b57cec5SDimitry Andric     for (unsigned j = 0; j != NumEltsGrowth; ++j) {
2970b57cec5SDimitry Andric       if (Idx < 0)
2980b57cec5SDimitry Andric         NewMask.push_back(-1);
2990b57cec5SDimitry Andric       else
3000b57cec5SDimitry Andric         NewMask.push_back(Idx * NumEltsGrowth + j);
3010b57cec5SDimitry Andric     }
3020b57cec5SDimitry Andric   }
3030b57cec5SDimitry Andric   assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?");
3040b57cec5SDimitry Andric   assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?");
3050b57cec5SDimitry Andric   return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask);
3060b57cec5SDimitry Andric }
3070b57cec5SDimitry Andric 
3080b57cec5SDimitry Andric /// Expands the ConstantFP node to an integer constant or
3090b57cec5SDimitry Andric /// a load from the constant pool.
3100b57cec5SDimitry Andric SDValue
3110b57cec5SDimitry Andric SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) {
3120b57cec5SDimitry Andric   bool Extend = false;
3130b57cec5SDimitry Andric   SDLoc dl(CFP);
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   // If a FP immediate is precise when represented as a float and if the
3160b57cec5SDimitry Andric   // target can do an extending load from float to double, we put it into
3170b57cec5SDimitry Andric   // the constant pool as a float, even if it's is statically typed as a
3180b57cec5SDimitry Andric   // double.  This shrinks FP constants and canonicalizes them for targets where
3190b57cec5SDimitry Andric   // an FP extending load is the same cost as a normal load (such as on the x87
3200b57cec5SDimitry Andric   // fp stack or PPC FP unit).
3210b57cec5SDimitry Andric   EVT VT = CFP->getValueType(0);
3220b57cec5SDimitry Andric   ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
3230b57cec5SDimitry Andric   if (!UseCP) {
3240b57cec5SDimitry Andric     assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion");
3250b57cec5SDimitry Andric     return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl,
3260b57cec5SDimitry Andric                            (VT == MVT::f64) ? MVT::i64 : MVT::i32);
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   APFloat APF = CFP->getValueAPF();
3300b57cec5SDimitry Andric   EVT OrigVT = VT;
3310b57cec5SDimitry Andric   EVT SVT = VT;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   // We don't want to shrink SNaNs. Converting the SNaN back to its real type
3340b57cec5SDimitry Andric   // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ).
3350b57cec5SDimitry Andric   if (!APF.isSignaling()) {
336bdd1243dSDimitry Andric     while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) {
3370b57cec5SDimitry Andric       SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1);
3380b57cec5SDimitry Andric       if (ConstantFPSDNode::isValueValidForType(SVT, APF) &&
3390b57cec5SDimitry Andric           // Only do this if the target has a native EXTLOAD instruction from
3400b57cec5SDimitry Andric           // smaller type.
3410b57cec5SDimitry Andric           TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) &&
3420b57cec5SDimitry Andric           TLI.ShouldShrinkFPConstant(OrigVT)) {
3430b57cec5SDimitry Andric         Type *SType = SVT.getTypeForEVT(*DAG.getContext());
3445f757f3fSDimitry Andric         LLVMC = cast<ConstantFP>(ConstantFoldCastOperand(
3455f757f3fSDimitry Andric             Instruction::FPTrunc, LLVMC, SType, DAG.getDataLayout()));
3460b57cec5SDimitry Andric         VT = SVT;
3470b57cec5SDimitry Andric         Extend = true;
3480b57cec5SDimitry Andric       }
3490b57cec5SDimitry Andric     }
3500b57cec5SDimitry Andric   }
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   SDValue CPIdx =
3530b57cec5SDimitry Andric       DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout()));
3545ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3550b57cec5SDimitry Andric   if (Extend) {
3560b57cec5SDimitry Andric     SDValue Result = DAG.getExtLoad(
3570b57cec5SDimitry Andric         ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx,
3580b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT,
3590b57cec5SDimitry Andric         Alignment);
3600b57cec5SDimitry Andric     return Result;
3610b57cec5SDimitry Andric   }
3620b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3630b57cec5SDimitry Andric       OrigVT, dl, DAG.getEntryNode(), CPIdx,
3640b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3650b57cec5SDimitry Andric   return Result;
3660b57cec5SDimitry Andric }
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric /// Expands the Constant node to a load from the constant pool.
3690b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) {
3700b57cec5SDimitry Andric   SDLoc dl(CP);
3710b57cec5SDimitry Andric   EVT VT = CP->getValueType(0);
3720b57cec5SDimitry Andric   SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(),
3730b57cec5SDimitry Andric                                       TLI.getPointerTy(DAG.getDataLayout()));
3745ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
3750b57cec5SDimitry Andric   SDValue Result = DAG.getLoad(
3760b57cec5SDimitry Andric       VT, dl, DAG.getEntryNode(), CPIdx,
3770b57cec5SDimitry Andric       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment);
3780b57cec5SDimitry Andric   return Result;
3790b57cec5SDimitry Andric }
3800b57cec5SDimitry Andric 
381*0fca6ea1SDimitry Andric SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Op) {
382*0fca6ea1SDimitry Andric   SDValue Vec = Op.getOperand(0);
383*0fca6ea1SDimitry Andric   SDValue Val = Op.getOperand(1);
384*0fca6ea1SDimitry Andric   SDValue Idx = Op.getOperand(2);
385*0fca6ea1SDimitry Andric   SDLoc dl(Op);
3860b57cec5SDimitry Andric 
3870b57cec5SDimitry Andric   if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) {
3880b57cec5SDimitry Andric     // SCALAR_TO_VECTOR requires that the type of the value being inserted
3890b57cec5SDimitry Andric     // match the element type of the vector being created, except for
3900b57cec5SDimitry Andric     // integers in which case the inserted value can be over width.
3910b57cec5SDimitry Andric     EVT EltVT = Vec.getValueType().getVectorElementType();
3920b57cec5SDimitry Andric     if (Val.getValueType() == EltVT ||
3930b57cec5SDimitry Andric         (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) {
3940b57cec5SDimitry Andric       SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3950b57cec5SDimitry Andric                                   Vec.getValueType(), Val);
3960b57cec5SDimitry Andric 
3970b57cec5SDimitry Andric       unsigned NumElts = Vec.getValueType().getVectorNumElements();
3980b57cec5SDimitry Andric       // We generate a shuffle of InVec and ScVec, so the shuffle mask
3990b57cec5SDimitry Andric       // should be 0,1,2,3,4,5... with the appropriate element replaced with
4000b57cec5SDimitry Andric       // elt 0 of the RHS.
4010b57cec5SDimitry Andric       SmallVector<int, 8> ShufOps;
4020b57cec5SDimitry Andric       for (unsigned i = 0; i != NumElts; ++i)
4030b57cec5SDimitry Andric         ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts);
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric       return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps);
4060b57cec5SDimitry Andric     }
4070b57cec5SDimitry Andric   }
408*0fca6ea1SDimitry Andric   return ExpandInsertToVectorThroughStack(Op);
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric 
4110b57cec5SDimitry Andric SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) {
412480093f4SDimitry Andric   if (!ISD::isNormalStore(ST))
413480093f4SDimitry Andric     return SDValue();
414480093f4SDimitry Andric 
4150b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Optimizing float store operations\n");
4160b57cec5SDimitry Andric   // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
4170b57cec5SDimitry Andric   // FIXME: move this to the DAG Combiner!  Note that we can't regress due
4180b57cec5SDimitry Andric   // to phase ordering between legalized code and the dag combiner.  This
4190b57cec5SDimitry Andric   // probably means that we need to integrate dag combiner and legalizer
4200b57cec5SDimitry Andric   // together.
4210b57cec5SDimitry Andric   // We generally can't do this one for long doubles.
4220b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
4230b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
424e8d8bef9SDimitry Andric   SDValue Value = ST->getValue();
4250b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
4260b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
4270b57cec5SDimitry Andric   SDLoc dl(ST);
428e8d8bef9SDimitry Andric 
429e8d8bef9SDimitry Andric   // Don't optimise TargetConstantFP
430e8d8bef9SDimitry Andric   if (Value.getOpcode() == ISD::TargetConstantFP)
431e8d8bef9SDimitry Andric     return SDValue();
432e8d8bef9SDimitry Andric 
433e8d8bef9SDimitry Andric   if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) {
4340b57cec5SDimitry Andric     if (CFP->getValueType(0) == MVT::f32 &&
4350b57cec5SDimitry Andric         TLI.isTypeLegal(MVT::i32)) {
4360b57cec5SDimitry Andric       SDValue Con = DAG.getConstant(CFP->getValueAPF().
4370b57cec5SDimitry Andric                                       bitcastToAPInt().zextOrTrunc(32),
4380b57cec5SDimitry Andric                                     SDLoc(CFP), MVT::i32);
4395ffd83dbSDimitry Andric       return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4405ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4410b57cec5SDimitry Andric     }
4420b57cec5SDimitry Andric 
4435f757f3fSDimitry Andric     if (CFP->getValueType(0) == MVT::f64 &&
4445f757f3fSDimitry Andric         !TLI.isFPImmLegal(CFP->getValueAPF(), MVT::f64)) {
4450b57cec5SDimitry Andric       // If this target supports 64-bit registers, do a single 64-bit store.
4460b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i64)) {
4470b57cec5SDimitry Andric         SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
4480b57cec5SDimitry Andric                                       zextOrTrunc(64), SDLoc(CFP), MVT::i64);
4490b57cec5SDimitry Andric         return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(),
4505ffd83dbSDimitry Andric                             ST->getOriginalAlign(), MMOFlags, AAInfo);
4510b57cec5SDimitry Andric       }
4520b57cec5SDimitry Andric 
4530b57cec5SDimitry Andric       if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) {
4540b57cec5SDimitry Andric         // Otherwise, if the target supports 32-bit registers, use 2 32-bit
4550b57cec5SDimitry Andric         // stores.  If the target supports neither 32- nor 64-bits, this
4560b57cec5SDimitry Andric         // xform is certainly not worth it.
4570b57cec5SDimitry Andric         const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt();
4580b57cec5SDimitry Andric         SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32);
4590b57cec5SDimitry Andric         SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32);
4600b57cec5SDimitry Andric         if (DAG.getDataLayout().isBigEndian())
4610b57cec5SDimitry Andric           std::swap(Lo, Hi);
4620b57cec5SDimitry Andric 
4635ffd83dbSDimitry Andric         Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(),
4645ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4655f757f3fSDimitry Andric         Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(4), dl);
4660b57cec5SDimitry Andric         Hi = DAG.getStore(Chain, dl, Hi, Ptr,
4670b57cec5SDimitry Andric                           ST->getPointerInfo().getWithOffset(4),
4685ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
4690b57cec5SDimitry Andric 
4700b57cec5SDimitry Andric         return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
4710b57cec5SDimitry Andric       }
4720b57cec5SDimitry Andric     }
4730b57cec5SDimitry Andric   }
474e8d8bef9SDimitry Andric   return SDValue();
4750b57cec5SDimitry Andric }
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) {
4780b57cec5SDimitry Andric   StoreSDNode *ST = cast<StoreSDNode>(Node);
4790b57cec5SDimitry Andric   SDValue Chain = ST->getChain();
4800b57cec5SDimitry Andric   SDValue Ptr = ST->getBasePtr();
4810b57cec5SDimitry Andric   SDLoc dl(Node);
4820b57cec5SDimitry Andric 
4830b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags();
4840b57cec5SDimitry Andric   AAMDNodes AAInfo = ST->getAAInfo();
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric   if (!ST->isTruncatingStore()) {
4870b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing store operation\n");
4880b57cec5SDimitry Andric     if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) {
4890b57cec5SDimitry Andric       ReplaceNode(ST, OptStore);
4900b57cec5SDimitry Andric       return;
4910b57cec5SDimitry Andric     }
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric     SDValue Value = ST->getValue();
4940b57cec5SDimitry Andric     MVT VT = Value.getSimpleValueType();
4950b57cec5SDimitry Andric     switch (TLI.getOperationAction(ISD::STORE, VT)) {
4960b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
4970b57cec5SDimitry Andric     case TargetLowering::Legal: {
4980b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
4990b57cec5SDimitry Andric       // expand it.
5000b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
5010b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
5028bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
5030b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
5040b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n");
5050b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
5060b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
5070b57cec5SDimitry Andric       } else
5080b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Legal store\n");
5090b57cec5SDimitry Andric       break;
5100b57cec5SDimitry Andric     }
5110b57cec5SDimitry Andric     case TargetLowering::Custom: {
5120b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom lowering\n");
5130b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
5140b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
5150b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
5160b57cec5SDimitry Andric       return;
5170b57cec5SDimitry Andric     }
5180b57cec5SDimitry Andric     case TargetLowering::Promote: {
5190b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT);
5200b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
5210b57cec5SDimitry Andric              "Can only promote stores to same size type");
5220b57cec5SDimitry Andric       Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value);
5235ffd83dbSDimitry Andric       SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5245ffd83dbSDimitry Andric                                     ST->getOriginalAlign(), MMOFlags, AAInfo);
5250b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
5260b57cec5SDimitry Andric       break;
5270b57cec5SDimitry Andric     }
5280b57cec5SDimitry Andric     }
5290b57cec5SDimitry Andric     return;
5300b57cec5SDimitry Andric   }
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n");
5330b57cec5SDimitry Andric   SDValue Value = ST->getValue();
5340b57cec5SDimitry Andric   EVT StVT = ST->getMemoryVT();
535e8d8bef9SDimitry Andric   TypeSize StWidth = StVT.getSizeInBits();
536e8d8bef9SDimitry Andric   TypeSize StSize = StVT.getStoreSizeInBits();
5370b57cec5SDimitry Andric   auto &DL = DAG.getDataLayout();
5380b57cec5SDimitry Andric 
539e8d8bef9SDimitry Andric   if (StWidth != StSize) {
5400b57cec5SDimitry Andric     // Promote to a byte-sized store with upper bits zero if not
5410b57cec5SDimitry Andric     // storing an integral number of bytes.  For example, promote
5420b57cec5SDimitry Andric     // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
543bdd1243dSDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue());
5440b57cec5SDimitry Andric     Value = DAG.getZeroExtendInReg(Value, dl, StVT);
5450b57cec5SDimitry Andric     SDValue Result =
5460b57cec5SDimitry Andric         DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT,
5475ffd83dbSDimitry Andric                           ST->getOriginalAlign(), MMOFlags, AAInfo);
5480b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
549bdd1243dSDimitry Andric   } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) {
5500b57cec5SDimitry Andric     // If not storing a power-of-2 number of bits, expand as two stores.
5510b57cec5SDimitry Andric     assert(!StVT.isVector() && "Unsupported truncstore!");
552bdd1243dSDimitry Andric     unsigned StWidthBits = StWidth.getFixedValue();
553e8d8bef9SDimitry Andric     unsigned LogStWidth = Log2_32(StWidthBits);
5540b57cec5SDimitry Andric     assert(LogStWidth < 32);
5550b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogStWidth;
556e8d8bef9SDimitry Andric     assert(RoundWidth < StWidthBits);
557e8d8bef9SDimitry Andric     unsigned ExtraWidth = StWidthBits - RoundWidth;
5580b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
5590b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
5600b57cec5SDimitry Andric            "Store size not an integral number of bytes!");
5610b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
5620b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
5630b57cec5SDimitry Andric     SDValue Lo, Hi;
5640b57cec5SDimitry Andric     unsigned IncrementSize;
5650b57cec5SDimitry Andric 
5660b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
5670b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
5680b57cec5SDimitry Andric       // Store the bottom RoundWidth bits.
5690b57cec5SDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
5705ffd83dbSDimitry Andric                              RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
5710b57cec5SDimitry Andric 
5720b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
5730b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
5745f757f3fSDimitry Andric       Ptr =
5755f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
5760b57cec5SDimitry Andric       Hi = DAG.getNode(
5770b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
5780b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
5790b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
5805ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr,
5815ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
5825ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
5830b57cec5SDimitry Andric     } else {
5840b57cec5SDimitry Andric       // Big endian - avoid unaligned stores.
5850b57cec5SDimitry Andric       // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
5860b57cec5SDimitry Andric       // Store the top RoundWidth bits.
5870b57cec5SDimitry Andric       Hi = DAG.getNode(
5880b57cec5SDimitry Andric           ISD::SRL, dl, Value.getValueType(), Value,
5890b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
5900b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Value.getValueType(), DL)));
5915ffd83dbSDimitry Andric       Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT,
5925ffd83dbSDimitry Andric                              ST->getOriginalAlign(), MMOFlags, AAInfo);
5930b57cec5SDimitry Andric 
5940b57cec5SDimitry Andric       // Store the remaining ExtraWidth bits.
5950b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
5960b57cec5SDimitry Andric       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr,
5970b57cec5SDimitry Andric                         DAG.getConstant(IncrementSize, dl,
5980b57cec5SDimitry Andric                                         Ptr.getValueType()));
5995ffd83dbSDimitry Andric       Lo = DAG.getTruncStore(Chain, dl, Value, Ptr,
6005ffd83dbSDimitry Andric                              ST->getPointerInfo().getWithOffset(IncrementSize),
6015ffd83dbSDimitry Andric                              ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo);
6020b57cec5SDimitry Andric     }
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric     // The order of the stores doesn't matter.
6050b57cec5SDimitry Andric     SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi);
6060b57cec5SDimitry Andric     ReplaceNode(SDValue(Node, 0), Result);
6070b57cec5SDimitry Andric   } else {
6080b57cec5SDimitry Andric     switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
6090b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6100b57cec5SDimitry Andric     case TargetLowering::Legal: {
6110b57cec5SDimitry Andric       EVT MemVT = ST->getMemoryVT();
6120b57cec5SDimitry Andric       // If this is an unaligned store and the target doesn't support it,
6130b57cec5SDimitry Andric       // expand it.
6148bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6150b57cec5SDimitry Andric                                               *ST->getMemOperand())) {
6160b57cec5SDimitry Andric         SDValue Result = TLI.expandUnalignedStore(ST, DAG);
6170b57cec5SDimitry Andric         ReplaceNode(SDValue(ST, 0), Result);
6180b57cec5SDimitry Andric       }
6190b57cec5SDimitry Andric       break;
6200b57cec5SDimitry Andric     }
6210b57cec5SDimitry Andric     case TargetLowering::Custom: {
6220b57cec5SDimitry Andric       SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG);
6230b57cec5SDimitry Andric       if (Res && Res != SDValue(Node, 0))
6240b57cec5SDimitry Andric         ReplaceNode(SDValue(Node, 0), Res);
6250b57cec5SDimitry Andric       return;
6260b57cec5SDimitry Andric     }
6270b57cec5SDimitry Andric     case TargetLowering::Expand:
6280b57cec5SDimitry Andric       assert(!StVT.isVector() &&
6290b57cec5SDimitry Andric              "Vector Stores are handled in LegalizeVectorOps");
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric       SDValue Result;
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric       // TRUNCSTORE:i16 i32 -> STORE i16
6340b57cec5SDimitry Andric       if (TLI.isTypeLegal(StVT)) {
6350b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value);
6360b57cec5SDimitry Andric         Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(),
6375ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6380b57cec5SDimitry Andric       } else {
6390b57cec5SDimitry Andric         // The in-memory type isn't legal. Truncate to the type it would promote
6400b57cec5SDimitry Andric         // to, and then do a truncstore.
6410b57cec5SDimitry Andric         Value = DAG.getNode(ISD::TRUNCATE, dl,
6420b57cec5SDimitry Andric                             TLI.getTypeToTransformTo(*DAG.getContext(), StVT),
6430b57cec5SDimitry Andric                             Value);
6445ffd83dbSDimitry Andric         Result =
6455ffd83dbSDimitry Andric             DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT,
6465ffd83dbSDimitry Andric                               ST->getOriginalAlign(), MMOFlags, AAInfo);
6470b57cec5SDimitry Andric       }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric       ReplaceNode(SDValue(Node, 0), Result);
6500b57cec5SDimitry Andric       break;
6510b57cec5SDimitry Andric     }
6520b57cec5SDimitry Andric   }
6530b57cec5SDimitry Andric }
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) {
6560b57cec5SDimitry Andric   LoadSDNode *LD = cast<LoadSDNode>(Node);
6570b57cec5SDimitry Andric   SDValue Chain = LD->getChain();  // The chain.
6580b57cec5SDimitry Andric   SDValue Ptr = LD->getBasePtr();  // The base pointer.
6590b57cec5SDimitry Andric   SDValue Value;                   // The value returned by the load op.
6600b57cec5SDimitry Andric   SDLoc dl(Node);
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   ISD::LoadExtType ExtType = LD->getExtensionType();
6630b57cec5SDimitry Andric   if (ExtType == ISD::NON_EXTLOAD) {
6640b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n");
6650b57cec5SDimitry Andric     MVT VT = Node->getSimpleValueType(0);
6660b57cec5SDimitry Andric     SDValue RVal = SDValue(Node, 0);
6670b57cec5SDimitry Andric     SDValue RChain = SDValue(Node, 1);
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric     switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
6700b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
6710b57cec5SDimitry Andric     case TargetLowering::Legal: {
6720b57cec5SDimitry Andric       EVT MemVT = LD->getMemoryVT();
6730b57cec5SDimitry Andric       const DataLayout &DL = DAG.getDataLayout();
6740b57cec5SDimitry Andric       // If this is an unaligned load and the target doesn't support it,
6750b57cec5SDimitry Andric       // expand it.
6768bcb0991SDimitry Andric       if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT,
6770b57cec5SDimitry Andric                                               *LD->getMemOperand())) {
6780b57cec5SDimitry Andric         std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG);
6790b57cec5SDimitry Andric       }
6800b57cec5SDimitry Andric       break;
6810b57cec5SDimitry Andric     }
6820b57cec5SDimitry Andric     case TargetLowering::Custom:
6830b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(RVal, DAG)) {
6840b57cec5SDimitry Andric         RVal = Res;
6850b57cec5SDimitry Andric         RChain = Res.getValue(1);
6860b57cec5SDimitry Andric       }
6870b57cec5SDimitry Andric       break;
6880b57cec5SDimitry Andric 
6890b57cec5SDimitry Andric     case TargetLowering::Promote: {
6900b57cec5SDimitry Andric       MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
6910b57cec5SDimitry Andric       assert(NVT.getSizeInBits() == VT.getSizeInBits() &&
6920b57cec5SDimitry Andric              "Can only promote loads to same size type");
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric       SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand());
6950b57cec5SDimitry Andric       RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res);
6960b57cec5SDimitry Andric       RChain = Res.getValue(1);
6970b57cec5SDimitry Andric       break;
6980b57cec5SDimitry Andric     }
6990b57cec5SDimitry Andric     }
7000b57cec5SDimitry Andric     if (RChain.getNode() != Node) {
7010b57cec5SDimitry Andric       assert(RVal.getNode() != Node && "Load must be completely replaced");
7020b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal);
7030b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain);
7040b57cec5SDimitry Andric       if (UpdatedNodes) {
7050b57cec5SDimitry Andric         UpdatedNodes->insert(RVal.getNode());
7060b57cec5SDimitry Andric         UpdatedNodes->insert(RChain.getNode());
7070b57cec5SDimitry Andric       }
7080b57cec5SDimitry Andric       ReplacedNode(Node);
7090b57cec5SDimitry Andric     }
7100b57cec5SDimitry Andric     return;
7110b57cec5SDimitry Andric   }
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n");
7140b57cec5SDimitry Andric   EVT SrcVT = LD->getMemoryVT();
715e8d8bef9SDimitry Andric   TypeSize SrcWidth = SrcVT.getSizeInBits();
7160b57cec5SDimitry Andric   MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags();
7170b57cec5SDimitry Andric   AAMDNodes AAInfo = LD->getAAInfo();
7180b57cec5SDimitry Andric 
7190b57cec5SDimitry Andric   if (SrcWidth != SrcVT.getStoreSizeInBits() &&
7200b57cec5SDimitry Andric       // Some targets pretend to have an i1 loading operation, and actually
7210b57cec5SDimitry Andric       // load an i8.  This trick is correct for ZEXTLOAD because the top 7
7220b57cec5SDimitry Andric       // bits are guaranteed to be zero; it helps the optimizers understand
7230b57cec5SDimitry Andric       // that these bits are zero.  It is also useful for EXTLOAD, since it
7240b57cec5SDimitry Andric       // tells the optimizers that those bits are undefined.  It would be
7250b57cec5SDimitry Andric       // nice to have an effective generic way of getting these benefits...
7260b57cec5SDimitry Andric       // Until such a way is found, don't insist on promoting i1 here.
7270b57cec5SDimitry Andric       (SrcVT != MVT::i1 ||
7280b57cec5SDimitry Andric        TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) ==
7290b57cec5SDimitry Andric          TargetLowering::Promote)) {
7300b57cec5SDimitry Andric     // Promote to a byte-sized load if not loading an integral number of
7310b57cec5SDimitry Andric     // bytes.  For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
7320b57cec5SDimitry Andric     unsigned NewWidth = SrcVT.getStoreSizeInBits();
7330b57cec5SDimitry Andric     EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth);
7340b57cec5SDimitry Andric     SDValue Ch;
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric     // The extra bits are guaranteed to be zero, since we stored them that
7370b57cec5SDimitry Andric     // way.  A zext load from NVT thus automatically gives zext from SrcVT.
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric     ISD::LoadExtType NewExtType =
7400b57cec5SDimitry Andric       ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
7410b57cec5SDimitry Andric 
7425ffd83dbSDimitry Andric     SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0),
7435ffd83dbSDimitry Andric                                     Chain, Ptr, LD->getPointerInfo(), NVT,
7445ffd83dbSDimitry Andric                                     LD->getOriginalAlign(), MMOFlags, AAInfo);
7450b57cec5SDimitry Andric 
7460b57cec5SDimitry Andric     Ch = Result.getValue(1); // The chain.
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric     if (ExtType == ISD::SEXTLOAD)
7490b57cec5SDimitry Andric       // Having the top bits zero doesn't help when sign extending.
7500b57cec5SDimitry Andric       Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
7510b57cec5SDimitry Andric                            Result.getValueType(),
7520b57cec5SDimitry Andric                            Result, DAG.getValueType(SrcVT));
7530b57cec5SDimitry Andric     else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
7540b57cec5SDimitry Andric       // All the top bits are guaranteed to be zero - inform the optimizers.
7550b57cec5SDimitry Andric       Result = DAG.getNode(ISD::AssertZext, dl,
7560b57cec5SDimitry Andric                            Result.getValueType(), Result,
7570b57cec5SDimitry Andric                            DAG.getValueType(SrcVT));
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric     Value = Result;
7600b57cec5SDimitry Andric     Chain = Ch;
761bdd1243dSDimitry Andric   } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) {
7620b57cec5SDimitry Andric     // If not loading a power-of-2 number of bits, expand as two loads.
7630b57cec5SDimitry Andric     assert(!SrcVT.isVector() && "Unsupported extload!");
764bdd1243dSDimitry Andric     unsigned SrcWidthBits = SrcWidth.getFixedValue();
765e8d8bef9SDimitry Andric     unsigned LogSrcWidth = Log2_32(SrcWidthBits);
7660b57cec5SDimitry Andric     assert(LogSrcWidth < 32);
7670b57cec5SDimitry Andric     unsigned RoundWidth = 1 << LogSrcWidth;
768e8d8bef9SDimitry Andric     assert(RoundWidth < SrcWidthBits);
769e8d8bef9SDimitry Andric     unsigned ExtraWidth = SrcWidthBits - RoundWidth;
7700b57cec5SDimitry Andric     assert(ExtraWidth < RoundWidth);
7710b57cec5SDimitry Andric     assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
7720b57cec5SDimitry Andric            "Load size not an integral number of bytes!");
7730b57cec5SDimitry Andric     EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth);
7740b57cec5SDimitry Andric     EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth);
7750b57cec5SDimitry Andric     SDValue Lo, Hi, Ch;
7760b57cec5SDimitry Andric     unsigned IncrementSize;
7770b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
7780b57cec5SDimitry Andric 
7790b57cec5SDimitry Andric     if (DL.isLittleEndian()) {
7800b57cec5SDimitry Andric       // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
7810b57cec5SDimitry Andric       // Load the bottom RoundWidth bits.
7820b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
7835ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
7845ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
7870b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
7885f757f3fSDimitry Andric       Ptr =
7895f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
7900b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
7910b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
7925ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
7950b57cec5SDimitry Andric       // the other one.
7960b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
7970b57cec5SDimitry Andric                        Hi.getValue(1));
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric       // Move the top bits to the right place.
8000b57cec5SDimitry Andric       Hi = DAG.getNode(
8010b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8020b57cec5SDimitry Andric           DAG.getConstant(RoundWidth, dl,
8030b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric       // Join the hi and lo parts.
8060b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8070b57cec5SDimitry Andric     } else {
8080b57cec5SDimitry Andric       // Big endian - avoid unaligned loads.
8090b57cec5SDimitry Andric       // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
8100b57cec5SDimitry Andric       // Load the top RoundWidth bits.
8110b57cec5SDimitry Andric       Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr,
8125ffd83dbSDimitry Andric                           LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(),
8135ffd83dbSDimitry Andric                           MMOFlags, AAInfo);
8140b57cec5SDimitry Andric 
8150b57cec5SDimitry Andric       // Load the remaining ExtraWidth bits.
8160b57cec5SDimitry Andric       IncrementSize = RoundWidth / 8;
8175f757f3fSDimitry Andric       Ptr =
8185f757f3fSDimitry Andric           DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl);
8190b57cec5SDimitry Andric       Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr,
8200b57cec5SDimitry Andric                           LD->getPointerInfo().getWithOffset(IncrementSize),
8215ffd83dbSDimitry Andric                           ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo);
8220b57cec5SDimitry Andric 
8230b57cec5SDimitry Andric       // Build a factor node to remember that this load is independent of
8240b57cec5SDimitry Andric       // the other one.
8250b57cec5SDimitry Andric       Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1),
8260b57cec5SDimitry Andric                        Hi.getValue(1));
8270b57cec5SDimitry Andric 
8280b57cec5SDimitry Andric       // Move the top bits to the right place.
8290b57cec5SDimitry Andric       Hi = DAG.getNode(
8300b57cec5SDimitry Andric           ISD::SHL, dl, Hi.getValueType(), Hi,
8310b57cec5SDimitry Andric           DAG.getConstant(ExtraWidth, dl,
8320b57cec5SDimitry Andric                           TLI.getShiftAmountTy(Hi.getValueType(), DL)));
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric       // Join the hi and lo parts.
8350b57cec5SDimitry Andric       Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi);
8360b57cec5SDimitry Andric     }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric     Chain = Ch;
8390b57cec5SDimitry Andric   } else {
8400b57cec5SDimitry Andric     bool isCustom = false;
8410b57cec5SDimitry Andric     switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0),
8420b57cec5SDimitry Andric                                  SrcVT.getSimpleVT())) {
8430b57cec5SDimitry Andric     default: llvm_unreachable("This action is not supported yet!");
8440b57cec5SDimitry Andric     case TargetLowering::Custom:
8450b57cec5SDimitry Andric       isCustom = true;
846bdd1243dSDimitry Andric       [[fallthrough]];
8470b57cec5SDimitry Andric     case TargetLowering::Legal:
8480b57cec5SDimitry Andric       Value = SDValue(Node, 0);
8490b57cec5SDimitry Andric       Chain = SDValue(Node, 1);
8500b57cec5SDimitry Andric 
8510b57cec5SDimitry Andric       if (isCustom) {
8520b57cec5SDimitry Andric         if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
8530b57cec5SDimitry Andric           Value = Res;
8540b57cec5SDimitry Andric           Chain = Res.getValue(1);
8550b57cec5SDimitry Andric         }
8560b57cec5SDimitry Andric       } else {
8570b57cec5SDimitry Andric         // If this is an unaligned load and the target doesn't support it,
8580b57cec5SDimitry Andric         // expand it.
8590b57cec5SDimitry Andric         EVT MemVT = LD->getMemoryVT();
8600b57cec5SDimitry Andric         const DataLayout &DL = DAG.getDataLayout();
8610b57cec5SDimitry Andric         if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT,
8620b57cec5SDimitry Andric                                     *LD->getMemOperand())) {
8630b57cec5SDimitry Andric           std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG);
8640b57cec5SDimitry Andric         }
8650b57cec5SDimitry Andric       }
8660b57cec5SDimitry Andric       break;
8670b57cec5SDimitry Andric 
8680b57cec5SDimitry Andric     case TargetLowering::Expand: {
8690b57cec5SDimitry Andric       EVT DestVT = Node->getValueType(0);
8700b57cec5SDimitry Andric       if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) {
8710b57cec5SDimitry Andric         // If the source type is not legal, see if there is a legal extload to
8720b57cec5SDimitry Andric         // an intermediate type that we can then extend further.
8730b57cec5SDimitry Andric         EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT());
87406c3fb27SDimitry Andric         if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) &&
87506c3fb27SDimitry Andric             (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT?
87606c3fb27SDimitry Andric              TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) {
8770b57cec5SDimitry Andric           // If we are loading a legal type, this is a non-extload followed by a
8780b57cec5SDimitry Andric           // full extend.
8790b57cec5SDimitry Andric           ISD::LoadExtType MidExtType =
8800b57cec5SDimitry Andric               (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType;
8810b57cec5SDimitry Andric 
8820b57cec5SDimitry Andric           SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr,
8830b57cec5SDimitry Andric                                         SrcVT, LD->getMemOperand());
8840b57cec5SDimitry Andric           unsigned ExtendOp =
8850b57cec5SDimitry Andric               ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType);
8860b57cec5SDimitry Andric           Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load);
8870b57cec5SDimitry Andric           Chain = Load.getValue(1);
8880b57cec5SDimitry Andric           break;
8890b57cec5SDimitry Andric         }
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric         // Handle the special case of fp16 extloads. EXTLOAD doesn't have the
8920b57cec5SDimitry Andric         // normal undefined upper bits behavior to allow using an in-reg extend
8930b57cec5SDimitry Andric         // with the illegal FP type, so load as an integer and do the
8940b57cec5SDimitry Andric         // from-integer conversion.
895cb14a3feSDimitry Andric         EVT SVT = SrcVT.getScalarType();
896cb14a3feSDimitry Andric         if (SVT == MVT::f16 || SVT == MVT::bf16) {
8970b57cec5SDimitry Andric           EVT ISrcVT = SrcVT.changeTypeToInteger();
8980b57cec5SDimitry Andric           EVT IDestVT = DestVT.changeTypeToInteger();
8998bcb0991SDimitry Andric           EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT());
9000b57cec5SDimitry Andric 
9018bcb0991SDimitry Andric           SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain,
9028bcb0991SDimitry Andric                                           Ptr, ISrcVT, LD->getMemOperand());
903cb14a3feSDimitry Andric           Value =
904cb14a3feSDimitry Andric               DAG.getNode(SVT == MVT::f16 ? ISD::FP16_TO_FP : ISD::BF16_TO_FP,
905cb14a3feSDimitry Andric                           dl, DestVT, Result);
9060b57cec5SDimitry Andric           Chain = Result.getValue(1);
9070b57cec5SDimitry Andric           break;
9080b57cec5SDimitry Andric         }
9090b57cec5SDimitry Andric       }
9100b57cec5SDimitry Andric 
9110b57cec5SDimitry Andric       assert(!SrcVT.isVector() &&
9120b57cec5SDimitry Andric              "Vector Loads are handled in LegalizeVectorOps");
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric       // FIXME: This does not work for vectors on most targets.  Sign-
9150b57cec5SDimitry Andric       // and zero-extend operations are currently folded into extending
9160b57cec5SDimitry Andric       // loads, whether they are legal or not, and then we end up here
9170b57cec5SDimitry Andric       // without any support for legalizing them.
9180b57cec5SDimitry Andric       assert(ExtType != ISD::EXTLOAD &&
9190b57cec5SDimitry Andric              "EXTLOAD should always be supported!");
9200b57cec5SDimitry Andric       // Turn the unsupported load into an EXTLOAD followed by an
9210b57cec5SDimitry Andric       // explicit zero/sign extend inreg.
9220b57cec5SDimitry Andric       SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl,
9230b57cec5SDimitry Andric                                       Node->getValueType(0),
9240b57cec5SDimitry Andric                                       Chain, Ptr, SrcVT,
9250b57cec5SDimitry Andric                                       LD->getMemOperand());
9260b57cec5SDimitry Andric       SDValue ValRes;
9270b57cec5SDimitry Andric       if (ExtType == ISD::SEXTLOAD)
9280b57cec5SDimitry Andric         ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl,
9290b57cec5SDimitry Andric                              Result.getValueType(),
9300b57cec5SDimitry Andric                              Result, DAG.getValueType(SrcVT));
9310b57cec5SDimitry Andric       else
9325ffd83dbSDimitry Andric         ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT);
9330b57cec5SDimitry Andric       Value = ValRes;
9340b57cec5SDimitry Andric       Chain = Result.getValue(1);
9350b57cec5SDimitry Andric       break;
9360b57cec5SDimitry Andric     }
9370b57cec5SDimitry Andric     }
9380b57cec5SDimitry Andric   }
9390b57cec5SDimitry Andric 
9400b57cec5SDimitry Andric   // Since loads produce two values, make sure to remember that we legalized
9410b57cec5SDimitry Andric   // both of them.
9420b57cec5SDimitry Andric   if (Chain.getNode() != Node) {
9430b57cec5SDimitry Andric     assert(Value.getNode() != Node && "Load must be completely replaced");
9440b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value);
9450b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
9460b57cec5SDimitry Andric     if (UpdatedNodes) {
9470b57cec5SDimitry Andric       UpdatedNodes->insert(Value.getNode());
9480b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
9490b57cec5SDimitry Andric     }
9500b57cec5SDimitry Andric     ReplacedNode(Node);
9510b57cec5SDimitry Andric   }
9520b57cec5SDimitry Andric }
9530b57cec5SDimitry Andric 
9540b57cec5SDimitry Andric /// Return a legal replacement for the given operation, with all legal operands.
9550b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeOp(SDNode *Node) {
9560b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG));
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric   // Allow illegal target nodes and illegal registers.
9590b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::TargetConstant ||
9600b57cec5SDimitry Andric       Node->getOpcode() == ISD::Register)
9610b57cec5SDimitry Andric     return;
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric #ifndef NDEBUG
9640b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
9658bcb0991SDimitry Andric     assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) ==
9668bcb0991SDimitry Andric              TargetLowering::TypeLegal &&
9670b57cec5SDimitry Andric            "Unexpected illegal type!");
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values())
9700b57cec5SDimitry Andric     assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) ==
9710b57cec5SDimitry Andric               TargetLowering::TypeLegal ||
9720b57cec5SDimitry Andric             Op.getOpcode() == ISD::TargetConstant ||
9730b57cec5SDimitry Andric             Op.getOpcode() == ISD::Register) &&
9740b57cec5SDimitry Andric             "Unexpected illegal type!");
9750b57cec5SDimitry Andric #endif
9760b57cec5SDimitry Andric 
9770b57cec5SDimitry Andric   // Figure out the correct action; the way to query this varies by opcode
9780b57cec5SDimitry Andric   TargetLowering::LegalizeAction Action = TargetLowering::Legal;
9790b57cec5SDimitry Andric   bool SimpleFinishLegalizing = true;
9800b57cec5SDimitry Andric   switch (Node->getOpcode()) {
9810b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
9820b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
9830b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
9840b57cec5SDimitry Andric   case ISD::STACKSAVE:
9850b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
9860b57cec5SDimitry Andric     break;
9870b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
9880b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
9890b57cec5SDimitry Andric                                     Node->getValueType(0));
9900b57cec5SDimitry Andric     break;
9910b57cec5SDimitry Andric   case ISD::VAARG:
9920b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
9930b57cec5SDimitry Andric                                     Node->getValueType(0));
9940b57cec5SDimitry Andric     if (Action != TargetLowering::Promote)
9950b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other);
9960b57cec5SDimitry Andric     break;
99706c3fb27SDimitry Andric   case ISD::SET_FPENV:
9985f757f3fSDimitry Andric   case ISD::SET_FPMODE:
99906c3fb27SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
100006c3fb27SDimitry Andric                                     Node->getOperand(1).getValueType());
100106c3fb27SDimitry Andric     break;
10020b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
100381ad6265SDimitry Andric   case ISD::FP_TO_BF16:
10040b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
10050b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
10060b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
10070b57cec5SDimitry Andric   case ISD::LROUND:
10080b57cec5SDimitry Andric   case ISD::LLROUND:
10090b57cec5SDimitry Andric   case ISD::LRINT:
10100b57cec5SDimitry Andric   case ISD::LLRINT:
10110b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10120b57cec5SDimitry Andric                                     Node->getOperand(0).getValueType());
10130b57cec5SDimitry Andric     break;
10145ffd83dbSDimitry Andric   case ISD::STRICT_FP_TO_FP16:
1015*0fca6ea1SDimitry Andric   case ISD::STRICT_FP_TO_BF16:
1016480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
1017480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
1018480093f4SDimitry Andric   case ISD::STRICT_LRINT:
1019480093f4SDimitry Andric   case ISD::STRICT_LLRINT:
1020480093f4SDimitry Andric   case ISD::STRICT_LROUND:
1021480093f4SDimitry Andric   case ISD::STRICT_LLROUND:
1022480093f4SDimitry Andric     // These pseudo-ops are the same as the other STRICT_ ops except
1023480093f4SDimitry Andric     // they are registered with setOperationAction() using the input type
1024480093f4SDimitry Andric     // instead of the output type.
1025480093f4SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1026480093f4SDimitry Andric                                     Node->getOperand(1).getValueType());
1027480093f4SDimitry Andric     break;
10280b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
10290b57cec5SDimitry Andric     EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT();
10300b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), InnerType);
10310b57cec5SDimitry Andric     break;
10320b57cec5SDimitry Andric   }
10330b57cec5SDimitry Andric   case ISD::ATOMIC_STORE:
10340b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
10355f757f3fSDimitry Andric                                     Node->getOperand(1).getValueType());
10360b57cec5SDimitry Andric     break;
10370b57cec5SDimitry Andric   case ISD::SELECT_CC:
1038480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
1039480093f4SDimitry Andric   case ISD::STRICT_FSETCCS:
10400b57cec5SDimitry Andric   case ISD::SETCC:
1041bdd1243dSDimitry Andric   case ISD::SETCCCARRY:
104281ad6265SDimitry Andric   case ISD::VP_SETCC:
10430b57cec5SDimitry Andric   case ISD::BR_CC: {
104481ad6265SDimitry Andric     unsigned Opc = Node->getOpcode();
104581ad6265SDimitry Andric     unsigned CCOperand = Opc == ISD::SELECT_CC                         ? 4
104681ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCC                   ? 3
104781ad6265SDimitry Andric                          : Opc == ISD::STRICT_FSETCCS                  ? 3
1048bdd1243dSDimitry Andric                          : Opc == ISD::SETCCCARRY                      ? 3
104981ad6265SDimitry Andric                          : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2
105081ad6265SDimitry Andric                                                                        : 1;
105181ad6265SDimitry Andric     unsigned CompareOperand = Opc == ISD::BR_CC            ? 2
105281ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCC  ? 1
105381ad6265SDimitry Andric                               : Opc == ISD::STRICT_FSETCCS ? 1
105481ad6265SDimitry Andric                                                            : 0;
10550b57cec5SDimitry Andric     MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType();
10560b57cec5SDimitry Andric     ISD::CondCode CCCode =
10570b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get();
10580b57cec5SDimitry Andric     Action = TLI.getCondCodeAction(CCCode, OpVT);
10590b57cec5SDimitry Andric     if (Action == TargetLowering::Legal) {
10600b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::SELECT_CC)
10610b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(),
10620b57cec5SDimitry Andric                                         Node->getValueType(0));
10630b57cec5SDimitry Andric       else
10640b57cec5SDimitry Andric         Action = TLI.getOperationAction(Node->getOpcode(), OpVT);
10650b57cec5SDimitry Andric     }
10660b57cec5SDimitry Andric     break;
10670b57cec5SDimitry Andric   }
10680b57cec5SDimitry Andric   case ISD::LOAD:
10690b57cec5SDimitry Andric   case ISD::STORE:
10700b57cec5SDimitry Andric     // FIXME: Model these properly.  LOAD and STORE are complicated, and
10710b57cec5SDimitry Andric     // STORE expects the unlegalized operand in some cases.
10720b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10730b57cec5SDimitry Andric     break;
10740b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
10750b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
10760b57cec5SDimitry Andric     // FIXME: This shouldn't be necessary.  These nodes have special properties
10770b57cec5SDimitry Andric     // dealing with the recursive nature of legalization.  Removing this
10780b57cec5SDimitry Andric     // special case should be done as part of making LegalizeDAG non-recursive.
10790b57cec5SDimitry Andric     SimpleFinishLegalizing = false;
10800b57cec5SDimitry Andric     break;
10810b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT:
1082bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
10830b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
10840b57cec5SDimitry Andric   case ISD::EH_RETURN:
10850b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
10860b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA:
10870b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
10880b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
10890b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETUP_DISPATCH:
10900b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
10910b57cec5SDimitry Andric     // they should actually be expanded.
10920b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
10930b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
10940b57cec5SDimitry Andric       Action = TargetLowering::Expand;
10950b57cec5SDimitry Andric     break;
10960b57cec5SDimitry Andric   case ISD::INIT_TRAMPOLINE:
10970b57cec5SDimitry Andric   case ISD::ADJUST_TRAMPOLINE:
10980b57cec5SDimitry Andric   case ISD::FRAMEADDR:
10990b57cec5SDimitry Andric   case ISD::RETURNADDR:
11000b57cec5SDimitry Andric   case ISD::ADDROFRETURNADDR:
11010b57cec5SDimitry Andric   case ISD::SPONENTRY:
11020b57cec5SDimitry Andric     // These operations lie about being legal: when they claim to be legal,
11030b57cec5SDimitry Andric     // they should actually be custom-lowered.
11040b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11050b57cec5SDimitry Andric     if (Action == TargetLowering::Legal)
11060b57cec5SDimitry Andric       Action = TargetLowering::Custom;
11070b57cec5SDimitry Andric     break;
1108*0fca6ea1SDimitry Andric   case ISD::CLEAR_CACHE:
1109*0fca6ea1SDimitry Andric     // This operation is typically going to be LibCall unless the target wants
1110*0fca6ea1SDimitry Andric     // something differrent.
1111*0fca6ea1SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1112*0fca6ea1SDimitry Andric     break;
11130b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
1114*0fca6ea1SDimitry Andric   case ISD::READSTEADYCOUNTER:
1115*0fca6ea1SDimitry Andric     // READCYCLECOUNTER and READSTEADYCOUNTER return a i64, even if type
1116*0fca6ea1SDimitry Andric     // legalization might have expanded that to several smaller types.
11170b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64);
11180b57cec5SDimitry Andric     break;
11190b57cec5SDimitry Andric   case ISD::READ_REGISTER:
11200b57cec5SDimitry Andric   case ISD::WRITE_REGISTER:
11210b57cec5SDimitry Andric     // Named register is legal in the DAG, but blocked by register name
11220b57cec5SDimitry Andric     // selection if not implemented by target (to chose the correct register)
11230b57cec5SDimitry Andric     // They'll be converted to Copy(To/From)Reg.
11240b57cec5SDimitry Andric     Action = TargetLowering::Legal;
11250b57cec5SDimitry Andric     break;
1126e8d8bef9SDimitry Andric   case ISD::UBSANTRAP:
1127e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
1128e8d8bef9SDimitry Andric     if (Action == TargetLowering::Expand) {
1129e8d8bef9SDimitry Andric       // replace ISD::UBSANTRAP with ISD::TRAP
1130e8d8bef9SDimitry Andric       SDValue NewVal;
1131e8d8bef9SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
1132e8d8bef9SDimitry Andric                            Node->getOperand(0));
1133e8d8bef9SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
1134e8d8bef9SDimitry Andric       LegalizeOp(NewVal.getNode());
1135e8d8bef9SDimitry Andric       return;
1136e8d8bef9SDimitry Andric     }
1137e8d8bef9SDimitry Andric     break;
11380b57cec5SDimitry Andric   case ISD::DEBUGTRAP:
11390b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11400b57cec5SDimitry Andric     if (Action == TargetLowering::Expand) {
11410b57cec5SDimitry Andric       // replace ISD::DEBUGTRAP with ISD::TRAP
11420b57cec5SDimitry Andric       SDValue NewVal;
11430b57cec5SDimitry Andric       NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(),
11440b57cec5SDimitry Andric                            Node->getOperand(0));
11450b57cec5SDimitry Andric       ReplaceNode(Node, NewVal.getNode());
11460b57cec5SDimitry Andric       LegalizeOp(NewVal.getNode());
11470b57cec5SDimitry Andric       return;
11480b57cec5SDimitry Andric     }
11490b57cec5SDimitry Andric     break;
11500b57cec5SDimitry Andric   case ISD::SADDSAT:
11510b57cec5SDimitry Andric   case ISD::UADDSAT:
11520b57cec5SDimitry Andric   case ISD::SSUBSAT:
1153e8d8bef9SDimitry Andric   case ISD::USUBSAT:
1154e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
1155e8d8bef9SDimitry Andric   case ISD::USHLSAT:
1156*0fca6ea1SDimitry Andric   case ISD::SCMP:
1157*0fca6ea1SDimitry Andric   case ISD::UCMP:
1158e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
1159e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
11600b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
11610b57cec5SDimitry Andric     break;
11620b57cec5SDimitry Andric   case ISD::SMULFIX:
11630b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
11648bcb0991SDimitry Andric   case ISD::UMULFIX:
1165480093f4SDimitry Andric   case ISD::UMULFIXSAT:
1166480093f4SDimitry Andric   case ISD::SDIVFIX:
11675ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
11685ffd83dbSDimitry Andric   case ISD::UDIVFIX:
11695ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT: {
11700b57cec5SDimitry Andric     unsigned Scale = Node->getConstantOperandVal(2);
11710b57cec5SDimitry Andric     Action = TLI.getFixedPointOperationAction(Node->getOpcode(),
11720b57cec5SDimitry Andric                                               Node->getValueType(0), Scale);
11730b57cec5SDimitry Andric     break;
11740b57cec5SDimitry Andric   }
11750b57cec5SDimitry Andric   case ISD::MSCATTER:
11760b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11770b57cec5SDimitry Andric                     cast<MaskedScatterSDNode>(Node)->getValue().getValueType());
11780b57cec5SDimitry Andric     break;
11790b57cec5SDimitry Andric   case ISD::MSTORE:
11800b57cec5SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
11810b57cec5SDimitry Andric                     cast<MaskedStoreSDNode>(Node)->getValue().getValueType());
11820b57cec5SDimitry Andric     break;
1183349cc55cSDimitry Andric   case ISD::VP_SCATTER:
1184349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1185349cc55cSDimitry Andric         Node->getOpcode(),
1186349cc55cSDimitry Andric         cast<VPScatterSDNode>(Node)->getValue().getValueType());
1187349cc55cSDimitry Andric     break;
1188349cc55cSDimitry Andric   case ISD::VP_STORE:
1189349cc55cSDimitry Andric     Action = TLI.getOperationAction(
1190349cc55cSDimitry Andric         Node->getOpcode(),
1191349cc55cSDimitry Andric         cast<VPStoreSDNode>(Node)->getValue().getValueType());
1192349cc55cSDimitry Andric     break;
119381ad6265SDimitry Andric   case ISD::EXPERIMENTAL_VP_STRIDED_STORE:
119481ad6265SDimitry Andric     Action = TLI.getOperationAction(
119581ad6265SDimitry Andric         Node->getOpcode(),
119681ad6265SDimitry Andric         cast<VPStridedStoreSDNode>(Node)->getValue().getValueType());
119781ad6265SDimitry Andric     break;
11980b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
11990b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
12000b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
12010b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
12020b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
12030b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
12040b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
12050b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
12060b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
12070b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
12080b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
12090b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
12100b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
121106c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
121206c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
121381ad6265SDimitry Andric   case ISD::IS_FPCLASS:
12140b57cec5SDimitry Andric     Action = TLI.getOperationAction(
12150b57cec5SDimitry Andric         Node->getOpcode(), Node->getOperand(0).getValueType());
12160b57cec5SDimitry Andric     break;
1217e8d8bef9SDimitry Andric   case ISD::VECREDUCE_SEQ_FADD:
1218349cc55cSDimitry Andric   case ISD::VECREDUCE_SEQ_FMUL:
1219349cc55cSDimitry Andric   case ISD::VP_REDUCE_FADD:
1220349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMUL:
1221349cc55cSDimitry Andric   case ISD::VP_REDUCE_ADD:
1222349cc55cSDimitry Andric   case ISD::VP_REDUCE_MUL:
1223349cc55cSDimitry Andric   case ISD::VP_REDUCE_AND:
1224349cc55cSDimitry Andric   case ISD::VP_REDUCE_OR:
1225349cc55cSDimitry Andric   case ISD::VP_REDUCE_XOR:
1226349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMAX:
1227349cc55cSDimitry Andric   case ISD::VP_REDUCE_SMIN:
1228349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMAX:
1229349cc55cSDimitry Andric   case ISD::VP_REDUCE_UMIN:
1230349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMAX:
1231349cc55cSDimitry Andric   case ISD::VP_REDUCE_FMIN:
1232*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMAXIMUM:
1233*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMINIMUM:
1234349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FADD:
1235349cc55cSDimitry Andric   case ISD::VP_REDUCE_SEQ_FMUL:
1236e8d8bef9SDimitry Andric     Action = TLI.getOperationAction(
1237e8d8bef9SDimitry Andric         Node->getOpcode(), Node->getOperand(1).getValueType());
1238e8d8bef9SDimitry Andric     break;
1239*0fca6ea1SDimitry Andric   case ISD::VP_CTTZ_ELTS:
1240*0fca6ea1SDimitry Andric   case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
1241*0fca6ea1SDimitry Andric     Action = TLI.getOperationAction(Node->getOpcode(),
1242*0fca6ea1SDimitry Andric                                     Node->getOperand(0).getValueType());
1243*0fca6ea1SDimitry Andric     break;
12440b57cec5SDimitry Andric   default:
12450b57cec5SDimitry Andric     if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
124681ad6265SDimitry Andric       Action = TLI.getCustomOperationAction(*Node);
12470b57cec5SDimitry Andric     } else {
12480b57cec5SDimitry Andric       Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0));
12490b57cec5SDimitry Andric     }
12500b57cec5SDimitry Andric     break;
12510b57cec5SDimitry Andric   }
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric   if (SimpleFinishLegalizing) {
12540b57cec5SDimitry Andric     SDNode *NewNode = Node;
12550b57cec5SDimitry Andric     switch (Node->getOpcode()) {
12560b57cec5SDimitry Andric     default: break;
12570b57cec5SDimitry Andric     case ISD::SHL:
12580b57cec5SDimitry Andric     case ISD::SRL:
12590b57cec5SDimitry Andric     case ISD::SRA:
12600b57cec5SDimitry Andric     case ISD::ROTL:
12610b57cec5SDimitry Andric     case ISD::ROTR: {
12620b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12630b57cec5SDimitry Andric       // to the appropriate width.
12640b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12650b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12660b57cec5SDimitry Andric       if (!Op1.getValueType().isVector()) {
12670b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1);
12680b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12690b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12700b57cec5SDimitry Andric         // to update the parent node.
12710b57cec5SDimitry Andric         // Do not try to legalize SAO here! It will be automatically legalized
12720b57cec5SDimitry Andric         // in the next round.
12730b57cec5SDimitry Andric         if (SAO != Op1)
12740b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO);
12750b57cec5SDimitry Andric       }
12760b57cec5SDimitry Andric     }
12770b57cec5SDimitry Andric     break;
12780b57cec5SDimitry Andric     case ISD::FSHL:
12790b57cec5SDimitry Andric     case ISD::FSHR:
12800b57cec5SDimitry Andric     case ISD::SRL_PARTS:
12810b57cec5SDimitry Andric     case ISD::SRA_PARTS:
12820b57cec5SDimitry Andric     case ISD::SHL_PARTS: {
12830b57cec5SDimitry Andric       // Legalizing shifts/rotates requires adjusting the shift amount
12840b57cec5SDimitry Andric       // to the appropriate width.
12850b57cec5SDimitry Andric       SDValue Op0 = Node->getOperand(0);
12860b57cec5SDimitry Andric       SDValue Op1 = Node->getOperand(1);
12870b57cec5SDimitry Andric       SDValue Op2 = Node->getOperand(2);
12880b57cec5SDimitry Andric       if (!Op2.getValueType().isVector()) {
12890b57cec5SDimitry Andric         SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2);
12900b57cec5SDimitry Andric         // The getShiftAmountOperand() may create a new operand node or
12910b57cec5SDimitry Andric         // return the existing one. If new operand is created we need
12920b57cec5SDimitry Andric         // to update the parent node.
12930b57cec5SDimitry Andric         if (SAO != Op2)
12940b57cec5SDimitry Andric           NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO);
12950b57cec5SDimitry Andric       }
12960b57cec5SDimitry Andric       break;
12970b57cec5SDimitry Andric     }
12980b57cec5SDimitry Andric     }
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric     if (NewNode != Node) {
13010b57cec5SDimitry Andric       ReplaceNode(Node, NewNode);
13020b57cec5SDimitry Andric       Node = NewNode;
13030b57cec5SDimitry Andric     }
13040b57cec5SDimitry Andric     switch (Action) {
13050b57cec5SDimitry Andric     case TargetLowering::Legal:
13060b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n");
13070b57cec5SDimitry Andric       return;
13080b57cec5SDimitry Andric     case TargetLowering::Custom:
13090b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Trying custom legalization\n");
13100b57cec5SDimitry Andric       // FIXME: The handling for custom lowering with multiple results is
13110b57cec5SDimitry Andric       // a complete mess.
13120b57cec5SDimitry Andric       if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) {
13130b57cec5SDimitry Andric         if (!(Res.getNode() != Node || Res.getResNo() != 0))
13140b57cec5SDimitry Andric           return;
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric         if (Node->getNumValues() == 1) {
1317fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1318fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1319fe6060f1SDimitry Andric           assert((Res.getValueType() == Node->getValueType(0) ||
1320fe6060f1SDimitry Andric                   Node->getValueType(0) == MVT::Glue) &&
1321fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13220b57cec5SDimitry Andric           LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13230b57cec5SDimitry Andric           // We can just directly replace this node with the lowered value.
13240b57cec5SDimitry Andric           ReplaceNode(SDValue(Node, 0), Res);
13250b57cec5SDimitry Andric           return;
13260b57cec5SDimitry Andric         }
13270b57cec5SDimitry Andric 
13280b57cec5SDimitry Andric         SmallVector<SDValue, 8> ResultVals;
1329fe6060f1SDimitry Andric         for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
1330fe6060f1SDimitry Andric           // Verify the new types match the original. Glue is waived because
1331fe6060f1SDimitry Andric           // ISD::ADDC can be legalized by replacing Glue with an integer type.
1332fe6060f1SDimitry Andric           assert((Res->getValueType(i) == Node->getValueType(i) ||
1333fe6060f1SDimitry Andric                   Node->getValueType(i) == MVT::Glue) &&
1334fe6060f1SDimitry Andric                  "Type mismatch for custom legalized operation");
13350b57cec5SDimitry Andric           ResultVals.push_back(Res.getValue(i));
1336fe6060f1SDimitry Andric         }
13370b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n");
13380b57cec5SDimitry Andric         ReplaceNode(Node, ResultVals.data());
13390b57cec5SDimitry Andric         return;
13400b57cec5SDimitry Andric       }
13410b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Could not custom legalize node\n");
1342bdd1243dSDimitry Andric       [[fallthrough]];
13430b57cec5SDimitry Andric     case TargetLowering::Expand:
13440b57cec5SDimitry Andric       if (ExpandNode(Node))
13450b57cec5SDimitry Andric         return;
1346bdd1243dSDimitry Andric       [[fallthrough]];
13470b57cec5SDimitry Andric     case TargetLowering::LibCall:
13480b57cec5SDimitry Andric       ConvertNodeToLibcall(Node);
13490b57cec5SDimitry Andric       return;
13500b57cec5SDimitry Andric     case TargetLowering::Promote:
13510b57cec5SDimitry Andric       PromoteNode(Node);
13520b57cec5SDimitry Andric       return;
13530b57cec5SDimitry Andric     }
13540b57cec5SDimitry Andric   }
13550b57cec5SDimitry Andric 
13560b57cec5SDimitry Andric   switch (Node->getOpcode()) {
13570b57cec5SDimitry Andric   default:
13580b57cec5SDimitry Andric #ifndef NDEBUG
13590b57cec5SDimitry Andric     dbgs() << "NODE: ";
13600b57cec5SDimitry Andric     Node->dump( &DAG);
13610b57cec5SDimitry Andric     dbgs() << "\n";
13620b57cec5SDimitry Andric #endif
13630b57cec5SDimitry Andric     llvm_unreachable("Do not know how to legalize this operator!");
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric   case ISD::CALLSEQ_START:
13660b57cec5SDimitry Andric   case ISD::CALLSEQ_END:
13670b57cec5SDimitry Andric     break;
13680b57cec5SDimitry Andric   case ISD::LOAD:
13690b57cec5SDimitry Andric     return LegalizeLoadOps(Node);
13700b57cec5SDimitry Andric   case ISD::STORE:
13710b57cec5SDimitry Andric     return LegalizeStoreOps(Node);
13720b57cec5SDimitry Andric   }
13730b57cec5SDimitry Andric }
13740b57cec5SDimitry Andric 
13750b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
13760b57cec5SDimitry Andric   SDValue Vec = Op.getOperand(0);
13770b57cec5SDimitry Andric   SDValue Idx = Op.getOperand(1);
13780b57cec5SDimitry Andric   SDLoc dl(Op);
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric   // Before we generate a new store to a temporary stack slot, see if there is
13810b57cec5SDimitry Andric   // already one that we can use. There often is because when we scalarize
13820b57cec5SDimitry Andric   // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole
13830b57cec5SDimitry Andric   // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in
13840b57cec5SDimitry Andric   // the vector. If all are expanded here, we don't want one store per vector
13850b57cec5SDimitry Andric   // element.
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric   // Caches for hasPredecessorHelper
13880b57cec5SDimitry Andric   SmallPtrSet<const SDNode *, 32> Visited;
13890b57cec5SDimitry Andric   SmallVector<const SDNode *, 16> Worklist;
13900b57cec5SDimitry Andric   Visited.insert(Op.getNode());
13910b57cec5SDimitry Andric   Worklist.push_back(Idx.getNode());
13920b57cec5SDimitry Andric   SDValue StackPtr, Ch;
1393349cc55cSDimitry Andric   for (SDNode *User : Vec.getNode()->uses()) {
13940b57cec5SDimitry Andric     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) {
13950b57cec5SDimitry Andric       if (ST->isIndexed() || ST->isTruncatingStore() ||
13960b57cec5SDimitry Andric           ST->getValue() != Vec)
13970b57cec5SDimitry Andric         continue;
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric       // Make sure that nothing else could have stored into the destination of
14000b57cec5SDimitry Andric       // this store.
14010b57cec5SDimitry Andric       if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode()))
14020b57cec5SDimitry Andric         continue;
14030b57cec5SDimitry Andric 
14040b57cec5SDimitry Andric       // If the index is dependent on the store we will introduce a cycle when
14050b57cec5SDimitry Andric       // creating the load (the load uses the index, and by replacing the chain
14060b57cec5SDimitry Andric       // we will make the index dependent on the load). Also, the store might be
14070b57cec5SDimitry Andric       // dependent on the extractelement and introduce a cycle when creating
14080b57cec5SDimitry Andric       // the load.
14090b57cec5SDimitry Andric       if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) ||
14100b57cec5SDimitry Andric           ST->hasPredecessor(Op.getNode()))
14110b57cec5SDimitry Andric         continue;
14120b57cec5SDimitry Andric 
14130b57cec5SDimitry Andric       StackPtr = ST->getBasePtr();
14140b57cec5SDimitry Andric       Ch = SDValue(ST, 0);
14150b57cec5SDimitry Andric       break;
14160b57cec5SDimitry Andric     }
14170b57cec5SDimitry Andric   }
14180b57cec5SDimitry Andric 
14190b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   if (!Ch.getNode()) {
14220b57cec5SDimitry Andric     // Store the value to a temporary stack slot, then LOAD the returned part.
14230b57cec5SDimitry Andric     StackPtr = DAG.CreateStackTemporary(VecVT);
1424*0fca6ea1SDimitry Andric     MachineMemOperand *StoreMMO = getStackAlignedMMO(
1425*0fca6ea1SDimitry Andric         StackPtr, DAG.getMachineFunction(), VecVT.isScalableVector());
1426*0fca6ea1SDimitry Andric     Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, StoreMMO);
14270b57cec5SDimitry Andric   }
14280b57cec5SDimitry Andric 
14290b57cec5SDimitry Andric   SDValue NewLoad;
1430fcaf7f86SDimitry Andric   Align ElementAlignment =
1431fcaf7f86SDimitry Andric       std::min(cast<StoreSDNode>(Ch)->getAlign(),
1432fcaf7f86SDimitry Andric                DAG.getDataLayout().getPrefTypeAlign(
1433fcaf7f86SDimitry Andric                    Op.getValueType().getTypeForEVT(*DAG.getContext())));
14340b57cec5SDimitry Andric 
1435fe6060f1SDimitry Andric   if (Op.getValueType().isVector()) {
1436fe6060f1SDimitry Andric     StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
1437fe6060f1SDimitry Andric                                           Op.getValueType(), Idx);
1438fcaf7f86SDimitry Andric     NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
1439fcaf7f86SDimitry Andric                           MachinePointerInfo(), ElementAlignment);
1440fe6060f1SDimitry Andric   } else {
1441fe6060f1SDimitry Andric     StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
14420b57cec5SDimitry Andric     NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
1443fcaf7f86SDimitry Andric                              MachinePointerInfo(), VecVT.getVectorElementType(),
1444fcaf7f86SDimitry Andric                              ElementAlignment);
1445fe6060f1SDimitry Andric   }
14460b57cec5SDimitry Andric 
14470b57cec5SDimitry Andric   // Replace the chain going out of the store, by the one out of the load.
14480b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1));
14490b57cec5SDimitry Andric 
14500b57cec5SDimitry Andric   // We introduced a cycle though, so update the loads operands, making sure
14510b57cec5SDimitry Andric   // to use the original store's chain as an incoming chain.
14520b57cec5SDimitry Andric   SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(),
14530b57cec5SDimitry Andric                                           NewLoad->op_end());
14540b57cec5SDimitry Andric   NewLoadOperands[0] = Ch;
14550b57cec5SDimitry Andric   NewLoad =
14560b57cec5SDimitry Andric       SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0);
14570b57cec5SDimitry Andric   return NewLoad;
14580b57cec5SDimitry Andric }
14590b57cec5SDimitry Andric 
14600b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) {
14610b57cec5SDimitry Andric   assert(Op.getValueType().isVector() && "Non-vector insert subvector!");
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric   SDValue Vec  = Op.getOperand(0);
14640b57cec5SDimitry Andric   SDValue Part = Op.getOperand(1);
14650b57cec5SDimitry Andric   SDValue Idx  = Op.getOperand(2);
14660b57cec5SDimitry Andric   SDLoc dl(Op);
14670b57cec5SDimitry Andric 
14680b57cec5SDimitry Andric   // Store the value to a temporary stack slot, then LOAD the returned part.
14690b57cec5SDimitry Andric   EVT VecVT = Vec.getValueType();
1470*0fca6ea1SDimitry Andric   EVT PartVT = Part.getValueType();
14710b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(VecVT);
14720b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
14730b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
14740b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
14750b57cec5SDimitry Andric 
14760b57cec5SDimitry Andric   // First store the whole vector.
14770b57cec5SDimitry Andric   SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo);
14780b57cec5SDimitry Andric 
1479*0fca6ea1SDimitry Andric   // Freeze the index so we don't poison the clamping code we're about to emit.
1480*0fca6ea1SDimitry Andric   Idx = DAG.getFreeze(Idx);
1481*0fca6ea1SDimitry Andric 
14820b57cec5SDimitry Andric   // Then store the inserted part.
1483*0fca6ea1SDimitry Andric   if (PartVT.isVector()) {
1484fe6060f1SDimitry Andric     SDValue SubStackPtr =
1485*0fca6ea1SDimitry Andric         TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, PartVT, Idx);
14860b57cec5SDimitry Andric 
14870b57cec5SDimitry Andric     // Store the subvector.
14885ffd83dbSDimitry Andric     Ch = DAG.getStore(
14895ffd83dbSDimitry Andric         Ch, dl, Part, SubStackPtr,
14905ffd83dbSDimitry Andric         MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()));
1491*0fca6ea1SDimitry Andric   } else {
1492*0fca6ea1SDimitry Andric     SDValue SubStackPtr =
1493*0fca6ea1SDimitry Andric         TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
1494*0fca6ea1SDimitry Andric 
1495*0fca6ea1SDimitry Andric     // Store the scalar value.
1496*0fca6ea1SDimitry Andric     Ch = DAG.getTruncStore(
1497*0fca6ea1SDimitry Andric         Ch, dl, Part, SubStackPtr,
1498*0fca6ea1SDimitry Andric         MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()),
1499*0fca6ea1SDimitry Andric         VecVT.getVectorElementType());
1500*0fca6ea1SDimitry Andric   }
15010b57cec5SDimitry Andric 
15020b57cec5SDimitry Andric   // Finally, load the updated vector.
15030b57cec5SDimitry Andric   return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo);
15040b57cec5SDimitry Andric }
15050b57cec5SDimitry Andric 
15060b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) {
15075ffd83dbSDimitry Andric   assert((Node->getOpcode() == ISD::BUILD_VECTOR ||
15085ffd83dbSDimitry Andric           Node->getOpcode() == ISD::CONCAT_VECTORS) &&
15095ffd83dbSDimitry Andric          "Unexpected opcode!");
15105ffd83dbSDimitry Andric 
15110b57cec5SDimitry Andric   // We can't handle this case efficiently.  Allocate a sufficiently
15125ffd83dbSDimitry Andric   // aligned object on the stack, store each operand into it, then load
15130b57cec5SDimitry Andric   // the result as a vector.
15140b57cec5SDimitry Andric   // Create the stack frame object.
15150b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
15165ffd83dbSDimitry Andric   EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType()
15175ffd83dbSDimitry Andric                                            : Node->getOperand(0).getValueType();
15180b57cec5SDimitry Andric   SDLoc dl(Node);
15190b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(VT);
15200b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex();
15210b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
15220b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI);
15230b57cec5SDimitry Andric 
15240b57cec5SDimitry Andric   // Emit a store of each element to the stack slot.
15250b57cec5SDimitry Andric   SmallVector<SDValue, 8> Stores;
15265ffd83dbSDimitry Andric   unsigned TypeByteSize = MemVT.getSizeInBits() / 8;
15270b57cec5SDimitry Andric   assert(TypeByteSize > 0 && "Vector element type too small for stack store!");
1528e8d8bef9SDimitry Andric 
1529e8d8bef9SDimitry Andric   // If the destination vector element type of a BUILD_VECTOR is narrower than
1530e8d8bef9SDimitry Andric   // the source element type, only store the bits necessary.
1531e8d8bef9SDimitry Andric   bool Truncate = isa<BuildVectorSDNode>(Node) &&
1532e8d8bef9SDimitry Andric                   MemVT.bitsLT(Node->getOperand(0).getValueType());
1533e8d8bef9SDimitry Andric 
15340b57cec5SDimitry Andric   // Store (in the right endianness) the elements to memory.
15350b57cec5SDimitry Andric   for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
15360b57cec5SDimitry Andric     // Ignore undef elements.
15370b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef()) continue;
15380b57cec5SDimitry Andric 
15390b57cec5SDimitry Andric     unsigned Offset = TypeByteSize*i;
15400b57cec5SDimitry Andric 
15415f757f3fSDimitry Andric     SDValue Idx =
15425f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(FIPtr, TypeSize::getFixed(Offset), dl);
15430b57cec5SDimitry Andric 
1544e8d8bef9SDimitry Andric     if (Truncate)
15450b57cec5SDimitry Andric       Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl,
15460b57cec5SDimitry Andric                                          Node->getOperand(i), Idx,
15475ffd83dbSDimitry Andric                                          PtrInfo.getWithOffset(Offset), MemVT));
15485ffd83dbSDimitry Andric     else
15490b57cec5SDimitry Andric       Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i),
15500b57cec5SDimitry Andric                                     Idx, PtrInfo.getWithOffset(Offset)));
15510b57cec5SDimitry Andric   }
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric   SDValue StoreChain;
15540b57cec5SDimitry Andric   if (!Stores.empty())    // Not all undef elements?
15550b57cec5SDimitry Andric     StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores);
15560b57cec5SDimitry Andric   else
15570b57cec5SDimitry Andric     StoreChain = DAG.getEntryNode();
15580b57cec5SDimitry Andric 
15590b57cec5SDimitry Andric   // Result is a load from the stack slot.
15600b57cec5SDimitry Andric   return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo);
15610b57cec5SDimitry Andric }
15620b57cec5SDimitry Andric 
15630b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part
15640b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of
15650b57cec5SDimitry Andric /// holding all bits of the floating-point value.
15660b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State,
15670b57cec5SDimitry Andric                                              const SDLoc &DL,
15680b57cec5SDimitry Andric                                              SDValue Value) const {
15690b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
1570e8d8bef9SDimitry Andric   unsigned NumBits = FloatVT.getScalarSizeInBits();
15710b57cec5SDimitry Andric   State.FloatVT = FloatVT;
15720b57cec5SDimitry Andric   EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits);
15730b57cec5SDimitry Andric   // Convert to an integer of the same size.
15740b57cec5SDimitry Andric   if (TLI.isTypeLegal(IVT)) {
15750b57cec5SDimitry Andric     State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value);
15760b57cec5SDimitry Andric     State.SignMask = APInt::getSignMask(NumBits);
15770b57cec5SDimitry Andric     State.SignBit = NumBits - 1;
15780b57cec5SDimitry Andric     return;
15790b57cec5SDimitry Andric   }
15800b57cec5SDimitry Andric 
15810b57cec5SDimitry Andric   auto &DataLayout = DAG.getDataLayout();
15820b57cec5SDimitry Andric   // Store the float to memory, then load the sign part out as an integer.
158306c3fb27SDimitry Andric   MVT LoadTy = TLI.getRegisterType(MVT::i8);
15840b57cec5SDimitry Andric   // First create a temporary that is aligned for both the load and store.
15850b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy);
15860b57cec5SDimitry Andric   int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
15870b57cec5SDimitry Andric   // Then store the float to it.
15880b57cec5SDimitry Andric   State.FloatPtr = StackPtr;
15890b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
15900b57cec5SDimitry Andric   State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI);
15910b57cec5SDimitry Andric   State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr,
15920b57cec5SDimitry Andric                              State.FloatPointerInfo);
15930b57cec5SDimitry Andric 
15940b57cec5SDimitry Andric   SDValue IntPtr;
15950b57cec5SDimitry Andric   if (DataLayout.isBigEndian()) {
15960b57cec5SDimitry Andric     assert(FloatVT.isByteSized() && "Unsupported floating point type!");
15970b57cec5SDimitry Andric     // Load out a legal integer with the same sign bit as the float.
15980b57cec5SDimitry Andric     IntPtr = StackPtr;
15990b57cec5SDimitry Andric     State.IntPointerInfo = State.FloatPointerInfo;
16000b57cec5SDimitry Andric   } else {
16010b57cec5SDimitry Andric     // Advance the pointer so that the loaded byte will contain the sign bit.
1602e8d8bef9SDimitry Andric     unsigned ByteOffset = (NumBits / 8) - 1;
1603e8d8bef9SDimitry Andric     IntPtr =
16045f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL);
16050b57cec5SDimitry Andric     State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI,
16060b57cec5SDimitry Andric                                                              ByteOffset);
16070b57cec5SDimitry Andric   }
16080b57cec5SDimitry Andric 
16090b57cec5SDimitry Andric   State.IntPtr = IntPtr;
16100b57cec5SDimitry Andric   State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr,
16110b57cec5SDimitry Andric                                   State.IntPointerInfo, MVT::i8);
1612e8d8bef9SDimitry Andric   State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7);
16130b57cec5SDimitry Andric   State.SignBit = 7;
16140b57cec5SDimitry Andric }
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value
16170b57cec5SDimitry Andric /// and cast the result back to a floating-point type.
16180b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State,
16190b57cec5SDimitry Andric                                               const SDLoc &DL,
16200b57cec5SDimitry Andric                                               SDValue NewIntValue) const {
16210b57cec5SDimitry Andric   if (!State.Chain)
16220b57cec5SDimitry Andric     return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue);
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   // Override the part containing the sign bit in the value stored on the stack.
16250b57cec5SDimitry Andric   SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr,
16260b57cec5SDimitry Andric                                     State.IntPointerInfo, MVT::i8);
16270b57cec5SDimitry Andric   return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr,
16280b57cec5SDimitry Andric                      State.FloatPointerInfo);
16290b57cec5SDimitry Andric }
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const {
16320b57cec5SDimitry Andric   SDLoc DL(Node);
16330b57cec5SDimitry Andric   SDValue Mag = Node->getOperand(0);
16340b57cec5SDimitry Andric   SDValue Sign = Node->getOperand(1);
16350b57cec5SDimitry Andric 
16360b57cec5SDimitry Andric   // Get sign bit into an integer value.
16370b57cec5SDimitry Andric   FloatSignAsInt SignAsInt;
16380b57cec5SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Sign);
16390b57cec5SDimitry Andric 
16400b57cec5SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
16410b57cec5SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
16420b57cec5SDimitry Andric   SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue,
16430b57cec5SDimitry Andric                                 SignMask);
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X)
16460b57cec5SDimitry Andric   EVT FloatVT = Mag.getValueType();
16470b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) &&
16480b57cec5SDimitry Andric       TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) {
16490b57cec5SDimitry Andric     SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag);
16500b57cec5SDimitry Andric     SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue);
16510b57cec5SDimitry Andric     SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit,
16520b57cec5SDimitry Andric                                 DAG.getConstant(0, DL, IntVT), ISD::SETNE);
16530b57cec5SDimitry Andric     return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue);
16540b57cec5SDimitry Andric   }
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   // Transform Mag value to integer, and clear the sign bit.
16570b57cec5SDimitry Andric   FloatSignAsInt MagAsInt;
16580b57cec5SDimitry Andric   getSignAsIntValue(MagAsInt, DL, Mag);
16590b57cec5SDimitry Andric   EVT MagVT = MagAsInt.IntValue.getValueType();
16600b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT);
16610b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue,
16620b57cec5SDimitry Andric                                     ClearSignMask);
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric   // Get the signbit at the right position for MagAsInt.
16650b57cec5SDimitry Andric   int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit;
16660b57cec5SDimitry Andric   EVT ShiftVT = IntVT;
1667e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() <
1668e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16690b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit);
16700b57cec5SDimitry Andric     ShiftVT = MagVT;
16710b57cec5SDimitry Andric   }
16720b57cec5SDimitry Andric   if (ShiftAmount > 0) {
16730b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT);
16740b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst);
16750b57cec5SDimitry Andric   } else if (ShiftAmount < 0) {
16760b57cec5SDimitry Andric     SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT);
16770b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst);
16780b57cec5SDimitry Andric   }
1679e8d8bef9SDimitry Andric   if (SignBit.getScalarValueSizeInBits() >
1680e8d8bef9SDimitry Andric       ClearedSign.getScalarValueSizeInBits()) {
16810b57cec5SDimitry Andric     SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit);
16820b57cec5SDimitry Andric   }
16830b57cec5SDimitry Andric 
1684*0fca6ea1SDimitry Andric   SDNodeFlags Flags;
1685*0fca6ea1SDimitry Andric   Flags.setDisjoint(true);
1686*0fca6ea1SDimitry Andric 
16870b57cec5SDimitry Andric   // Store the part with the modified sign and convert back to float.
1688*0fca6ea1SDimitry Andric   SDValue CopiedSign =
1689*0fca6ea1SDimitry Andric       DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit, Flags);
1690*0fca6ea1SDimitry Andric 
16910b57cec5SDimitry Andric   return modifySignAsInt(MagAsInt, DL, CopiedSign);
16920b57cec5SDimitry Andric }
16930b57cec5SDimitry Andric 
1694e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const {
1695e8d8bef9SDimitry Andric   // Get the sign bit as an integer.
1696e8d8bef9SDimitry Andric   SDLoc DL(Node);
1697e8d8bef9SDimitry Andric   FloatSignAsInt SignAsInt;
1698e8d8bef9SDimitry Andric   getSignAsIntValue(SignAsInt, DL, Node->getOperand(0));
1699e8d8bef9SDimitry Andric   EVT IntVT = SignAsInt.IntValue.getValueType();
1700e8d8bef9SDimitry Andric 
1701e8d8bef9SDimitry Andric   // Flip the sign.
1702e8d8bef9SDimitry Andric   SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT);
1703e8d8bef9SDimitry Andric   SDValue SignFlip =
1704e8d8bef9SDimitry Andric       DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask);
1705e8d8bef9SDimitry Andric 
1706e8d8bef9SDimitry Andric   // Convert back to float.
1707e8d8bef9SDimitry Andric   return modifySignAsInt(SignAsInt, DL, SignFlip);
1708e8d8bef9SDimitry Andric }
1709e8d8bef9SDimitry Andric 
17100b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const {
17110b57cec5SDimitry Andric   SDLoc DL(Node);
17120b57cec5SDimitry Andric   SDValue Value = Node->getOperand(0);
17130b57cec5SDimitry Andric 
17140b57cec5SDimitry Andric   // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal.
17150b57cec5SDimitry Andric   EVT FloatVT = Value.getValueType();
17160b57cec5SDimitry Andric   if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) {
17170b57cec5SDimitry Andric     SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT);
17180b57cec5SDimitry Andric     return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero);
17190b57cec5SDimitry Andric   }
17200b57cec5SDimitry Andric 
17210b57cec5SDimitry Andric   // Transform value to integer, clear the sign bit and transform back.
17220b57cec5SDimitry Andric   FloatSignAsInt ValueAsInt;
17230b57cec5SDimitry Andric   getSignAsIntValue(ValueAsInt, DL, Value);
17240b57cec5SDimitry Andric   EVT IntVT = ValueAsInt.IntValue.getValueType();
17250b57cec5SDimitry Andric   SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT);
17260b57cec5SDimitry Andric   SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue,
17270b57cec5SDimitry Andric                                     ClearSignMask);
17280b57cec5SDimitry Andric   return modifySignAsInt(ValueAsInt, DL, ClearedSign);
17290b57cec5SDimitry Andric }
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node,
17320b57cec5SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
1733e8d8bef9SDimitry Andric   Register SPReg = TLI.getStackPointerRegisterToSaveRestore();
17340b57cec5SDimitry Andric   assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
17350b57cec5SDimitry Andric           " not tell us which reg is the stack pointer!");
17360b57cec5SDimitry Andric   SDLoc dl(Node);
17370b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
17380b57cec5SDimitry Andric   SDValue Tmp1 = SDValue(Node, 0);
17390b57cec5SDimitry Andric   SDValue Tmp2 = SDValue(Node, 1);
17400b57cec5SDimitry Andric   SDValue Tmp3 = Node->getOperand(2);
17410b57cec5SDimitry Andric   SDValue Chain = Tmp1.getOperand(0);
17420b57cec5SDimitry Andric 
17430b57cec5SDimitry Andric   // Chain the dynamic stack allocation so that it doesn't modify the stack
17440b57cec5SDimitry Andric   // pointer when other instructions are using the stack.
17450b57cec5SDimitry Andric   Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl);
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   SDValue Size  = Tmp2.getOperand(1);
17480b57cec5SDimitry Andric   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
17490b57cec5SDimitry Andric   Chain = SP.getValue(1);
17505ffd83dbSDimitry Andric   Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue();
17515ffd83dbSDimitry Andric   const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering();
17525ffd83dbSDimitry Andric   unsigned Opc =
17535ffd83dbSDimitry Andric     TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ?
17545ffd83dbSDimitry Andric     ISD::ADD : ISD::SUB;
17555ffd83dbSDimitry Andric 
17565ffd83dbSDimitry Andric   Align StackAlign = TFL->getStackAlign();
17575ffd83dbSDimitry Andric   Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size);       // Value
17585ffd83dbSDimitry Andric   if (Alignment > StackAlign)
17590b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1,
17605ffd83dbSDimitry Andric                        DAG.getConstant(-Alignment.value(), dl, VT));
17610b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1);     // Output chain
17620b57cec5SDimitry Andric 
1763bdd1243dSDimitry Andric   Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl);
17640b57cec5SDimitry Andric 
17650b57cec5SDimitry Andric   Results.push_back(Tmp1);
17660b57cec5SDimitry Andric   Results.push_back(Tmp2);
17670b57cec5SDimitry Andric }
17680b57cec5SDimitry Andric 
17690b57cec5SDimitry Andric /// Emit a store/load combination to the stack.  This stores
17700b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed.  It then does
17710b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed.
17720b57cec5SDimitry Andric /// The resultant code need not be legal.
17730b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17740b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl) {
17750b57cec5SDimitry Andric   return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode());
17760b57cec5SDimitry Andric }
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT,
17790b57cec5SDimitry Andric                                                EVT DestVT, const SDLoc &dl,
17800b57cec5SDimitry Andric                                                SDValue Chain) {
178181ad6265SDimitry Andric   EVT SrcVT = SrcOp.getValueType();
1782e8d8bef9SDimitry Andric   Type *DestType = DestVT.getTypeForEVT(*DAG.getContext());
1783e8d8bef9SDimitry Andric   Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType);
1784e8d8bef9SDimitry Andric 
1785e8d8bef9SDimitry Andric   // Don't convert with stack if the load/store is expensive.
178681ad6265SDimitry Andric   if ((SrcVT.bitsGT(SlotVT) &&
1787e8d8bef9SDimitry Andric        !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) ||
178881ad6265SDimitry Andric       (SlotVT.bitsLT(DestVT) &&
1789e8d8bef9SDimitry Andric        !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT)))
1790e8d8bef9SDimitry Andric     return SDValue();
1791e8d8bef9SDimitry Andric 
17920b57cec5SDimitry Andric   // Create the stack frame object.
1793e8d8bef9SDimitry Andric   Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign(
17940b57cec5SDimitry Andric       SrcOp.getValueType().getTypeForEVT(*DAG.getContext()));
1795e8d8bef9SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign);
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
17980b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
17990b57cec5SDimitry Andric   MachinePointerInfo PtrInfo =
18000b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI);
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric   // Emit a store to the stack slot.  Use a truncstore if the input value is
18030b57cec5SDimitry Andric   // later than DestVT.
18040b57cec5SDimitry Andric   SDValue Store;
18050b57cec5SDimitry Andric 
180681ad6265SDimitry Andric   if (SrcVT.bitsGT(SlotVT))
18070b57cec5SDimitry Andric     Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo,
18080b57cec5SDimitry Andric                               SlotVT, SrcAlign);
18090b57cec5SDimitry Andric   else {
181081ad6265SDimitry Andric     assert(SrcVT.bitsEq(SlotVT) && "Invalid store");
181181ad6265SDimitry Andric     Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign);
18120b57cec5SDimitry Andric   }
18130b57cec5SDimitry Andric 
18140b57cec5SDimitry Andric   // Result is a load from the stack slot.
181581ad6265SDimitry Andric   if (SlotVT.bitsEq(DestVT))
18160b57cec5SDimitry Andric     return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign);
18170b57cec5SDimitry Andric 
181881ad6265SDimitry Andric   assert(SlotVT.bitsLT(DestVT) && "Unknown extension!");
18190b57cec5SDimitry Andric   return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT,
18200b57cec5SDimitry Andric                         DestAlign);
18210b57cec5SDimitry Andric }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
18240b57cec5SDimitry Andric   SDLoc dl(Node);
18250b57cec5SDimitry Andric   // Create a vector sized/aligned stack slot, store the value to element #0,
18260b57cec5SDimitry Andric   // then load the whole vector back out.
18270b57cec5SDimitry Andric   SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
18280b57cec5SDimitry Andric 
18290b57cec5SDimitry Andric   FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
18300b57cec5SDimitry Andric   int SPFI = StackPtrFI->getIndex();
18310b57cec5SDimitry Andric 
18320b57cec5SDimitry Andric   SDValue Ch = DAG.getTruncStore(
18330b57cec5SDimitry Andric       DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr,
18340b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI),
18350b57cec5SDimitry Andric       Node->getValueType(0).getVectorElementType());
18360b57cec5SDimitry Andric   return DAG.getLoad(
18370b57cec5SDimitry Andric       Node->getValueType(0), dl, Ch, StackPtr,
18380b57cec5SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
18390b57cec5SDimitry Andric }
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric static bool
18420b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG,
18430b57cec5SDimitry Andric                      const TargetLowering &TLI, SDValue &Res) {
18440b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
18450b57cec5SDimitry Andric   SDLoc dl(Node);
18460b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric   // Try to group the scalars into pairs, shuffle the pairs together, then
18490b57cec5SDimitry Andric   // shuffle the pairs of pairs together, etc. until the vector has
18500b57cec5SDimitry Andric   // been built. This will work only if all of the necessary shuffle masks
18510b57cec5SDimitry Andric   // are legal.
18520b57cec5SDimitry Andric 
18530b57cec5SDimitry Andric   // We do this in two phases; first to check the legality of the shuffles,
18540b57cec5SDimitry Andric   // and next, assuming that all shuffles are legal, to create the new nodes.
18550b57cec5SDimitry Andric   for (int Phase = 0; Phase < 2; ++Phase) {
18560b57cec5SDimitry Andric     SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals,
18570b57cec5SDimitry Andric                                                               NewIntermedVals;
18580b57cec5SDimitry Andric     for (unsigned i = 0; i < NumElems; ++i) {
18590b57cec5SDimitry Andric       SDValue V = Node->getOperand(i);
18600b57cec5SDimitry Andric       if (V.isUndef())
18610b57cec5SDimitry Andric         continue;
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric       SDValue Vec;
18640b57cec5SDimitry Andric       if (Phase)
18650b57cec5SDimitry Andric         Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V);
18660b57cec5SDimitry Andric       IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i)));
18670b57cec5SDimitry Andric     }
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric     while (IntermedVals.size() > 2) {
18700b57cec5SDimitry Andric       NewIntermedVals.clear();
18710b57cec5SDimitry Andric       for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) {
18720b57cec5SDimitry Andric         // This vector and the next vector are shuffled together (simply to
18730b57cec5SDimitry Andric         // append the one to the other).
18740b57cec5SDimitry Andric         SmallVector<int, 16> ShuffleVec(NumElems, -1);
18750b57cec5SDimitry Andric 
18760b57cec5SDimitry Andric         SmallVector<int, 16> FinalIndices;
18770b57cec5SDimitry Andric         FinalIndices.reserve(IntermedVals[i].second.size() +
18780b57cec5SDimitry Andric                              IntermedVals[i+1].second.size());
18790b57cec5SDimitry Andric 
18800b57cec5SDimitry Andric         int k = 0;
18810b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f;
18820b57cec5SDimitry Andric              ++j, ++k) {
18830b57cec5SDimitry Andric           ShuffleVec[k] = j;
18840b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i].second[j]);
18850b57cec5SDimitry Andric         }
18860b57cec5SDimitry Andric         for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f;
18870b57cec5SDimitry Andric              ++j, ++k) {
18880b57cec5SDimitry Andric           ShuffleVec[k] = NumElems + j;
18890b57cec5SDimitry Andric           FinalIndices.push_back(IntermedVals[i+1].second[j]);
18900b57cec5SDimitry Andric         }
18910b57cec5SDimitry Andric 
18920b57cec5SDimitry Andric         SDValue Shuffle;
18930b57cec5SDimitry Andric         if (Phase)
18940b57cec5SDimitry Andric           Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first,
18950b57cec5SDimitry Andric                                          IntermedVals[i+1].first,
18960b57cec5SDimitry Andric                                          ShuffleVec);
18970b57cec5SDimitry Andric         else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
18980b57cec5SDimitry Andric           return false;
18990b57cec5SDimitry Andric         NewIntermedVals.push_back(
19000b57cec5SDimitry Andric             std::make_pair(Shuffle, std::move(FinalIndices)));
19010b57cec5SDimitry Andric       }
19020b57cec5SDimitry Andric 
19030b57cec5SDimitry Andric       // If we had an odd number of defined values, then append the last
19040b57cec5SDimitry Andric       // element to the array of new vectors.
19050b57cec5SDimitry Andric       if ((IntermedVals.size() & 1) != 0)
19060b57cec5SDimitry Andric         NewIntermedVals.push_back(IntermedVals.back());
19070b57cec5SDimitry Andric 
19080b57cec5SDimitry Andric       IntermedVals.swap(NewIntermedVals);
19090b57cec5SDimitry Andric     }
19100b57cec5SDimitry Andric 
19110b57cec5SDimitry Andric     assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 &&
19120b57cec5SDimitry Andric            "Invalid number of intermediate vectors");
19130b57cec5SDimitry Andric     SDValue Vec1 = IntermedVals[0].first;
19140b57cec5SDimitry Andric     SDValue Vec2;
19150b57cec5SDimitry Andric     if (IntermedVals.size() > 1)
19160b57cec5SDimitry Andric       Vec2 = IntermedVals[1].first;
19170b57cec5SDimitry Andric     else if (Phase)
19180b57cec5SDimitry Andric       Vec2 = DAG.getUNDEF(VT);
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric     SmallVector<int, 16> ShuffleVec(NumElems, -1);
19210b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i)
19220b57cec5SDimitry Andric       ShuffleVec[IntermedVals[0].second[i]] = i;
19230b57cec5SDimitry Andric     for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i)
19240b57cec5SDimitry Andric       ShuffleVec[IntermedVals[1].second[i]] = NumElems + i;
19250b57cec5SDimitry Andric 
19260b57cec5SDimitry Andric     if (Phase)
19270b57cec5SDimitry Andric       Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
19280b57cec5SDimitry Andric     else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT))
19290b57cec5SDimitry Andric       return false;
19300b57cec5SDimitry Andric   }
19310b57cec5SDimitry Andric 
19320b57cec5SDimitry Andric   return true;
19330b57cec5SDimitry Andric }
19340b57cec5SDimitry Andric 
19350b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't
19360b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type.
19370b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
19380b57cec5SDimitry Andric   unsigned NumElems = Node->getNumOperands();
19390b57cec5SDimitry Andric   SDValue Value1, Value2;
19400b57cec5SDimitry Andric   SDLoc dl(Node);
19410b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
19420b57cec5SDimitry Andric   EVT OpVT = Node->getOperand(0).getValueType();
19430b57cec5SDimitry Andric   EVT EltVT = VT.getVectorElementType();
19440b57cec5SDimitry Andric 
19450b57cec5SDimitry Andric   // If the only non-undef value is the low element, turn this into a
19460b57cec5SDimitry Andric   // SCALAR_TO_VECTOR node.  If this is { X, X, X, X }, determine X.
19470b57cec5SDimitry Andric   bool isOnlyLowElement = true;
19480b57cec5SDimitry Andric   bool MoreThanTwoValues = false;
19490b57cec5SDimitry Andric   bool isConstant = true;
19500b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
19510b57cec5SDimitry Andric     SDValue V = Node->getOperand(i);
19520b57cec5SDimitry Andric     if (V.isUndef())
19530b57cec5SDimitry Andric       continue;
19540b57cec5SDimitry Andric     if (i > 0)
19550b57cec5SDimitry Andric       isOnlyLowElement = false;
19560b57cec5SDimitry Andric     if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V))
19570b57cec5SDimitry Andric       isConstant = false;
19580b57cec5SDimitry Andric 
19590b57cec5SDimitry Andric     if (!Value1.getNode()) {
19600b57cec5SDimitry Andric       Value1 = V;
19610b57cec5SDimitry Andric     } else if (!Value2.getNode()) {
19620b57cec5SDimitry Andric       if (V != Value1)
19630b57cec5SDimitry Andric         Value2 = V;
19640b57cec5SDimitry Andric     } else if (V != Value1 && V != Value2) {
19650b57cec5SDimitry Andric       MoreThanTwoValues = true;
19660b57cec5SDimitry Andric     }
19670b57cec5SDimitry Andric   }
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric   if (!Value1.getNode())
19700b57cec5SDimitry Andric     return DAG.getUNDEF(VT);
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric   if (isOnlyLowElement)
19730b57cec5SDimitry Andric     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0));
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric   // If all elements are constants, create a load from the constant pool.
19760b57cec5SDimitry Andric   if (isConstant) {
19770b57cec5SDimitry Andric     SmallVector<Constant*, 16> CV;
19780b57cec5SDimitry Andric     for (unsigned i = 0, e = NumElems; i != e; ++i) {
19790b57cec5SDimitry Andric       if (ConstantFPSDNode *V =
19800b57cec5SDimitry Andric           dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
19810b57cec5SDimitry Andric         CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
19820b57cec5SDimitry Andric       } else if (ConstantSDNode *V =
19830b57cec5SDimitry Andric                  dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
19840b57cec5SDimitry Andric         if (OpVT==EltVT)
19850b57cec5SDimitry Andric           CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
19860b57cec5SDimitry Andric         else {
19870b57cec5SDimitry Andric           // If OpVT and EltVT don't match, EltVT is not legal and the
19880b57cec5SDimitry Andric           // element values have been promoted/truncated earlier.  Undo this;
19890b57cec5SDimitry Andric           // we don't want a v16i8 to become a v16i32 for example.
19900b57cec5SDimitry Andric           const ConstantInt *CI = V->getConstantIntValue();
19910b57cec5SDimitry Andric           CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()),
19920b57cec5SDimitry Andric                                         CI->getZExtValue()));
19930b57cec5SDimitry Andric         }
19940b57cec5SDimitry Andric       } else {
19950b57cec5SDimitry Andric         assert(Node->getOperand(i).isUndef());
19960b57cec5SDimitry Andric         Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext());
19970b57cec5SDimitry Andric         CV.push_back(UndefValue::get(OpNTy));
19980b57cec5SDimitry Andric       }
19990b57cec5SDimitry Andric     }
20000b57cec5SDimitry Andric     Constant *CP = ConstantVector::get(CV);
20010b57cec5SDimitry Andric     SDValue CPIdx =
20020b57cec5SDimitry Andric         DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout()));
20035ffd83dbSDimitry Andric     Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
20040b57cec5SDimitry Andric     return DAG.getLoad(
20050b57cec5SDimitry Andric         VT, dl, DAG.getEntryNode(), CPIdx,
20060b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
20070b57cec5SDimitry Andric         Alignment);
20080b57cec5SDimitry Andric   }
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric   SmallSet<SDValue, 16> DefinedValues;
20110b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElems; ++i) {
20120b57cec5SDimitry Andric     if (Node->getOperand(i).isUndef())
20130b57cec5SDimitry Andric       continue;
20140b57cec5SDimitry Andric     DefinedValues.insert(Node->getOperand(i));
20150b57cec5SDimitry Andric   }
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric   if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) {
20180b57cec5SDimitry Andric     if (!MoreThanTwoValues) {
20190b57cec5SDimitry Andric       SmallVector<int, 8> ShuffleVec(NumElems, -1);
20200b57cec5SDimitry Andric       for (unsigned i = 0; i < NumElems; ++i) {
20210b57cec5SDimitry Andric         SDValue V = Node->getOperand(i);
20220b57cec5SDimitry Andric         if (V.isUndef())
20230b57cec5SDimitry Andric           continue;
20240b57cec5SDimitry Andric         ShuffleVec[i] = V == Value1 ? 0 : NumElems;
20250b57cec5SDimitry Andric       }
20260b57cec5SDimitry Andric       if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) {
20270b57cec5SDimitry Andric         // Get the splatted value into the low element of a vector register.
20280b57cec5SDimitry Andric         SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1);
20290b57cec5SDimitry Andric         SDValue Vec2;
20300b57cec5SDimitry Andric         if (Value2.getNode())
20310b57cec5SDimitry Andric           Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2);
20320b57cec5SDimitry Andric         else
20330b57cec5SDimitry Andric           Vec2 = DAG.getUNDEF(VT);
20340b57cec5SDimitry Andric 
20350b57cec5SDimitry Andric         // Return shuffle(LowValVec, undef, <0,0,0,0>)
20360b57cec5SDimitry Andric         return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec);
20370b57cec5SDimitry Andric       }
20380b57cec5SDimitry Andric     } else {
20390b57cec5SDimitry Andric       SDValue Res;
20400b57cec5SDimitry Andric       if (ExpandBVWithShuffles(Node, DAG, TLI, Res))
20410b57cec5SDimitry Andric         return Res;
20420b57cec5SDimitry Andric     }
20430b57cec5SDimitry Andric   }
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric   // Otherwise, we can't handle this case efficiently.
20460b57cec5SDimitry Andric   return ExpandVectorBuildThroughStack(Node);
20470b57cec5SDimitry Andric }
20480b57cec5SDimitry Andric 
20498bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) {
20508bcb0991SDimitry Andric   SDLoc DL(Node);
20518bcb0991SDimitry Andric   EVT VT = Node->getValueType(0);
20528bcb0991SDimitry Andric   SDValue SplatVal = Node->getOperand(0);
20538bcb0991SDimitry Andric 
20548bcb0991SDimitry Andric   return DAG.getSplatBuildVector(VT, DL, SplatVal);
20558bcb0991SDimitry Andric }
20568bcb0991SDimitry Andric 
205706c3fb27SDimitry Andric // Expand a node into a call to a libcall, returning the value as the first
205806c3fb27SDimitry Andric // result and the chain as the second.  If the result value does not fit into a
205906c3fb27SDimitry Andric // register, return the lo part and set the hi part to the by-reg argument in
206006c3fb27SDimitry Andric // the first.  If it does fit into a single register, return the result and
206106c3fb27SDimitry Andric // leave the Hi part unset.
206206c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
206306c3fb27SDimitry Andric                                             TargetLowering::ArgListTy &&Args,
20640b57cec5SDimitry Andric                                             bool isSigned) {
2065*0fca6ea1SDimitry Andric   EVT CodePtrTy = TLI.getPointerTy(DAG.getDataLayout());
2066*0fca6ea1SDimitry Andric   SDValue Callee;
2067*0fca6ea1SDimitry Andric   if (const char *LibcallName = TLI.getLibcallName(LC))
2068*0fca6ea1SDimitry Andric     Callee = DAG.getExternalSymbol(LibcallName, CodePtrTy);
2069*0fca6ea1SDimitry Andric   else {
2070*0fca6ea1SDimitry Andric     Callee = DAG.getUNDEF(CodePtrTy);
2071*0fca6ea1SDimitry Andric     DAG.getContext()->emitError(Twine("no libcall available for ") +
2072*0fca6ea1SDimitry Andric                                 Node->getOperationName(&DAG));
2073*0fca6ea1SDimitry Andric   }
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
20760b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
20770b57cec5SDimitry Andric 
20780b57cec5SDimitry Andric   // By default, the input chain to this libcall is the entry node of the
20790b57cec5SDimitry Andric   // function. If the libcall is going to be emitted as a tail call then
20800b57cec5SDimitry Andric   // TLI.isUsedByReturnOnly will change it to the right chain if the return
20810b57cec5SDimitry Andric   // node which is being folded has a non-entry input chain.
20820b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
20830b57cec5SDimitry Andric 
20840b57cec5SDimitry Andric   // isTailCall may be true since the callee does not reference caller stack
20850b57cec5SDimitry Andric   // frame. Check if it's in the right position and that the return types match.
20860b57cec5SDimitry Andric   SDValue TCChain = InChain;
20870b57cec5SDimitry Andric   const Function &F = DAG.getMachineFunction().getFunction();
20880b57cec5SDimitry Andric   bool isTailCall =
20890b57cec5SDimitry Andric       TLI.isInTailCallPosition(DAG, Node, TCChain) &&
20900b57cec5SDimitry Andric       (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy());
20910b57cec5SDimitry Andric   if (isTailCall)
20920b57cec5SDimitry Andric     InChain = TCChain;
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
20950b57cec5SDimitry Andric   bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned);
20960b57cec5SDimitry Andric   CLI.setDebugLoc(SDLoc(Node))
20970b57cec5SDimitry Andric       .setChain(InChain)
20980b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
20990b57cec5SDimitry Andric                     std::move(Args))
21000b57cec5SDimitry Andric       .setTailCall(isTailCall)
21010b57cec5SDimitry Andric       .setSExtResult(signExtend)
21020b57cec5SDimitry Andric       .setZExtResult(!signExtend)
21030b57cec5SDimitry Andric       .setIsPostTypeLegalization(true);
21040b57cec5SDimitry Andric 
21050b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
21060b57cec5SDimitry Andric 
21070b57cec5SDimitry Andric   if (!CallInfo.second.getNode()) {
21088bcb0991SDimitry Andric     LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG));
21090b57cec5SDimitry Andric     // It's a tailcall, return the chain (which is the DAG root).
211006c3fb27SDimitry Andric     return {DAG.getRoot(), DAG.getRoot()};
21110b57cec5SDimitry Andric   }
21120b57cec5SDimitry Andric 
21138bcb0991SDimitry Andric   LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG));
211406c3fb27SDimitry Andric   return CallInfo;
211506c3fb27SDimitry Andric }
211606c3fb27SDimitry Andric 
211706c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
211806c3fb27SDimitry Andric                                             bool isSigned) {
211906c3fb27SDimitry Andric   TargetLowering::ArgListTy Args;
212006c3fb27SDimitry Andric   TargetLowering::ArgListEntry Entry;
212106c3fb27SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
212206c3fb27SDimitry Andric     EVT ArgVT = Op.getValueType();
212306c3fb27SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
212406c3fb27SDimitry Andric     Entry.Node = Op;
212506c3fb27SDimitry Andric     Entry.Ty = ArgTy;
212606c3fb27SDimitry Andric     Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned);
212706c3fb27SDimitry Andric     Entry.IsZExt = !Entry.IsSExt;
212806c3fb27SDimitry Andric     Args.push_back(Entry);
212906c3fb27SDimitry Andric   }
213006c3fb27SDimitry Andric 
213106c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, std::move(Args), isSigned);
213206c3fb27SDimitry Andric }
213306c3fb27SDimitry Andric 
213406c3fb27SDimitry Andric void SelectionDAGLegalize::ExpandFrexpLibCall(
213506c3fb27SDimitry Andric     SDNode *Node, SmallVectorImpl<SDValue> &Results) {
213606c3fb27SDimitry Andric   SDLoc dl(Node);
213706c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
213806c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
213906c3fb27SDimitry Andric 
214006c3fb27SDimitry Andric   SDValue FPOp = Node->getOperand(0);
214106c3fb27SDimitry Andric 
214206c3fb27SDimitry Andric   EVT ArgVT = FPOp.getValueType();
214306c3fb27SDimitry Andric   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
214406c3fb27SDimitry Andric 
214506c3fb27SDimitry Andric   TargetLowering::ArgListEntry FPArgEntry;
214606c3fb27SDimitry Andric   FPArgEntry.Node = FPOp;
214706c3fb27SDimitry Andric   FPArgEntry.Ty = ArgTy;
214806c3fb27SDimitry Andric 
214906c3fb27SDimitry Andric   SDValue StackSlot = DAG.CreateStackTemporary(ExpVT);
215006c3fb27SDimitry Andric   TargetLowering::ArgListEntry PtrArgEntry;
215106c3fb27SDimitry Andric   PtrArgEntry.Node = StackSlot;
215206c3fb27SDimitry Andric   PtrArgEntry.Ty = PointerType::get(*DAG.getContext(),
215306c3fb27SDimitry Andric                                     DAG.getDataLayout().getAllocaAddrSpace());
215406c3fb27SDimitry Andric 
215506c3fb27SDimitry Andric   TargetLowering::ArgListTy Args = {FPArgEntry, PtrArgEntry};
215606c3fb27SDimitry Andric 
215706c3fb27SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFREXP(VT);
215806c3fb27SDimitry Andric   auto [Call, Chain] = ExpandLibCall(LC, Node, std::move(Args), false);
215906c3fb27SDimitry Andric 
216006c3fb27SDimitry Andric   // FIXME: Get type of int for libcall declaration and cast
216106c3fb27SDimitry Andric 
216206c3fb27SDimitry Andric   int FrameIdx = cast<FrameIndexSDNode>(StackSlot)->getIndex();
216306c3fb27SDimitry Andric   auto PtrInfo =
216406c3fb27SDimitry Andric       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx);
216506c3fb27SDimitry Andric 
216606c3fb27SDimitry Andric   SDValue LoadExp = DAG.getLoad(ExpVT, dl, Chain, StackSlot, PtrInfo);
216706c3fb27SDimitry Andric   SDValue OutputChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
216806c3fb27SDimitry Andric                                     LoadExp.getValue(1), DAG.getRoot());
216906c3fb27SDimitry Andric   DAG.setRoot(OutputChain);
217006c3fb27SDimitry Andric 
217106c3fb27SDimitry Andric   Results.push_back(Call);
217206c3fb27SDimitry Andric   Results.push_back(LoadExp);
21730b57cec5SDimitry Andric }
21740b57cec5SDimitry Andric 
2175480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2176fe6060f1SDimitry Andric                                            RTLIB::Libcall LC,
2177480093f4SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2178fe6060f1SDimitry Andric   if (LC == RTLIB::UNKNOWN_LIBCALL)
2179fe6060f1SDimitry Andric     llvm_unreachable("Can't create an unknown libcall!");
2180480093f4SDimitry Andric 
2181480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2182480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
2183e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops(drop_begin(Node->ops()));
2184480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
2185480093f4SDimitry Andric     // FIXME: This doesn't support tail calls.
2186480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
2187480093f4SDimitry Andric                                                       Ops, CallOptions,
2188480093f4SDimitry Andric                                                       SDLoc(Node),
2189480093f4SDimitry Andric                                                       Node->getOperand(0));
2190480093f4SDimitry Andric     Results.push_back(Tmp.first);
2191480093f4SDimitry Andric     Results.push_back(Tmp.second);
2192480093f4SDimitry Andric   } else {
219306c3fb27SDimitry Andric     SDValue Tmp = ExpandLibCall(LC, Node, false).first;
2194480093f4SDimitry Andric     Results.push_back(Tmp);
2195480093f4SDimitry Andric   }
21960b57cec5SDimitry Andric }
21970b57cec5SDimitry Andric 
2198fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type.
2199fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node,
2200fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F32,
2201fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F64,
2202fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F80,
2203fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_F128,
2204fe6060f1SDimitry Andric                                            RTLIB::Libcall Call_PPCF128,
2205fe6060f1SDimitry Andric                                            SmallVectorImpl<SDValue> &Results) {
2206fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0),
2207fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2208fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2209fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
2210fe6060f1SDimitry Andric }
2211fe6060f1SDimitry Andric 
2212bdd1243dSDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned,
2213bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I8,
2214bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I16,
2215bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I32,
2216bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I64,
2217bdd1243dSDimitry Andric                                                RTLIB::Libcall Call_I128) {
22180b57cec5SDimitry Andric   RTLIB::Libcall LC;
22190b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2220bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22210b57cec5SDimitry Andric   case MVT::i8:   LC = Call_I8; break;
22220b57cec5SDimitry Andric   case MVT::i16:  LC = Call_I16; break;
22230b57cec5SDimitry Andric   case MVT::i32:  LC = Call_I32; break;
22240b57cec5SDimitry Andric   case MVT::i64:  LC = Call_I64; break;
22250b57cec5SDimitry Andric   case MVT::i128: LC = Call_I128; break;
22260b57cec5SDimitry Andric   }
222706c3fb27SDimitry Andric   return ExpandLibCall(LC, Node, isSigned).first;
22280b57cec5SDimitry Andric }
22290b57cec5SDimitry Andric 
22300b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance
22310b57cec5SDimitry Andric /// lround and its variant).
2232480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node,
22330b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F32,
22340b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F64,
22350b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F80,
22360b57cec5SDimitry Andric                                             RTLIB::Libcall Call_F128,
2237480093f4SDimitry Andric                                             RTLIB::Libcall Call_PPCF128,
2238480093f4SDimitry Andric                                             SmallVectorImpl<SDValue> &Results) {
2239480093f4SDimitry Andric   EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType();
2240fe6060f1SDimitry Andric   RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(),
2241fe6060f1SDimitry Andric                                           Call_F32, Call_F64, Call_F80,
2242fe6060f1SDimitry Andric                                           Call_F128, Call_PPCF128);
2243fe6060f1SDimitry Andric   ExpandFPLibCall(Node, LC, Results);
22440b57cec5SDimitry Andric }
22450b57cec5SDimitry Andric 
22460b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs.
22470b57cec5SDimitry Andric void
22480b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node,
22490b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
22500b57cec5SDimitry Andric   unsigned Opcode = Node->getOpcode();
22510b57cec5SDimitry Andric   bool isSigned = Opcode == ISD::SDIVREM;
22520b57cec5SDimitry Andric 
22530b57cec5SDimitry Andric   RTLIB::Libcall LC;
22540b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
2255bdd1243dSDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
22560b57cec5SDimitry Andric   case MVT::i8:   LC= isSigned ? RTLIB::SDIVREM_I8  : RTLIB::UDIVREM_I8;  break;
22570b57cec5SDimitry Andric   case MVT::i16:  LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break;
22580b57cec5SDimitry Andric   case MVT::i32:  LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break;
22590b57cec5SDimitry Andric   case MVT::i64:  LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break;
22600b57cec5SDimitry Andric   case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break;
22610b57cec5SDimitry Andric   }
22620b57cec5SDimitry Andric 
22630b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
22640b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
22650b57cec5SDimitry Andric   // dependence.
22660b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
22690b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
22700b57cec5SDimitry Andric 
22710b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
22720b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
22730b57cec5SDimitry Andric   for (const SDValue &Op : Node->op_values()) {
22740b57cec5SDimitry Andric     EVT ArgVT = Op.getValueType();
22750b57cec5SDimitry Andric     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
22760b57cec5SDimitry Andric     Entry.Node = Op;
22770b57cec5SDimitry Andric     Entry.Ty = ArgTy;
22780b57cec5SDimitry Andric     Entry.IsSExt = isSigned;
22790b57cec5SDimitry Andric     Entry.IsZExt = !isSigned;
22800b57cec5SDimitry Andric     Args.push_back(Entry);
22810b57cec5SDimitry Andric   }
22820b57cec5SDimitry Andric 
22830b57cec5SDimitry Andric   // Also pass the return address of the remainder.
22840b57cec5SDimitry Andric   SDValue FIPtr = DAG.CreateStackTemporary(RetVT);
22850b57cec5SDimitry Andric   Entry.Node = FIPtr;
22865f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
22870b57cec5SDimitry Andric   Entry.IsSExt = isSigned;
22880b57cec5SDimitry Andric   Entry.IsZExt = !isSigned;
22890b57cec5SDimitry Andric   Args.push_back(Entry);
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
22920b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric   SDLoc dl(Node);
22950b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
22960b57cec5SDimitry Andric   CLI.setDebugLoc(dl)
22970b57cec5SDimitry Andric       .setChain(InChain)
22980b57cec5SDimitry Andric       .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee,
22990b57cec5SDimitry Andric                     std::move(Args))
23000b57cec5SDimitry Andric       .setSExtResult(isSigned)
23010b57cec5SDimitry Andric       .setZExtResult(!isSigned);
23020b57cec5SDimitry Andric 
23030b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
23040b57cec5SDimitry Andric 
23050b57cec5SDimitry Andric   // Remainder is loaded back from the stack frame.
23060b57cec5SDimitry Andric   SDValue Rem =
23070b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo());
23080b57cec5SDimitry Andric   Results.push_back(CallInfo.first);
23090b57cec5SDimitry Andric   Results.push_back(Rem);
23100b57cec5SDimitry Andric }
23110b57cec5SDimitry Andric 
23120b57cec5SDimitry Andric /// Return true if sincos libcall is available.
23130b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
23140b57cec5SDimitry Andric   RTLIB::Libcall LC;
23150b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
23160b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
23170b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
23180b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
23190b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
23200b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
23210b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
23220b57cec5SDimitry Andric   }
23230b57cec5SDimitry Andric   return TLI.getLibcallName(LC) != nullptr;
23240b57cec5SDimitry Andric }
23250b57cec5SDimitry Andric 
23260b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed.
23270b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) {
23280b57cec5SDimitry Andric   unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
23290b57cec5SDimitry Andric     ? ISD::FCOS : ISD::FSIN;
23300b57cec5SDimitry Andric 
23310b57cec5SDimitry Andric   SDValue Op0 = Node->getOperand(0);
2332349cc55cSDimitry Andric   for (const SDNode *User : Op0.getNode()->uses()) {
23330b57cec5SDimitry Andric     if (User == Node)
23340b57cec5SDimitry Andric       continue;
23350b57cec5SDimitry Andric     // The other user might have been turned into sincos already.
23360b57cec5SDimitry Andric     if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS)
23370b57cec5SDimitry Andric       return true;
23380b57cec5SDimitry Andric   }
23390b57cec5SDimitry Andric   return false;
23400b57cec5SDimitry Andric }
23410b57cec5SDimitry Andric 
23420b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs.
23430b57cec5SDimitry Andric void
23440b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node,
23450b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results) {
23460b57cec5SDimitry Andric   RTLIB::Libcall LC;
23470b57cec5SDimitry Andric   switch (Node->getSimpleValueType(0).SimpleTy) {
23480b57cec5SDimitry Andric   default: llvm_unreachable("Unexpected request for libcall!");
23490b57cec5SDimitry Andric   case MVT::f32:     LC = RTLIB::SINCOS_F32; break;
23500b57cec5SDimitry Andric   case MVT::f64:     LC = RTLIB::SINCOS_F64; break;
23510b57cec5SDimitry Andric   case MVT::f80:     LC = RTLIB::SINCOS_F80; break;
23520b57cec5SDimitry Andric   case MVT::f128:    LC = RTLIB::SINCOS_F128; break;
23530b57cec5SDimitry Andric   case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break;
23540b57cec5SDimitry Andric   }
23550b57cec5SDimitry Andric 
23560b57cec5SDimitry Andric   // The input chain to this libcall is the entry node of the function.
23570b57cec5SDimitry Andric   // Legalizing the call will automatically add the previous call to the
23580b57cec5SDimitry Andric   // dependence.
23590b57cec5SDimitry Andric   SDValue InChain = DAG.getEntryNode();
23600b57cec5SDimitry Andric 
23610b57cec5SDimitry Andric   EVT RetVT = Node->getValueType(0);
23620b57cec5SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
23630b57cec5SDimitry Andric 
23640b57cec5SDimitry Andric   TargetLowering::ArgListTy Args;
23650b57cec5SDimitry Andric   TargetLowering::ArgListEntry Entry;
23660b57cec5SDimitry Andric 
23670b57cec5SDimitry Andric   // Pass the argument.
23680b57cec5SDimitry Andric   Entry.Node = Node->getOperand(0);
23690b57cec5SDimitry Andric   Entry.Ty = RetTy;
23700b57cec5SDimitry Andric   Entry.IsSExt = false;
23710b57cec5SDimitry Andric   Entry.IsZExt = false;
23720b57cec5SDimitry Andric   Args.push_back(Entry);
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric   // Pass the return address of sin.
23750b57cec5SDimitry Andric   SDValue SinPtr = DAG.CreateStackTemporary(RetVT);
23760b57cec5SDimitry Andric   Entry.Node = SinPtr;
23775f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
23780b57cec5SDimitry Andric   Entry.IsSExt = false;
23790b57cec5SDimitry Andric   Entry.IsZExt = false;
23800b57cec5SDimitry Andric   Args.push_back(Entry);
23810b57cec5SDimitry Andric 
23820b57cec5SDimitry Andric   // Also pass the return address of the cos.
23830b57cec5SDimitry Andric   SDValue CosPtr = DAG.CreateStackTemporary(RetVT);
23840b57cec5SDimitry Andric   Entry.Node = CosPtr;
23855f757f3fSDimitry Andric   Entry.Ty = PointerType::getUnqual(RetTy->getContext());
23860b57cec5SDimitry Andric   Entry.IsSExt = false;
23870b57cec5SDimitry Andric   Entry.IsZExt = false;
23880b57cec5SDimitry Andric   Args.push_back(Entry);
23890b57cec5SDimitry Andric 
23900b57cec5SDimitry Andric   SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
23910b57cec5SDimitry Andric                                          TLI.getPointerTy(DAG.getDataLayout()));
23920b57cec5SDimitry Andric 
23930b57cec5SDimitry Andric   SDLoc dl(Node);
23940b57cec5SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
23950b57cec5SDimitry Andric   CLI.setDebugLoc(dl).setChain(InChain).setLibCallee(
23960b57cec5SDimitry Andric       TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee,
23970b57cec5SDimitry Andric       std::move(Args));
23980b57cec5SDimitry Andric 
23990b57cec5SDimitry Andric   std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI);
24000b57cec5SDimitry Andric 
24010b57cec5SDimitry Andric   Results.push_back(
24020b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo()));
24030b57cec5SDimitry Andric   Results.push_back(
24040b57cec5SDimitry Andric       DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo()));
24050b57cec5SDimitry Andric }
24060b57cec5SDimitry Andric 
240706c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const {
240806c3fb27SDimitry Andric   SDLoc dl(Node);
240906c3fb27SDimitry Andric   EVT VT = Node->getValueType(0);
241006c3fb27SDimitry Andric   SDValue X = Node->getOperand(0);
241106c3fb27SDimitry Andric   SDValue N = Node->getOperand(1);
241206c3fb27SDimitry Andric   EVT ExpVT = N.getValueType();
241306c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
241406c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
241506c3fb27SDimitry Andric     return SDValue();
241606c3fb27SDimitry Andric 
241706c3fb27SDimitry Andric   if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO
241806c3fb27SDimitry Andric     return SDValue();
241906c3fb27SDimitry Andric 
242006c3fb27SDimitry Andric   SDNodeFlags NSW;
242106c3fb27SDimitry Andric   NSW.setNoSignedWrap(true);
242206c3fb27SDimitry Andric   SDNodeFlags NUW_NSW;
242306c3fb27SDimitry Andric   NUW_NSW.setNoUnsignedWrap(true);
242406c3fb27SDimitry Andric   NUW_NSW.setNoSignedWrap(true);
242506c3fb27SDimitry Andric 
242606c3fb27SDimitry Andric   EVT SetCCVT =
242706c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT);
242806c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
242906c3fb27SDimitry Andric 
243006c3fb27SDimitry Andric   const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem);
243106c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
243206c3fb27SDimitry Andric   const int Precision = APFloat::semanticsPrecision(FltSem);
243306c3fb27SDimitry Andric 
243406c3fb27SDimitry Andric   const SDValue MaxExp = DAG.getConstant(MaxExpVal, dl, ExpVT);
243506c3fb27SDimitry Andric   const SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
243606c3fb27SDimitry Andric 
243706c3fb27SDimitry Andric   const SDValue DoubleMaxExp = DAG.getConstant(2 * MaxExpVal, dl, ExpVT);
243806c3fb27SDimitry Andric 
243906c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
244006c3fb27SDimitry Andric   APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven);
244106c3fb27SDimitry Andric 
244206c3fb27SDimitry Andric   // Offset by precision to avoid denormal range.
244306c3fb27SDimitry Andric   APFloat ScaleDownK =
244406c3fb27SDimitry Andric       scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven);
244506c3fb27SDimitry Andric 
244606c3fb27SDimitry Andric   // TODO: Should really introduce control flow and use a block for the >
244706c3fb27SDimitry Andric   // MaxExp, < MinExp cases
244806c3fb27SDimitry Andric 
244906c3fb27SDimitry Andric   // First, handle exponents Exp > MaxExp and scale down.
245006c3fb27SDimitry Andric   SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT);
245106c3fb27SDimitry Andric 
245206c3fb27SDimitry Andric   SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW);
245306c3fb27SDimitry Andric   SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT);
245406c3fb27SDimitry Andric   SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal);
245506c3fb27SDimitry Andric   SDValue DecN1 =
245606c3fb27SDimitry Andric       DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW);
245706c3fb27SDimitry Andric 
245806c3fb27SDimitry Andric   SDValue ScaleUpTwice =
245906c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT);
246006c3fb27SDimitry Andric 
246106c3fb27SDimitry Andric   const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT);
246206c3fb27SDimitry Andric   SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal);
246306c3fb27SDimitry Andric   SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal);
246406c3fb27SDimitry Andric 
246506c3fb27SDimitry Andric   SDValue SelectN_Big =
246606c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0);
246706c3fb27SDimitry Andric   SDValue SelectX_Big =
246806c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0);
246906c3fb27SDimitry Andric 
247006c3fb27SDimitry Andric   // Now handle exponents Exp < MinExp
247106c3fb27SDimitry Andric   SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT);
247206c3fb27SDimitry Andric 
247306c3fb27SDimitry Andric   SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT);
247406c3fb27SDimitry Andric   SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT);
247506c3fb27SDimitry Andric 
247606c3fb27SDimitry Andric   SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW);
247706c3fb27SDimitry Andric 
247806c3fb27SDimitry Andric   SDValue ClampMinVal =
247906c3fb27SDimitry Andric       DAG.getConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT);
248006c3fb27SDimitry Andric   SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal);
248106c3fb27SDimitry Andric   SDValue IncN1 =
248206c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW);
248306c3fb27SDimitry Andric 
248406c3fb27SDimitry Andric   const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT);
248506c3fb27SDimitry Andric   SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal);
248606c3fb27SDimitry Andric   SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal);
248706c3fb27SDimitry Andric 
248806c3fb27SDimitry Andric   SDValue ScaleDownTwice = DAG.getSetCC(
248906c3fb27SDimitry Andric       dl, SetCCVT, N, DAG.getConstant(2 * MinExpVal + Precision, dl, ExpVT),
249006c3fb27SDimitry Andric       ISD::SETULT);
249106c3fb27SDimitry Andric 
249206c3fb27SDimitry Andric   SDValue SelectN_Small =
249306c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0);
249406c3fb27SDimitry Andric   SDValue SelectX_Small =
249506c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0);
249606c3fb27SDimitry Andric 
249706c3fb27SDimitry Andric   // Now combine the two out of range exponent handling cases with the base
249806c3fb27SDimitry Andric   // case.
249906c3fb27SDimitry Andric   SDValue NewX = DAG.getNode(
250006c3fb27SDimitry Andric       ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big,
250106c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X));
250206c3fb27SDimitry Andric 
250306c3fb27SDimitry Andric   SDValue NewN = DAG.getNode(
250406c3fb27SDimitry Andric       ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big,
250506c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N));
250606c3fb27SDimitry Andric 
250706c3fb27SDimitry Andric   SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW);
250806c3fb27SDimitry Andric 
250906c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
251006c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl);
251106c3fb27SDimitry Andric   SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT);
251206c3fb27SDimitry Andric 
251306c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy,
251406c3fb27SDimitry Andric                               ExponentShiftAmt, NUW_NSW);
251506c3fb27SDimitry Andric   SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt);
251606c3fb27SDimitry Andric   return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP);
251706c3fb27SDimitry Andric }
251806c3fb27SDimitry Andric 
251906c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const {
252006c3fb27SDimitry Andric   SDLoc dl(Node);
252106c3fb27SDimitry Andric   SDValue Val = Node->getOperand(0);
252206c3fb27SDimitry Andric   EVT VT = Val.getValueType();
252306c3fb27SDimitry Andric   EVT ExpVT = Node->getValueType(1);
252406c3fb27SDimitry Andric   EVT AsIntVT = VT.changeTypeToInteger();
252506c3fb27SDimitry Andric   if (AsIntVT == EVT()) // TODO: How to handle f80?
252606c3fb27SDimitry Andric     return SDValue();
252706c3fb27SDimitry Andric 
252806c3fb27SDimitry Andric   const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT);
252906c3fb27SDimitry Andric   const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem);
253006c3fb27SDimitry Andric   const unsigned Precision = APFloat::semanticsPrecision(FltSem);
253106c3fb27SDimitry Andric   const unsigned BitSize = VT.getScalarSizeInBits();
253206c3fb27SDimitry Andric 
253306c3fb27SDimitry Andric   // TODO: Could introduce control flow and skip over the denormal handling.
253406c3fb27SDimitry Andric 
253506c3fb27SDimitry Andric   // scale_up = fmul value, scalbn(1.0, precision + 1)
253606c3fb27SDimitry Andric   // extracted_exp = (bitcast value to uint) >> precision - 1
253706c3fb27SDimitry Andric   // biased_exp = extracted_exp + min_exp
253806c3fb27SDimitry Andric   // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask)
253906c3fb27SDimitry Andric   //
254006c3fb27SDimitry Andric   // is_denormal = val < smallest_normalized
254106c3fb27SDimitry Andric   // computed_fract = is_denormal ? scale_up : extracted_fract
254206c3fb27SDimitry Andric   // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp
254306c3fb27SDimitry Andric   //
254406c3fb27SDimitry Andric   // result_0 =  (!isfinite(val) || iszero(val)) ? val : computed_fract
254506c3fb27SDimitry Andric   // result_1 =  (!isfinite(val) || iszero(val)) ? 0 : computed_exp
254606c3fb27SDimitry Andric 
254706c3fb27SDimitry Andric   SDValue NegSmallestNormalizedInt = DAG.getConstant(
254806c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl,
254906c3fb27SDimitry Andric       AsIntVT);
255006c3fb27SDimitry Andric 
255106c3fb27SDimitry Andric   SDValue SmallestNormalizedInt = DAG.getConstant(
255206c3fb27SDimitry Andric       APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl,
255306c3fb27SDimitry Andric       AsIntVT);
255406c3fb27SDimitry Andric 
255506c3fb27SDimitry Andric   // Masks out the exponent bits.
255606c3fb27SDimitry Andric   SDValue ExpMask =
255706c3fb27SDimitry Andric       DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT);
255806c3fb27SDimitry Andric 
255906c3fb27SDimitry Andric   // Mask out the exponent part of the value.
256006c3fb27SDimitry Andric   //
256106c3fb27SDimitry Andric   // e.g, for f32 FractSignMaskVal = 0x807fffff
256206c3fb27SDimitry Andric   APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1);
256306c3fb27SDimitry Andric   FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit
256406c3fb27SDimitry Andric 
256506c3fb27SDimitry Andric   APInt SignMaskVal = APInt::getSignedMaxValue(BitSize);
256606c3fb27SDimitry Andric   SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT);
256706c3fb27SDimitry Andric 
256806c3fb27SDimitry Andric   SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT);
256906c3fb27SDimitry Andric 
257006c3fb27SDimitry Andric   const APFloat One(FltSem, "1.0");
257106c3fb27SDimitry Andric   // Scale a possible denormal input.
257206c3fb27SDimitry Andric   // e.g., for f64, 0x1p+54
257306c3fb27SDimitry Andric   APFloat ScaleUpKVal =
257406c3fb27SDimitry Andric       scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven);
257506c3fb27SDimitry Andric 
257606c3fb27SDimitry Andric   SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT);
257706c3fb27SDimitry Andric   SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK);
257806c3fb27SDimitry Andric 
257906c3fb27SDimitry Andric   EVT SetCCVT =
258006c3fb27SDimitry Andric       TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
258106c3fb27SDimitry Andric 
258206c3fb27SDimitry Andric   SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val);
258306c3fb27SDimitry Andric 
258406c3fb27SDimitry Andric   SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask);
258506c3fb27SDimitry Andric 
258606c3fb27SDimitry Andric   SDValue AddNegSmallestNormal =
258706c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt);
258806c3fb27SDimitry Andric   SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal,
258906c3fb27SDimitry Andric                                       NegSmallestNormalizedInt, ISD::SETULE);
259006c3fb27SDimitry Andric 
259106c3fb27SDimitry Andric   SDValue IsDenormal =
259206c3fb27SDimitry Andric       DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT);
259306c3fb27SDimitry Andric 
259406c3fb27SDimitry Andric   SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT);
259506c3fb27SDimitry Andric   SDValue Zero = DAG.getConstant(0, dl, ExpVT);
259606c3fb27SDimitry Andric 
259706c3fb27SDimitry Andric   SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp);
259806c3fb27SDimitry Andric   SDValue ScaledSelect =
259906c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt);
260006c3fb27SDimitry Andric 
260106c3fb27SDimitry Andric   SDValue ExpMaskScaled =
260206c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask);
260306c3fb27SDimitry Andric 
260406c3fb27SDimitry Andric   SDValue ScaledValue =
260506c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs);
260606c3fb27SDimitry Andric 
260706c3fb27SDimitry Andric   // Extract the exponent bits.
260806c3fb27SDimitry Andric   SDValue ExponentShiftAmt =
260906c3fb27SDimitry Andric       DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl);
261006c3fb27SDimitry Andric   SDValue ShiftedExp =
261106c3fb27SDimitry Andric       DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt);
261206c3fb27SDimitry Andric   SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT);
261306c3fb27SDimitry Andric 
261406c3fb27SDimitry Andric   SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp);
261506c3fb27SDimitry Andric   SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT);
261606c3fb27SDimitry Andric   SDValue DenormalExpBias =
261706c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero);
261806c3fb27SDimitry Andric 
261906c3fb27SDimitry Andric   SDValue MaskedFractAsInt =
262006c3fb27SDimitry Andric       DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask);
262106c3fb27SDimitry Andric   const APFloat Half(FltSem, "0.5");
262206c3fb27SDimitry Andric   SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT);
262306c3fb27SDimitry Andric   SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf);
262406c3fb27SDimitry Andric   SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or);
262506c3fb27SDimitry Andric 
262606c3fb27SDimitry Andric   SDValue ComputedExp =
262706c3fb27SDimitry Andric       DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias);
262806c3fb27SDimitry Andric 
262906c3fb27SDimitry Andric   SDValue Result0 =
263006c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract);
263106c3fb27SDimitry Andric 
263206c3fb27SDimitry Andric   SDValue Result1 =
263306c3fb27SDimitry Andric       DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp);
263406c3fb27SDimitry Andric 
263506c3fb27SDimitry Andric   return DAG.getMergeValues({Result0, Result1}, dl);
263606c3fb27SDimitry Andric }
263706c3fb27SDimitry Andric 
26380b57cec5SDimitry Andric /// This function is responsible for legalizing a
26390b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that
26400b57cec5SDimitry Andric /// we expand it.  At this point, we know that the result and operand types are
26410b57cec5SDimitry Andric /// legal for the target.
2642480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node,
2643480093f4SDimitry Andric                                                    SDValue &Chain) {
2644480093f4SDimitry Andric   bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
2645480093f4SDimitry Andric                    Node->getOpcode() == ISD::SINT_TO_FP);
2646480093f4SDimitry Andric   EVT DestVT = Node->getValueType(0);
2647480093f4SDimitry Andric   SDLoc dl(Node);
2648480093f4SDimitry Andric   unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0;
2649480093f4SDimitry Andric   SDValue Op0 = Node->getOperand(OpNo);
26500b57cec5SDimitry Andric   EVT SrcVT = Op0.getValueType();
26510b57cec5SDimitry Andric 
26520b57cec5SDimitry Andric   // TODO: Should any fast-math-flags be set for the created nodes?
26530b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n");
2654e8d8bef9SDimitry Andric   if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) &&
2655e8d8bef9SDimitry Andric       (DestVT.bitsLE(MVT::f64) ||
2656e8d8bef9SDimitry Andric        TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND
2657e8d8bef9SDimitry Andric                                                      : ISD::FP_EXTEND,
2658e8d8bef9SDimitry Andric                             DestVT))) {
26590b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double "
26600b57cec5SDimitry Andric                          "expansion\n");
26610b57cec5SDimitry Andric 
26620b57cec5SDimitry Andric     // Get the stack frame index of a 8 byte buffer.
26630b57cec5SDimitry Andric     SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
26640b57cec5SDimitry Andric 
26655ffd83dbSDimitry Andric     SDValue Lo = Op0;
26660b57cec5SDimitry Andric     // if signed map to unsigned space
26670b57cec5SDimitry Andric     if (isSigned) {
26685ffd83dbSDimitry Andric       // Invert sign bit (signed to unsigned mapping).
26695ffd83dbSDimitry Andric       Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo,
26705ffd83dbSDimitry Andric                        DAG.getConstant(0x80000000u, dl, MVT::i32));
26710b57cec5SDimitry Andric     }
26725ffd83dbSDimitry Andric     // Initial hi portion of constructed double.
26735ffd83dbSDimitry Andric     SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32);
26745ffd83dbSDimitry Andric 
26755ffd83dbSDimitry Andric     // If this a big endian target, swap the lo and high data.
26765ffd83dbSDimitry Andric     if (DAG.getDataLayout().isBigEndian())
26775ffd83dbSDimitry Andric       std::swap(Lo, Hi);
26785ffd83dbSDimitry Andric 
26795ffd83dbSDimitry Andric     SDValue MemChain = DAG.getEntryNode();
26805ffd83dbSDimitry Andric 
26815ffd83dbSDimitry Andric     // Store the lo of the constructed double.
26825ffd83dbSDimitry Andric     SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot,
26830b57cec5SDimitry Andric                                   MachinePointerInfo());
26845ffd83dbSDimitry Andric     // Store the hi of the constructed double.
26855f757f3fSDimitry Andric     SDValue HiPtr =
26865f757f3fSDimitry Andric         DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl);
26870b57cec5SDimitry Andric     SDValue Store2 =
26885ffd83dbSDimitry Andric         DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo());
26895ffd83dbSDimitry Andric     MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2);
26905ffd83dbSDimitry Andric 
26910b57cec5SDimitry Andric     // load the constructed double
26920b57cec5SDimitry Andric     SDValue Load =
26935ffd83dbSDimitry Andric         DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo());
26940b57cec5SDimitry Andric     // FP constant to bias correct the final result
269506c3fb27SDimitry Andric     SDValue Bias = DAG.getConstantFP(
269606c3fb27SDimitry Andric         isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL)
269706c3fb27SDimitry Andric                  : llvm::bit_cast<double>(0x4330000000000000ULL),
26980b57cec5SDimitry Andric         dl, MVT::f64);
2699480093f4SDimitry Andric     // Subtract the bias and get the final result.
2700480093f4SDimitry Andric     SDValue Sub;
2701480093f4SDimitry Andric     SDValue Result;
2702480093f4SDimitry Andric     if (Node->isStrictFPOpcode()) {
2703480093f4SDimitry Andric       Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other},
2704480093f4SDimitry Andric                         {Node->getOperand(0), Load, Bias});
2705480093f4SDimitry Andric       Chain = Sub.getValue(1);
2706480093f4SDimitry Andric       if (DestVT != Sub.getValueType()) {
2707480093f4SDimitry Andric         std::pair<SDValue, SDValue> ResultPair;
2708480093f4SDimitry Andric         ResultPair =
2709480093f4SDimitry Andric             DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT);
2710480093f4SDimitry Andric         Result = ResultPair.first;
2711480093f4SDimitry Andric         Chain = ResultPair.second;
2712480093f4SDimitry Andric       }
2713480093f4SDimitry Andric       else
2714480093f4SDimitry Andric         Result = Sub;
2715480093f4SDimitry Andric     } else {
2716480093f4SDimitry Andric       Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias);
2717480093f4SDimitry Andric       Result = DAG.getFPExtendOrRound(Sub, dl, DestVT);
2718480093f4SDimitry Andric     }
27190b57cec5SDimitry Andric     return Result;
27200b57cec5SDimitry Andric   }
2721e8d8bef9SDimitry Andric 
2722e8d8bef9SDimitry Andric   if (isSigned)
2723e8d8bef9SDimitry Andric     return SDValue();
27245ffd83dbSDimitry Andric 
27255ffd83dbSDimitry Andric   // TODO: Generalize this for use with other types.
2726e8d8bef9SDimitry Andric   if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) ||
2727e8d8bef9SDimitry Andric       (SrcVT == MVT::i64 && DestVT == MVT::f64)) {
2728e8d8bef9SDimitry Andric     LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n");
27295ffd83dbSDimitry Andric     // For unsigned conversions, convert them to signed conversions using the
27305ffd83dbSDimitry Andric     // algorithm from the x86_64 __floatundisf in compiler_rt. That method
27315ffd83dbSDimitry Andric     // should be valid for i32->f32 as well.
27325ffd83dbSDimitry Andric 
2733e8d8bef9SDimitry Andric     // More generally this transform should be valid if there are 3 more bits
2734e8d8bef9SDimitry Andric     // in the integer type than the significand. Rounding uses the first bit
2735e8d8bef9SDimitry Andric     // after the width of the significand and the OR of all bits after that. So
2736e8d8bef9SDimitry Andric     // we need to be able to OR the shifted out bit into one of the bits that
2737e8d8bef9SDimitry Andric     // participate in the OR.
2738e8d8bef9SDimitry Andric 
27395ffd83dbSDimitry Andric     // TODO: This really should be implemented using a branch rather than a
27405ffd83dbSDimitry Andric     // select.  We happen to get lucky and machinesink does the right
27415ffd83dbSDimitry Andric     // thing most of the time.  This would be a good candidate for a
27425ffd83dbSDimitry Andric     // pseudo-op, or, even better, for whole-function isel.
27435ffd83dbSDimitry Andric     EVT SetCCVT = getSetCCResultType(SrcVT);
27445ffd83dbSDimitry Andric 
27455ffd83dbSDimitry Andric     SDValue SignBitTest = DAG.getSetCC(
27465ffd83dbSDimitry Andric         dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
27475ffd83dbSDimitry Andric 
27485ffd83dbSDimitry Andric     EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout());
27495ffd83dbSDimitry Andric     SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT);
27505ffd83dbSDimitry Andric     SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst);
27515ffd83dbSDimitry Andric     SDValue AndConst = DAG.getConstant(1, dl, SrcVT);
27525ffd83dbSDimitry Andric     SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst);
27535ffd83dbSDimitry Andric     SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr);
27545ffd83dbSDimitry Andric 
27555ffd83dbSDimitry Andric     SDValue Slow, Fast;
27565ffd83dbSDimitry Andric     if (Node->isStrictFPOpcode()) {
27575ffd83dbSDimitry Andric       // In strict mode, we must avoid spurious exceptions, and therefore
27585ffd83dbSDimitry Andric       // must make sure to only emit a single STRICT_SINT_TO_FP.
27595ffd83dbSDimitry Andric       SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0);
27605ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
27615ffd83dbSDimitry Andric                          { Node->getOperand(0), InCvt });
27625ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
27635ffd83dbSDimitry Andric                          { Fast.getValue(1), Fast, Fast });
27645ffd83dbSDimitry Andric       Chain = Slow.getValue(1);
27655ffd83dbSDimitry Andric       // The STRICT_SINT_TO_FP inherits the exception mode from the
27665ffd83dbSDimitry Andric       // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can
27675ffd83dbSDimitry Andric       // never raise any exception.
27685ffd83dbSDimitry Andric       SDNodeFlags Flags;
27695ffd83dbSDimitry Andric       Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept());
27705ffd83dbSDimitry Andric       Fast->setFlags(Flags);
27715ffd83dbSDimitry Andric       Flags.setNoFPExcept(true);
27725ffd83dbSDimitry Andric       Slow->setFlags(Flags);
27735ffd83dbSDimitry Andric     } else {
27745ffd83dbSDimitry Andric       SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or);
27755ffd83dbSDimitry Andric       Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt);
27765ffd83dbSDimitry Andric       Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
27775ffd83dbSDimitry Andric     }
27785ffd83dbSDimitry Andric 
27795ffd83dbSDimitry Andric     return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast);
27805ffd83dbSDimitry Andric   }
27815ffd83dbSDimitry Andric 
2782e8d8bef9SDimitry Andric   // Don't expand it if there isn't cheap fadd.
2783e8d8bef9SDimitry Andric   if (!TLI.isOperationLegalOrCustom(
2784e8d8bef9SDimitry Andric           Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT))
2785e8d8bef9SDimitry Andric     return SDValue();
2786e8d8bef9SDimitry Andric 
27875ffd83dbSDimitry Andric   // The following optimization is valid only if every value in SrcVT (when
27885ffd83dbSDimitry Andric   // treated as signed) is representable in DestVT.  Check that the mantissa
27895ffd83dbSDimitry Andric   // size of DestVT is >= than the number of bits in SrcVT -1.
27905ffd83dbSDimitry Andric   assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >=
27915ffd83dbSDimitry Andric              SrcVT.getSizeInBits() - 1 &&
27925ffd83dbSDimitry Andric          "Cannot perform lossless SINT_TO_FP!");
27930b57cec5SDimitry Andric 
2794480093f4SDimitry Andric   SDValue Tmp1;
2795480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2796480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other },
2797480093f4SDimitry Andric                        { Node->getOperand(0), Op0 });
2798480093f4SDimitry Andric   } else
2799480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0);
28000b57cec5SDimitry Andric 
28010b57cec5SDimitry Andric   SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0,
28020b57cec5SDimitry Andric                                  DAG.getConstant(0, dl, SrcVT), ISD::SETLT);
28030b57cec5SDimitry Andric   SDValue Zero = DAG.getIntPtrConstant(0, dl),
28040b57cec5SDimitry Andric           Four = DAG.getIntPtrConstant(4, dl);
28050b57cec5SDimitry Andric   SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(),
28060b57cec5SDimitry Andric                                     SignSet, Four, Zero);
28070b57cec5SDimitry Andric 
28080b57cec5SDimitry Andric   // If the sign bit of the integer is set, the large number will be treated
28090b57cec5SDimitry Andric   // as a negative number.  To counteract this, the dynamic code adds an
28100b57cec5SDimitry Andric   // offset depending on the data type.
28110b57cec5SDimitry Andric   uint64_t FF;
28120b57cec5SDimitry Andric   switch (SrcVT.getSimpleVT().SimpleTy) {
2813e8d8bef9SDimitry Andric   default:
2814e8d8bef9SDimitry Andric     return SDValue();
28150b57cec5SDimitry Andric   case MVT::i8 : FF = 0x43800000ULL; break;  // 2^8  (as a float)
28160b57cec5SDimitry Andric   case MVT::i16: FF = 0x47800000ULL; break;  // 2^16 (as a float)
28170b57cec5SDimitry Andric   case MVT::i32: FF = 0x4F800000ULL; break;  // 2^32 (as a float)
28180b57cec5SDimitry Andric   case MVT::i64: FF = 0x5F800000ULL; break;  // 2^64 (as a float)
28190b57cec5SDimitry Andric   }
28200b57cec5SDimitry Andric   if (DAG.getDataLayout().isLittleEndian())
28210b57cec5SDimitry Andric     FF <<= 32;
28220b57cec5SDimitry Andric   Constant *FudgeFactor = ConstantInt::get(
28230b57cec5SDimitry Andric                                        Type::getInt64Ty(*DAG.getContext()), FF);
28240b57cec5SDimitry Andric 
28250b57cec5SDimitry Andric   SDValue CPIdx =
28260b57cec5SDimitry Andric       DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout()));
28275ffd83dbSDimitry Andric   Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign();
28280b57cec5SDimitry Andric   CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset);
28295ffd83dbSDimitry Andric   Alignment = commonAlignment(Alignment, 4);
28300b57cec5SDimitry Andric   SDValue FudgeInReg;
28310b57cec5SDimitry Andric   if (DestVT == MVT::f32)
28320b57cec5SDimitry Andric     FudgeInReg = DAG.getLoad(
28330b57cec5SDimitry Andric         MVT::f32, dl, DAG.getEntryNode(), CPIdx,
28340b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
28350b57cec5SDimitry Andric         Alignment);
28360b57cec5SDimitry Andric   else {
28370b57cec5SDimitry Andric     SDValue Load = DAG.getExtLoad(
28380b57cec5SDimitry Andric         ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx,
28390b57cec5SDimitry Andric         MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
28400b57cec5SDimitry Andric         Alignment);
28410b57cec5SDimitry Andric     HandleSDNode Handle(Load);
28420b57cec5SDimitry Andric     LegalizeOp(Load.getNode());
28430b57cec5SDimitry Andric     FudgeInReg = Handle.getValue();
28440b57cec5SDimitry Andric   }
28450b57cec5SDimitry Andric 
2846480093f4SDimitry Andric   if (Node->isStrictFPOpcode()) {
2847480093f4SDimitry Andric     SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other },
2848480093f4SDimitry Andric                                  { Tmp1.getValue(1), Tmp1, FudgeInReg });
2849480093f4SDimitry Andric     Chain = Result.getValue(1);
2850480093f4SDimitry Andric     return Result;
2851480093f4SDimitry Andric   }
2852480093f4SDimitry Andric 
28530b57cec5SDimitry Andric   return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg);
28540b57cec5SDimitry Andric }
28550b57cec5SDimitry Andric 
28560b57cec5SDimitry Andric /// This function is responsible for legalizing a
28570b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that
28580b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
28590b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
28600b57cec5SDimitry Andric /// operation that takes a larger input.
2861480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP(
2862480093f4SDimitry Andric     SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) {
2863480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2864480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP ||
2865480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_SINT_TO_FP;
2866480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2867480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
2868480093f4SDimitry Andric   unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP;
2869480093f4SDimitry Andric   unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP;
2870480093f4SDimitry Andric 
28710b57cec5SDimitry Andric   // First step, figure out the appropriate *INT_TO_FP operation to use.
28720b57cec5SDimitry Andric   EVT NewInTy = LegalOp.getValueType();
28730b57cec5SDimitry Andric 
28740b57cec5SDimitry Andric   unsigned OpToUse = 0;
28750b57cec5SDimitry Andric 
28760b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
28770b57cec5SDimitry Andric   while (true) {
28780b57cec5SDimitry Andric     NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1);
28790b57cec5SDimitry Andric     assert(NewInTy.isInteger() && "Ran out of possibilities!");
28800b57cec5SDimitry Andric 
28810b57cec5SDimitry Andric     // If the target supports SINT_TO_FP of this type, use it.
2882480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) {
2883480093f4SDimitry Andric       OpToUse = SIntOp;
28840b57cec5SDimitry Andric       break;
28850b57cec5SDimitry Andric     }
2886480093f4SDimitry Andric     if (IsSigned)
2887480093f4SDimitry Andric       continue;
28880b57cec5SDimitry Andric 
28890b57cec5SDimitry Andric     // If the target supports UINT_TO_FP of this type, use it.
2890480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) {
2891480093f4SDimitry Andric       OpToUse = UIntOp;
28920b57cec5SDimitry Andric       break;
28930b57cec5SDimitry Andric     }
28940b57cec5SDimitry Andric 
28950b57cec5SDimitry Andric     // Otherwise, try a larger type.
28960b57cec5SDimitry Andric   }
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   // Okay, we found the operation and type to use.  Zero extend our input to the
28990b57cec5SDimitry Andric   // desired type then run the operation on it.
2900480093f4SDimitry Andric   if (IsStrict) {
2901480093f4SDimitry Andric     SDValue Res =
2902480093f4SDimitry Andric         DAG.getNode(OpToUse, dl, {DestVT, MVT::Other},
2903480093f4SDimitry Andric                     {N->getOperand(0),
2904480093f4SDimitry Andric                      DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2905480093f4SDimitry Andric                                  dl, NewInTy, LegalOp)});
2906480093f4SDimitry Andric     Results.push_back(Res);
2907480093f4SDimitry Andric     Results.push_back(Res.getValue(1));
2908480093f4SDimitry Andric     return;
2909480093f4SDimitry Andric   }
2910480093f4SDimitry Andric 
2911480093f4SDimitry Andric   Results.push_back(
2912480093f4SDimitry Andric       DAG.getNode(OpToUse, dl, DestVT,
2913480093f4SDimitry Andric                   DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
2914480093f4SDimitry Andric                               dl, NewInTy, LegalOp)));
29150b57cec5SDimitry Andric }
29160b57cec5SDimitry Andric 
29170b57cec5SDimitry Andric /// This function is responsible for legalizing a
29180b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that
29190b57cec5SDimitry Andric /// we promote it.  At this point, we know that the result and operand types are
29200b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
29210b57cec5SDimitry Andric /// operation that returns a larger result.
2922480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl,
2923480093f4SDimitry Andric                                                  SmallVectorImpl<SDValue> &Results) {
2924480093f4SDimitry Andric   bool IsStrict = N->isStrictFPOpcode();
2925480093f4SDimitry Andric   bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT ||
2926480093f4SDimitry Andric                   N->getOpcode() == ISD::STRICT_FP_TO_SINT;
2927480093f4SDimitry Andric   EVT DestVT = N->getValueType(0);
2928480093f4SDimitry Andric   SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0);
29290b57cec5SDimitry Andric   // First step, figure out the appropriate FP_TO*INT operation to use.
29300b57cec5SDimitry Andric   EVT NewOutTy = DestVT;
29310b57cec5SDimitry Andric 
29320b57cec5SDimitry Andric   unsigned OpToUse = 0;
29330b57cec5SDimitry Andric 
29340b57cec5SDimitry Andric   // Scan for the appropriate larger type to use.
29350b57cec5SDimitry Andric   while (true) {
29360b57cec5SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1);
29370b57cec5SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
29380b57cec5SDimitry Andric 
29390b57cec5SDimitry Andric     // A larger signed type can hold all unsigned values of the requested type,
29400b57cec5SDimitry Andric     // so using FP_TO_SINT is valid
2941480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT;
2942480093f4SDimitry Andric     if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29430b57cec5SDimitry Andric       break;
29440b57cec5SDimitry Andric 
29450b57cec5SDimitry Andric     // However, if the value may be < 0.0, we *must* use some FP_TO_SINT.
2946480093f4SDimitry Andric     OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT;
2947480093f4SDimitry Andric     if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy))
29480b57cec5SDimitry Andric       break;
29490b57cec5SDimitry Andric 
29500b57cec5SDimitry Andric     // Otherwise, try a larger type.
29510b57cec5SDimitry Andric   }
29520b57cec5SDimitry Andric 
29530b57cec5SDimitry Andric   // Okay, we found the operation and type to use.
2954480093f4SDimitry Andric   SDValue Operation;
2955480093f4SDimitry Andric   if (IsStrict) {
2956480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other);
2957480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp);
2958480093f4SDimitry Andric   } else
2959480093f4SDimitry Andric     Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp);
29600b57cec5SDimitry Andric 
29610b57cec5SDimitry Andric   // Truncate the result of the extended FP_TO_*INT operation to the desired
29620b57cec5SDimitry Andric   // size.
2963480093f4SDimitry Andric   SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation);
2964480093f4SDimitry Andric   Results.push_back(Trunc);
2965480093f4SDimitry Andric   if (IsStrict)
2966480093f4SDimitry Andric     Results.push_back(Operation.getValue(1));
29670b57cec5SDimitry Andric }
29680b57cec5SDimitry Andric 
2969e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point
2970e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal
2971e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type.
2972e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node,
2973e8d8bef9SDimitry Andric                                                         const SDLoc &dl) {
2974e8d8bef9SDimitry Andric   unsigned Opcode = Node->getOpcode();
2975e8d8bef9SDimitry Andric 
2976e8d8bef9SDimitry Andric   // Scan for the appropriate larger type to use.
2977e8d8bef9SDimitry Andric   EVT NewOutTy = Node->getValueType(0);
2978e8d8bef9SDimitry Andric   while (true) {
2979e8d8bef9SDimitry Andric     NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1);
2980e8d8bef9SDimitry Andric     assert(NewOutTy.isInteger() && "Ran out of possibilities!");
2981e8d8bef9SDimitry Andric 
2982e8d8bef9SDimitry Andric     if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy))
2983e8d8bef9SDimitry Andric       break;
2984e8d8bef9SDimitry Andric   }
2985e8d8bef9SDimitry Andric 
2986e8d8bef9SDimitry Andric   // Saturation width is determined by second operand, so we don't have to
2987e8d8bef9SDimitry Andric   // perform any fixup and can directly truncate the result.
2988e8d8bef9SDimitry Andric   SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0),
2989e8d8bef9SDimitry Andric                                Node->getOperand(1));
2990e8d8bef9SDimitry Andric   return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result);
2991e8d8bef9SDimitry Andric }
2992e8d8bef9SDimitry Andric 
2993e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation.
2994e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) {
2995e8d8bef9SDimitry Andric   EVT VT = Op.getValueType();
2996e8d8bef9SDimitry Andric   EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
2997e8d8bef9SDimitry Andric   unsigned Sz = VT.getScalarSizeInBits();
2998e8d8bef9SDimitry Andric 
2999e8d8bef9SDimitry Andric   // If CTPOP is legal, use it. Otherwise use shifts and xor.
3000e8d8bef9SDimitry Andric   SDValue Result;
3001349cc55cSDimitry Andric   if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) {
3002e8d8bef9SDimitry Andric     Result = DAG.getNode(ISD::CTPOP, dl, VT, Op);
3003e8d8bef9SDimitry Andric   } else {
3004e8d8bef9SDimitry Andric     Result = Op;
3005e8d8bef9SDimitry Andric     for (unsigned i = Log2_32_Ceil(Sz); i != 0;) {
3006e8d8bef9SDimitry Andric       SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result,
3007e8d8bef9SDimitry Andric                                   DAG.getConstant(1ULL << (--i), dl, ShVT));
3008e8d8bef9SDimitry Andric       Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift);
3009e8d8bef9SDimitry Andric     }
3010e8d8bef9SDimitry Andric   }
3011e8d8bef9SDimitry Andric 
3012e8d8bef9SDimitry Andric   return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT));
3013e8d8bef9SDimitry Andric }
3014e8d8bef9SDimitry Andric 
3015*0fca6ea1SDimitry Andric SDValue SelectionDAGLegalize::PromoteReduction(SDNode *Node) {
3016*0fca6ea1SDimitry Andric   MVT VecVT = Node->getOperand(1).getSimpleValueType();
3017*0fca6ea1SDimitry Andric   MVT NewVecVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VecVT);
3018*0fca6ea1SDimitry Andric   MVT ScalarVT = Node->getSimpleValueType(0);
3019*0fca6ea1SDimitry Andric   MVT NewScalarVT = NewVecVT.getVectorElementType();
3020*0fca6ea1SDimitry Andric 
3021*0fca6ea1SDimitry Andric   SDLoc DL(Node);
3022*0fca6ea1SDimitry Andric   SmallVector<SDValue, 4> Operands(Node->getNumOperands());
3023*0fca6ea1SDimitry Andric 
3024*0fca6ea1SDimitry Andric   // promote the initial value.
3025*0fca6ea1SDimitry Andric   // FIXME: Support integer.
3026*0fca6ea1SDimitry Andric   assert(Node->getOperand(0).getValueType().isFloatingPoint() &&
3027*0fca6ea1SDimitry Andric          "Only FP promotion is supported");
3028*0fca6ea1SDimitry Andric   Operands[0] =
3029*0fca6ea1SDimitry Andric       DAG.getNode(ISD::FP_EXTEND, DL, NewScalarVT, Node->getOperand(0));
3030*0fca6ea1SDimitry Andric 
3031*0fca6ea1SDimitry Andric   for (unsigned j = 1; j != Node->getNumOperands(); ++j)
3032*0fca6ea1SDimitry Andric     if (Node->getOperand(j).getValueType().isVector() &&
3033*0fca6ea1SDimitry Andric         !(ISD::isVPOpcode(Node->getOpcode()) &&
3034*0fca6ea1SDimitry Andric           ISD::getVPMaskIdx(Node->getOpcode()) == j)) { // Skip mask operand.
3035*0fca6ea1SDimitry Andric       // promote the vector operand.
3036*0fca6ea1SDimitry Andric       // FIXME: Support integer.
3037*0fca6ea1SDimitry Andric       assert(Node->getOperand(j).getValueType().isFloatingPoint() &&
3038*0fca6ea1SDimitry Andric              "Only FP promotion is supported");
3039*0fca6ea1SDimitry Andric       Operands[j] =
3040*0fca6ea1SDimitry Andric           DAG.getNode(ISD::FP_EXTEND, DL, NewVecVT, Node->getOperand(j));
3041*0fca6ea1SDimitry Andric     } else {
3042*0fca6ea1SDimitry Andric       Operands[j] = Node->getOperand(j); // Skip VL operand.
3043*0fca6ea1SDimitry Andric     }
3044*0fca6ea1SDimitry Andric 
3045*0fca6ea1SDimitry Andric   SDValue Res = DAG.getNode(Node->getOpcode(), DL, NewScalarVT, Operands,
3046*0fca6ea1SDimitry Andric                             Node->getFlags());
3047*0fca6ea1SDimitry Andric 
3048*0fca6ea1SDimitry Andric   assert(ScalarVT.isFloatingPoint() && "Only FP promotion is supported");
3049*0fca6ea1SDimitry Andric   return DAG.getNode(ISD::FP_ROUND, DL, ScalarVT, Res,
3050*0fca6ea1SDimitry Andric                      DAG.getIntPtrConstant(0, DL, /*isTarget=*/true));
3051*0fca6ea1SDimitry Andric }
3052*0fca6ea1SDimitry Andric 
30530b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
30540b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to expand node\n");
30550b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
30560b57cec5SDimitry Andric   SDLoc dl(Node);
30570b57cec5SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
30580b57cec5SDimitry Andric   bool NeedInvert;
30590b57cec5SDimitry Andric   switch (Node->getOpcode()) {
30600b57cec5SDimitry Andric   case ISD::ABS:
3061349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandABS(Node, DAG)))
30620b57cec5SDimitry Andric       Results.push_back(Tmp1);
30630b57cec5SDimitry Andric     break;
306406c3fb27SDimitry Andric   case ISD::ABDS:
306506c3fb27SDimitry Andric   case ISD::ABDU:
306606c3fb27SDimitry Andric     if ((Tmp1 = TLI.expandABD(Node, DAG)))
306706c3fb27SDimitry Andric       Results.push_back(Tmp1);
306806c3fb27SDimitry Andric     break;
3069*0fca6ea1SDimitry Andric   case ISD::AVGCEILS:
3070*0fca6ea1SDimitry Andric   case ISD::AVGCEILU:
3071*0fca6ea1SDimitry Andric   case ISD::AVGFLOORS:
3072*0fca6ea1SDimitry Andric   case ISD::AVGFLOORU:
3073*0fca6ea1SDimitry Andric     if ((Tmp1 = TLI.expandAVG(Node, DAG)))
3074*0fca6ea1SDimitry Andric       Results.push_back(Tmp1);
3075*0fca6ea1SDimitry Andric     break;
30760b57cec5SDimitry Andric   case ISD::CTPOP:
3077349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTPOP(Node, DAG)))
30780b57cec5SDimitry Andric       Results.push_back(Tmp1);
30790b57cec5SDimitry Andric     break;
30800b57cec5SDimitry Andric   case ISD::CTLZ:
30810b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
3082349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTLZ(Node, DAG)))
30830b57cec5SDimitry Andric       Results.push_back(Tmp1);
30840b57cec5SDimitry Andric     break;
30850b57cec5SDimitry Andric   case ISD::CTTZ:
30860b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
3087349cc55cSDimitry Andric     if ((Tmp1 = TLI.expandCTTZ(Node, DAG)))
30880b57cec5SDimitry Andric       Results.push_back(Tmp1);
30890b57cec5SDimitry Andric     break;
30900b57cec5SDimitry Andric   case ISD::BITREVERSE:
3091fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG)))
3092fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30930b57cec5SDimitry Andric     break;
30940b57cec5SDimitry Andric   case ISD::BSWAP:
3095fe6060f1SDimitry Andric     if ((Tmp1 = TLI.expandBSWAP(Node, DAG)))
3096fe6060f1SDimitry Andric       Results.push_back(Tmp1);
30970b57cec5SDimitry Andric     break;
3098e8d8bef9SDimitry Andric   case ISD::PARITY:
3099e8d8bef9SDimitry Andric     Results.push_back(ExpandPARITY(Node->getOperand(0), dl));
3100e8d8bef9SDimitry Andric     break;
31010b57cec5SDimitry Andric   case ISD::FRAMEADDR:
31020b57cec5SDimitry Andric   case ISD::RETURNADDR:
31030b57cec5SDimitry Andric   case ISD::FRAME_TO_ARGS_OFFSET:
31040b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
31050b57cec5SDimitry Andric     break;
31060b57cec5SDimitry Andric   case ISD::EH_DWARF_CFA: {
31070b57cec5SDimitry Andric     SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl,
31080b57cec5SDimitry Andric                                         TLI.getPointerTy(DAG.getDataLayout()));
31090b57cec5SDimitry Andric     SDValue Offset = DAG.getNode(ISD::ADD, dl,
31100b57cec5SDimitry Andric                                  CfaArg.getValueType(),
31110b57cec5SDimitry Andric                                  DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
31120b57cec5SDimitry Andric                                              CfaArg.getValueType()),
31130b57cec5SDimitry Andric                                  CfaArg);
31140b57cec5SDimitry Andric     SDValue FA = DAG.getNode(
31150b57cec5SDimitry Andric         ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()),
31160b57cec5SDimitry Andric         DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout())));
31170b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(),
31180b57cec5SDimitry Andric                                   FA, Offset));
31190b57cec5SDimitry Andric     break;
31200b57cec5SDimitry Andric   }
3121bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
31220b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0)));
31235ffd83dbSDimitry Andric     Results.push_back(Node->getOperand(0));
31240b57cec5SDimitry Andric     break;
31250b57cec5SDimitry Andric   case ISD::EH_RETURN:
31260b57cec5SDimitry Andric   case ISD::EH_LABEL:
31270b57cec5SDimitry Andric   case ISD::PREFETCH:
31280b57cec5SDimitry Andric   case ISD::VAEND:
31290b57cec5SDimitry Andric   case ISD::EH_SJLJ_LONGJMP:
31300b57cec5SDimitry Andric     // If the target didn't expand these, there's nothing to do, so just
31310b57cec5SDimitry Andric     // preserve the chain and be done.
31320b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
31330b57cec5SDimitry Andric     break;
31340b57cec5SDimitry Andric   case ISD::READCYCLECOUNTER:
3135*0fca6ea1SDimitry Andric   case ISD::READSTEADYCOUNTER:
31360b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
31370b57cec5SDimitry Andric     // chain.
31380b57cec5SDimitry Andric     Results.append(Node->getNumValues() - 1,
31390b57cec5SDimitry Andric                    DAG.getConstant(0, dl, Node->getValueType(0)));
31400b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
31410b57cec5SDimitry Andric     break;
31420b57cec5SDimitry Andric   case ISD::EH_SJLJ_SETJMP:
31430b57cec5SDimitry Andric     // If the target didn't expand this, just return 'zero' and preserve the
31440b57cec5SDimitry Andric     // chain.
31450b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, MVT::i32));
31460b57cec5SDimitry Andric     Results.push_back(Node->getOperand(0));
31470b57cec5SDimitry Andric     break;
31480b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD: {
31490b57cec5SDimitry Andric     // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP.
31500b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0));
31510b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
31520b57cec5SDimitry Andric     SDValue Swap = DAG.getAtomicCmpSwap(
31530b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
31540b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Zero, Zero,
31550b57cec5SDimitry Andric         cast<AtomicSDNode>(Node)->getMemOperand());
31560b57cec5SDimitry Andric     Results.push_back(Swap.getValue(0));
31570b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
31580b57cec5SDimitry Andric     break;
31590b57cec5SDimitry Andric   }
31600b57cec5SDimitry Andric   case ISD::ATOMIC_STORE: {
31610b57cec5SDimitry Andric     // There is no libcall for atomic store; fake it with ATOMIC_SWAP.
31625f757f3fSDimitry Andric     SDValue Swap = DAG.getAtomic(
31635f757f3fSDimitry Andric         ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(),
31645f757f3fSDimitry Andric         Node->getOperand(0), Node->getOperand(2), Node->getOperand(1),
31650b57cec5SDimitry Andric         cast<AtomicSDNode>(Node)->getMemOperand());
31660b57cec5SDimitry Andric     Results.push_back(Swap.getValue(1));
31670b57cec5SDimitry Andric     break;
31680b57cec5SDimitry Andric   }
31690b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
31700b57cec5SDimitry Andric     // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and
31710b57cec5SDimitry Andric     // splits out the success value as a comparison. Expanding the resulting
31720b57cec5SDimitry Andric     // ATOMIC_CMP_SWAP will produce a libcall.
31730b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
31740b57cec5SDimitry Andric     SDValue Res = DAG.getAtomicCmpSwap(
31750b57cec5SDimitry Andric         ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs,
31760b57cec5SDimitry Andric         Node->getOperand(0), Node->getOperand(1), Node->getOperand(2),
31770b57cec5SDimitry Andric         Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand());
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric     SDValue ExtRes = Res;
31800b57cec5SDimitry Andric     SDValue LHS = Res;
31810b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
31820b57cec5SDimitry Andric 
31830b57cec5SDimitry Andric     EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT();
31840b57cec5SDimitry Andric     EVT OuterType = Node->getValueType(0);
31850b57cec5SDimitry Andric     switch (TLI.getExtendForAtomicOps()) {
31860b57cec5SDimitry Andric     case ISD::SIGN_EXTEND:
31870b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res,
31880b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31890b57cec5SDimitry Andric       RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType,
31900b57cec5SDimitry Andric                         Node->getOperand(2), DAG.getValueType(AtomicType));
31910b57cec5SDimitry Andric       ExtRes = LHS;
31920b57cec5SDimitry Andric       break;
31930b57cec5SDimitry Andric     case ISD::ZERO_EXTEND:
31940b57cec5SDimitry Andric       LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res,
31950b57cec5SDimitry Andric                         DAG.getValueType(AtomicType));
31960b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
31970b57cec5SDimitry Andric       ExtRes = LHS;
31980b57cec5SDimitry Andric       break;
31990b57cec5SDimitry Andric     case ISD::ANY_EXTEND:
32000b57cec5SDimitry Andric       LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType);
32010b57cec5SDimitry Andric       RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType);
32020b57cec5SDimitry Andric       break;
32030b57cec5SDimitry Andric     default:
32040b57cec5SDimitry Andric       llvm_unreachable("Invalid atomic op extension");
32050b57cec5SDimitry Andric     }
32060b57cec5SDimitry Andric 
32070b57cec5SDimitry Andric     SDValue Success =
32080b57cec5SDimitry Andric         DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ);
32090b57cec5SDimitry Andric 
32100b57cec5SDimitry Andric     Results.push_back(ExtRes.getValue(0));
32110b57cec5SDimitry Andric     Results.push_back(Success);
32120b57cec5SDimitry Andric     Results.push_back(Res.getValue(1));
32130b57cec5SDimitry Andric     break;
32140b57cec5SDimitry Andric   }
32155f757f3fSDimitry Andric   case ISD::ATOMIC_LOAD_SUB: {
32165f757f3fSDimitry Andric     SDLoc DL(Node);
32175f757f3fSDimitry Andric     EVT VT = Node->getValueType(0);
32185f757f3fSDimitry Andric     SDValue RHS = Node->getOperand(2);
32195f757f3fSDimitry Andric     AtomicSDNode *AN = cast<AtomicSDNode>(Node);
32205f757f3fSDimitry Andric     if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG &&
32215f757f3fSDimitry Andric         cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT())
32225f757f3fSDimitry Andric       RHS = RHS->getOperand(0);
32235f757f3fSDimitry Andric     SDValue NewRHS =
32245f757f3fSDimitry Andric         DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS);
32255f757f3fSDimitry Andric     SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(),
32265f757f3fSDimitry Andric                                 Node->getOperand(0), Node->getOperand(1),
32275f757f3fSDimitry Andric                                 NewRHS, AN->getMemOperand());
32285f757f3fSDimitry Andric     Results.push_back(Res);
32295f757f3fSDimitry Andric     Results.push_back(Res.getValue(1));
32305f757f3fSDimitry Andric     break;
32315f757f3fSDimitry Andric   }
32320b57cec5SDimitry Andric   case ISD::DYNAMIC_STACKALLOC:
32330b57cec5SDimitry Andric     ExpandDYNAMIC_STACKALLOC(Node, Results);
32340b57cec5SDimitry Andric     break;
32350b57cec5SDimitry Andric   case ISD::MERGE_VALUES:
32360b57cec5SDimitry Andric     for (unsigned i = 0; i < Node->getNumValues(); i++)
32370b57cec5SDimitry Andric       Results.push_back(Node->getOperand(i));
32380b57cec5SDimitry Andric     break;
32390b57cec5SDimitry Andric   case ISD::UNDEF: {
32400b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
32410b57cec5SDimitry Andric     if (VT.isInteger())
32420b57cec5SDimitry Andric       Results.push_back(DAG.getConstant(0, dl, VT));
32430b57cec5SDimitry Andric     else {
32440b57cec5SDimitry Andric       assert(VT.isFloatingPoint() && "Unknown value type!");
32450b57cec5SDimitry Andric       Results.push_back(DAG.getConstantFP(0, dl, VT));
32460b57cec5SDimitry Andric     }
32470b57cec5SDimitry Andric     break;
32480b57cec5SDimitry Andric   }
32490b57cec5SDimitry Andric   case ISD::STRICT_FP_ROUND:
3250480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3251480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3252480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3253480093f4SDimitry Andric       break;
3254480093f4SDimitry Andric     // We might as well mutate to FP_ROUND when FP_ROUND operation is legal
3255480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
32568bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
32578bcb0991SDimitry Andric                                        Node->getValueType(0))
32588bcb0991SDimitry Andric         == TargetLowering::Legal)
32598bcb0991SDimitry Andric       break;
3260480093f4SDimitry Andric     // We fall back to use stack operation when the FP_ROUND operation
3261480093f4SDimitry Andric     // isn't available.
3262e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0),
3263e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl,
3264e8d8bef9SDimitry Andric                                  Node->getOperand(0)))) {
32650b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
32660b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n");
32670b57cec5SDimitry Andric       return true;
3268e8d8bef9SDimitry Andric     }
3269e8d8bef9SDimitry Andric     break;
32701db9f3b2SDimitry Andric   case ISD::FP_ROUND: {
3271*0fca6ea1SDimitry Andric     if ((Tmp1 = TLI.expandFP_ROUND(Node, DAG))) {
3272*0fca6ea1SDimitry Andric       Results.push_back(Tmp1);
32731db9f3b2SDimitry Andric       break;
32741db9f3b2SDimitry Andric     }
32751db9f3b2SDimitry Andric 
3276*0fca6ea1SDimitry Andric     [[fallthrough]];
32771db9f3b2SDimitry Andric   }
32780b57cec5SDimitry Andric   case ISD::BITCAST:
3279e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3280e8d8bef9SDimitry Andric                                  Node->getValueType(0), dl)))
32810b57cec5SDimitry Andric       Results.push_back(Tmp1);
32820b57cec5SDimitry Andric     break;
32830b57cec5SDimitry Andric   case ISD::STRICT_FP_EXTEND:
3284480093f4SDimitry Andric     // When strict mode is enforced we can't do expansion because it
3285480093f4SDimitry Andric     // does not honor the "strict" properties. Only libcall is allowed.
3286480093f4SDimitry Andric     if (TLI.isStrictFPEnabled())
3287480093f4SDimitry Andric       break;
3288480093f4SDimitry Andric     // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal
3289480093f4SDimitry Andric     // since this operation is more efficient than stack operation.
32908bcb0991SDimitry Andric     if (TLI.getStrictFPOperationAction(Node->getOpcode(),
32918bcb0991SDimitry Andric                                        Node->getValueType(0))
32928bcb0991SDimitry Andric         == TargetLowering::Legal)
32938bcb0991SDimitry Andric       break;
3294480093f4SDimitry Andric     // We fall back to use stack operation when the FP_EXTEND operation
3295480093f4SDimitry Andric     // isn't available.
3296e8d8bef9SDimitry Andric     if ((Tmp1 = EmitStackConvert(
3297e8d8bef9SDimitry Andric              Node->getOperand(1), Node->getOperand(1).getValueType(),
3298e8d8bef9SDimitry Andric              Node->getValueType(0), dl, Node->getOperand(0)))) {
32990b57cec5SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
33000b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n");
33010b57cec5SDimitry Andric       return true;
3302e8d8bef9SDimitry Andric     }
3303e8d8bef9SDimitry Andric     break;
33041db9f3b2SDimitry Andric   case ISD::FP_EXTEND: {
33051db9f3b2SDimitry Andric     SDValue Op = Node->getOperand(0);
33061db9f3b2SDimitry Andric     EVT SrcVT = Op.getValueType();
33071db9f3b2SDimitry Andric     EVT DstVT = Node->getValueType(0);
33081db9f3b2SDimitry Andric     if (SrcVT.getScalarType() == MVT::bf16) {
33091db9f3b2SDimitry Andric       Results.push_back(DAG.getNode(ISD::BF16_TO_FP, SDLoc(Node), DstVT, Op));
33101db9f3b2SDimitry Andric       break;
33111db9f3b2SDimitry Andric     }
33121db9f3b2SDimitry Andric 
33131db9f3b2SDimitry Andric     if ((Tmp1 = EmitStackConvert(Op, SrcVT, DstVT, dl)))
33140b57cec5SDimitry Andric       Results.push_back(Tmp1);
33150b57cec5SDimitry Andric     break;
33161db9f3b2SDimitry Andric   }
331781ad6265SDimitry Andric   case ISD::BF16_TO_FP: {
331881ad6265SDimitry Andric     // Always expand bf16 to f32 casts, they lower to ext + shift.
3319bdd1243dSDimitry Andric     //
3320bdd1243dSDimitry Andric     // Note that the operand of this code can be bf16 or an integer type in case
3321bdd1243dSDimitry Andric     // bf16 is not supported on the target and was softened.
3322bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3323bdd1243dSDimitry Andric     if (Op.getValueType() == MVT::bf16) {
3324bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32,
3325bdd1243dSDimitry Andric                        DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op));
3326bdd1243dSDimitry Andric     } else {
3327bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32);
3328bdd1243dSDimitry Andric     }
332981ad6265SDimitry Andric     Op = DAG.getNode(
333081ad6265SDimitry Andric         ISD::SHL, dl, MVT::i32, Op,
333181ad6265SDimitry Andric         DAG.getConstant(16, dl,
333281ad6265SDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
333381ad6265SDimitry Andric     Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op);
3334bdd1243dSDimitry Andric     // Add fp_extend in case the output is bigger than f32.
3335bdd1243dSDimitry Andric     if (Node->getValueType(0) != MVT::f32)
3336bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op);
3337bdd1243dSDimitry Andric     Results.push_back(Op);
3338bdd1243dSDimitry Andric     break;
3339bdd1243dSDimitry Andric   }
3340bdd1243dSDimitry Andric   case ISD::FP_TO_BF16: {
3341bdd1243dSDimitry Andric     SDValue Op = Node->getOperand(0);
3342bdd1243dSDimitry Andric     if (Op.getValueType() != MVT::f32)
3343bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3344bdd1243dSDimitry Andric                        DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
3345*0fca6ea1SDimitry Andric     // Certain SNaNs will turn into infinities if we do a simple shift right.
3346*0fca6ea1SDimitry Andric     if (!DAG.isKnownNeverSNaN(Op)) {
3347*0fca6ea1SDimitry Andric       Op = DAG.getNode(ISD::FCANONICALIZE, dl, MVT::f32, Op, Node->getFlags());
3348*0fca6ea1SDimitry Andric     }
3349bdd1243dSDimitry Andric     Op = DAG.getNode(
3350bdd1243dSDimitry Andric         ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op),
3351bdd1243dSDimitry Andric         DAG.getConstant(16, dl,
3352bdd1243dSDimitry Andric                         TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout())));
3353bdd1243dSDimitry Andric     // The result of this node can be bf16 or an integer type in case bf16 is
3354bdd1243dSDimitry Andric     // not supported on the target and was softened to i16 for storage.
3355bdd1243dSDimitry Andric     if (Node->getValueType(0) == MVT::bf16) {
3356bdd1243dSDimitry Andric       Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16,
3357bdd1243dSDimitry Andric                        DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op));
3358bdd1243dSDimitry Andric     } else {
3359bdd1243dSDimitry Andric       Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0));
3360bdd1243dSDimitry Andric     }
336181ad6265SDimitry Andric     Results.push_back(Op);
336281ad6265SDimitry Andric     break;
336381ad6265SDimitry Andric   }
33640b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG: {
33650b57cec5SDimitry Andric     EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
33660b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
33670b57cec5SDimitry Andric 
33680b57cec5SDimitry Andric     // An in-register sign-extend of a boolean is a negation:
33690b57cec5SDimitry Andric     // 'true' (1) sign-extended is -1.
33700b57cec5SDimitry Andric     // 'false' (0) sign-extended is 0.
33710b57cec5SDimitry Andric     // However, we must mask the high bits of the source operand because the
33720b57cec5SDimitry Andric     // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero.
33730b57cec5SDimitry Andric 
33740b57cec5SDimitry Andric     // TODO: Do this for vectors too?
337581ad6265SDimitry Andric     if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) {
33760b57cec5SDimitry Andric       SDValue One = DAG.getConstant(1, dl, VT);
33770b57cec5SDimitry Andric       SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One);
33780b57cec5SDimitry Andric       SDValue Zero = DAG.getConstant(0, dl, VT);
33790b57cec5SDimitry Andric       SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And);
33800b57cec5SDimitry Andric       Results.push_back(Neg);
33810b57cec5SDimitry Andric       break;
33820b57cec5SDimitry Andric     }
33830b57cec5SDimitry Andric 
33840b57cec5SDimitry Andric     // NOTE: we could fall back on load/store here too for targets without
33850b57cec5SDimitry Andric     // SRA.  However, it is doubtful that any exist.
33860b57cec5SDimitry Andric     EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout());
33870b57cec5SDimitry Andric     unsigned BitsDiff = VT.getScalarSizeInBits() -
33880b57cec5SDimitry Andric                         ExtraVT.getScalarSizeInBits();
33890b57cec5SDimitry Andric     SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy);
33900b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0),
33910b57cec5SDimitry Andric                        Node->getOperand(0), ShiftCst);
33920b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst);
33930b57cec5SDimitry Andric     Results.push_back(Tmp1);
33940b57cec5SDimitry Andric     break;
33950b57cec5SDimitry Andric   }
33960b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
3397480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
3398480093f4SDimitry Andric     if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) {
33990b57cec5SDimitry Andric       Results.push_back(Tmp1);
3400480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3401480093f4SDimitry Andric         Results.push_back(Tmp2);
34020b57cec5SDimitry Andric       break;
34030b57cec5SDimitry Andric     }
3404bdd1243dSDimitry Andric     [[fallthrough]];
34050b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
3406480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
3407e8d8bef9SDimitry Andric     if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) {
34080b57cec5SDimitry Andric       Results.push_back(Tmp1);
3409480093f4SDimitry Andric       if (Node->isStrictFPOpcode())
3410480093f4SDimitry Andric         Results.push_back(Tmp2);
3411e8d8bef9SDimitry Andric     }
34120b57cec5SDimitry Andric     break;
34130b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
34140b57cec5SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG))
34150b57cec5SDimitry Andric       Results.push_back(Tmp1);
34160b57cec5SDimitry Andric     break;
34178bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
34188bcb0991SDimitry Andric     if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) {
34198bcb0991SDimitry Andric       ReplaceNode(Node, Tmp1.getNode());
34208bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n");
34218bcb0991SDimitry Andric       return true;
34228bcb0991SDimitry Andric     }
34238bcb0991SDimitry Andric     break;
34240b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
34258bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG))
34260b57cec5SDimitry Andric       Results.push_back(Tmp1);
34270b57cec5SDimitry Andric     break;
34288bcb0991SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
34298bcb0991SDimitry Andric     if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) {
34308bcb0991SDimitry Andric       // Relink the chain.
34318bcb0991SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2);
34328bcb0991SDimitry Andric       // Replace the new UINT result.
34338bcb0991SDimitry Andric       ReplaceNodeWithValue(SDValue(Node, 0), Tmp1);
34348bcb0991SDimitry Andric       LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n");
34358bcb0991SDimitry Andric       return true;
34368bcb0991SDimitry Andric     }
34370b57cec5SDimitry Andric     break;
3438e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
3439e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
3440e8d8bef9SDimitry Andric     Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG));
3441e8d8bef9SDimitry Andric     break;
34420b57cec5SDimitry Andric   case ISD::VAARG:
34430b57cec5SDimitry Andric     Results.push_back(DAG.expandVAArg(Node));
34440b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(1));
34450b57cec5SDimitry Andric     break;
34460b57cec5SDimitry Andric   case ISD::VACOPY:
34470b57cec5SDimitry Andric     Results.push_back(DAG.expandVACopy(Node));
34480b57cec5SDimitry Andric     break;
34490b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
34505f757f3fSDimitry Andric     if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar())
34510b57cec5SDimitry Andric       // This must be an access of the only element.  Return it.
34520b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0),
34530b57cec5SDimitry Andric                          Node->getOperand(0));
34540b57cec5SDimitry Andric     else
34550b57cec5SDimitry Andric       Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0));
34560b57cec5SDimitry Andric     Results.push_back(Tmp1);
34570b57cec5SDimitry Andric     break;
34580b57cec5SDimitry Andric   case ISD::EXTRACT_SUBVECTOR:
34590b57cec5SDimitry Andric     Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0)));
34600b57cec5SDimitry Andric     break;
34610b57cec5SDimitry Andric   case ISD::INSERT_SUBVECTOR:
34620b57cec5SDimitry Andric     Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0)));
34630b57cec5SDimitry Andric     break;
34640b57cec5SDimitry Andric   case ISD::CONCAT_VECTORS:
34650b57cec5SDimitry Andric     Results.push_back(ExpandVectorBuildThroughStack(Node));
34660b57cec5SDimitry Andric     break;
34670b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR:
34680b57cec5SDimitry Andric     Results.push_back(ExpandSCALAR_TO_VECTOR(Node));
34690b57cec5SDimitry Andric     break;
34700b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT:
3471*0fca6ea1SDimitry Andric     Results.push_back(ExpandINSERT_VECTOR_ELT(SDValue(Node, 0)));
34720b57cec5SDimitry Andric     break;
34730b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
34740b57cec5SDimitry Andric     SmallVector<int, 32> NewMask;
34750b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
34760b57cec5SDimitry Andric 
34770b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
34780b57cec5SDimitry Andric     EVT EltVT = VT.getVectorElementType();
34790b57cec5SDimitry Andric     SDValue Op0 = Node->getOperand(0);
34800b57cec5SDimitry Andric     SDValue Op1 = Node->getOperand(1);
34810b57cec5SDimitry Andric     if (!TLI.isTypeLegal(EltVT)) {
34820b57cec5SDimitry Andric       EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT);
34830b57cec5SDimitry Andric 
34840b57cec5SDimitry Andric       // BUILD_VECTOR operands are allowed to be wider than the element type.
34850b57cec5SDimitry Andric       // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept
34860b57cec5SDimitry Andric       // it.
34870b57cec5SDimitry Andric       if (NewEltVT.bitsLT(EltVT)) {
34880b57cec5SDimitry Andric         // Convert shuffle node.
34890b57cec5SDimitry Andric         // If original node was v4i64 and the new EltVT is i32,
34900b57cec5SDimitry Andric         // cast operands to v8i32 and re-build the mask.
34910b57cec5SDimitry Andric 
34920b57cec5SDimitry Andric         // Calculate new VT, the size of the new VT should be equal to original.
34930b57cec5SDimitry Andric         EVT NewVT =
34940b57cec5SDimitry Andric             EVT::getVectorVT(*DAG.getContext(), NewEltVT,
34950b57cec5SDimitry Andric                              VT.getSizeInBits() / NewEltVT.getSizeInBits());
34960b57cec5SDimitry Andric         assert(NewVT.bitsEq(VT));
34970b57cec5SDimitry Andric 
34980b57cec5SDimitry Andric         // cast operands to new VT
34990b57cec5SDimitry Andric         Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0);
35000b57cec5SDimitry Andric         Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1);
35010b57cec5SDimitry Andric 
35020b57cec5SDimitry Andric         // Convert the shuffle mask
35030b57cec5SDimitry Andric         unsigned int factor =
35040b57cec5SDimitry Andric                          NewVT.getVectorNumElements()/VT.getVectorNumElements();
35050b57cec5SDimitry Andric 
35060b57cec5SDimitry Andric         // EltVT gets smaller
35070b57cec5SDimitry Andric         assert(factor > 0);
35080b57cec5SDimitry Andric 
35090b57cec5SDimitry Andric         for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) {
35100b57cec5SDimitry Andric           if (Mask[i] < 0) {
35110b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
35120b57cec5SDimitry Andric               NewMask.push_back(Mask[i]);
35130b57cec5SDimitry Andric           }
35140b57cec5SDimitry Andric           else {
35150b57cec5SDimitry Andric             for (unsigned fi = 0; fi < factor; ++fi)
35160b57cec5SDimitry Andric               NewMask.push_back(Mask[i]*factor+fi);
35170b57cec5SDimitry Andric           }
35180b57cec5SDimitry Andric         }
35190b57cec5SDimitry Andric         Mask = NewMask;
35200b57cec5SDimitry Andric         VT = NewVT;
35210b57cec5SDimitry Andric       }
35220b57cec5SDimitry Andric       EltVT = NewEltVT;
35230b57cec5SDimitry Andric     }
35240b57cec5SDimitry Andric     unsigned NumElems = VT.getVectorNumElements();
35250b57cec5SDimitry Andric     SmallVector<SDValue, 16> Ops;
35260b57cec5SDimitry Andric     for (unsigned i = 0; i != NumElems; ++i) {
35270b57cec5SDimitry Andric       if (Mask[i] < 0) {
35280b57cec5SDimitry Andric         Ops.push_back(DAG.getUNDEF(EltVT));
35290b57cec5SDimitry Andric         continue;
35300b57cec5SDimitry Andric       }
35310b57cec5SDimitry Andric       unsigned Idx = Mask[i];
35320b57cec5SDimitry Andric       if (Idx < NumElems)
35335ffd83dbSDimitry Andric         Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0,
35345ffd83dbSDimitry Andric                                   DAG.getVectorIdxConstant(Idx, dl)));
35350b57cec5SDimitry Andric       else
35365ffd83dbSDimitry Andric         Ops.push_back(
35375ffd83dbSDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1,
35385ffd83dbSDimitry Andric                         DAG.getVectorIdxConstant(Idx - NumElems, dl)));
35390b57cec5SDimitry Andric     }
35400b57cec5SDimitry Andric 
35410b57cec5SDimitry Andric     Tmp1 = DAG.getBuildVector(VT, dl, Ops);
35420b57cec5SDimitry Andric     // We may have changed the BUILD_VECTOR type. Cast it back to the Node type.
35430b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1);
35440b57cec5SDimitry Andric     Results.push_back(Tmp1);
35450b57cec5SDimitry Andric     break;
35460b57cec5SDimitry Andric   }
3547fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
3548fe6060f1SDimitry Andric     Results.push_back(TLI.expandVectorSplice(Node, DAG));
3549fe6060f1SDimitry Andric     break;
3550fe6060f1SDimitry Andric   }
35510b57cec5SDimitry Andric   case ISD::EXTRACT_ELEMENT: {
35520b57cec5SDimitry Andric     EVT OpTy = Node->getOperand(0).getValueType();
3553bdd1243dSDimitry Andric     if (Node->getConstantOperandVal(1)) {
35540b57cec5SDimitry Andric       // 1 -> Hi
35550b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0),
35560b57cec5SDimitry Andric                          DAG.getConstant(OpTy.getSizeInBits() / 2, dl,
35570b57cec5SDimitry Andric                                          TLI.getShiftAmountTy(
35580b57cec5SDimitry Andric                                              Node->getOperand(0).getValueType(),
35590b57cec5SDimitry Andric                                              DAG.getDataLayout())));
35600b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1);
35610b57cec5SDimitry Andric     } else {
35620b57cec5SDimitry Andric       // 0 -> Lo
35630b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0),
35640b57cec5SDimitry Andric                          Node->getOperand(0));
35650b57cec5SDimitry Andric     }
35660b57cec5SDimitry Andric     Results.push_back(Tmp1);
35670b57cec5SDimitry Andric     break;
35680b57cec5SDimitry Andric   }
35690b57cec5SDimitry Andric   case ISD::STACKSAVE:
35700b57cec5SDimitry Andric     // Expand to CopyFromReg if the target set
35710b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3572e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
35730b57cec5SDimitry Andric       Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP,
35740b57cec5SDimitry Andric                                            Node->getValueType(0)));
35750b57cec5SDimitry Andric       Results.push_back(Results[0].getValue(1));
35760b57cec5SDimitry Andric     } else {
35770b57cec5SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
35780b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
35790b57cec5SDimitry Andric     }
35800b57cec5SDimitry Andric     break;
35810b57cec5SDimitry Andric   case ISD::STACKRESTORE:
35820b57cec5SDimitry Andric     // Expand to CopyToReg if the target set
35830b57cec5SDimitry Andric     // StackPointerRegisterToSaveRestore.
3584e8d8bef9SDimitry Andric     if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) {
35850b57cec5SDimitry Andric       Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP,
35860b57cec5SDimitry Andric                                          Node->getOperand(1)));
35870b57cec5SDimitry Andric     } else {
35880b57cec5SDimitry Andric       Results.push_back(Node->getOperand(0));
35890b57cec5SDimitry Andric     }
35900b57cec5SDimitry Andric     break;
35910b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
35920b57cec5SDimitry Andric     Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0)));
35930b57cec5SDimitry Andric     Results.push_back(Results[0].getValue(0));
35940b57cec5SDimitry Andric     break;
35950b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
35960b57cec5SDimitry Andric     Results.push_back(ExpandFCOPYSIGN(Node));
35970b57cec5SDimitry Andric     break;
35980b57cec5SDimitry Andric   case ISD::FNEG:
3599e8d8bef9SDimitry Andric     Results.push_back(ExpandFNEG(Node));
36000b57cec5SDimitry Andric     break;
36010b57cec5SDimitry Andric   case ISD::FABS:
36020b57cec5SDimitry Andric     Results.push_back(ExpandFABS(Node));
36030b57cec5SDimitry Andric     break;
360481ad6265SDimitry Andric   case ISD::IS_FPCLASS: {
36057a6dacacSDimitry Andric     auto Test = static_cast<FPClassTest>(Node->getConstantOperandVal(1));
360681ad6265SDimitry Andric     if (SDValue Expanded =
360781ad6265SDimitry Andric             TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0),
360881ad6265SDimitry Andric                                  Test, Node->getFlags(), SDLoc(Node), DAG))
360981ad6265SDimitry Andric       Results.push_back(Expanded);
361081ad6265SDimitry Andric     break;
361181ad6265SDimitry Andric   }
36120b57cec5SDimitry Andric   case ISD::SMIN:
36130b57cec5SDimitry Andric   case ISD::SMAX:
36140b57cec5SDimitry Andric   case ISD::UMIN:
36150b57cec5SDimitry Andric   case ISD::UMAX: {
36160b57cec5SDimitry Andric     // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B
36170b57cec5SDimitry Andric     ISD::CondCode Pred;
36180b57cec5SDimitry Andric     switch (Node->getOpcode()) {
36190b57cec5SDimitry Andric     default: llvm_unreachable("How did we get here?");
36200b57cec5SDimitry Andric     case ISD::SMAX: Pred = ISD::SETGT; break;
36210b57cec5SDimitry Andric     case ISD::SMIN: Pred = ISD::SETLT; break;
36220b57cec5SDimitry Andric     case ISD::UMAX: Pred = ISD::SETUGT; break;
36230b57cec5SDimitry Andric     case ISD::UMIN: Pred = ISD::SETULT; break;
36240b57cec5SDimitry Andric     }
36250b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
36260b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
36270b57cec5SDimitry Andric     Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred);
36280b57cec5SDimitry Andric     Results.push_back(Tmp1);
36290b57cec5SDimitry Andric     break;
36300b57cec5SDimitry Andric   }
36310b57cec5SDimitry Andric   case ISD::FMINNUM:
36320b57cec5SDimitry Andric   case ISD::FMAXNUM: {
36330b57cec5SDimitry Andric     if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG))
36340b57cec5SDimitry Andric       Results.push_back(Expanded);
36350b57cec5SDimitry Andric     break;
36360b57cec5SDimitry Andric   }
3637*0fca6ea1SDimitry Andric   case ISD::FMINIMUM:
3638*0fca6ea1SDimitry Andric   case ISD::FMAXIMUM: {
3639*0fca6ea1SDimitry Andric     if (SDValue Expanded = TLI.expandFMINIMUM_FMAXIMUM(Node, DAG))
3640*0fca6ea1SDimitry Andric       Results.push_back(Expanded);
3641*0fca6ea1SDimitry Andric     break;
3642*0fca6ea1SDimitry Andric   }
36430b57cec5SDimitry Andric   case ISD::FSIN:
36440b57cec5SDimitry Andric   case ISD::FCOS: {
36450b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
36460b57cec5SDimitry Andric     // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
36470b57cec5SDimitry Andric     // fcos which share the same operand and both are used.
36480b57cec5SDimitry Andric     if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
36490b57cec5SDimitry Andric          isSinCosLibcallAvailable(Node, TLI))
36500b57cec5SDimitry Andric         && useSinCos(Node)) {
36510b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
36520b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));
36530b57cec5SDimitry Andric       if (Node->getOpcode() == ISD::FCOS)
36540b57cec5SDimitry Andric         Tmp1 = Tmp1.getValue(1);
36550b57cec5SDimitry Andric       Results.push_back(Tmp1);
36560b57cec5SDimitry Andric     }
36570b57cec5SDimitry Andric     break;
36580b57cec5SDimitry Andric   }
365906c3fb27SDimitry Andric   case ISD::FLDEXP:
366006c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP: {
366106c3fb27SDimitry Andric     EVT VT = Node->getValueType(0);
366206c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getLDEXP(VT);
366306c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
366406c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
366506c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
366606c3fb27SDimitry Andric       break;
366706c3fb27SDimitry Andric 
366806c3fb27SDimitry Andric     if (SDValue Expanded = expandLdexp(Node)) {
366906c3fb27SDimitry Andric       Results.push_back(Expanded);
367006c3fb27SDimitry Andric       if (Node->getOpcode() == ISD::STRICT_FLDEXP)
367106c3fb27SDimitry Andric         Results.push_back(Expanded.getValue(1));
367206c3fb27SDimitry Andric     }
367306c3fb27SDimitry Andric 
367406c3fb27SDimitry Andric     break;
367506c3fb27SDimitry Andric   }
367606c3fb27SDimitry Andric   case ISD::FFREXP: {
367706c3fb27SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0));
367806c3fb27SDimitry Andric     // Use the LibCall instead, it is very likely faster
367906c3fb27SDimitry Andric     // FIXME: Use separate LibCall action.
368006c3fb27SDimitry Andric     if (TLI.getLibcallName(LC))
368106c3fb27SDimitry Andric       break;
368206c3fb27SDimitry Andric 
368306c3fb27SDimitry Andric     if (SDValue Expanded = expandFrexp(Node)) {
368406c3fb27SDimitry Andric       Results.push_back(Expanded);
368506c3fb27SDimitry Andric       Results.push_back(Expanded.getValue(1));
368606c3fb27SDimitry Andric     }
368706c3fb27SDimitry Andric     break;
368806c3fb27SDimitry Andric   }
36890b57cec5SDimitry Andric   case ISD::FMAD:
36900b57cec5SDimitry Andric     llvm_unreachable("Illegal fmad should never be formed");
36910b57cec5SDimitry Andric 
36920b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
36930b57cec5SDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
36940b57cec5SDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
36950b57cec5SDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
36960b57cec5SDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
36970b57cec5SDimitry Andric       SDValue Res =
36980b57cec5SDimitry Andric           DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0));
36990b57cec5SDimitry Andric       Results.push_back(
37000b57cec5SDimitry Andric           DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res));
37010b57cec5SDimitry Andric     }
37020b57cec5SDimitry Andric     break;
3703*0fca6ea1SDimitry Andric   case ISD::STRICT_BF16_TO_FP:
37045ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP:
37055ffd83dbSDimitry Andric     if (Node->getValueType(0) != MVT::f32) {
37065ffd83dbSDimitry Andric       // We can extend to types bigger than f32 in two steps without changing
37075ffd83dbSDimitry Andric       // the result. Since "f16 -> f32" is much more commonly available, give
37085ffd83dbSDimitry Andric       // CodeGen the option of emitting that before resorting to a libcall.
3709*0fca6ea1SDimitry Andric       SDValue Res = DAG.getNode(Node->getOpcode(), dl, {MVT::f32, MVT::Other},
37105ffd83dbSDimitry Andric                                 {Node->getOperand(0), Node->getOperand(1)});
37115ffd83dbSDimitry Andric       Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl,
37125ffd83dbSDimitry Andric                         {Node->getValueType(0), MVT::Other},
37135ffd83dbSDimitry Andric                         {Res.getValue(1), Res});
37145ffd83dbSDimitry Andric       Results.push_back(Res);
37155ffd83dbSDimitry Andric       Results.push_back(Res.getValue(1));
37165ffd83dbSDimitry Andric     }
37175ffd83dbSDimitry Andric     break;
37180b57cec5SDimitry Andric   case ISD::FP_TO_FP16:
37190b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n");
37200b57cec5SDimitry Andric     if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) {
37210b57cec5SDimitry Andric       SDValue Op = Node->getOperand(0);
37220b57cec5SDimitry Andric       MVT SVT = Op.getSimpleValueType();
37230b57cec5SDimitry Andric       if ((SVT == MVT::f64 || SVT == MVT::f80) &&
37240b57cec5SDimitry Andric           TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) {
37250b57cec5SDimitry Andric         // Under fastmath, we can expand this node into a fround followed by
37260b57cec5SDimitry Andric         // a float-half conversion.
3727bdd1243dSDimitry Andric         SDValue FloatVal =
3728bdd1243dSDimitry Andric             DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op,
3729bdd1243dSDimitry Andric                         DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
37300b57cec5SDimitry Andric         Results.push_back(
37310b57cec5SDimitry Andric             DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal));
37320b57cec5SDimitry Andric       }
37330b57cec5SDimitry Andric     }
37340b57cec5SDimitry Andric     break;
37350b57cec5SDimitry Andric   case ISD::ConstantFP: {
37360b57cec5SDimitry Andric     ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
37370b57cec5SDimitry Andric     // Check to see if this FP immediate is already legal.
37380b57cec5SDimitry Andric     // If this is a legal constant, turn it into a TargetConstantFP node.
37390b57cec5SDimitry Andric     if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0),
3740e8d8bef9SDimitry Andric                           DAG.shouldOptForSize()))
37410b57cec5SDimitry Andric       Results.push_back(ExpandConstantFP(CFP, true));
37420b57cec5SDimitry Andric     break;
37430b57cec5SDimitry Andric   }
37440b57cec5SDimitry Andric   case ISD::Constant: {
37450b57cec5SDimitry Andric     ConstantSDNode *CP = cast<ConstantSDNode>(Node);
37460b57cec5SDimitry Andric     Results.push_back(ExpandConstant(CP));
37470b57cec5SDimitry Andric     break;
37480b57cec5SDimitry Andric   }
37490b57cec5SDimitry Andric   case ISD::FSUB: {
37500b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37510b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) &&
37520b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) {
37530b57cec5SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
37540b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1));
37550b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags);
37560b57cec5SDimitry Andric       Results.push_back(Tmp1);
37570b57cec5SDimitry Andric     }
37580b57cec5SDimitry Andric     break;
37590b57cec5SDimitry Andric   }
37600b57cec5SDimitry Andric   case ISD::SUB: {
37610b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37620b57cec5SDimitry Andric     assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) &&
37630b57cec5SDimitry Andric            TLI.isOperationLegalOrCustom(ISD::XOR, VT) &&
37640b57cec5SDimitry Andric            "Don't know how to expand this subtraction!");
3765349cc55cSDimitry Andric     Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT);
37660b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT));
37670b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1));
37680b57cec5SDimitry Andric     break;
37690b57cec5SDimitry Andric   }
37700b57cec5SDimitry Andric   case ISD::UREM:
37715ffd83dbSDimitry Andric   case ISD::SREM:
37725ffd83dbSDimitry Andric     if (TLI.expandREM(Node, Tmp1, DAG))
37730b57cec5SDimitry Andric       Results.push_back(Tmp1);
37740b57cec5SDimitry Andric     break;
37750b57cec5SDimitry Andric   case ISD::UDIV:
37760b57cec5SDimitry Andric   case ISD::SDIV: {
37770b57cec5SDimitry Andric     bool isSigned = Node->getOpcode() == ISD::SDIV;
37780b57cec5SDimitry Andric     unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM;
37790b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37800b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) {
37810b57cec5SDimitry Andric       SDVTList VTs = DAG.getVTList(VT, VT);
37820b57cec5SDimitry Andric       Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0),
37830b57cec5SDimitry Andric                          Node->getOperand(1));
37840b57cec5SDimitry Andric       Results.push_back(Tmp1);
37850b57cec5SDimitry Andric     }
37860b57cec5SDimitry Andric     break;
37870b57cec5SDimitry Andric   }
37880b57cec5SDimitry Andric   case ISD::MULHU:
37890b57cec5SDimitry Andric   case ISD::MULHS: {
37900b57cec5SDimitry Andric     unsigned ExpandOpcode =
37910b57cec5SDimitry Andric         Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI;
37920b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
37930b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
37940b57cec5SDimitry Andric 
37950b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0),
37960b57cec5SDimitry Andric                        Node->getOperand(1));
37970b57cec5SDimitry Andric     Results.push_back(Tmp1.getValue(1));
37980b57cec5SDimitry Andric     break;
37990b57cec5SDimitry Andric   }
38000b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
38010b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
38020b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
38030b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
38040b57cec5SDimitry Andric     MVT VT = LHS.getSimpleValueType();
38050b57cec5SDimitry Andric     unsigned MULHOpcode =
38060b57cec5SDimitry Andric         Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS;
38070b57cec5SDimitry Andric 
38080b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) {
38090b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS));
38100b57cec5SDimitry Andric       Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS));
38110b57cec5SDimitry Andric       break;
38120b57cec5SDimitry Andric     }
38130b57cec5SDimitry Andric 
38140b57cec5SDimitry Andric     SmallVector<SDValue, 4> Halves;
38150b57cec5SDimitry Andric     EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext());
38160b57cec5SDimitry Andric     assert(TLI.isTypeLegal(HalfType));
3817e8d8bef9SDimitry Andric     if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves,
38180b57cec5SDimitry Andric                            HalfType, DAG,
38190b57cec5SDimitry Andric                            TargetLowering::MulExpansionKind::Always)) {
38200b57cec5SDimitry Andric       for (unsigned i = 0; i < 2; ++i) {
38210b57cec5SDimitry Andric         SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]);
38220b57cec5SDimitry Andric         SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]);
38230b57cec5SDimitry Andric         SDValue Shift = DAG.getConstant(
38240b57cec5SDimitry Andric             HalfType.getScalarSizeInBits(), dl,
38250b57cec5SDimitry Andric             TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
38260b57cec5SDimitry Andric         Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
38270b57cec5SDimitry Andric         Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
38280b57cec5SDimitry Andric       }
38290b57cec5SDimitry Andric       break;
38300b57cec5SDimitry Andric     }
38310b57cec5SDimitry Andric     break;
38320b57cec5SDimitry Andric   }
38330b57cec5SDimitry Andric   case ISD::MUL: {
38340b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
38350b57cec5SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, VT);
38360b57cec5SDimitry Andric     // See if multiply or divide can be lowered using two-result operations.
38370b57cec5SDimitry Andric     // We just need the low half of the multiply; try both the signed
38380b57cec5SDimitry Andric     // and unsigned forms. If the target supports both SMUL_LOHI and
38390b57cec5SDimitry Andric     // UMUL_LOHI, form a preference by checking which forms of plain
38400b57cec5SDimitry Andric     // MULH it supports.
38410b57cec5SDimitry Andric     bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT);
38420b57cec5SDimitry Andric     bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT);
38430b57cec5SDimitry Andric     bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT);
38440b57cec5SDimitry Andric     bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT);
38450b57cec5SDimitry Andric     unsigned OpToUse = 0;
38460b57cec5SDimitry Andric     if (HasSMUL_LOHI && !HasMULHS) {
38470b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
38480b57cec5SDimitry Andric     } else if (HasUMUL_LOHI && !HasMULHU) {
38490b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
38500b57cec5SDimitry Andric     } else if (HasSMUL_LOHI) {
38510b57cec5SDimitry Andric       OpToUse = ISD::SMUL_LOHI;
38520b57cec5SDimitry Andric     } else if (HasUMUL_LOHI) {
38530b57cec5SDimitry Andric       OpToUse = ISD::UMUL_LOHI;
38540b57cec5SDimitry Andric     }
38550b57cec5SDimitry Andric     if (OpToUse) {
38560b57cec5SDimitry Andric       Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0),
38570b57cec5SDimitry Andric                                     Node->getOperand(1)));
38580b57cec5SDimitry Andric       break;
38590b57cec5SDimitry Andric     }
38600b57cec5SDimitry Andric 
38610b57cec5SDimitry Andric     SDValue Lo, Hi;
38620b57cec5SDimitry Andric     EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext());
38630b57cec5SDimitry Andric     if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) &&
38640b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) &&
38650b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::SHL, VT) &&
38660b57cec5SDimitry Andric         TLI.isOperationLegalOrCustom(ISD::OR, VT) &&
38670b57cec5SDimitry Andric         TLI.expandMUL(Node, Lo, Hi, HalfType, DAG,
38680b57cec5SDimitry Andric                       TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) {
38690b57cec5SDimitry Andric       Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo);
38700b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi);
38710b57cec5SDimitry Andric       SDValue Shift =
38720b57cec5SDimitry Andric           DAG.getConstant(HalfType.getSizeInBits(), dl,
38730b57cec5SDimitry Andric                           TLI.getShiftAmountTy(HalfType, DAG.getDataLayout()));
38740b57cec5SDimitry Andric       Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift);
38750b57cec5SDimitry Andric       Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi));
38760b57cec5SDimitry Andric     }
38770b57cec5SDimitry Andric     break;
38780b57cec5SDimitry Andric   }
38790b57cec5SDimitry Andric   case ISD::FSHL:
38800b57cec5SDimitry Andric   case ISD::FSHR:
38810eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG))
38820eae32dcSDimitry Andric       Results.push_back(Expanded);
38830b57cec5SDimitry Andric     break;
38840b57cec5SDimitry Andric   case ISD::ROTL:
38850b57cec5SDimitry Andric   case ISD::ROTR:
38860eae32dcSDimitry Andric     if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG))
38870eae32dcSDimitry Andric       Results.push_back(Expanded);
38880b57cec5SDimitry Andric     break;
38890b57cec5SDimitry Andric   case ISD::SADDSAT:
38900b57cec5SDimitry Andric   case ISD::UADDSAT:
38910b57cec5SDimitry Andric   case ISD::SSUBSAT:
38920b57cec5SDimitry Andric   case ISD::USUBSAT:
38930b57cec5SDimitry Andric     Results.push_back(TLI.expandAddSubSat(Node, DAG));
38940b57cec5SDimitry Andric     break;
3895*0fca6ea1SDimitry Andric   case ISD::SCMP:
3896*0fca6ea1SDimitry Andric   case ISD::UCMP:
3897*0fca6ea1SDimitry Andric     Results.push_back(TLI.expandCMP(Node, DAG));
3898*0fca6ea1SDimitry Andric     break;
3899e8d8bef9SDimitry Andric   case ISD::SSHLSAT:
3900e8d8bef9SDimitry Andric   case ISD::USHLSAT:
3901e8d8bef9SDimitry Andric     Results.push_back(TLI.expandShlSat(Node, DAG));
3902e8d8bef9SDimitry Andric     break;
39030b57cec5SDimitry Andric   case ISD::SMULFIX:
39040b57cec5SDimitry Andric   case ISD::SMULFIXSAT:
39050b57cec5SDimitry Andric   case ISD::UMULFIX:
39068bcb0991SDimitry Andric   case ISD::UMULFIXSAT:
39070b57cec5SDimitry Andric     Results.push_back(TLI.expandFixedPointMul(Node, DAG));
39080b57cec5SDimitry Andric     break;
3909480093f4SDimitry Andric   case ISD::SDIVFIX:
39105ffd83dbSDimitry Andric   case ISD::SDIVFIXSAT:
3911480093f4SDimitry Andric   case ISD::UDIVFIX:
39125ffd83dbSDimitry Andric   case ISD::UDIVFIXSAT:
3913480093f4SDimitry Andric     if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node),
3914480093f4SDimitry Andric                                             Node->getOperand(0),
3915480093f4SDimitry Andric                                             Node->getOperand(1),
3916480093f4SDimitry Andric                                             Node->getConstantOperandVal(2),
3917480093f4SDimitry Andric                                             DAG)) {
3918480093f4SDimitry Andric       Results.push_back(V);
3919480093f4SDimitry Andric       break;
3920480093f4SDimitry Andric     }
3921480093f4SDimitry Andric     // FIXME: We might want to retry here with a wider type if we fail, if that
3922480093f4SDimitry Andric     // type is legal.
3923480093f4SDimitry Andric     // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is
3924480093f4SDimitry Andric     // <= 128 (which is the case for all of the default Embedded-C types),
3925480093f4SDimitry Andric     // we will only get here with types and scales that we could always expand
3926480093f4SDimitry Andric     // if we were allowed to generate libcalls to division functions of illegal
3927480093f4SDimitry Andric     // type. But we cannot do that.
3928480093f4SDimitry Andric     llvm_unreachable("Cannot expand DIVFIX!");
392906c3fb27SDimitry Andric   case ISD::UADDO_CARRY:
393006c3fb27SDimitry Andric   case ISD::USUBO_CARRY: {
39310b57cec5SDimitry Andric     SDValue LHS = Node->getOperand(0);
39320b57cec5SDimitry Andric     SDValue RHS = Node->getOperand(1);
39330b57cec5SDimitry Andric     SDValue Carry = Node->getOperand(2);
39340b57cec5SDimitry Andric 
393506c3fb27SDimitry Andric     bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY;
39360b57cec5SDimitry Andric 
39370b57cec5SDimitry Andric     // Initial add of the 2 operands.
39380b57cec5SDimitry Andric     unsigned Op = IsAdd ? ISD::ADD : ISD::SUB;
39390b57cec5SDimitry Andric     EVT VT = LHS.getValueType();
39400b57cec5SDimitry Andric     SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS);
39410b57cec5SDimitry Andric 
39420b57cec5SDimitry Andric     // Initial check for overflow.
39430b57cec5SDimitry Andric     EVT CarryType = Node->getValueType(1);
39440b57cec5SDimitry Andric     EVT SetCCType = getSetCCResultType(Node->getValueType(0));
39450b57cec5SDimitry Andric     ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT;
39460b57cec5SDimitry Andric     SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC);
39470b57cec5SDimitry Andric 
39480b57cec5SDimitry Andric     // Add of the sum and the carry.
39495ffd83dbSDimitry Andric     SDValue One = DAG.getConstant(1, dl, VT);
39500b57cec5SDimitry Andric     SDValue CarryExt =
39515ffd83dbSDimitry Andric         DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One);
39520b57cec5SDimitry Andric     SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt);
39530b57cec5SDimitry Andric 
39540b57cec5SDimitry Andric     // Second check for overflow. If we are adding, we can only overflow if the
39550b57cec5SDimitry Andric     // initial sum is all 1s ang the carry is set, resulting in a new sum of 0.
39560b57cec5SDimitry Andric     // If we are subtracting, we can only overflow if the initial sum is 0 and
39570b57cec5SDimitry Andric     // the carry is set, resulting in a new sum of all 1s.
39580b57cec5SDimitry Andric     SDValue Zero = DAG.getConstant(0, dl, VT);
39590b57cec5SDimitry Andric     SDValue Overflow2 =
39600b57cec5SDimitry Andric         IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ)
39610b57cec5SDimitry Andric               : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ);
39620b57cec5SDimitry Andric     Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2,
39630b57cec5SDimitry Andric                             DAG.getZExtOrTrunc(Carry, dl, SetCCType));
39640b57cec5SDimitry Andric 
39650b57cec5SDimitry Andric     SDValue ResultCarry =
39660b57cec5SDimitry Andric         DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2);
39670b57cec5SDimitry Andric 
39680b57cec5SDimitry Andric     Results.push_back(Sum2);
39690b57cec5SDimitry Andric     Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT));
39700b57cec5SDimitry Andric     break;
39710b57cec5SDimitry Andric   }
39720b57cec5SDimitry Andric   case ISD::SADDO:
39730b57cec5SDimitry Andric   case ISD::SSUBO: {
39740b57cec5SDimitry Andric     SDValue Result, Overflow;
39750b57cec5SDimitry Andric     TLI.expandSADDSUBO(Node, Result, Overflow, DAG);
39760b57cec5SDimitry Andric     Results.push_back(Result);
39770b57cec5SDimitry Andric     Results.push_back(Overflow);
39780b57cec5SDimitry Andric     break;
39790b57cec5SDimitry Andric   }
39800b57cec5SDimitry Andric   case ISD::UADDO:
39810b57cec5SDimitry Andric   case ISD::USUBO: {
39820b57cec5SDimitry Andric     SDValue Result, Overflow;
39830b57cec5SDimitry Andric     TLI.expandUADDSUBO(Node, Result, Overflow, DAG);
39840b57cec5SDimitry Andric     Results.push_back(Result);
39850b57cec5SDimitry Andric     Results.push_back(Overflow);
39860b57cec5SDimitry Andric     break;
39870b57cec5SDimitry Andric   }
39880b57cec5SDimitry Andric   case ISD::UMULO:
39890b57cec5SDimitry Andric   case ISD::SMULO: {
39900b57cec5SDimitry Andric     SDValue Result, Overflow;
39910b57cec5SDimitry Andric     if (TLI.expandMULO(Node, Result, Overflow, DAG)) {
39920b57cec5SDimitry Andric       Results.push_back(Result);
39930b57cec5SDimitry Andric       Results.push_back(Overflow);
39940b57cec5SDimitry Andric     }
39950b57cec5SDimitry Andric     break;
39960b57cec5SDimitry Andric   }
39970b57cec5SDimitry Andric   case ISD::BUILD_PAIR: {
39980b57cec5SDimitry Andric     EVT PairTy = Node->getValueType(0);
39990b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0));
40000b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1));
40010b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
40020b57cec5SDimitry Andric         ISD::SHL, dl, PairTy, Tmp2,
40030b57cec5SDimitry Andric         DAG.getConstant(PairTy.getSizeInBits() / 2, dl,
40040b57cec5SDimitry Andric                         TLI.getShiftAmountTy(PairTy, DAG.getDataLayout())));
40050b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2));
40060b57cec5SDimitry Andric     break;
40070b57cec5SDimitry Andric   }
40080b57cec5SDimitry Andric   case ISD::SELECT:
40090b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
40100b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
40110b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);
40120b57cec5SDimitry Andric     if (Tmp1.getOpcode() == ISD::SETCC) {
40130b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1),
40140b57cec5SDimitry Andric                              Tmp2, Tmp3,
40150b57cec5SDimitry Andric                              cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
40160b57cec5SDimitry Andric     } else {
40170b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1,
40180b57cec5SDimitry Andric                              DAG.getConstant(0, dl, Tmp1.getValueType()),
40190b57cec5SDimitry Andric                              Tmp2, Tmp3, ISD::SETNE);
40200b57cec5SDimitry Andric     }
40210b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
40220b57cec5SDimitry Andric     Results.push_back(Tmp1);
40230b57cec5SDimitry Andric     break;
40240b57cec5SDimitry Andric   case ISD::BR_JT: {
40250b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0);
40260b57cec5SDimitry Andric     SDValue Table = Node->getOperand(1);
40270b57cec5SDimitry Andric     SDValue Index = Node->getOperand(2);
40285f757f3fSDimitry Andric     int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex();
40290b57cec5SDimitry Andric 
40300b57cec5SDimitry Andric     const DataLayout &TD = DAG.getDataLayout();
40310b57cec5SDimitry Andric     EVT PTy = TLI.getPointerTy(TD);
40320b57cec5SDimitry Andric 
40330b57cec5SDimitry Andric     unsigned EntrySize =
40340b57cec5SDimitry Andric       DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD);
40350b57cec5SDimitry Andric 
40360b57cec5SDimitry Andric     // For power-of-two jumptable entry sizes convert multiplication to a shift.
40370b57cec5SDimitry Andric     // This transformation needs to be done here since otherwise the MIPS
40380b57cec5SDimitry Andric     // backend will end up emitting a three instruction multiply sequence
40390b57cec5SDimitry Andric     // instead of a single shift and MSP430 will call a runtime function.
40400b57cec5SDimitry Andric     if (llvm::isPowerOf2_32(EntrySize))
40410b57cec5SDimitry Andric       Index = DAG.getNode(
40420b57cec5SDimitry Andric           ISD::SHL, dl, Index.getValueType(), Index,
40430b57cec5SDimitry Andric           DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType()));
40440b57cec5SDimitry Andric     else
40450b57cec5SDimitry Andric       Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index,
40460b57cec5SDimitry Andric                           DAG.getConstant(EntrySize, dl, Index.getValueType()));
40470b57cec5SDimitry Andric     SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(),
40480b57cec5SDimitry Andric                                Index, Table);
40490b57cec5SDimitry Andric 
40500b57cec5SDimitry Andric     EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
40510b57cec5SDimitry Andric     SDValue LD = DAG.getExtLoad(
40520b57cec5SDimitry Andric         ISD::SEXTLOAD, dl, PTy, Chain, Addr,
40530b57cec5SDimitry Andric         MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT);
40540b57cec5SDimitry Andric     Addr = LD;
40550b57cec5SDimitry Andric     if (TLI.isJumpTableRelative()) {
40560b57cec5SDimitry Andric       // For PIC, the sequence is:
40570b57cec5SDimitry Andric       // BRIND(load(Jumptable + index) + RelocBase)
40580b57cec5SDimitry Andric       // RelocBase can be JumpTable, GOT or some sort of global base.
40590b57cec5SDimitry Andric       Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr,
40600b57cec5SDimitry Andric                           TLI.getPICJumpTableRelocBase(Table, DAG));
40610b57cec5SDimitry Andric     }
40620b57cec5SDimitry Andric 
40635f757f3fSDimitry Andric     Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG);
40640b57cec5SDimitry Andric     Results.push_back(Tmp1);
40650b57cec5SDimitry Andric     break;
40660b57cec5SDimitry Andric   }
40670b57cec5SDimitry Andric   case ISD::BRCOND:
40680b57cec5SDimitry Andric     // Expand brcond's setcc into its constituent parts and create a BR_CC
40690b57cec5SDimitry Andric     // Node.
40700b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
40710b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
40724824e7fdSDimitry Andric     if (Tmp2.getOpcode() == ISD::SETCC &&
40734824e7fdSDimitry Andric         TLI.isOperationLegalOrCustom(ISD::BR_CC,
40744824e7fdSDimitry Andric                                      Tmp2.getOperand(0).getValueType())) {
40754824e7fdSDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2),
40760b57cec5SDimitry Andric                          Tmp2.getOperand(0), Tmp2.getOperand(1),
40770b57cec5SDimitry Andric                          Node->getOperand(2));
40780b57cec5SDimitry Andric     } else {
40790b57cec5SDimitry Andric       // We test only the i1 bit.  Skip the AND if UNDEF or another AND.
40800b57cec5SDimitry Andric       if (Tmp2.isUndef() ||
408106c3fb27SDimitry Andric           (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1))))
40820b57cec5SDimitry Andric         Tmp3 = Tmp2;
40830b57cec5SDimitry Andric       else
40840b57cec5SDimitry Andric         Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2,
40850b57cec5SDimitry Andric                            DAG.getConstant(1, dl, Tmp2.getValueType()));
40860b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1,
40870b57cec5SDimitry Andric                          DAG.getCondCode(ISD::SETNE), Tmp3,
40880b57cec5SDimitry Andric                          DAG.getConstant(0, dl, Tmp3.getValueType()),
40890b57cec5SDimitry Andric                          Node->getOperand(2));
40900b57cec5SDimitry Andric     }
40910b57cec5SDimitry Andric     Results.push_back(Tmp1);
40920b57cec5SDimitry Andric     break;
4093480093f4SDimitry Andric   case ISD::SETCC:
409481ad6265SDimitry Andric   case ISD::VP_SETCC:
4095480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
4096480093f4SDimitry Andric   case ISD::STRICT_FSETCCS: {
409781ad6265SDimitry Andric     bool IsVP = Node->getOpcode() == ISD::VP_SETCC;
409881ad6265SDimitry Andric     bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC ||
409981ad6265SDimitry Andric                     Node->getOpcode() == ISD::STRICT_FSETCCS;
4100480093f4SDimitry Andric     bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS;
4101480093f4SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4102480093f4SDimitry Andric     unsigned Offset = IsStrict ? 1 : 0;
4103480093f4SDimitry Andric     Tmp1 = Node->getOperand(0 + Offset);
4104480093f4SDimitry Andric     Tmp2 = Node->getOperand(1 + Offset);
4105480093f4SDimitry Andric     Tmp3 = Node->getOperand(2 + Offset);
410681ad6265SDimitry Andric     SDValue Mask, EVL;
410781ad6265SDimitry Andric     if (IsVP) {
410881ad6265SDimitry Andric       Mask = Node->getOperand(3 + Offset);
410981ad6265SDimitry Andric       EVL = Node->getOperand(4 + Offset);
411081ad6265SDimitry Andric     }
411181ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
411281ad6265SDimitry Andric         DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl,
411381ad6265SDimitry Andric         Chain, IsSignaling);
41140b57cec5SDimitry Andric 
41150b57cec5SDimitry Andric     if (Legalized) {
41160b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
41170b57cec5SDimitry Andric       // condition code, create a new SETCC node.
411804eeddc0SDimitry Andric       if (Tmp3.getNode()) {
411904eeddc0SDimitry Andric         if (IsStrict) {
412004eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(),
412104eeddc0SDimitry Andric                              {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags());
412204eeddc0SDimitry Andric           Chain = Tmp1.getValue(1);
412381ad6265SDimitry Andric         } else if (IsVP) {
412481ad6265SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0),
412581ad6265SDimitry Andric                              {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags());
412604eeddc0SDimitry Andric         } else {
412704eeddc0SDimitry Andric           Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1,
412804eeddc0SDimitry Andric                              Tmp2, Tmp3, Node->getFlags());
412904eeddc0SDimitry Andric         }
413004eeddc0SDimitry Andric       }
41310b57cec5SDimitry Andric 
41320b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then wrap
41330b57cec5SDimitry Andric       // the existing SETCC in a NOT to restore the intended condition.
413481ad6265SDimitry Andric       if (NeedInvert) {
413581ad6265SDimitry Andric         if (!IsVP)
41360b57cec5SDimitry Andric           Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0));
413781ad6265SDimitry Andric         else
413881ad6265SDimitry Andric           Tmp1 =
413981ad6265SDimitry Andric               DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0));
414081ad6265SDimitry Andric       }
41410b57cec5SDimitry Andric 
41420b57cec5SDimitry Andric       Results.push_back(Tmp1);
4143480093f4SDimitry Andric       if (IsStrict)
4144480093f4SDimitry Andric         Results.push_back(Chain);
4145480093f4SDimitry Andric 
41460b57cec5SDimitry Andric       break;
41470b57cec5SDimitry Andric     }
41480b57cec5SDimitry Andric 
4149480093f4SDimitry Andric     // FIXME: It seems Legalized is false iff CCCode is Legal. I don't
4150480093f4SDimitry Andric     // understand if this code is useful for strict nodes.
4151480093f4SDimitry Andric     assert(!IsStrict && "Don't know how to expand for strict nodes.");
4152480093f4SDimitry Andric 
41530b57cec5SDimitry Andric     // Otherwise, SETCC for the given comparison type must be completely
41540b57cec5SDimitry Andric     // illegal; expand it into a SELECT_CC.
415581ad6265SDimitry Andric     // FIXME: This drops the mask/evl for VP_SETCC.
41560b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
415781ad6265SDimitry Andric     EVT Tmp1VT = Tmp1.getValueType();
41580b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2,
415981ad6265SDimitry Andric                        DAG.getBoolConstant(true, dl, VT, Tmp1VT),
416081ad6265SDimitry Andric                        DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3);
41610b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
41620b57cec5SDimitry Andric     Results.push_back(Tmp1);
41630b57cec5SDimitry Andric     break;
41640b57cec5SDimitry Andric   }
41650b57cec5SDimitry Andric   case ISD::SELECT_CC: {
4166480093f4SDimitry Andric     // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS
41670b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);   // LHS
41680b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);   // RHS
41690b57cec5SDimitry Andric     Tmp3 = Node->getOperand(2);   // True
41700b57cec5SDimitry Andric     Tmp4 = Node->getOperand(3);   // False
41710b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
4172480093f4SDimitry Andric     SDValue Chain;
41730b57cec5SDimitry Andric     SDValue CC = Node->getOperand(4);
41740b57cec5SDimitry Andric     ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get();
41750b57cec5SDimitry Andric 
41760b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) {
41770b57cec5SDimitry Andric       // If the condition code is legal, then we need to expand this
41780b57cec5SDimitry Andric       // node using SETCC and SELECT.
41790b57cec5SDimitry Andric       EVT CmpVT = Tmp1.getValueType();
41800b57cec5SDimitry Andric       assert(!TLI.isOperationExpand(ISD::SELECT, VT) &&
41810b57cec5SDimitry Andric              "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be "
41820b57cec5SDimitry Andric              "expanded.");
41830b57cec5SDimitry Andric       EVT CCVT = getSetCCResultType(CmpVT);
41840b57cec5SDimitry Andric       SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags());
4185*0fca6ea1SDimitry Andric       Results.push_back(
4186*0fca6ea1SDimitry Andric           DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4, Node->getFlags()));
41870b57cec5SDimitry Andric       break;
41880b57cec5SDimitry Andric     }
41890b57cec5SDimitry Andric 
41900b57cec5SDimitry Andric     // SELECT_CC is legal, so the condition code must not be.
41910b57cec5SDimitry Andric     bool Legalized = false;
41920b57cec5SDimitry Andric     // Try to legalize by inverting the condition.  This is for targets that
41930b57cec5SDimitry Andric     // might support an ordered version of a condition, but not the unordered
41940b57cec5SDimitry Andric     // version (or vice versa).
4195480093f4SDimitry Andric     ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType());
41960b57cec5SDimitry Andric     if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) {
41970b57cec5SDimitry Andric       // Use the new condition code and swap true and false
41980b57cec5SDimitry Andric       Legalized = true;
41990b57cec5SDimitry Andric       Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC);
42000b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
42010b57cec5SDimitry Andric     } else {
42020b57cec5SDimitry Andric       // If The inverse is not legal, then try to swap the arguments using
42030b57cec5SDimitry Andric       // the inverse condition code.
42040b57cec5SDimitry Andric       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC);
42050b57cec5SDimitry Andric       if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) {
42060b57cec5SDimitry Andric         // The swapped inverse condition is legal, so swap true and false,
42070b57cec5SDimitry Andric         // lhs and rhs.
42080b57cec5SDimitry Andric         Legalized = true;
42090b57cec5SDimitry Andric         Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC);
42100b57cec5SDimitry Andric         Tmp1->setFlags(Node->getFlags());
42110b57cec5SDimitry Andric       }
42120b57cec5SDimitry Andric     }
42130b57cec5SDimitry Andric 
42140b57cec5SDimitry Andric     if (!Legalized) {
4215fe6060f1SDimitry Andric       Legalized = TLI.LegalizeSetCCCondCode(
4216fe6060f1SDimitry Andric           DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC,
421781ad6265SDimitry Andric           /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
42180b57cec5SDimitry Andric 
42190b57cec5SDimitry Andric       assert(Legalized && "Can't legalize SELECT_CC with legal condition!");
42200b57cec5SDimitry Andric 
42210b57cec5SDimitry Andric       // If we expanded the SETCC by inverting the condition code, then swap
42220b57cec5SDimitry Andric       // the True/False operands to match.
42230b57cec5SDimitry Andric       if (NeedInvert)
42240b57cec5SDimitry Andric         std::swap(Tmp3, Tmp4);
42250b57cec5SDimitry Andric 
42260b57cec5SDimitry Andric       // If we expanded the SETCC by swapping LHS and RHS, or by inverting the
42270b57cec5SDimitry Andric       // condition code, create a new SELECT_CC node.
42280b57cec5SDimitry Andric       if (CC.getNode()) {
42290b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0),
42300b57cec5SDimitry Andric                            Tmp1, Tmp2, Tmp3, Tmp4, CC);
42310b57cec5SDimitry Andric       } else {
42320b57cec5SDimitry Andric         Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType());
42330b57cec5SDimitry Andric         CC = DAG.getCondCode(ISD::SETNE);
42340b57cec5SDimitry Andric         Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1,
42350b57cec5SDimitry Andric                            Tmp2, Tmp3, Tmp4, CC);
42360b57cec5SDimitry Andric       }
42370b57cec5SDimitry Andric       Tmp1->setFlags(Node->getFlags());
42380b57cec5SDimitry Andric     }
42390b57cec5SDimitry Andric     Results.push_back(Tmp1);
42400b57cec5SDimitry Andric     break;
42410b57cec5SDimitry Andric   }
42420b57cec5SDimitry Andric   case ISD::BR_CC: {
4243480093f4SDimitry Andric     // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS
4244480093f4SDimitry Andric     SDValue Chain;
42450b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);              // Chain
42460b57cec5SDimitry Andric     Tmp2 = Node->getOperand(2);              // LHS
42470b57cec5SDimitry Andric     Tmp3 = Node->getOperand(3);              // RHS
42480b57cec5SDimitry Andric     Tmp4 = Node->getOperand(1);              // CC
42490b57cec5SDimitry Andric 
425081ad6265SDimitry Andric     bool Legalized = TLI.LegalizeSetCCCondCode(
425181ad6265SDimitry Andric         DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4,
425281ad6265SDimitry Andric         /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain);
42530b57cec5SDimitry Andric     (void)Legalized;
42540b57cec5SDimitry Andric     assert(Legalized && "Can't legalize BR_CC with legal condition!");
42550b57cec5SDimitry Andric 
42560b57cec5SDimitry Andric     // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC
42570b57cec5SDimitry Andric     // node.
42580b57cec5SDimitry Andric     if (Tmp4.getNode()) {
4259e8d8bef9SDimitry Andric       assert(!NeedInvert && "Don't know how to invert BR_CC!");
4260e8d8bef9SDimitry Andric 
42610b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1,
42620b57cec5SDimitry Andric                          Tmp4, Tmp2, Tmp3, Node->getOperand(4));
42630b57cec5SDimitry Andric     } else {
42640b57cec5SDimitry Andric       Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType());
4265e8d8bef9SDimitry Andric       Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE);
42660b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4,
42670b57cec5SDimitry Andric                          Tmp2, Tmp3, Node->getOperand(4));
42680b57cec5SDimitry Andric     }
42690b57cec5SDimitry Andric     Results.push_back(Tmp1);
42700b57cec5SDimitry Andric     break;
42710b57cec5SDimitry Andric   }
42720b57cec5SDimitry Andric   case ISD::BUILD_VECTOR:
42730b57cec5SDimitry Andric     Results.push_back(ExpandBUILD_VECTOR(Node));
42740b57cec5SDimitry Andric     break;
42758bcb0991SDimitry Andric   case ISD::SPLAT_VECTOR:
42768bcb0991SDimitry Andric     Results.push_back(ExpandSPLAT_VECTOR(Node));
42778bcb0991SDimitry Andric     break;
42780b57cec5SDimitry Andric   case ISD::SRA:
42790b57cec5SDimitry Andric   case ISD::SRL:
42800b57cec5SDimitry Andric   case ISD::SHL: {
42810b57cec5SDimitry Andric     // Scalarize vector SRA/SRL/SHL.
42820b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
42830b57cec5SDimitry Andric     assert(VT.isVector() && "Unable to legalize non-vector shift");
42840b57cec5SDimitry Andric     assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal");
42850b57cec5SDimitry Andric     unsigned NumElem = VT.getVectorNumElements();
42860b57cec5SDimitry Andric 
42870b57cec5SDimitry Andric     SmallVector<SDValue, 8> Scalars;
42880b57cec5SDimitry Andric     for (unsigned Idx = 0; Idx < NumElem; Idx++) {
42895ffd83dbSDimitry Andric       SDValue Ex =
42905ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
42915ffd83dbSDimitry Andric                       Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl));
42925ffd83dbSDimitry Andric       SDValue Sh =
42935ffd83dbSDimitry Andric           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(),
42945ffd83dbSDimitry Andric                       Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl));
42950b57cec5SDimitry Andric       Scalars.push_back(DAG.getNode(Node->getOpcode(), dl,
42960b57cec5SDimitry Andric                                     VT.getScalarType(), Ex, Sh));
42970b57cec5SDimitry Andric     }
42980b57cec5SDimitry Andric 
42990b57cec5SDimitry Andric     SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars);
4300480093f4SDimitry Andric     Results.push_back(Result);
43010b57cec5SDimitry Andric     break;
43020b57cec5SDimitry Andric   }
43030b57cec5SDimitry Andric   case ISD::VECREDUCE_FADD:
43040b57cec5SDimitry Andric   case ISD::VECREDUCE_FMUL:
43050b57cec5SDimitry Andric   case ISD::VECREDUCE_ADD:
43060b57cec5SDimitry Andric   case ISD::VECREDUCE_MUL:
43070b57cec5SDimitry Andric   case ISD::VECREDUCE_AND:
43080b57cec5SDimitry Andric   case ISD::VECREDUCE_OR:
43090b57cec5SDimitry Andric   case ISD::VECREDUCE_XOR:
43100b57cec5SDimitry Andric   case ISD::VECREDUCE_SMAX:
43110b57cec5SDimitry Andric   case ISD::VECREDUCE_SMIN:
43120b57cec5SDimitry Andric   case ISD::VECREDUCE_UMAX:
43130b57cec5SDimitry Andric   case ISD::VECREDUCE_UMIN:
43140b57cec5SDimitry Andric   case ISD::VECREDUCE_FMAX:
43150b57cec5SDimitry Andric   case ISD::VECREDUCE_FMIN:
431606c3fb27SDimitry Andric   case ISD::VECREDUCE_FMAXIMUM:
431706c3fb27SDimitry Andric   case ISD::VECREDUCE_FMINIMUM:
43180b57cec5SDimitry Andric     Results.push_back(TLI.expandVecReduce(Node, DAG));
43190b57cec5SDimitry Andric     break;
4320*0fca6ea1SDimitry Andric   case ISD::VP_CTTZ_ELTS:
4321*0fca6ea1SDimitry Andric   case ISD::VP_CTTZ_ELTS_ZERO_UNDEF:
4322*0fca6ea1SDimitry Andric     Results.push_back(TLI.expandVPCTTZElements(Node, DAG));
4323*0fca6ea1SDimitry Andric     break;
4324*0fca6ea1SDimitry Andric   case ISD::CLEAR_CACHE:
4325*0fca6ea1SDimitry Andric     // The default expansion of llvm.clear_cache is simply a no-op for those
4326*0fca6ea1SDimitry Andric     // targets where it is not needed.
4327*0fca6ea1SDimitry Andric     Results.push_back(Node->getOperand(0));
4328*0fca6ea1SDimitry Andric     break;
43290b57cec5SDimitry Andric   case ISD::GLOBAL_OFFSET_TABLE:
43300b57cec5SDimitry Andric   case ISD::GlobalAddress:
43310b57cec5SDimitry Andric   case ISD::GlobalTLSAddress:
43320b57cec5SDimitry Andric   case ISD::ExternalSymbol:
43330b57cec5SDimitry Andric   case ISD::ConstantPool:
43340b57cec5SDimitry Andric   case ISD::JumpTable:
43350b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
43360b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
43370b57cec5SDimitry Andric   case ISD::INTRINSIC_VOID:
43380b57cec5SDimitry Andric     // FIXME: Custom lowering for these operations shouldn't return null!
4339480093f4SDimitry Andric     // Return true so that we don't call ConvertNodeToLibcall which also won't
4340480093f4SDimitry Andric     // do anything.
4341480093f4SDimitry Andric     return true;
43420b57cec5SDimitry Andric   }
43430b57cec5SDimitry Andric 
4344480093f4SDimitry Andric   if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) {
43458bcb0991SDimitry Andric     // FIXME: We were asked to expand a strict floating-point operation,
43468bcb0991SDimitry Andric     // but there is currently no expansion implemented that would preserve
43478bcb0991SDimitry Andric     // the "strict" properties.  For now, we just fall back to the non-strict
43488bcb0991SDimitry Andric     // version if that is legal on the target.  The actual mutation of the
43498bcb0991SDimitry Andric     // operation will happen in SelectionDAGISel::DoInstructionSelection.
43508bcb0991SDimitry Andric     switch (Node->getOpcode()) {
43518bcb0991SDimitry Andric     default:
43528bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
43538bcb0991SDimitry Andric                                          Node->getValueType(0))
43548bcb0991SDimitry Andric           == TargetLowering::Legal)
43558bcb0991SDimitry Andric         return true;
43568bcb0991SDimitry Andric       break;
4357e8d8bef9SDimitry Andric     case ISD::STRICT_FSUB: {
4358e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4359e8d8bef9SDimitry Andric               ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal)
4360e8d8bef9SDimitry Andric         return true;
4361e8d8bef9SDimitry Andric       if (TLI.getStrictFPOperationAction(
4362e8d8bef9SDimitry Andric               ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal)
4363e8d8bef9SDimitry Andric         break;
4364e8d8bef9SDimitry Andric 
4365e8d8bef9SDimitry Andric       EVT VT = Node->getValueType(0);
4366e8d8bef9SDimitry Andric       const SDNodeFlags Flags = Node->getFlags();
4367e8d8bef9SDimitry Andric       SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags);
4368e8d8bef9SDimitry Andric       SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(),
4369e8d8bef9SDimitry Andric                                  {Node->getOperand(0), Node->getOperand(1), Neg},
4370e8d8bef9SDimitry Andric                          Flags);
4371e8d8bef9SDimitry Andric 
4372e8d8bef9SDimitry Andric       Results.push_back(Fadd);
4373e8d8bef9SDimitry Andric       Results.push_back(Fadd.getValue(1));
4374e8d8bef9SDimitry Andric       break;
4375e8d8bef9SDimitry Andric     }
4376e8d8bef9SDimitry Andric     case ISD::STRICT_SINT_TO_FP:
4377e8d8bef9SDimitry Andric     case ISD::STRICT_UINT_TO_FP:
43788bcb0991SDimitry Andric     case ISD::STRICT_LRINT:
43798bcb0991SDimitry Andric     case ISD::STRICT_LLRINT:
43808bcb0991SDimitry Andric     case ISD::STRICT_LROUND:
43818bcb0991SDimitry Andric     case ISD::STRICT_LLROUND:
43828bcb0991SDimitry Andric       // These are registered by the operand type instead of the value
43838bcb0991SDimitry Andric       // type. Reflect that here.
43848bcb0991SDimitry Andric       if (TLI.getStrictFPOperationAction(Node->getOpcode(),
43858bcb0991SDimitry Andric                                          Node->getOperand(1).getValueType())
43868bcb0991SDimitry Andric           == TargetLowering::Legal)
43878bcb0991SDimitry Andric         return true;
43888bcb0991SDimitry Andric       break;
43898bcb0991SDimitry Andric     }
43908bcb0991SDimitry Andric   }
43918bcb0991SDimitry Andric 
43920b57cec5SDimitry Andric   // Replace the original node with the legalized result.
43930b57cec5SDimitry Andric   if (Results.empty()) {
43940b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Cannot expand node\n");
43950b57cec5SDimitry Andric     return false;
43960b57cec5SDimitry Andric   }
43970b57cec5SDimitry Andric 
43980b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Successfully expanded node\n");
43990b57cec5SDimitry Andric   ReplaceNode(Node, Results.data());
44000b57cec5SDimitry Andric   return true;
44010b57cec5SDimitry Andric }
44020b57cec5SDimitry Andric 
44030b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
44040b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n");
44050b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
44060b57cec5SDimitry Andric   SDLoc dl(Node);
44070b57cec5SDimitry Andric   // FIXME: Check flags on the node to see if we can use a finite call.
44080b57cec5SDimitry Andric   unsigned Opc = Node->getOpcode();
44090b57cec5SDimitry Andric   switch (Opc) {
44100b57cec5SDimitry Andric   case ISD::ATOMIC_FENCE: {
44110b57cec5SDimitry Andric     // If the target didn't lower this, lower it to '__sync_synchronize()' call
44120b57cec5SDimitry Andric     // FIXME: handle "fence singlethread" more efficiently.
44130b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
44140b57cec5SDimitry Andric 
44150b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
44160b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
44170b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
44180b57cec5SDimitry Andric         .setLibCallee(
44190b57cec5SDimitry Andric             CallingConv::C, Type::getVoidTy(*DAG.getContext()),
44200b57cec5SDimitry Andric             DAG.getExternalSymbol("__sync_synchronize",
44210b57cec5SDimitry Andric                                   TLI.getPointerTy(DAG.getDataLayout())),
44220b57cec5SDimitry Andric             std::move(Args));
44230b57cec5SDimitry Andric 
44240b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
44250b57cec5SDimitry Andric 
44260b57cec5SDimitry Andric     Results.push_back(CallResult.second);
44270b57cec5SDimitry Andric     break;
44280b57cec5SDimitry Andric   }
44290b57cec5SDimitry Andric   // By default, atomic intrinsics are marked Legal and lowered. Targets
44300b57cec5SDimitry Andric   // which don't support them directly, however, may want libcalls, in which
44310b57cec5SDimitry Andric   // case they mark them Expand, and we get here.
44320b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP:
44330b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_ADD:
44340b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_SUB:
44350b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_AND:
44360b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_CLR:
44370b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_OR:
44380b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_XOR:
44390b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_NAND:
44400b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MIN:
44410b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MAX:
44420b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMIN:
44430b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMAX:
44440b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP: {
44450b57cec5SDimitry Andric     MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT();
4446fe6060f1SDimitry Andric     AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering();
4447e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT);
4448480093f4SDimitry Andric     EVT RetVT = Node->getValueType(0);
4449480093f4SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4450e8d8bef9SDimitry Andric     SmallVector<SDValue, 4> Ops;
4451e8d8bef9SDimitry Andric     if (TLI.getLibcallName(LC)) {
4452e8d8bef9SDimitry Andric       // If outline atomic available, prepare its arguments and expand.
4453e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 2, Node->op_end());
4454e8d8bef9SDimitry Andric       Ops.push_back(Node->getOperand(1));
4455e8d8bef9SDimitry Andric 
4456e8d8bef9SDimitry Andric     } else {
4457e8d8bef9SDimitry Andric       LC = RTLIB::getSYNC(Opc, VT);
4458e8d8bef9SDimitry Andric       assert(LC != RTLIB::UNKNOWN_LIBCALL &&
4459e8d8bef9SDimitry Andric              "Unexpected atomic op or value type!");
4460e8d8bef9SDimitry Andric       // Arguments for expansion to sync libcall
4461e8d8bef9SDimitry Andric       Ops.append(Node->op_begin() + 1, Node->op_end());
4462e8d8bef9SDimitry Andric     }
4463480093f4SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT,
4464480093f4SDimitry Andric                                                       Ops, CallOptions,
4465480093f4SDimitry Andric                                                       SDLoc(Node),
4466480093f4SDimitry Andric                                                       Node->getOperand(0));
44670b57cec5SDimitry Andric     Results.push_back(Tmp.first);
44680b57cec5SDimitry Andric     Results.push_back(Tmp.second);
44690b57cec5SDimitry Andric     break;
44700b57cec5SDimitry Andric   }
44710b57cec5SDimitry Andric   case ISD::TRAP: {
44720b57cec5SDimitry Andric     // If this operation is not supported, lower it to 'abort()' call
44730b57cec5SDimitry Andric     TargetLowering::ArgListTy Args;
44740b57cec5SDimitry Andric     TargetLowering::CallLoweringInfo CLI(DAG);
44750b57cec5SDimitry Andric     CLI.setDebugLoc(dl)
44760b57cec5SDimitry Andric         .setChain(Node->getOperand(0))
44770b57cec5SDimitry Andric         .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
44780b57cec5SDimitry Andric                       DAG.getExternalSymbol(
44790b57cec5SDimitry Andric                           "abort", TLI.getPointerTy(DAG.getDataLayout())),
44800b57cec5SDimitry Andric                       std::move(Args));
44810b57cec5SDimitry Andric     std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
44820b57cec5SDimitry Andric 
44830b57cec5SDimitry Andric     Results.push_back(CallResult.second);
44840b57cec5SDimitry Andric     break;
44850b57cec5SDimitry Andric   }
4486*0fca6ea1SDimitry Andric   case ISD::CLEAR_CACHE: {
4487*0fca6ea1SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4488*0fca6ea1SDimitry Andric     SDValue InputChain = Node->getOperand(0);
4489*0fca6ea1SDimitry Andric     SDValue StartVal = Node->getOperand(1);
4490*0fca6ea1SDimitry Andric     SDValue EndVal = Node->getOperand(2);
4491*0fca6ea1SDimitry Andric     std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4492*0fca6ea1SDimitry Andric         DAG, RTLIB::CLEAR_CACHE, MVT::isVoid, {StartVal, EndVal}, CallOptions,
4493*0fca6ea1SDimitry Andric         SDLoc(Node), InputChain);
4494*0fca6ea1SDimitry Andric     Results.push_back(Tmp.second);
4495*0fca6ea1SDimitry Andric     break;
4496*0fca6ea1SDimitry Andric   }
44970b57cec5SDimitry Andric   case ISD::FMINNUM:
44980b57cec5SDimitry Andric   case ISD::STRICT_FMINNUM:
4499480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64,
45000b57cec5SDimitry Andric                     RTLIB::FMIN_F80, RTLIB::FMIN_F128,
4501480093f4SDimitry Andric                     RTLIB::FMIN_PPCF128, Results);
45020b57cec5SDimitry Andric     break;
450306c3fb27SDimitry Andric   // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use
450406c3fb27SDimitry Andric   // libcall legalization for these nodes, but there is no default expasion for
450506c3fb27SDimitry Andric   // these nodes either (see PR63267 for example).
45060b57cec5SDimitry Andric   case ISD::FMAXNUM:
45070b57cec5SDimitry Andric   case ISD::STRICT_FMAXNUM:
4508480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64,
45090b57cec5SDimitry Andric                     RTLIB::FMAX_F80, RTLIB::FMAX_F128,
4510480093f4SDimitry Andric                     RTLIB::FMAX_PPCF128, Results);
45110b57cec5SDimitry Andric     break;
45120b57cec5SDimitry Andric   case ISD::FSQRT:
45130b57cec5SDimitry Andric   case ISD::STRICT_FSQRT:
4514480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
45150b57cec5SDimitry Andric                     RTLIB::SQRT_F80, RTLIB::SQRT_F128,
4516480093f4SDimitry Andric                     RTLIB::SQRT_PPCF128, Results);
45170b57cec5SDimitry Andric     break;
45180b57cec5SDimitry Andric   case ISD::FCBRT:
4519480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64,
45200b57cec5SDimitry Andric                     RTLIB::CBRT_F80, RTLIB::CBRT_F128,
4521480093f4SDimitry Andric                     RTLIB::CBRT_PPCF128, Results);
45220b57cec5SDimitry Andric     break;
45230b57cec5SDimitry Andric   case ISD::FSIN:
45240b57cec5SDimitry Andric   case ISD::STRICT_FSIN:
4525480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64,
45260b57cec5SDimitry Andric                     RTLIB::SIN_F80, RTLIB::SIN_F128,
4527480093f4SDimitry Andric                     RTLIB::SIN_PPCF128, Results);
45280b57cec5SDimitry Andric     break;
45290b57cec5SDimitry Andric   case ISD::FCOS:
45300b57cec5SDimitry Andric   case ISD::STRICT_FCOS:
4531480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64,
45320b57cec5SDimitry Andric                     RTLIB::COS_F80, RTLIB::COS_F128,
4533480093f4SDimitry Andric                     RTLIB::COS_PPCF128, Results);
45340b57cec5SDimitry Andric     break;
4535*0fca6ea1SDimitry Andric   case ISD::FTAN:
4536*0fca6ea1SDimitry Andric   case ISD::STRICT_FTAN:
4537*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::TAN_F32, RTLIB::TAN_F64, RTLIB::TAN_F80,
4538*0fca6ea1SDimitry Andric                     RTLIB::TAN_F128, RTLIB::TAN_PPCF128, Results);
4539*0fca6ea1SDimitry Andric     break;
4540*0fca6ea1SDimitry Andric   case ISD::FASIN:
4541*0fca6ea1SDimitry Andric   case ISD::STRICT_FASIN:
4542*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ASIN_F32, RTLIB::ASIN_F64, RTLIB::ASIN_F80,
4543*0fca6ea1SDimitry Andric                     RTLIB::ASIN_F128, RTLIB::ASIN_PPCF128, Results);
4544*0fca6ea1SDimitry Andric     break;
4545*0fca6ea1SDimitry Andric   case ISD::FACOS:
4546*0fca6ea1SDimitry Andric   case ISD::STRICT_FACOS:
4547*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ACOS_F32, RTLIB::ACOS_F64, RTLIB::ACOS_F80,
4548*0fca6ea1SDimitry Andric                     RTLIB::ACOS_F128, RTLIB::ACOS_PPCF128, Results);
4549*0fca6ea1SDimitry Andric     break;
4550*0fca6ea1SDimitry Andric   case ISD::FATAN:
4551*0fca6ea1SDimitry Andric   case ISD::STRICT_FATAN:
4552*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
4553*0fca6ea1SDimitry Andric                     RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
4554*0fca6ea1SDimitry Andric     break;
4555*0fca6ea1SDimitry Andric   case ISD::FSINH:
4556*0fca6ea1SDimitry Andric   case ISD::STRICT_FSINH:
4557*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
4558*0fca6ea1SDimitry Andric                     RTLIB::SINH_F128, RTLIB::SINH_PPCF128, Results);
4559*0fca6ea1SDimitry Andric     break;
4560*0fca6ea1SDimitry Andric   case ISD::FCOSH:
4561*0fca6ea1SDimitry Andric   case ISD::STRICT_FCOSH:
4562*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::COSH_F32, RTLIB::COSH_F64, RTLIB::COSH_F80,
4563*0fca6ea1SDimitry Andric                     RTLIB::COSH_F128, RTLIB::COSH_PPCF128, Results);
4564*0fca6ea1SDimitry Andric     break;
4565*0fca6ea1SDimitry Andric   case ISD::FTANH:
4566*0fca6ea1SDimitry Andric   case ISD::STRICT_FTANH:
4567*0fca6ea1SDimitry Andric     ExpandFPLibCall(Node, RTLIB::TANH_F32, RTLIB::TANH_F64, RTLIB::TANH_F80,
4568*0fca6ea1SDimitry Andric                     RTLIB::TANH_F128, RTLIB::TANH_PPCF128, Results);
4569*0fca6ea1SDimitry Andric     break;
45700b57cec5SDimitry Andric   case ISD::FSINCOS:
45710b57cec5SDimitry Andric     // Expand into sincos libcall.
45720b57cec5SDimitry Andric     ExpandSinCosLibCall(Node, Results);
45730b57cec5SDimitry Andric     break;
45740b57cec5SDimitry Andric   case ISD::FLOG:
45750b57cec5SDimitry Andric   case ISD::STRICT_FLOG:
45768c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80,
45778c27c554SDimitry Andric                     RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results);
45780b57cec5SDimitry Andric     break;
45790b57cec5SDimitry Andric   case ISD::FLOG2:
45800b57cec5SDimitry Andric   case ISD::STRICT_FLOG2:
45818c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80,
45828c27c554SDimitry Andric                     RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results);
45830b57cec5SDimitry Andric     break;
45840b57cec5SDimitry Andric   case ISD::FLOG10:
45850b57cec5SDimitry Andric   case ISD::STRICT_FLOG10:
45868c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80,
45878c27c554SDimitry Andric                     RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results);
45880b57cec5SDimitry Andric     break;
45890b57cec5SDimitry Andric   case ISD::FEXP:
45900b57cec5SDimitry Andric   case ISD::STRICT_FEXP:
45918c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80,
45928c27c554SDimitry Andric                     RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results);
45930b57cec5SDimitry Andric     break;
45940b57cec5SDimitry Andric   case ISD::FEXP2:
45950b57cec5SDimitry Andric   case ISD::STRICT_FEXP2:
45968c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80,
45978c27c554SDimitry Andric                     RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results);
45980b57cec5SDimitry Andric     break;
45995f757f3fSDimitry Andric   case ISD::FEXP10:
46005f757f3fSDimitry Andric     ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80,
46015f757f3fSDimitry Andric                     RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results);
46025f757f3fSDimitry Andric     break;
46030b57cec5SDimitry Andric   case ISD::FTRUNC:
46040b57cec5SDimitry Andric   case ISD::STRICT_FTRUNC:
4605480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
46060b57cec5SDimitry Andric                     RTLIB::TRUNC_F80, RTLIB::TRUNC_F128,
4607480093f4SDimitry Andric                     RTLIB::TRUNC_PPCF128, Results);
46080b57cec5SDimitry Andric     break;
46090b57cec5SDimitry Andric   case ISD::FFLOOR:
46100b57cec5SDimitry Andric   case ISD::STRICT_FFLOOR:
4611480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
46120b57cec5SDimitry Andric                     RTLIB::FLOOR_F80, RTLIB::FLOOR_F128,
4613480093f4SDimitry Andric                     RTLIB::FLOOR_PPCF128, Results);
46140b57cec5SDimitry Andric     break;
46150b57cec5SDimitry Andric   case ISD::FCEIL:
46160b57cec5SDimitry Andric   case ISD::STRICT_FCEIL:
4617480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
46180b57cec5SDimitry Andric                     RTLIB::CEIL_F80, RTLIB::CEIL_F128,
4619480093f4SDimitry Andric                     RTLIB::CEIL_PPCF128, Results);
46200b57cec5SDimitry Andric     break;
46210b57cec5SDimitry Andric   case ISD::FRINT:
46220b57cec5SDimitry Andric   case ISD::STRICT_FRINT:
4623480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64,
46240b57cec5SDimitry Andric                     RTLIB::RINT_F80, RTLIB::RINT_F128,
4625480093f4SDimitry Andric                     RTLIB::RINT_PPCF128, Results);
46260b57cec5SDimitry Andric     break;
46270b57cec5SDimitry Andric   case ISD::FNEARBYINT:
46280b57cec5SDimitry Andric   case ISD::STRICT_FNEARBYINT:
4629480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32,
46300b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F64,
46310b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F80,
46320b57cec5SDimitry Andric                     RTLIB::NEARBYINT_F128,
4633480093f4SDimitry Andric                     RTLIB::NEARBYINT_PPCF128, Results);
46340b57cec5SDimitry Andric     break;
46350b57cec5SDimitry Andric   case ISD::FROUND:
46360b57cec5SDimitry Andric   case ISD::STRICT_FROUND:
4637480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUND_F32,
46380b57cec5SDimitry Andric                     RTLIB::ROUND_F64,
46390b57cec5SDimitry Andric                     RTLIB::ROUND_F80,
46400b57cec5SDimitry Andric                     RTLIB::ROUND_F128,
4641480093f4SDimitry Andric                     RTLIB::ROUND_PPCF128, Results);
46420b57cec5SDimitry Andric     break;
46435ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
46445ffd83dbSDimitry Andric   case ISD::STRICT_FROUNDEVEN:
46455ffd83dbSDimitry Andric     ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32,
46465ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F64,
46475ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F80,
46485ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_F128,
46495ffd83dbSDimitry Andric                     RTLIB::ROUNDEVEN_PPCF128, Results);
46505ffd83dbSDimitry Andric     break;
465106c3fb27SDimitry Andric   case ISD::FLDEXP:
465206c3fb27SDimitry Andric   case ISD::STRICT_FLDEXP:
465306c3fb27SDimitry Andric     ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80,
465406c3fb27SDimitry Andric                     RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results);
465506c3fb27SDimitry Andric     break;
465606c3fb27SDimitry Andric   case ISD::FFREXP: {
465706c3fb27SDimitry Andric     ExpandFrexpLibCall(Node, Results);
465806c3fb27SDimitry Andric     break;
465906c3fb27SDimitry Andric   }
46600b57cec5SDimitry Andric   case ISD::FPOWI:
4661480093f4SDimitry Andric   case ISD::STRICT_FPOWI: {
4662fe6060f1SDimitry Andric     RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0));
4663fe6060f1SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi.");
4664480093f4SDimitry Andric     if (!TLI.getLibcallName(LC)) {
4665480093f4SDimitry Andric       // Some targets don't have a powi libcall; use pow instead.
466681ad6265SDimitry Andric       if (Node->isStrictFPOpcode()) {
466781ad6265SDimitry Andric         SDValue Exponent =
466881ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node),
466981ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
467081ad6265SDimitry Andric                         {Node->getOperand(0), Node->getOperand(2)});
467181ad6265SDimitry Andric         SDValue FPOW =
467281ad6265SDimitry Andric             DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node),
467381ad6265SDimitry Andric                         {Node->getValueType(0), Node->getValueType(1)},
467481ad6265SDimitry Andric                         {Exponent.getValue(1), Node->getOperand(1), Exponent});
467581ad6265SDimitry Andric         Results.push_back(FPOW);
467681ad6265SDimitry Andric         Results.push_back(FPOW.getValue(1));
467781ad6265SDimitry Andric       } else {
467881ad6265SDimitry Andric         SDValue Exponent =
467981ad6265SDimitry Andric             DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0),
4680480093f4SDimitry Andric                         Node->getOperand(1));
4681480093f4SDimitry Andric         Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node),
468281ad6265SDimitry Andric                                       Node->getValueType(0),
468381ad6265SDimitry Andric                                       Node->getOperand(0), Exponent));
468481ad6265SDimitry Andric       }
46850b57cec5SDimitry Andric       break;
4686480093f4SDimitry Andric     }
4687fe6060f1SDimitry Andric     unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0;
4688fe6060f1SDimitry Andric     bool ExponentHasSizeOfInt =
4689fe6060f1SDimitry Andric         DAG.getLibInfo().getIntSize() ==
4690fe6060f1SDimitry Andric         Node->getOperand(1 + Offset).getValueType().getSizeInBits();
4691fe6060f1SDimitry Andric     if (!ExponentHasSizeOfInt) {
4692fe6060f1SDimitry Andric       // If the exponent does not match with sizeof(int) a libcall to
4693fe6060f1SDimitry Andric       // RTLIB::POWI would use the wrong type for the argument.
4694fe6060f1SDimitry Andric       DAG.getContext()->emitError("POWI exponent does not match sizeof(int)");
4695fe6060f1SDimitry Andric       Results.push_back(DAG.getUNDEF(Node->getValueType(0)));
4696fe6060f1SDimitry Andric       break;
4697fe6060f1SDimitry Andric     }
4698fe6060f1SDimitry Andric     ExpandFPLibCall(Node, LC, Results);
4699480093f4SDimitry Andric     break;
4700480093f4SDimitry Andric   }
47010b57cec5SDimitry Andric   case ISD::FPOW:
47020b57cec5SDimitry Andric   case ISD::STRICT_FPOW:
47038c27c554SDimitry Andric     ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
47048c27c554SDimitry Andric                     RTLIB::POW_F128, RTLIB::POW_PPCF128, Results);
47050b57cec5SDimitry Andric     break;
47068bcb0991SDimitry Andric   case ISD::LROUND:
47078bcb0991SDimitry Andric   case ISD::STRICT_LROUND:
4708480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LROUND_F32,
47098bcb0991SDimitry Andric                        RTLIB::LROUND_F64, RTLIB::LROUND_F80,
47108bcb0991SDimitry Andric                        RTLIB::LROUND_F128,
4711480093f4SDimitry Andric                        RTLIB::LROUND_PPCF128, Results);
47128bcb0991SDimitry Andric     break;
47138bcb0991SDimitry Andric   case ISD::LLROUND:
47148bcb0991SDimitry Andric   case ISD::STRICT_LLROUND:
4715480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32,
47168bcb0991SDimitry Andric                        RTLIB::LLROUND_F64, RTLIB::LLROUND_F80,
47178bcb0991SDimitry Andric                        RTLIB::LLROUND_F128,
4718480093f4SDimitry Andric                        RTLIB::LLROUND_PPCF128, Results);
47198bcb0991SDimitry Andric     break;
47208bcb0991SDimitry Andric   case ISD::LRINT:
47218bcb0991SDimitry Andric   case ISD::STRICT_LRINT:
4722480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LRINT_F32,
47238bcb0991SDimitry Andric                        RTLIB::LRINT_F64, RTLIB::LRINT_F80,
47248bcb0991SDimitry Andric                        RTLIB::LRINT_F128,
4725480093f4SDimitry Andric                        RTLIB::LRINT_PPCF128, Results);
47268bcb0991SDimitry Andric     break;
47278bcb0991SDimitry Andric   case ISD::LLRINT:
47288bcb0991SDimitry Andric   case ISD::STRICT_LLRINT:
4729480093f4SDimitry Andric     ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32,
47308bcb0991SDimitry Andric                        RTLIB::LLRINT_F64, RTLIB::LLRINT_F80,
47318bcb0991SDimitry Andric                        RTLIB::LLRINT_F128,
4732480093f4SDimitry Andric                        RTLIB::LLRINT_PPCF128, Results);
47338bcb0991SDimitry Andric     break;
47340b57cec5SDimitry Andric   case ISD::FDIV:
4735480093f4SDimitry Andric   case ISD::STRICT_FDIV:
4736480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64,
47370b57cec5SDimitry Andric                     RTLIB::DIV_F80, RTLIB::DIV_F128,
4738480093f4SDimitry Andric                     RTLIB::DIV_PPCF128, Results);
47390b57cec5SDimitry Andric     break;
47400b57cec5SDimitry Andric   case ISD::FREM:
47410b57cec5SDimitry Andric   case ISD::STRICT_FREM:
4742480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64,
47430b57cec5SDimitry Andric                     RTLIB::REM_F80, RTLIB::REM_F128,
4744480093f4SDimitry Andric                     RTLIB::REM_PPCF128, Results);
47450b57cec5SDimitry Andric     break;
47460b57cec5SDimitry Andric   case ISD::FMA:
47470b57cec5SDimitry Andric   case ISD::STRICT_FMA:
4748480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64,
47490b57cec5SDimitry Andric                     RTLIB::FMA_F80, RTLIB::FMA_F128,
4750480093f4SDimitry Andric                     RTLIB::FMA_PPCF128, Results);
47510b57cec5SDimitry Andric     break;
47520b57cec5SDimitry Andric   case ISD::FADD:
4753480093f4SDimitry Andric   case ISD::STRICT_FADD:
4754480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64,
47550b57cec5SDimitry Andric                     RTLIB::ADD_F80, RTLIB::ADD_F128,
4756480093f4SDimitry Andric                     RTLIB::ADD_PPCF128, Results);
47570b57cec5SDimitry Andric     break;
47580b57cec5SDimitry Andric   case ISD::FMUL:
4759480093f4SDimitry Andric   case ISD::STRICT_FMUL:
4760480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64,
47610b57cec5SDimitry Andric                     RTLIB::MUL_F80, RTLIB::MUL_F128,
4762480093f4SDimitry Andric                     RTLIB::MUL_PPCF128, Results);
47630b57cec5SDimitry Andric     break;
47640b57cec5SDimitry Andric   case ISD::FP16_TO_FP:
47650b57cec5SDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
476606c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first);
47670b57cec5SDimitry Andric     }
47680b57cec5SDimitry Andric     break;
4769*0fca6ea1SDimitry Andric   case ISD::STRICT_BF16_TO_FP:
4770*0fca6ea1SDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
4771*0fca6ea1SDimitry Andric       TargetLowering::MakeLibCallOptions CallOptions;
4772*0fca6ea1SDimitry Andric       std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
4773*0fca6ea1SDimitry Andric           DAG, RTLIB::FPEXT_BF16_F32, MVT::f32, Node->getOperand(1),
4774*0fca6ea1SDimitry Andric           CallOptions, SDLoc(Node), Node->getOperand(0));
4775*0fca6ea1SDimitry Andric       Results.push_back(Tmp.first);
4776*0fca6ea1SDimitry Andric       Results.push_back(Tmp.second);
4777*0fca6ea1SDimitry Andric     }
4778*0fca6ea1SDimitry Andric     break;
47795ffd83dbSDimitry Andric   case ISD::STRICT_FP16_TO_FP: {
47805ffd83dbSDimitry Andric     if (Node->getValueType(0) == MVT::f32) {
47815ffd83dbSDimitry Andric       TargetLowering::MakeLibCallOptions CallOptions;
47825ffd83dbSDimitry Andric       std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(
47835ffd83dbSDimitry Andric           DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions,
47845ffd83dbSDimitry Andric           SDLoc(Node), Node->getOperand(0));
47855ffd83dbSDimitry Andric       Results.push_back(Tmp.first);
47865ffd83dbSDimitry Andric       Results.push_back(Tmp.second);
47875ffd83dbSDimitry Andric     }
47885ffd83dbSDimitry Andric     break;
47895ffd83dbSDimitry Andric   }
47900b57cec5SDimitry Andric   case ISD::FP_TO_FP16: {
47910b57cec5SDimitry Andric     RTLIB::Libcall LC =
47920b57cec5SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16);
47930b57cec5SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16");
479406c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
47950b57cec5SDimitry Andric     break;
47960b57cec5SDimitry Andric   }
479781ad6265SDimitry Andric   case ISD::FP_TO_BF16: {
479881ad6265SDimitry Andric     RTLIB::Libcall LC =
479981ad6265SDimitry Andric         RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16);
480081ad6265SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16");
480106c3fb27SDimitry Andric     Results.push_back(ExpandLibCall(LC, Node, false).first);
480281ad6265SDimitry Andric     break;
480381ad6265SDimitry Andric   }
4804e8d8bef9SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
4805e8d8bef9SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
4806e8d8bef9SDimitry Andric   case ISD::SINT_TO_FP:
4807e8d8bef9SDimitry Andric   case ISD::UINT_TO_FP: {
4808e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP
4809e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4810e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::SINT_TO_FP ||
4811e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_SINT_TO_FP;
4812e8d8bef9SDimitry Andric     EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType();
4813e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4814e8d8bef9SDimitry Andric     EVT NVT = EVT();
4815e8d8bef9SDimitry Andric     SDLoc dl(Node);
4816e8d8bef9SDimitry Andric 
4817e8d8bef9SDimitry Andric     // Even if the input is legal, no libcall may exactly match, eg. we don't
4818e8d8bef9SDimitry Andric     // have i1 -> fp conversions. So, it needs to be promoted to a larger type,
4819e8d8bef9SDimitry Andric     // eg: i13 -> fp. Then, look for an appropriate libcall.
4820e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4821e8d8bef9SDimitry Andric     for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE;
4822e8d8bef9SDimitry Andric          t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4823e8d8bef9SDimitry Andric          ++t) {
4824e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)t;
4825e8d8bef9SDimitry Andric       // The source needs to big enough to hold the operand.
4826e8d8bef9SDimitry Andric       if (NVT.bitsGE(SVT))
4827e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT)
4828e8d8bef9SDimitry Andric                     : RTLIB::getUINTTOFP(NVT, RVT);
4829e8d8bef9SDimitry Andric     }
4830e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4831e8d8bef9SDimitry Andric 
4832e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4833e8d8bef9SDimitry Andric     // Sign/zero extend the argument if the libcall takes a larger type.
4834e8d8bef9SDimitry Andric     SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl,
4835e8d8bef9SDimitry Andric                              NVT, Node->getOperand(IsStrict ? 1 : 0));
4836e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4837e8d8bef9SDimitry Andric     CallOptions.setSExt(Signed);
4838e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4839e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain);
4840e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4841e8d8bef9SDimitry Andric     if (IsStrict)
4842e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4843e8d8bef9SDimitry Andric     break;
4844e8d8bef9SDimitry Andric   }
4845e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT:
4846e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT:
4847e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
4848e8d8bef9SDimitry Andric   case ISD::STRICT_FP_TO_UINT: {
4849e8d8bef9SDimitry Andric     // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT.
4850e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4851e8d8bef9SDimitry Andric     bool Signed = Node->getOpcode() == ISD::FP_TO_SINT ||
4852e8d8bef9SDimitry Andric                   Node->getOpcode() == ISD::STRICT_FP_TO_SINT;
4853e8d8bef9SDimitry Andric 
4854e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4855e8d8bef9SDimitry Andric     EVT SVT = Op.getValueType();
4856e8d8bef9SDimitry Andric     EVT RVT = Node->getValueType(0);
4857e8d8bef9SDimitry Andric     EVT NVT = EVT();
4858e8d8bef9SDimitry Andric     SDLoc dl(Node);
4859e8d8bef9SDimitry Andric 
4860e8d8bef9SDimitry Andric     // Even if the result is legal, no libcall may exactly match, eg. we don't
4861e8d8bef9SDimitry Andric     // have fp -> i1 conversions. So, it needs to be promoted to a larger type,
4862e8d8bef9SDimitry Andric     // eg: fp -> i32. Then, look for an appropriate libcall.
4863e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4864e8d8bef9SDimitry Andric     for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE;
4865e8d8bef9SDimitry Andric          IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL;
4866e8d8bef9SDimitry Andric          ++IntVT) {
4867e8d8bef9SDimitry Andric       NVT = (MVT::SimpleValueType)IntVT;
4868e8d8bef9SDimitry Andric       // The type needs to big enough to hold the result.
4869e8d8bef9SDimitry Andric       if (NVT.bitsGE(RVT))
4870e8d8bef9SDimitry Andric         LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT)
4871e8d8bef9SDimitry Andric                     : RTLIB::getFPTOUINT(SVT, NVT);
4872e8d8bef9SDimitry Andric     }
4873e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4874e8d8bef9SDimitry Andric 
4875e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4876e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4877e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4878e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain);
4879e8d8bef9SDimitry Andric 
4880e8d8bef9SDimitry Andric     // Truncate the result if the libcall returns a larger type.
4881e8d8bef9SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first));
4882e8d8bef9SDimitry Andric     if (IsStrict)
4883e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4884e8d8bef9SDimitry Andric     break;
4885e8d8bef9SDimitry Andric   }
4886e8d8bef9SDimitry Andric 
4887e8d8bef9SDimitry Andric   case ISD::FP_ROUND:
4888e8d8bef9SDimitry Andric   case ISD::STRICT_FP_ROUND: {
4889e8d8bef9SDimitry Andric     // X = FP_ROUND(Y, TRUNC)
4890e8d8bef9SDimitry Andric     // TRUNC is a flag, which is always an integer that is zero or one.
4891e8d8bef9SDimitry Andric     // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND
4892e8d8bef9SDimitry Andric     // is known to not change the value of Y.
4893e8d8bef9SDimitry Andric     // We can only expand it into libcall if the TRUNC is 0.
4894e8d8bef9SDimitry Andric     bool IsStrict = Node->isStrictFPOpcode();
4895e8d8bef9SDimitry Andric     SDValue Op = Node->getOperand(IsStrict ? 1 : 0);
4896e8d8bef9SDimitry Andric     SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue();
4897e8d8bef9SDimitry Andric     EVT VT = Node->getValueType(0);
4898349cc55cSDimitry Andric     assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() &&
4899e8d8bef9SDimitry Andric            "Unable to expand as libcall if it is not normal rounding");
4900e8d8bef9SDimitry Andric 
4901e8d8bef9SDimitry Andric     RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT);
4902e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4903e8d8bef9SDimitry Andric 
4904e8d8bef9SDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
4905e8d8bef9SDimitry Andric     std::pair<SDValue, SDValue> Tmp =
4906e8d8bef9SDimitry Andric         TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain);
4907e8d8bef9SDimitry Andric     Results.push_back(Tmp.first);
4908e8d8bef9SDimitry Andric     if (IsStrict)
4909e8d8bef9SDimitry Andric       Results.push_back(Tmp.second);
4910e8d8bef9SDimitry Andric     break;
4911e8d8bef9SDimitry Andric   }
4912e8d8bef9SDimitry Andric   case ISD::FP_EXTEND: {
4913e8d8bef9SDimitry Andric     Results.push_back(
4914e8d8bef9SDimitry Andric         ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(),
4915e8d8bef9SDimitry Andric                                       Node->getValueType(0)),
491606c3fb27SDimitry Andric                       Node, false).first);
4917e8d8bef9SDimitry Andric     break;
4918e8d8bef9SDimitry Andric   }
4919e8d8bef9SDimitry Andric   case ISD::STRICT_FP_EXTEND:
4920*0fca6ea1SDimitry Andric   case ISD::STRICT_FP_TO_FP16:
4921*0fca6ea1SDimitry Andric   case ISD::STRICT_FP_TO_BF16: {
4922*0fca6ea1SDimitry Andric     RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
4923*0fca6ea1SDimitry Andric     if (Node->getOpcode() == ISD::STRICT_FP_TO_FP16)
4924*0fca6ea1SDimitry Andric       LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16);
4925*0fca6ea1SDimitry Andric     else if (Node->getOpcode() == ISD::STRICT_FP_TO_BF16)
4926*0fca6ea1SDimitry Andric       LC = RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::bf16);
4927*0fca6ea1SDimitry Andric     else
4928*0fca6ea1SDimitry Andric       LC = RTLIB::getFPEXT(Node->getOperand(1).getValueType(),
4929e8d8bef9SDimitry Andric                            Node->getValueType(0));
4930*0fca6ea1SDimitry Andric 
4931e8d8bef9SDimitry Andric     assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall");
4932e8d8bef9SDimitry Andric 
49335ffd83dbSDimitry Andric     TargetLowering::MakeLibCallOptions CallOptions;
49345ffd83dbSDimitry Andric     std::pair<SDValue, SDValue> Tmp =
49355ffd83dbSDimitry Andric         TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1),
49365ffd83dbSDimitry Andric                         CallOptions, SDLoc(Node), Node->getOperand(0));
49375ffd83dbSDimitry Andric     Results.push_back(Tmp.first);
49385ffd83dbSDimitry Andric     Results.push_back(Tmp.second);
49395ffd83dbSDimitry Andric     break;
49405ffd83dbSDimitry Andric   }
49410b57cec5SDimitry Andric   case ISD::FSUB:
4942480093f4SDimitry Andric   case ISD::STRICT_FSUB:
4943480093f4SDimitry Andric     ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64,
49440b57cec5SDimitry Andric                     RTLIB::SUB_F80, RTLIB::SUB_F128,
4945480093f4SDimitry Andric                     RTLIB::SUB_PPCF128, Results);
49460b57cec5SDimitry Andric     break;
49470b57cec5SDimitry Andric   case ISD::SREM:
4948bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4949bdd1243dSDimitry Andric                                        RTLIB::SREM_I8,
4950bdd1243dSDimitry Andric                                        RTLIB::SREM_I16, RTLIB::SREM_I32,
4951bdd1243dSDimitry Andric                                        RTLIB::SREM_I64, RTLIB::SREM_I128));
49520b57cec5SDimitry Andric     break;
49530b57cec5SDimitry Andric   case ISD::UREM:
4954bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4955bdd1243dSDimitry Andric                                        RTLIB::UREM_I8,
4956bdd1243dSDimitry Andric                                        RTLIB::UREM_I16, RTLIB::UREM_I32,
4957bdd1243dSDimitry Andric                                        RTLIB::UREM_I64, RTLIB::UREM_I128));
49580b57cec5SDimitry Andric     break;
49590b57cec5SDimitry Andric   case ISD::SDIV:
4960bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, true,
4961bdd1243dSDimitry Andric                                        RTLIB::SDIV_I8,
4962bdd1243dSDimitry Andric                                        RTLIB::SDIV_I16, RTLIB::SDIV_I32,
4963bdd1243dSDimitry Andric                                        RTLIB::SDIV_I64, RTLIB::SDIV_I128));
49640b57cec5SDimitry Andric     break;
49650b57cec5SDimitry Andric   case ISD::UDIV:
4966bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4967bdd1243dSDimitry Andric                                        RTLIB::UDIV_I8,
4968bdd1243dSDimitry Andric                                        RTLIB::UDIV_I16, RTLIB::UDIV_I32,
4969bdd1243dSDimitry Andric                                        RTLIB::UDIV_I64, RTLIB::UDIV_I128));
49700b57cec5SDimitry Andric     break;
49710b57cec5SDimitry Andric   case ISD::SDIVREM:
49720b57cec5SDimitry Andric   case ISD::UDIVREM:
49730b57cec5SDimitry Andric     // Expand into divrem libcall
49740b57cec5SDimitry Andric     ExpandDivRemLibCall(Node, Results);
49750b57cec5SDimitry Andric     break;
49760b57cec5SDimitry Andric   case ISD::MUL:
4977bdd1243dSDimitry Andric     Results.push_back(ExpandIntLibCall(Node, false,
4978bdd1243dSDimitry Andric                                        RTLIB::MUL_I8,
4979bdd1243dSDimitry Andric                                        RTLIB::MUL_I16, RTLIB::MUL_I32,
4980bdd1243dSDimitry Andric                                        RTLIB::MUL_I64, RTLIB::MUL_I128));
49810b57cec5SDimitry Andric     break;
49820b57cec5SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF:
49830b57cec5SDimitry Andric     switch (Node->getSimpleValueType(0).SimpleTy) {
49840b57cec5SDimitry Andric     default:
49850b57cec5SDimitry Andric       llvm_unreachable("LibCall explicitly requested, but not available");
49860b57cec5SDimitry Andric     case MVT::i32:
498706c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first);
49880b57cec5SDimitry Andric       break;
49890b57cec5SDimitry Andric     case MVT::i64:
499006c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first);
49910b57cec5SDimitry Andric       break;
49920b57cec5SDimitry Andric     case MVT::i128:
499306c3fb27SDimitry Andric       Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first);
49940b57cec5SDimitry Andric       break;
49950b57cec5SDimitry Andric     }
49960b57cec5SDimitry Andric     break;
499706c3fb27SDimitry Andric   case ISD::RESET_FPENV: {
499806c3fb27SDimitry Andric     // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets
499906c3fb27SDimitry Andric     // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc.
500006c3fb27SDimitry Andric     SDValue Ptr = DAG.getIntPtrConstant(-1LL, dl);
500106c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
500206c3fb27SDimitry Andric     Results.push_back(
500306c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl));
500406c3fb27SDimitry Andric     break;
500506c3fb27SDimitry Andric   }
500606c3fb27SDimitry Andric   case ISD::GET_FPENV_MEM: {
500706c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
500806c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
500906c3fb27SDimitry Andric     Results.push_back(
501006c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl));
501106c3fb27SDimitry Andric     break;
501206c3fb27SDimitry Andric   }
501306c3fb27SDimitry Andric   case ISD::SET_FPENV_MEM: {
501406c3fb27SDimitry Andric     SDValue Chain = Node->getOperand(0);
501506c3fb27SDimitry Andric     SDValue EnvPtr = Node->getOperand(1);
501606c3fb27SDimitry Andric     Results.push_back(
501706c3fb27SDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl));
501806c3fb27SDimitry Andric     break;
501906c3fb27SDimitry Andric   }
50205f757f3fSDimitry Andric   case ISD::GET_FPMODE: {
50215f757f3fSDimitry Andric     // Call fegetmode, which saves control modes into a stack slot. Then load
50225f757f3fSDimitry Andric     // the value to return from the stack.
50235f757f3fSDimitry Andric     EVT ModeVT = Node->getValueType(0);
50245f757f3fSDimitry Andric     SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
50255f757f3fSDimitry Andric     int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
50265f757f3fSDimitry Andric     SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr,
50275f757f3fSDimitry Andric                                               Node->getOperand(0), dl);
50285f757f3fSDimitry Andric     SDValue LdInst = DAG.getLoad(
50295f757f3fSDimitry Andric         ModeVT, dl, Chain, StackPtr,
50305f757f3fSDimitry Andric         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
50315f757f3fSDimitry Andric     Results.push_back(LdInst);
50325f757f3fSDimitry Andric     Results.push_back(LdInst.getValue(1));
50335f757f3fSDimitry Andric     break;
50345f757f3fSDimitry Andric   }
50355f757f3fSDimitry Andric   case ISD::SET_FPMODE: {
50365f757f3fSDimitry Andric     // Move control modes to stack slot and then call fesetmode with the pointer
50375f757f3fSDimitry Andric     // to the slot as argument.
50385f757f3fSDimitry Andric     SDValue Mode = Node->getOperand(1);
50395f757f3fSDimitry Andric     EVT ModeVT = Mode.getValueType();
50405f757f3fSDimitry Andric     SDValue StackPtr = DAG.CreateStackTemporary(ModeVT);
50415f757f3fSDimitry Andric     int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
50425f757f3fSDimitry Andric     SDValue StInst = DAG.getStore(
50435f757f3fSDimitry Andric         Node->getOperand(0), dl, Mode, StackPtr,
50445f757f3fSDimitry Andric         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI));
50455f757f3fSDimitry Andric     Results.push_back(
50465f757f3fSDimitry Andric         DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl));
50475f757f3fSDimitry Andric     break;
50485f757f3fSDimitry Andric   }
50495f757f3fSDimitry Andric   case ISD::RESET_FPMODE: {
50505f757f3fSDimitry Andric     // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets
50515f757f3fSDimitry Andric     // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the
50525f757f3fSDimitry Andric     // target must provide custom lowering.
50535f757f3fSDimitry Andric     const DataLayout &DL = DAG.getDataLayout();
50545f757f3fSDimitry Andric     EVT PtrTy = TLI.getPointerTy(DL);
50555f757f3fSDimitry Andric     SDValue Mode = DAG.getConstant(-1LL, dl, PtrTy);
50565f757f3fSDimitry Andric     Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode,
50575f757f3fSDimitry Andric                                                 Node->getOperand(0), dl));
50585f757f3fSDimitry Andric     break;
50595f757f3fSDimitry Andric   }
50600b57cec5SDimitry Andric   }
50610b57cec5SDimitry Andric 
50620b57cec5SDimitry Andric   // Replace the original node with the legalized result.
50630b57cec5SDimitry Andric   if (!Results.empty()) {
50640b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n");
50650b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
50660b57cec5SDimitry Andric   } else
50670b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n");
50680b57cec5SDimitry Andric }
50690b57cec5SDimitry Andric 
50700b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when
50710b57cec5SDimitry Andric // promoting equally sized vectors.
50720b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI,
50730b57cec5SDimitry Andric                                         MVT EltVT, MVT NewEltVT) {
50740b57cec5SDimitry Andric   unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits();
5075cb14a3feSDimitry Andric   MVT MidVT = OldEltsPerNewElt == 1
5076cb14a3feSDimitry Andric                   ? NewEltVT
5077cb14a3feSDimitry Andric                   : MVT::getVectorVT(NewEltVT, OldEltsPerNewElt);
50780b57cec5SDimitry Andric   assert(TLI.isTypeLegal(MidVT) && "unexpected");
50790b57cec5SDimitry Andric   return MidVT;
50800b57cec5SDimitry Andric }
50810b57cec5SDimitry Andric 
50820b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
50830b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Trying to promote node\n");
50840b57cec5SDimitry Andric   SmallVector<SDValue, 8> Results;
50850b57cec5SDimitry Andric   MVT OVT = Node->getSimpleValueType(0);
50860b57cec5SDimitry Andric   if (Node->getOpcode() == ISD::UINT_TO_FP ||
50870b57cec5SDimitry Andric       Node->getOpcode() == ISD::SINT_TO_FP ||
50880b57cec5SDimitry Andric       Node->getOpcode() == ISD::SETCC ||
50890b57cec5SDimitry Andric       Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
50900b57cec5SDimitry Andric       Node->getOpcode() == ISD::INSERT_VECTOR_ELT) {
50910b57cec5SDimitry Andric     OVT = Node->getOperand(0).getSimpleValueType();
50920b57cec5SDimitry Andric   }
5093*0fca6ea1SDimitry Andric   if (Node->getOpcode() == ISD::ATOMIC_STORE ||
5094*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::STRICT_UINT_TO_FP ||
5095e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_SINT_TO_FP ||
5096e8d8bef9SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCC ||
5097*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::STRICT_FSETCCS ||
5098*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FADD ||
5099*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FMUL ||
5100*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FMAX ||
5101*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FMIN ||
5102*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FMAXIMUM ||
5103*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_FMINIMUM ||
5104*0fca6ea1SDimitry Andric       Node->getOpcode() == ISD::VP_REDUCE_SEQ_FADD)
5105480093f4SDimitry Andric     OVT = Node->getOperand(1).getSimpleValueType();
5106fe6060f1SDimitry Andric   if (Node->getOpcode() == ISD::BR_CC ||
5107fe6060f1SDimitry Andric       Node->getOpcode() == ISD::SELECT_CC)
51080b57cec5SDimitry Andric     OVT = Node->getOperand(2).getSimpleValueType();
51090b57cec5SDimitry Andric   MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
51100b57cec5SDimitry Andric   SDLoc dl(Node);
5111fe6060f1SDimitry Andric   SDValue Tmp1, Tmp2, Tmp3, Tmp4;
51120b57cec5SDimitry Andric   switch (Node->getOpcode()) {
51130b57cec5SDimitry Andric   case ISD::CTTZ:
51140b57cec5SDimitry Andric   case ISD::CTTZ_ZERO_UNDEF:
51150b57cec5SDimitry Andric   case ISD::CTLZ:
5116*0fca6ea1SDimitry Andric   case ISD::CTPOP: {
51175ffd83dbSDimitry Andric     // Zero extend the argument unless its cttz, then use any_extend.
51185ffd83dbSDimitry Andric     if (Node->getOpcode() == ISD::CTTZ ||
51195ffd83dbSDimitry Andric         Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF)
51205ffd83dbSDimitry Andric       Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
51215ffd83dbSDimitry Andric     else
51220b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
51235ffd83dbSDimitry Andric 
5124*0fca6ea1SDimitry Andric     unsigned NewOpc = Node->getOpcode();
5125*0fca6ea1SDimitry Andric     if (NewOpc == ISD::CTTZ) {
51260b57cec5SDimitry Andric       // The count is the same in the promoted type except if the original
51270b57cec5SDimitry Andric       // value was zero.  This can be handled by setting the bit just off
51280b57cec5SDimitry Andric       // the top of the original type.
51290b57cec5SDimitry Andric       auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(),
51300b57cec5SDimitry Andric                                         OVT.getSizeInBits());
51310b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1,
51320b57cec5SDimitry Andric                          DAG.getConstant(TopBit, dl, NVT));
5133*0fca6ea1SDimitry Andric       NewOpc = ISD::CTTZ_ZERO_UNDEF;
51340b57cec5SDimitry Andric     }
51350b57cec5SDimitry Andric     // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is
51360b57cec5SDimitry Andric     // already the correct result.
5137*0fca6ea1SDimitry Andric     Tmp1 = DAG.getNode(NewOpc, dl, NVT, Tmp1);
5138*0fca6ea1SDimitry Andric     if (NewOpc == ISD::CTLZ) {
51390b57cec5SDimitry Andric       // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
51400b57cec5SDimitry Andric       Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1,
51410b57cec5SDimitry Andric                           DAG.getConstant(NVT.getSizeInBits() -
51420b57cec5SDimitry Andric                                           OVT.getSizeInBits(), dl, NVT));
51430b57cec5SDimitry Andric     }
51440b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
51450b57cec5SDimitry Andric     break;
5146*0fca6ea1SDimitry Andric   }
5147*0fca6ea1SDimitry Andric   case ISD::CTLZ_ZERO_UNDEF: {
5148*0fca6ea1SDimitry Andric     // We know that the argument is unlikely to be zero, hence we can take a
5149*0fca6ea1SDimitry Andric     // different approach as compared to ISD::CTLZ
5150*0fca6ea1SDimitry Andric 
5151*0fca6ea1SDimitry Andric     // Any Extend the argument
5152*0fca6ea1SDimitry Andric     auto AnyExtendedNode =
5153*0fca6ea1SDimitry Andric         DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5154*0fca6ea1SDimitry Andric 
5155*0fca6ea1SDimitry Andric     // Tmp1 = Tmp1 << (sizeinbits(NVT) - sizeinbits(Old VT))
5156*0fca6ea1SDimitry Andric     auto ShiftConstant = DAG.getShiftAmountConstant(
5157*0fca6ea1SDimitry Andric         NVT.getSizeInBits() - OVT.getSizeInBits(), NVT, dl);
5158*0fca6ea1SDimitry Andric     auto LeftShiftResult =
5159*0fca6ea1SDimitry Andric         DAG.getNode(ISD::SHL, dl, NVT, AnyExtendedNode, ShiftConstant);
5160*0fca6ea1SDimitry Andric 
5161*0fca6ea1SDimitry Andric     // Perform the larger operation
5162*0fca6ea1SDimitry Andric     auto CTLZResult = DAG.getNode(Node->getOpcode(), dl, NVT, LeftShiftResult);
5163*0fca6ea1SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, CTLZResult));
5164*0fca6ea1SDimitry Andric     break;
5165*0fca6ea1SDimitry Andric   }
51660b57cec5SDimitry Andric   case ISD::BITREVERSE:
51670b57cec5SDimitry Andric   case ISD::BSWAP: {
51680b57cec5SDimitry Andric     unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
51690b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0));
51700b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
51710b57cec5SDimitry Andric     Tmp1 = DAG.getNode(
51720b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
51730b57cec5SDimitry Andric         DAG.getConstant(DiffBits, dl,
51740b57cec5SDimitry Andric                         TLI.getShiftAmountTy(NVT, DAG.getDataLayout())));
51750b57cec5SDimitry Andric 
51760b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
51770b57cec5SDimitry Andric     break;
51780b57cec5SDimitry Andric   }
51790b57cec5SDimitry Andric   case ISD::FP_TO_UINT:
5180480093f4SDimitry Andric   case ISD::STRICT_FP_TO_UINT:
51810b57cec5SDimitry Andric   case ISD::FP_TO_SINT:
5182480093f4SDimitry Andric   case ISD::STRICT_FP_TO_SINT:
5183480093f4SDimitry Andric     PromoteLegalFP_TO_INT(Node, dl, Results);
51840b57cec5SDimitry Andric     break;
5185e8d8bef9SDimitry Andric   case ISD::FP_TO_UINT_SAT:
5186e8d8bef9SDimitry Andric   case ISD::FP_TO_SINT_SAT:
5187e8d8bef9SDimitry Andric     Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl));
5188e8d8bef9SDimitry Andric     break;
51890b57cec5SDimitry Andric   case ISD::UINT_TO_FP:
5190480093f4SDimitry Andric   case ISD::STRICT_UINT_TO_FP:
51910b57cec5SDimitry Andric   case ISD::SINT_TO_FP:
5192480093f4SDimitry Andric   case ISD::STRICT_SINT_TO_FP:
5193480093f4SDimitry Andric     PromoteLegalINT_TO_FP(Node, dl, Results);
51940b57cec5SDimitry Andric     break;
51950b57cec5SDimitry Andric   case ISD::VAARG: {
51960b57cec5SDimitry Andric     SDValue Chain = Node->getOperand(0); // Get the chain.
51970b57cec5SDimitry Andric     SDValue Ptr = Node->getOperand(1); // Get the pointer.
51980b57cec5SDimitry Andric 
51990b57cec5SDimitry Andric     unsigned TruncOp;
52000b57cec5SDimitry Andric     if (OVT.isVector()) {
52010b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
52020b57cec5SDimitry Andric     } else {
52030b57cec5SDimitry Andric       assert(OVT.isInteger()
52040b57cec5SDimitry Andric         && "VAARG promotion is supported only for vectors or integer types");
52050b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
52060b57cec5SDimitry Andric     }
52070b57cec5SDimitry Andric 
52080b57cec5SDimitry Andric     // Perform the larger operation, then convert back
52090b57cec5SDimitry Andric     Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2),
52100b57cec5SDimitry Andric              Node->getConstantOperandVal(3));
52110b57cec5SDimitry Andric     Chain = Tmp1.getValue(1);
52120b57cec5SDimitry Andric 
52130b57cec5SDimitry Andric     Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1);
52140b57cec5SDimitry Andric 
52150b57cec5SDimitry Andric     // Modified the chain result - switch anything that used the old chain to
52160b57cec5SDimitry Andric     // use the new one.
52170b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2);
52180b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain);
52190b57cec5SDimitry Andric     if (UpdatedNodes) {
52200b57cec5SDimitry Andric       UpdatedNodes->insert(Tmp2.getNode());
52210b57cec5SDimitry Andric       UpdatedNodes->insert(Chain.getNode());
52220b57cec5SDimitry Andric     }
52230b57cec5SDimitry Andric     ReplacedNode(Node);
52240b57cec5SDimitry Andric     break;
52250b57cec5SDimitry Andric   }
52260b57cec5SDimitry Andric   case ISD::MUL:
52270b57cec5SDimitry Andric   case ISD::SDIV:
52280b57cec5SDimitry Andric   case ISD::SREM:
52290b57cec5SDimitry Andric   case ISD::UDIV:
52300b57cec5SDimitry Andric   case ISD::UREM:
52315f757f3fSDimitry Andric   case ISD::SMIN:
52325f757f3fSDimitry Andric   case ISD::SMAX:
52335f757f3fSDimitry Andric   case ISD::UMIN:
52345f757f3fSDimitry Andric   case ISD::UMAX:
52350b57cec5SDimitry Andric   case ISD::AND:
52360b57cec5SDimitry Andric   case ISD::OR:
52370b57cec5SDimitry Andric   case ISD::XOR: {
52380b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
52390b57cec5SDimitry Andric     if (OVT.isVector()) {
52400b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
52410b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
52420b57cec5SDimitry Andric     } else {
52430b57cec5SDimitry Andric       assert(OVT.isInteger() && "Cannot promote logic operation");
52440b57cec5SDimitry Andric 
52450b57cec5SDimitry Andric       switch (Node->getOpcode()) {
52460b57cec5SDimitry Andric       default:
52470b57cec5SDimitry Andric         ExtOp = ISD::ANY_EXTEND;
52480b57cec5SDimitry Andric         break;
52490b57cec5SDimitry Andric       case ISD::SDIV:
52500b57cec5SDimitry Andric       case ISD::SREM:
52515f757f3fSDimitry Andric       case ISD::SMIN:
52525f757f3fSDimitry Andric       case ISD::SMAX:
52530b57cec5SDimitry Andric         ExtOp = ISD::SIGN_EXTEND;
52540b57cec5SDimitry Andric         break;
52550b57cec5SDimitry Andric       case ISD::UDIV:
52560b57cec5SDimitry Andric       case ISD::UREM:
52570b57cec5SDimitry Andric         ExtOp = ISD::ZERO_EXTEND;
52580b57cec5SDimitry Andric         break;
52595f757f3fSDimitry Andric       case ISD::UMIN:
52605f757f3fSDimitry Andric       case ISD::UMAX:
52615f757f3fSDimitry Andric         if (TLI.isSExtCheaperThanZExt(OVT, NVT))
52625f757f3fSDimitry Andric           ExtOp = ISD::SIGN_EXTEND;
52635f757f3fSDimitry Andric         else
52645f757f3fSDimitry Andric           ExtOp = ISD::ZERO_EXTEND;
52655f757f3fSDimitry Andric         break;
52660b57cec5SDimitry Andric       }
52670b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
52680b57cec5SDimitry Andric     }
52690b57cec5SDimitry Andric     // Promote each of the values to the new type.
52700b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
52710b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
52720b57cec5SDimitry Andric     // Perform the larger operation, then convert back
52730b57cec5SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
52740b57cec5SDimitry Andric     Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1));
52750b57cec5SDimitry Andric     break;
52760b57cec5SDimitry Andric   }
52770b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
52780b57cec5SDimitry Andric   case ISD::SMUL_LOHI: {
52790b57cec5SDimitry Andric     // Promote to a multiply in a wider integer type.
52800b57cec5SDimitry Andric     unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND
52810b57cec5SDimitry Andric                                                          : ISD::SIGN_EXTEND;
52820b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
52830b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
52840b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2);
52850b57cec5SDimitry Andric 
52860b57cec5SDimitry Andric     auto &DL = DAG.getDataLayout();
52870b57cec5SDimitry Andric     unsigned OriginalSize = OVT.getScalarSizeInBits();
52880b57cec5SDimitry Andric     Tmp2 = DAG.getNode(
52890b57cec5SDimitry Andric         ISD::SRL, dl, NVT, Tmp1,
52900b57cec5SDimitry Andric         DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT)));
52910b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1));
52920b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
52930b57cec5SDimitry Andric     break;
52940b57cec5SDimitry Andric   }
52950b57cec5SDimitry Andric   case ISD::SELECT: {
52960b57cec5SDimitry Andric     unsigned ExtOp, TruncOp;
52970b57cec5SDimitry Andric     if (Node->getValueType(0).isVector() ||
52980b57cec5SDimitry Andric         Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) {
52990b57cec5SDimitry Andric       ExtOp   = ISD::BITCAST;
53000b57cec5SDimitry Andric       TruncOp = ISD::BITCAST;
53010b57cec5SDimitry Andric     } else if (Node->getValueType(0).isInteger()) {
53020b57cec5SDimitry Andric       ExtOp   = ISD::ANY_EXTEND;
53030b57cec5SDimitry Andric       TruncOp = ISD::TRUNCATE;
53040b57cec5SDimitry Andric     } else {
53050b57cec5SDimitry Andric       ExtOp   = ISD::FP_EXTEND;
53060b57cec5SDimitry Andric       TruncOp = ISD::FP_ROUND;
53070b57cec5SDimitry Andric     }
53080b57cec5SDimitry Andric     Tmp1 = Node->getOperand(0);
53090b57cec5SDimitry Andric     // Promote each of the values to the new type.
53100b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
53110b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
53120b57cec5SDimitry Andric     // Perform the larger operation, then round down.
53130b57cec5SDimitry Andric     Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3);
53140b57cec5SDimitry Andric     Tmp1->setFlags(Node->getFlags());
53150b57cec5SDimitry Andric     if (TruncOp != ISD::FP_ROUND)
53160b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1);
53170b57cec5SDimitry Andric     else
53180b57cec5SDimitry Andric       Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1,
53190b57cec5SDimitry Andric                          DAG.getIntPtrConstant(0, dl));
53200b57cec5SDimitry Andric     Results.push_back(Tmp1);
53210b57cec5SDimitry Andric     break;
53220b57cec5SDimitry Andric   }
53230b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE: {
53240b57cec5SDimitry Andric     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask();
53250b57cec5SDimitry Andric 
53260b57cec5SDimitry Andric     // Cast the two input vectors.
53270b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0));
53280b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1));
53290b57cec5SDimitry Andric 
53300b57cec5SDimitry Andric     // Convert the shuffle mask to the right # elements.
53310b57cec5SDimitry Andric     Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask);
53320b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1);
53330b57cec5SDimitry Andric     Results.push_back(Tmp1);
53340b57cec5SDimitry Andric     break;
53350b57cec5SDimitry Andric   }
5336fe6060f1SDimitry Andric   case ISD::VECTOR_SPLICE: {
5337fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0));
5338fe6060f1SDimitry Andric     Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1));
5339fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2,
5340fe6060f1SDimitry Andric                        Node->getOperand(2));
5341fe6060f1SDimitry Andric     Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3));
5342fe6060f1SDimitry Andric     break;
5343fe6060f1SDimitry Andric   }
5344fe6060f1SDimitry Andric   case ISD::SELECT_CC: {
5345fe6060f1SDimitry Andric     SDValue Cond = Node->getOperand(4);
5346fe6060f1SDimitry Andric     ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get();
5347fe6060f1SDimitry Andric     // Type of the comparison operands.
5348fe6060f1SDimitry Andric     MVT CVT = Node->getSimpleValueType(0);
5349fe6060f1SDimitry Andric     assert(CVT == OVT && "not handled");
5350fe6060f1SDimitry Andric 
5351fe6060f1SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
5352fe6060f1SDimitry Andric     if (NVT.isInteger()) {
5353fe6060f1SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
5354fe6060f1SDimitry Andric     }
5355fe6060f1SDimitry Andric 
5356fe6060f1SDimitry Andric     // Promote the comparison operands, if needed.
5357fe6060f1SDimitry Andric     if (TLI.isCondCodeLegal(CCCode, CVT)) {
5358fe6060f1SDimitry Andric       Tmp1 = Node->getOperand(0);
5359fe6060f1SDimitry Andric       Tmp2 = Node->getOperand(1);
5360fe6060f1SDimitry Andric     } else {
5361fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
5362fe6060f1SDimitry Andric       Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
5363fe6060f1SDimitry Andric     }
5364fe6060f1SDimitry Andric     // Cast the true/false operands.
5365fe6060f1SDimitry Andric     Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
5366fe6060f1SDimitry Andric     Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
5367fe6060f1SDimitry Andric 
5368fe6060f1SDimitry Andric     Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond},
5369fe6060f1SDimitry Andric                        Node->getFlags());
5370fe6060f1SDimitry Andric 
5371fe6060f1SDimitry Andric     // Cast the result back to the original type.
5372fe6060f1SDimitry Andric     if (ExtOp != ISD::FP_EXTEND)
5373fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1);
5374fe6060f1SDimitry Andric     else
5375fe6060f1SDimitry Andric       Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1,
5376bdd1243dSDimitry Andric                          DAG.getIntPtrConstant(0, dl, /*isTarget=*/true));
5377fe6060f1SDimitry Andric 
5378fe6060f1SDimitry Andric     Results.push_back(Tmp1);
5379fe6060f1SDimitry Andric     break;
5380fe6060f1SDimitry Andric   }
5381e8d8bef9SDimitry Andric   case ISD::SETCC:
5382e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCC:
5383e8d8bef9SDimitry Andric   case ISD::STRICT_FSETCCS: {
53840b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
53850b57cec5SDimitry Andric     if (NVT.isInteger()) {
5386e8d8bef9SDimitry Andric       ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get();
53875f757f3fSDimitry Andric       if (isSignedIntSetCC(CCCode) ||
53885f757f3fSDimitry Andric           TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT))
53895f757f3fSDimitry Andric         ExtOp = ISD::SIGN_EXTEND;
53905f757f3fSDimitry Andric       else
53915f757f3fSDimitry Andric         ExtOp = ISD::ZERO_EXTEND;
53920b57cec5SDimitry Andric     }
5393e8d8bef9SDimitry Andric     if (Node->isStrictFPOpcode()) {
5394e8d8bef9SDimitry Andric       SDValue InChain = Node->getOperand(0);
5395e8d8bef9SDimitry Andric       std::tie(Tmp1, std::ignore) =
5396e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT);
5397e8d8bef9SDimitry Andric       std::tie(Tmp2, std::ignore) =
5398e8d8bef9SDimitry Andric           DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT);
5399e8d8bef9SDimitry Andric       SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)};
5400e8d8bef9SDimitry Andric       SDValue OutChain = DAG.getTokenFactor(dl, TmpChains);
5401e8d8bef9SDimitry Andric       SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other);
5402e8d8bef9SDimitry Andric       Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs,
5403e8d8bef9SDimitry Andric                                     {OutChain, Tmp1, Tmp2, Node->getOperand(3)},
5404e8d8bef9SDimitry Andric                                     Node->getFlags()));
5405e8d8bef9SDimitry Andric       Results.push_back(Results.back().getValue(1));
5406e8d8bef9SDimitry Andric       break;
5407e8d8bef9SDimitry Andric     }
54080b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0));
54090b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1));
54100b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1,
54110b57cec5SDimitry Andric                                   Tmp2, Node->getOperand(2), Node->getFlags()));
54120b57cec5SDimitry Andric     break;
54130b57cec5SDimitry Andric   }
54140b57cec5SDimitry Andric   case ISD::BR_CC: {
54150b57cec5SDimitry Andric     unsigned ExtOp = ISD::FP_EXTEND;
54160b57cec5SDimitry Andric     if (NVT.isInteger()) {
54170b57cec5SDimitry Andric       ISD::CondCode CCCode =
54180b57cec5SDimitry Andric         cast<CondCodeSDNode>(Node->getOperand(1))->get();
54190b57cec5SDimitry Andric       ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
54200b57cec5SDimitry Andric     }
54210b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2));
54220b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3));
54230b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0),
54240b57cec5SDimitry Andric                                   Node->getOperand(0), Node->getOperand(1),
54250b57cec5SDimitry Andric                                   Tmp1, Tmp2, Node->getOperand(4)));
54260b57cec5SDimitry Andric     break;
54270b57cec5SDimitry Andric   }
54280b57cec5SDimitry Andric   case ISD::FADD:
54290b57cec5SDimitry Andric   case ISD::FSUB:
54300b57cec5SDimitry Andric   case ISD::FMUL:
54310b57cec5SDimitry Andric   case ISD::FDIV:
54320b57cec5SDimitry Andric   case ISD::FREM:
54330b57cec5SDimitry Andric   case ISD::FMINNUM:
54340b57cec5SDimitry Andric   case ISD::FMAXNUM:
543506c3fb27SDimitry Andric   case ISD::FMINIMUM:
543606c3fb27SDimitry Andric   case ISD::FMAXIMUM:
54370b57cec5SDimitry Andric   case ISD::FPOW:
54380b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
54390b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
54400b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
54410b57cec5SDimitry Andric                        Node->getFlags());
5442bdd1243dSDimitry Andric     Results.push_back(
5443bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5444bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
54450b57cec5SDimitry Andric     break;
544681ad6265SDimitry Andric   case ISD::STRICT_FADD:
544781ad6265SDimitry Andric   case ISD::STRICT_FSUB:
544881ad6265SDimitry Andric   case ISD::STRICT_FMUL:
544981ad6265SDimitry Andric   case ISD::STRICT_FDIV:
545081ad6265SDimitry Andric   case ISD::STRICT_FMINNUM:
545181ad6265SDimitry Andric   case ISD::STRICT_FMAXNUM:
5452480093f4SDimitry Andric   case ISD::STRICT_FREM:
5453480093f4SDimitry Andric   case ISD::STRICT_FPOW:
5454480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5455480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5456480093f4SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5457480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
5458480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
5459480093f4SDimitry Andric                        Tmp2.getValue(1));
5460480093f4SDimitry Andric     Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5461480093f4SDimitry Andric                        {Tmp3, Tmp1, Tmp2});
5462480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5463480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)});
5464480093f4SDimitry Andric     Results.push_back(Tmp1);
5465480093f4SDimitry Andric     Results.push_back(Tmp1.getValue(1));
5466480093f4SDimitry Andric     break;
54670b57cec5SDimitry Andric   case ISD::FMA:
54680b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
54690b57cec5SDimitry Andric     Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
54700b57cec5SDimitry Andric     Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2));
54710b57cec5SDimitry Andric     Results.push_back(
54720b57cec5SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT,
54730b57cec5SDimitry Andric                     DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3),
5474bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
54750b57cec5SDimitry Andric     break;
547681ad6265SDimitry Andric   case ISD::STRICT_FMA:
547781ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
547881ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
547981ad6265SDimitry Andric     Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
548081ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(2)});
548181ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
548281ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(3)});
548381ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1),
548481ad6265SDimitry Andric                        Tmp2.getValue(1), Tmp3.getValue(1));
548581ad6265SDimitry Andric     Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
548681ad6265SDimitry Andric                        {Tmp4, Tmp1, Tmp2, Tmp3});
548781ad6265SDimitry Andric     Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
548881ad6265SDimitry Andric                        {Tmp4.getValue(1), Tmp4, DAG.getIntPtrConstant(0, dl)});
548981ad6265SDimitry Andric     Results.push_back(Tmp4);
549081ad6265SDimitry Andric     Results.push_back(Tmp4.getValue(1));
549181ad6265SDimitry Andric     break;
54920b57cec5SDimitry Andric   case ISD::FCOPYSIGN:
549306c3fb27SDimitry Andric   case ISD::FLDEXP:
54940b57cec5SDimitry Andric   case ISD::FPOWI: {
54950b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
54960b57cec5SDimitry Andric     Tmp2 = Node->getOperand(1);
54970b57cec5SDimitry Andric     Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2);
54980b57cec5SDimitry Andric 
54990b57cec5SDimitry Andric     // fcopysign doesn't change anything but the sign bit, so
55000b57cec5SDimitry Andric     //   (fp_round (fcopysign (fpext a), b))
55010b57cec5SDimitry Andric     // is as precise as
55020b57cec5SDimitry Andric     //   (fp_round (fpext a))
55030b57cec5SDimitry Andric     // which is a no-op. Mark it as a TRUNCating FP_ROUND.
55040b57cec5SDimitry Andric     const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN);
5505bdd1243dSDimitry Andric     Results.push_back(
5506bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3,
5507bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true)));
55080b57cec5SDimitry Andric     break;
55090b57cec5SDimitry Andric   }
551081ad6265SDimitry Andric   case ISD::STRICT_FPOWI:
551181ad6265SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
551281ad6265SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
551381ad6265SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
551481ad6265SDimitry Andric                        {Tmp1.getValue(1), Tmp1, Node->getOperand(2)});
551581ad6265SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
551681ad6265SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
551781ad6265SDimitry Andric     Results.push_back(Tmp3);
551881ad6265SDimitry Andric     Results.push_back(Tmp3.getValue(1));
551981ad6265SDimitry Andric     break;
552006c3fb27SDimitry Andric   case ISD::FFREXP: {
552106c3fb27SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
552206c3fb27SDimitry Andric     Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1);
552306c3fb27SDimitry Andric 
552406c3fb27SDimitry Andric     Results.push_back(
552506c3fb27SDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
552606c3fb27SDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
552706c3fb27SDimitry Andric 
552806c3fb27SDimitry Andric     Results.push_back(Tmp2.getValue(1));
552906c3fb27SDimitry Andric     break;
553006c3fb27SDimitry Andric   }
55310b57cec5SDimitry Andric   case ISD::FFLOOR:
55320b57cec5SDimitry Andric   case ISD::FCEIL:
55330b57cec5SDimitry Andric   case ISD::FRINT:
55340b57cec5SDimitry Andric   case ISD::FNEARBYINT:
55350b57cec5SDimitry Andric   case ISD::FROUND:
55365ffd83dbSDimitry Andric   case ISD::FROUNDEVEN:
55370b57cec5SDimitry Andric   case ISD::FTRUNC:
55380b57cec5SDimitry Andric   case ISD::FNEG:
55390b57cec5SDimitry Andric   case ISD::FSQRT:
55400b57cec5SDimitry Andric   case ISD::FSIN:
55410b57cec5SDimitry Andric   case ISD::FCOS:
5542*0fca6ea1SDimitry Andric   case ISD::FTAN:
5543*0fca6ea1SDimitry Andric   case ISD::FASIN:
5544*0fca6ea1SDimitry Andric   case ISD::FACOS:
5545*0fca6ea1SDimitry Andric   case ISD::FATAN:
5546*0fca6ea1SDimitry Andric   case ISD::FSINH:
5547*0fca6ea1SDimitry Andric   case ISD::FCOSH:
5548*0fca6ea1SDimitry Andric   case ISD::FTANH:
55490b57cec5SDimitry Andric   case ISD::FLOG:
55500b57cec5SDimitry Andric   case ISD::FLOG2:
55510b57cec5SDimitry Andric   case ISD::FLOG10:
55520b57cec5SDimitry Andric   case ISD::FABS:
55530b57cec5SDimitry Andric   case ISD::FEXP:
55540b57cec5SDimitry Andric   case ISD::FEXP2:
55555f757f3fSDimitry Andric   case ISD::FEXP10:
5556cb14a3feSDimitry Andric   case ISD::FCANONICALIZE:
55570b57cec5SDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
55580b57cec5SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
5559bdd1243dSDimitry Andric     Results.push_back(
5560bdd1243dSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
5561bdd1243dSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
55620b57cec5SDimitry Andric     break;
5563480093f4SDimitry Andric   case ISD::STRICT_FFLOOR:
5564480093f4SDimitry Andric   case ISD::STRICT_FCEIL:
556581ad6265SDimitry Andric   case ISD::STRICT_FRINT:
556681ad6265SDimitry Andric   case ISD::STRICT_FNEARBYINT:
5567349cc55cSDimitry Andric   case ISD::STRICT_FROUND:
556881ad6265SDimitry Andric   case ISD::STRICT_FROUNDEVEN:
556981ad6265SDimitry Andric   case ISD::STRICT_FTRUNC:
557081ad6265SDimitry Andric   case ISD::STRICT_FSQRT:
5571480093f4SDimitry Andric   case ISD::STRICT_FSIN:
5572480093f4SDimitry Andric   case ISD::STRICT_FCOS:
5573*0fca6ea1SDimitry Andric   case ISD::STRICT_FTAN:
5574*0fca6ea1SDimitry Andric   case ISD::STRICT_FASIN:
5575*0fca6ea1SDimitry Andric   case ISD::STRICT_FACOS:
5576*0fca6ea1SDimitry Andric   case ISD::STRICT_FATAN:
5577*0fca6ea1SDimitry Andric   case ISD::STRICT_FSINH:
5578*0fca6ea1SDimitry Andric   case ISD::STRICT_FCOSH:
5579*0fca6ea1SDimitry Andric   case ISD::STRICT_FTANH:
5580480093f4SDimitry Andric   case ISD::STRICT_FLOG:
558181ad6265SDimitry Andric   case ISD::STRICT_FLOG2:
5582480093f4SDimitry Andric   case ISD::STRICT_FLOG10:
5583480093f4SDimitry Andric   case ISD::STRICT_FEXP:
558481ad6265SDimitry Andric   case ISD::STRICT_FEXP2:
5585480093f4SDimitry Andric     Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
5586480093f4SDimitry Andric                        {Node->getOperand(0), Node->getOperand(1)});
5587480093f4SDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other},
5588480093f4SDimitry Andric                        {Tmp1.getValue(1), Tmp1});
5589480093f4SDimitry Andric     Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other},
5590480093f4SDimitry Andric                        {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)});
5591480093f4SDimitry Andric     Results.push_back(Tmp3);
5592480093f4SDimitry Andric     Results.push_back(Tmp3.getValue(1));
5593480093f4SDimitry Andric     break;
55940b57cec5SDimitry Andric   case ISD::BUILD_VECTOR: {
55950b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
55960b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
55970b57cec5SDimitry Andric 
55980b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
55990b57cec5SDimitry Andric     //
56000b57cec5SDimitry Andric     // e.g. v2i64 = build_vector i64:x, i64:y => v4i32
56010b57cec5SDimitry Andric     //  =>
56020b57cec5SDimitry Andric     //  v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y))
56030b57cec5SDimitry Andric 
56040b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
56050b57cec5SDimitry Andric            "Invalid promote type for build_vector");
5606cb14a3feSDimitry Andric     assert(NewEltVT.bitsLE(EltVT) && "not handled");
56070b57cec5SDimitry Andric 
56080b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
56090b57cec5SDimitry Andric 
56100b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
5611*0fca6ea1SDimitry Andric     for (const SDValue &Op : Node->op_values())
56120b57cec5SDimitry Andric       NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op));
56130b57cec5SDimitry Andric 
56140b57cec5SDimitry Andric     SDLoc SL(Node);
5615cb14a3feSDimitry Andric     SDValue Concat =
5616cb14a3feSDimitry Andric         DAG.getNode(MidVT == NewEltVT ? ISD::BUILD_VECTOR : ISD::CONCAT_VECTORS,
5617cb14a3feSDimitry Andric                     SL, NVT, NewOps);
56180b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
56190b57cec5SDimitry Andric     Results.push_back(CvtVec);
56200b57cec5SDimitry Andric     break;
56210b57cec5SDimitry Andric   }
56220b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT: {
56230b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
56240b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
56250b57cec5SDimitry Andric 
56260b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size.
56270b57cec5SDimitry Andric     //
56280b57cec5SDimitry Andric     // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32
56290b57cec5SDimitry Andric     //  =>
56300b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
56310b57cec5SDimitry Andric     //
56320b57cec5SDimitry Andric     // i64 = bitcast
56330b57cec5SDimitry Andric     //   (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))),
56340b57cec5SDimitry Andric     //                       (i32 (extract_vector_elt castx, (2 * y + 1)))
56350b57cec5SDimitry Andric     //
56360b57cec5SDimitry Andric 
56370b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
56380b57cec5SDimitry Andric            "Invalid promote type for extract_vector_elt");
56390b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
56400b57cec5SDimitry Andric 
56410b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
56420b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
56430b57cec5SDimitry Andric 
56440b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(1);
56450b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
56460b57cec5SDimitry Andric     SDLoc SL(Node);
56470b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT);
56480b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
56490b57cec5SDimitry Andric 
56500b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
56510b57cec5SDimitry Andric 
56520b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewOps;
56530b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
56540b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
56550b57cec5SDimitry Andric       SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
56560b57cec5SDimitry Andric 
56570b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
56580b57cec5SDimitry Andric                                 CastVec, TmpIdx);
56590b57cec5SDimitry Andric       NewOps.push_back(Elt);
56600b57cec5SDimitry Andric     }
56610b57cec5SDimitry Andric 
56620b57cec5SDimitry Andric     SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps);
56630b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec));
56640b57cec5SDimitry Andric     break;
56650b57cec5SDimitry Andric   }
56660b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT: {
56670b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
56680b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
56690b57cec5SDimitry Andric 
56700b57cec5SDimitry Andric     // Handle bitcasts to a different vector type with the same total bit size
56710b57cec5SDimitry Andric     //
56720b57cec5SDimitry Andric     // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32
56730b57cec5SDimitry Andric     //  =>
56740b57cec5SDimitry Andric     //  v4i32:castx = bitcast x:v2i64
56750b57cec5SDimitry Andric     //  v2i32:casty = bitcast y:i64
56760b57cec5SDimitry Andric     //
56770b57cec5SDimitry Andric     // v2i64 = bitcast
56780b57cec5SDimitry Andric     //   (v4i32 insert_vector_elt
56790b57cec5SDimitry Andric     //       (v4i32 insert_vector_elt v4i32:castx,
56800b57cec5SDimitry Andric     //                                (extract_vector_elt casty, 0), 2 * z),
56810b57cec5SDimitry Andric     //        (extract_vector_elt casty, 1), (2 * z + 1))
56820b57cec5SDimitry Andric 
56830b57cec5SDimitry Andric     assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() &&
56840b57cec5SDimitry Andric            "Invalid promote type for insert_vector_elt");
56850b57cec5SDimitry Andric     assert(NewEltVT.bitsLT(EltVT) && "not handled");
56860b57cec5SDimitry Andric 
56870b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
56880b57cec5SDimitry Andric     unsigned NewEltsPerOldElt = MidVT.getVectorNumElements();
56890b57cec5SDimitry Andric 
56900b57cec5SDimitry Andric     SDValue Val = Node->getOperand(1);
56910b57cec5SDimitry Andric     SDValue Idx = Node->getOperand(2);
56920b57cec5SDimitry Andric     EVT IdxVT = Idx.getValueType();
56930b57cec5SDimitry Andric     SDLoc SL(Node);
56940b57cec5SDimitry Andric 
56950b57cec5SDimitry Andric     SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT);
56960b57cec5SDimitry Andric     SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor);
56970b57cec5SDimitry Andric 
56980b57cec5SDimitry Andric     SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0));
56990b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
57000b57cec5SDimitry Andric 
57010b57cec5SDimitry Andric     SDValue NewVec = CastVec;
57020b57cec5SDimitry Andric     for (unsigned I = 0; I < NewEltsPerOldElt; ++I) {
57030b57cec5SDimitry Andric       SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT);
57040b57cec5SDimitry Andric       SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset);
57050b57cec5SDimitry Andric 
57060b57cec5SDimitry Andric       SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT,
57070b57cec5SDimitry Andric                                 CastVal, IdxOffset);
57080b57cec5SDimitry Andric 
57090b57cec5SDimitry Andric       NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT,
57100b57cec5SDimitry Andric                            NewVec, Elt, InEltIdx);
57110b57cec5SDimitry Andric     }
57120b57cec5SDimitry Andric 
57130b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec));
57140b57cec5SDimitry Andric     break;
57150b57cec5SDimitry Andric   }
57160b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR: {
57170b57cec5SDimitry Andric     MVT EltVT = OVT.getVectorElementType();
57180b57cec5SDimitry Andric     MVT NewEltVT = NVT.getVectorElementType();
57190b57cec5SDimitry Andric 
57200b57cec5SDimitry Andric     // Handle bitcasts to different vector type with the same total bit size.
57210b57cec5SDimitry Andric     //
57220b57cec5SDimitry Andric     // e.g. v2i64 = scalar_to_vector x:i64
57230b57cec5SDimitry Andric     //   =>
57240b57cec5SDimitry Andric     //  concat_vectors (v2i32 bitcast x:i64), (v2i32 undef)
57250b57cec5SDimitry Andric     //
57260b57cec5SDimitry Andric 
57270b57cec5SDimitry Andric     MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT);
57280b57cec5SDimitry Andric     SDValue Val = Node->getOperand(0);
57290b57cec5SDimitry Andric     SDLoc SL(Node);
57300b57cec5SDimitry Andric 
57310b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val);
57320b57cec5SDimitry Andric     SDValue Undef = DAG.getUNDEF(MidVT);
57330b57cec5SDimitry Andric 
57340b57cec5SDimitry Andric     SmallVector<SDValue, 8> NewElts;
57350b57cec5SDimitry Andric     NewElts.push_back(CastVal);
57360b57cec5SDimitry Andric     for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I)
57370b57cec5SDimitry Andric       NewElts.push_back(Undef);
57380b57cec5SDimitry Andric 
57390b57cec5SDimitry Andric     SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts);
57400b57cec5SDimitry Andric     SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat);
57410b57cec5SDimitry Andric     Results.push_back(CvtVec);
57420b57cec5SDimitry Andric     break;
57430b57cec5SDimitry Andric   }
5744*0fca6ea1SDimitry Andric   case ISD::ATOMIC_SWAP:
5745*0fca6ea1SDimitry Andric   case ISD::ATOMIC_STORE: {
57460b57cec5SDimitry Andric     AtomicSDNode *AM = cast<AtomicSDNode>(Node);
57470b57cec5SDimitry Andric     SDLoc SL(Node);
57480b57cec5SDimitry Andric     SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal());
57490b57cec5SDimitry Andric     assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
57500b57cec5SDimitry Andric            "unexpected promotion type");
57510b57cec5SDimitry Andric     assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
57520b57cec5SDimitry Andric            "unexpected atomic_swap with illegal type");
57530b57cec5SDimitry Andric 
5754*0fca6ea1SDimitry Andric     SDValue Op0 = AM->getBasePtr();
5755*0fca6ea1SDimitry Andric     SDValue Op1 = CastVal;
5756*0fca6ea1SDimitry Andric 
5757*0fca6ea1SDimitry Andric     // ATOMIC_STORE uses a swapped operand order from every other AtomicSDNode,
5758*0fca6ea1SDimitry Andric     // but really it should merge with ISD::STORE.
5759*0fca6ea1SDimitry Andric     if (AM->getOpcode() == ISD::ATOMIC_STORE)
5760*0fca6ea1SDimitry Andric       std::swap(Op0, Op1);
5761*0fca6ea1SDimitry Andric 
5762*0fca6ea1SDimitry Andric     SDValue NewAtomic = DAG.getAtomic(AM->getOpcode(), SL, NVT, AM->getChain(),
5763*0fca6ea1SDimitry Andric                                       Op0, Op1, AM->getMemOperand());
5764*0fca6ea1SDimitry Andric 
5765*0fca6ea1SDimitry Andric     if (AM->getOpcode() != ISD::ATOMIC_STORE) {
5766*0fca6ea1SDimitry Andric       Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
5767*0fca6ea1SDimitry Andric       Results.push_back(NewAtomic.getValue(1));
5768*0fca6ea1SDimitry Andric     } else
5769*0fca6ea1SDimitry Andric       Results.push_back(NewAtomic);
5770*0fca6ea1SDimitry Andric     break;
5771*0fca6ea1SDimitry Andric   }
5772*0fca6ea1SDimitry Andric   case ISD::ATOMIC_LOAD: {
5773*0fca6ea1SDimitry Andric     AtomicSDNode *AM = cast<AtomicSDNode>(Node);
5774*0fca6ea1SDimitry Andric     SDLoc SL(Node);
5775*0fca6ea1SDimitry Andric     assert(NVT.getSizeInBits() == OVT.getSizeInBits() &&
5776*0fca6ea1SDimitry Andric            "unexpected promotion type");
5777*0fca6ea1SDimitry Andric     assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() &&
5778*0fca6ea1SDimitry Andric            "unexpected atomic_load with illegal type");
5779*0fca6ea1SDimitry Andric 
5780*0fca6ea1SDimitry Andric     SDValue NewAtomic =
5781*0fca6ea1SDimitry Andric         DAG.getAtomic(ISD::ATOMIC_LOAD, SL, NVT, DAG.getVTList(NVT, MVT::Other),
5782*0fca6ea1SDimitry Andric                       {AM->getChain(), AM->getBasePtr()}, AM->getMemOperand());
57830b57cec5SDimitry Andric     Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic));
57840b57cec5SDimitry Andric     Results.push_back(NewAtomic.getValue(1));
57850b57cec5SDimitry Andric     break;
57860b57cec5SDimitry Andric   }
57875f757f3fSDimitry Andric   case ISD::SPLAT_VECTOR: {
57885f757f3fSDimitry Andric     SDValue Scalar = Node->getOperand(0);
57895f757f3fSDimitry Andric     MVT ScalarType = Scalar.getSimpleValueType();
57905f757f3fSDimitry Andric     MVT NewScalarType = NVT.getVectorElementType();
57915f757f3fSDimitry Andric     if (ScalarType.isInteger()) {
57925f757f3fSDimitry Andric       Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar);
57935f757f3fSDimitry Andric       Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
57945f757f3fSDimitry Andric       Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2));
57955f757f3fSDimitry Andric       break;
57965f757f3fSDimitry Andric     }
57975f757f3fSDimitry Andric     Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar);
57985f757f3fSDimitry Andric     Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1);
57995f757f3fSDimitry Andric     Results.push_back(
58005f757f3fSDimitry Andric         DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2,
58015f757f3fSDimitry Andric                     DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)));
58025f757f3fSDimitry Andric     break;
58035f757f3fSDimitry Andric   }
5804*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FADD:
5805*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMUL:
5806*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMAX:
5807*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMIN:
5808*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMAXIMUM:
5809*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_FMINIMUM:
5810*0fca6ea1SDimitry Andric   case ISD::VP_REDUCE_SEQ_FADD:
5811*0fca6ea1SDimitry Andric     Results.push_back(PromoteReduction(Node));
5812*0fca6ea1SDimitry Andric     break;
58130b57cec5SDimitry Andric   }
58140b57cec5SDimitry Andric 
58150b57cec5SDimitry Andric   // Replace the original node with the legalized result.
58160b57cec5SDimitry Andric   if (!Results.empty()) {
58170b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Successfully promoted node\n");
58180b57cec5SDimitry Andric     ReplaceNode(Node, Results.data());
58190b57cec5SDimitry Andric   } else
58200b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Could not promote node\n");
58210b57cec5SDimitry Andric }
58220b57cec5SDimitry Andric 
58230b57cec5SDimitry Andric /// This is the entry point for the file.
58240b57cec5SDimitry Andric void SelectionDAG::Legalize() {
58250b57cec5SDimitry Andric   AssignTopologicalOrder();
58260b57cec5SDimitry Andric 
58270b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
58280b57cec5SDimitry Andric   // Use a delete listener to remove nodes which were deleted during
58290b57cec5SDimitry Andric   // legalization from LegalizeNodes. This is needed to handle the situation
58300b57cec5SDimitry Andric   // where a new node is allocated by the object pool to the same address of a
58310b57cec5SDimitry Andric   // previously deleted node.
58320b57cec5SDimitry Andric   DAGNodeDeletedListener DeleteListener(
58330b57cec5SDimitry Andric       *this,
58340b57cec5SDimitry Andric       [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); });
58350b57cec5SDimitry Andric 
58360b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes);
58370b57cec5SDimitry Andric 
58380b57cec5SDimitry Andric   // Visit all the nodes. We start in topological order, so that we see
58390b57cec5SDimitry Andric   // nodes with their original operands intact. Legalization can produce
58400b57cec5SDimitry Andric   // new nodes which may themselves need to be legalized. Iterate until all
58410b57cec5SDimitry Andric   // nodes have been legalized.
58420b57cec5SDimitry Andric   while (true) {
58430b57cec5SDimitry Andric     bool AnyLegalized = false;
58440b57cec5SDimitry Andric     for (auto NI = allnodes_end(); NI != allnodes_begin();) {
58450b57cec5SDimitry Andric       --NI;
58460b57cec5SDimitry Andric 
58470b57cec5SDimitry Andric       SDNode *N = &*NI;
58480b57cec5SDimitry Andric       if (N->use_empty() && N != getRoot().getNode()) {
58490b57cec5SDimitry Andric         ++NI;
58500b57cec5SDimitry Andric         DeleteNode(N);
58510b57cec5SDimitry Andric         continue;
58520b57cec5SDimitry Andric       }
58530b57cec5SDimitry Andric 
58540b57cec5SDimitry Andric       if (LegalizedNodes.insert(N).second) {
58550b57cec5SDimitry Andric         AnyLegalized = true;
58560b57cec5SDimitry Andric         Legalizer.LegalizeOp(N);
58570b57cec5SDimitry Andric 
58580b57cec5SDimitry Andric         if (N->use_empty() && N != getRoot().getNode()) {
58590b57cec5SDimitry Andric           ++NI;
58600b57cec5SDimitry Andric           DeleteNode(N);
58610b57cec5SDimitry Andric         }
58620b57cec5SDimitry Andric       }
58630b57cec5SDimitry Andric     }
58640b57cec5SDimitry Andric     if (!AnyLegalized)
58650b57cec5SDimitry Andric       break;
58660b57cec5SDimitry Andric 
58670b57cec5SDimitry Andric   }
58680b57cec5SDimitry Andric 
58690b57cec5SDimitry Andric   // Remove dead nodes now.
58700b57cec5SDimitry Andric   RemoveDeadNodes();
58710b57cec5SDimitry Andric }
58720b57cec5SDimitry Andric 
58730b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N,
58740b57cec5SDimitry Andric                               SmallSetVector<SDNode *, 16> &UpdatedNodes) {
58750b57cec5SDimitry Andric   SmallPtrSet<SDNode *, 16> LegalizedNodes;
58760b57cec5SDimitry Andric   SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes);
58770b57cec5SDimitry Andric 
58780b57cec5SDimitry Andric   // Directly insert the node in question, and legalize it. This will recurse
58790b57cec5SDimitry Andric   // as needed through operands.
58800b57cec5SDimitry Andric   LegalizedNodes.insert(N);
58810b57cec5SDimitry Andric   Legalizer.LegalizeOp(N);
58820b57cec5SDimitry Andric 
58830b57cec5SDimitry Andric   return LegalizedNodes.count(N);
58840b57cec5SDimitry Andric }
5885