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" 160b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 208bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h" 320b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 330b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 340b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 350b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 360b57cec5SDimitry Andric #include "llvm/IR/Function.h" 370b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 380b57cec5SDimitry Andric #include "llvm/IR/Type.h" 390b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 400b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 410b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 420b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 430b57cec5SDimitry Andric #include "llvm/Support/MachineValueType.h" 440b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 450b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 460b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 470b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 480b57cec5SDimitry Andric #include <algorithm> 490b57cec5SDimitry Andric #include <cassert> 500b57cec5SDimitry Andric #include <cstdint> 510b57cec5SDimitry Andric #include <tuple> 520b57cec5SDimitry Andric #include <utility> 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric using namespace llvm; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric #define DEBUG_TYPE "legalizedag" 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric namespace { 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric /// Keeps track of state when getting the sign of a floating-point value as an 610b57cec5SDimitry Andric /// integer. 620b57cec5SDimitry Andric struct FloatSignAsInt { 630b57cec5SDimitry Andric EVT FloatVT; 640b57cec5SDimitry Andric SDValue Chain; 650b57cec5SDimitry Andric SDValue FloatPtr; 660b57cec5SDimitry Andric SDValue IntPtr; 670b57cec5SDimitry Andric MachinePointerInfo IntPointerInfo; 680b57cec5SDimitry Andric MachinePointerInfo FloatPointerInfo; 690b57cec5SDimitry Andric SDValue IntValue; 700b57cec5SDimitry Andric APInt SignMask; 710b57cec5SDimitry Andric uint8_t SignBit; 720b57cec5SDimitry Andric }; 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 750b57cec5SDimitry Andric /// This takes an arbitrary SelectionDAG as input and 760b57cec5SDimitry Andric /// hacks on it until the target machine can handle it. This involves 770b57cec5SDimitry Andric /// eliminating value sizes the machine cannot handle (promoting small sizes to 780b57cec5SDimitry Andric /// large sizes or splitting up large values into small values) as well as 790b57cec5SDimitry Andric /// eliminating operations the machine cannot handle. 800b57cec5SDimitry Andric /// 810b57cec5SDimitry Andric /// This code also does a small amount of optimization and recognition of idioms 820b57cec5SDimitry Andric /// as part of its processing. For example, if a target does not support a 830b57cec5SDimitry Andric /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 840b57cec5SDimitry Andric /// will attempt merge setcc and brc instructions into brcc's. 850b57cec5SDimitry Andric class SelectionDAGLegalize { 860b57cec5SDimitry Andric const TargetMachine &TM; 870b57cec5SDimitry Andric const TargetLowering &TLI; 880b57cec5SDimitry Andric SelectionDAG &DAG; 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric /// The set of nodes which have already been legalized. We hold a 910b57cec5SDimitry Andric /// reference to it in order to update as necessary on node deletion. 920b57cec5SDimitry Andric SmallPtrSetImpl<SDNode *> &LegalizedNodes; 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric /// A set of all the nodes updated during legalization. 950b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> *UpdatedNodes; 960b57cec5SDimitry Andric 970b57cec5SDimitry Andric EVT getSetCCResultType(EVT VT) const { 980b57cec5SDimitry Andric return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 990b57cec5SDimitry Andric } 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric // Libcall insertion helpers. 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric public: 1040b57cec5SDimitry Andric SelectionDAGLegalize(SelectionDAG &DAG, 1050b57cec5SDimitry Andric SmallPtrSetImpl<SDNode *> &LegalizedNodes, 1060b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr) 1070b57cec5SDimitry Andric : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG), 1080b57cec5SDimitry Andric LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {} 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric /// Legalizes the given operation. 1110b57cec5SDimitry Andric void LegalizeOp(SDNode *Node); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric private: 1140b57cec5SDimitry Andric SDValue OptimizeFloatStore(StoreSDNode *ST); 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric void LegalizeLoadOps(SDNode *Node); 1170b57cec5SDimitry Andric void LegalizeStoreOps(SDNode *Node); 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric /// Some targets cannot handle a variable 1200b57cec5SDimitry Andric /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 1210b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform 1220b57cec5SDimitry Andric /// the insert there, and then read the result back. 1230b57cec5SDimitry Andric SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, 1240b57cec5SDimitry Andric const SDLoc &dl); 1250b57cec5SDimitry Andric SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, 1260b57cec5SDimitry Andric const SDLoc &dl); 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric /// Return a vector shuffle operation which 1290b57cec5SDimitry Andric /// performs the same shuffe in terms of order or result bytes, but on a type 1300b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type. 1310b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 1320b57cec5SDimitry Andric SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl, 1330b57cec5SDimitry Andric SDValue N1, SDValue N2, 1340b57cec5SDimitry Andric ArrayRef<int> Mask) const; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); 1370b57cec5SDimitry Andric 138fe6060f1SDimitry Andric void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC, 139fe6060f1SDimitry Andric SmallVectorImpl<SDValue> &Results); 140480093f4SDimitry Andric void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, 1410b57cec5SDimitry Andric RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, 1420b57cec5SDimitry Andric RTLIB::Libcall Call_F128, 143480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 144480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 1450b57cec5SDimitry Andric SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, 1460b57cec5SDimitry Andric RTLIB::Libcall Call_I8, 1470b57cec5SDimitry Andric RTLIB::Libcall Call_I16, 1480b57cec5SDimitry Andric RTLIB::Libcall Call_I32, 1490b57cec5SDimitry Andric RTLIB::Libcall Call_I64, 1500b57cec5SDimitry Andric RTLIB::Libcall Call_I128); 151480093f4SDimitry Andric void ExpandArgFPLibCall(SDNode *Node, 1520b57cec5SDimitry Andric RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64, 1530b57cec5SDimitry Andric RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128, 154480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 155480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 1560b57cec5SDimitry Andric void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 1570b57cec5SDimitry Andric void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 1600b57cec5SDimitry Andric const SDLoc &dl); 1610b57cec5SDimitry Andric SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 1620b57cec5SDimitry Andric const SDLoc &dl, SDValue ChainIn); 1630b57cec5SDimitry Andric SDValue ExpandBUILD_VECTOR(SDNode *Node); 1648bcb0991SDimitry Andric SDValue ExpandSPLAT_VECTOR(SDNode *Node); 1650b57cec5SDimitry Andric SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); 1660b57cec5SDimitry Andric void ExpandDYNAMIC_STACKALLOC(SDNode *Node, 1670b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results); 1680b57cec5SDimitry Andric void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL, 1690b57cec5SDimitry Andric SDValue Value) const; 1700b57cec5SDimitry Andric SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL, 1710b57cec5SDimitry Andric SDValue NewIntValue) const; 1720b57cec5SDimitry Andric SDValue ExpandFCOPYSIGN(SDNode *Node) const; 1730b57cec5SDimitry Andric SDValue ExpandFABS(SDNode *Node) const; 174e8d8bef9SDimitry Andric SDValue ExpandFNEG(SDNode *Node) const; 175480093f4SDimitry Andric SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain); 176480093f4SDimitry Andric void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl, 177480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 178480093f4SDimitry Andric void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl, 179480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 180e8d8bef9SDimitry Andric SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl); 1810b57cec5SDimitry Andric 182e8d8bef9SDimitry Andric SDValue ExpandPARITY(SDValue Op, const SDLoc &dl); 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric SDValue ExpandExtractFromVectorThroughStack(SDValue Op); 1850b57cec5SDimitry Andric SDValue ExpandInsertToVectorThroughStack(SDValue Op); 1860b57cec5SDimitry Andric SDValue ExpandVectorBuildThroughStack(SDNode* Node); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP); 1890b57cec5SDimitry Andric SDValue ExpandConstant(ConstantSDNode *CP); 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall 1920b57cec5SDimitry Andric bool ExpandNode(SDNode *Node); 1930b57cec5SDimitry Andric void ConvertNodeToLibcall(SDNode *Node); 1940b57cec5SDimitry Andric void PromoteNode(SDNode *Node); 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric public: 1970b57cec5SDimitry Andric // Node replacement helpers 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric void ReplacedNode(SDNode *N) { 2000b57cec5SDimitry Andric LegalizedNodes.erase(N); 2010b57cec5SDimitry Andric if (UpdatedNodes) 2020b57cec5SDimitry Andric UpdatedNodes->insert(N); 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric void ReplaceNode(SDNode *Old, SDNode *New) { 2060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2070b57cec5SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric assert(Old->getNumValues() == New->getNumValues() && 2100b57cec5SDimitry Andric "Replacing one node with another that produces a different number " 2110b57cec5SDimitry Andric "of values!"); 2120b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2130b57cec5SDimitry Andric if (UpdatedNodes) 2140b57cec5SDimitry Andric UpdatedNodes->insert(New); 2150b57cec5SDimitry Andric ReplacedNode(Old); 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric void ReplaceNode(SDValue Old, SDValue New) { 2190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2200b57cec5SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2210b57cec5SDimitry Andric 2220b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2230b57cec5SDimitry Andric if (UpdatedNodes) 2240b57cec5SDimitry Andric UpdatedNodes->insert(New.getNode()); 2250b57cec5SDimitry Andric ReplacedNode(Old.getNode()); 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric void ReplaceNode(SDNode *Old, const SDValue *New) { 2290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG)); 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2320b57cec5SDimitry Andric for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) { 2330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (i == 0 ? " with: " : " and: "); 2340b57cec5SDimitry Andric New[i]->dump(&DAG)); 2350b57cec5SDimitry Andric if (UpdatedNodes) 2360b57cec5SDimitry Andric UpdatedNodes->insert(New[i].getNode()); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric ReplacedNode(Old); 2390b57cec5SDimitry Andric } 2408bcb0991SDimitry Andric 2418bcb0991SDimitry Andric void ReplaceNodeWithValue(SDValue Old, SDValue New) { 2428bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2438bcb0991SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2448bcb0991SDimitry Andric 2458bcb0991SDimitry Andric DAG.ReplaceAllUsesOfValueWith(Old, New); 2468bcb0991SDimitry Andric if (UpdatedNodes) 2478bcb0991SDimitry Andric UpdatedNodes->insert(New.getNode()); 2488bcb0991SDimitry Andric ReplacedNode(Old.getNode()); 2498bcb0991SDimitry Andric } 2500b57cec5SDimitry Andric }; 2510b57cec5SDimitry Andric 2520b57cec5SDimitry Andric } // end anonymous namespace 2530b57cec5SDimitry Andric 2540b57cec5SDimitry Andric /// Return a vector shuffle operation which 2550b57cec5SDimitry Andric /// performs the same shuffle in terms of order or result bytes, but on a type 2560b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type. 2570b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 2580b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType( 2590b57cec5SDimitry Andric EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, 2600b57cec5SDimitry Andric ArrayRef<int> Mask) const { 2610b57cec5SDimitry Andric unsigned NumMaskElts = VT.getVectorNumElements(); 2620b57cec5SDimitry Andric unsigned NumDestElts = NVT.getVectorNumElements(); 2630b57cec5SDimitry Andric unsigned NumEltsGrowth = NumDestElts / NumMaskElts; 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric if (NumEltsGrowth == 1) 2680b57cec5SDimitry Andric return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask); 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric SmallVector<int, 8> NewMask; 2710b57cec5SDimitry Andric for (unsigned i = 0; i != NumMaskElts; ++i) { 2720b57cec5SDimitry Andric int Idx = Mask[i]; 2730b57cec5SDimitry Andric for (unsigned j = 0; j != NumEltsGrowth; ++j) { 2740b57cec5SDimitry Andric if (Idx < 0) 2750b57cec5SDimitry Andric NewMask.push_back(-1); 2760b57cec5SDimitry Andric else 2770b57cec5SDimitry Andric NewMask.push_back(Idx * NumEltsGrowth + j); 2780b57cec5SDimitry Andric } 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); 2810b57cec5SDimitry Andric assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); 2820b57cec5SDimitry Andric return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric /// Expands the ConstantFP node to an integer constant or 2860b57cec5SDimitry Andric /// a load from the constant pool. 2870b57cec5SDimitry Andric SDValue 2880b57cec5SDimitry Andric SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) { 2890b57cec5SDimitry Andric bool Extend = false; 2900b57cec5SDimitry Andric SDLoc dl(CFP); 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // If a FP immediate is precise when represented as a float and if the 2930b57cec5SDimitry Andric // target can do an extending load from float to double, we put it into 2940b57cec5SDimitry Andric // the constant pool as a float, even if it's is statically typed as a 2950b57cec5SDimitry Andric // double. This shrinks FP constants and canonicalizes them for targets where 2960b57cec5SDimitry Andric // an FP extending load is the same cost as a normal load (such as on the x87 2970b57cec5SDimitry Andric // fp stack or PPC FP unit). 2980b57cec5SDimitry Andric EVT VT = CFP->getValueType(0); 2990b57cec5SDimitry Andric ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); 3000b57cec5SDimitry Andric if (!UseCP) { 3010b57cec5SDimitry Andric assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); 3020b57cec5SDimitry Andric return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl, 3030b57cec5SDimitry Andric (VT == MVT::f64) ? MVT::i64 : MVT::i32); 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric APFloat APF = CFP->getValueAPF(); 3070b57cec5SDimitry Andric EVT OrigVT = VT; 3080b57cec5SDimitry Andric EVT SVT = VT; 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric // We don't want to shrink SNaNs. Converting the SNaN back to its real type 3110b57cec5SDimitry Andric // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ). 3120b57cec5SDimitry Andric if (!APF.isSignaling()) { 3130b57cec5SDimitry Andric while (SVT != MVT::f32 && SVT != MVT::f16) { 3140b57cec5SDimitry Andric SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); 3150b57cec5SDimitry Andric if (ConstantFPSDNode::isValueValidForType(SVT, APF) && 3160b57cec5SDimitry Andric // Only do this if the target has a native EXTLOAD instruction from 3170b57cec5SDimitry Andric // smaller type. 3180b57cec5SDimitry Andric TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) && 3190b57cec5SDimitry Andric TLI.ShouldShrinkFPConstant(OrigVT)) { 3200b57cec5SDimitry Andric Type *SType = SVT.getTypeForEVT(*DAG.getContext()); 3210b57cec5SDimitry Andric LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType)); 3220b57cec5SDimitry Andric VT = SVT; 3230b57cec5SDimitry Andric Extend = true; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric } 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric SDValue CPIdx = 3290b57cec5SDimitry Andric DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout())); 3305ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 3310b57cec5SDimitry Andric if (Extend) { 3320b57cec5SDimitry Andric SDValue Result = DAG.getExtLoad( 3330b57cec5SDimitry Andric ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx, 3340b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT, 3350b57cec5SDimitry Andric Alignment); 3360b57cec5SDimitry Andric return Result; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric SDValue Result = DAG.getLoad( 3390b57cec5SDimitry Andric OrigVT, dl, DAG.getEntryNode(), CPIdx, 3400b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 3410b57cec5SDimitry Andric return Result; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric /// Expands the Constant node to a load from the constant pool. 3450b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) { 3460b57cec5SDimitry Andric SDLoc dl(CP); 3470b57cec5SDimitry Andric EVT VT = CP->getValueType(0); 3480b57cec5SDimitry Andric SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(), 3490b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 3505ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 3510b57cec5SDimitry Andric SDValue Result = DAG.getLoad( 3520b57cec5SDimitry Andric VT, dl, DAG.getEntryNode(), CPIdx, 3530b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 3540b57cec5SDimitry Andric return Result; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric 3570b57cec5SDimitry Andric /// Some target cannot handle a variable insertion index for the 3580b57cec5SDimitry Andric /// INSERT_VECTOR_ELT instruction. In this case, it 3590b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform 3600b57cec5SDimitry Andric /// the insert there, and then read the result back. 3610b57cec5SDimitry Andric SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec, 3620b57cec5SDimitry Andric SDValue Val, 3630b57cec5SDimitry Andric SDValue Idx, 3640b57cec5SDimitry Andric const SDLoc &dl) { 3650b57cec5SDimitry Andric SDValue Tmp1 = Vec; 3660b57cec5SDimitry Andric SDValue Tmp2 = Val; 3670b57cec5SDimitry Andric SDValue Tmp3 = Idx; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric // If the target doesn't support this, we have to spill the input vector 3700b57cec5SDimitry Andric // to a temporary stack slot, update the element, then reload it. This is 3710b57cec5SDimitry Andric // badness. We could also load the value into a vector register (either 3720b57cec5SDimitry Andric // with a "move to register" or "extload into register" instruction, then 3730b57cec5SDimitry Andric // permute it into place, if the idx is a constant and if the idx is 3740b57cec5SDimitry Andric // supported by the target. 3750b57cec5SDimitry Andric EVT VT = Tmp1.getValueType(); 3760b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 3770b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(VT); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // Store the vector. 3820b57cec5SDimitry Andric SDValue Ch = DAG.getStore( 3830b57cec5SDimitry Andric DAG.getEntryNode(), dl, Tmp1, StackPtr, 3840b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 3850b57cec5SDimitry Andric 3860b57cec5SDimitry Andric SDValue StackPtr2 = TLI.getVectorElementPointer(DAG, StackPtr, VT, Tmp3); 3870b57cec5SDimitry Andric 3880b57cec5SDimitry Andric // Store the scalar value. 3895ffd83dbSDimitry Andric Ch = DAG.getTruncStore( 3905ffd83dbSDimitry Andric Ch, dl, Tmp2, StackPtr2, 3915ffd83dbSDimitry Andric MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltVT); 3920b57cec5SDimitry Andric // Load the updated vector. 3930b57cec5SDimitry Andric return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack( 3940b57cec5SDimitry Andric DAG.getMachineFunction(), SPFI)); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric 3970b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, 3980b57cec5SDimitry Andric SDValue Idx, 3990b57cec5SDimitry Andric const SDLoc &dl) { 4000b57cec5SDimitry Andric if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { 4010b57cec5SDimitry Andric // SCALAR_TO_VECTOR requires that the type of the value being inserted 4020b57cec5SDimitry Andric // match the element type of the vector being created, except for 4030b57cec5SDimitry Andric // integers in which case the inserted value can be over width. 4040b57cec5SDimitry Andric EVT EltVT = Vec.getValueType().getVectorElementType(); 4050b57cec5SDimitry Andric if (Val.getValueType() == EltVT || 4060b57cec5SDimitry Andric (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { 4070b57cec5SDimitry Andric SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 4080b57cec5SDimitry Andric Vec.getValueType(), Val); 4090b57cec5SDimitry Andric 4100b57cec5SDimitry Andric unsigned NumElts = Vec.getValueType().getVectorNumElements(); 4110b57cec5SDimitry Andric // We generate a shuffle of InVec and ScVec, so the shuffle mask 4120b57cec5SDimitry Andric // should be 0,1,2,3,4,5... with the appropriate element replaced with 4130b57cec5SDimitry Andric // elt 0 of the RHS. 4140b57cec5SDimitry Andric SmallVector<int, 8> ShufOps; 4150b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) 4160b57cec5SDimitry Andric ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps); 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { 425480093f4SDimitry Andric if (!ISD::isNormalStore(ST)) 426480093f4SDimitry Andric return SDValue(); 427480093f4SDimitry Andric 4280b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing float store operations\n"); 4290b57cec5SDimitry Andric // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 4300b57cec5SDimitry Andric // FIXME: move this to the DAG Combiner! Note that we can't regress due 4310b57cec5SDimitry Andric // to phase ordering between legalized code and the dag combiner. This 4320b57cec5SDimitry Andric // probably means that we need to integrate dag combiner and legalizer 4330b57cec5SDimitry Andric // together. 4340b57cec5SDimitry Andric // We generally can't do this one for long doubles. 4350b57cec5SDimitry Andric SDValue Chain = ST->getChain(); 4360b57cec5SDimitry Andric SDValue Ptr = ST->getBasePtr(); 437e8d8bef9SDimitry Andric SDValue Value = ST->getValue(); 4380b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 4390b57cec5SDimitry Andric AAMDNodes AAInfo = ST->getAAInfo(); 4400b57cec5SDimitry Andric SDLoc dl(ST); 441e8d8bef9SDimitry Andric 442e8d8bef9SDimitry Andric // Don't optimise TargetConstantFP 443e8d8bef9SDimitry Andric if (Value.getOpcode() == ISD::TargetConstantFP) 444e8d8bef9SDimitry Andric return SDValue(); 445e8d8bef9SDimitry Andric 446e8d8bef9SDimitry Andric if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { 4470b57cec5SDimitry Andric if (CFP->getValueType(0) == MVT::f32 && 4480b57cec5SDimitry Andric TLI.isTypeLegal(MVT::i32)) { 4490b57cec5SDimitry Andric SDValue Con = DAG.getConstant(CFP->getValueAPF(). 4500b57cec5SDimitry Andric bitcastToAPInt().zextOrTrunc(32), 4510b57cec5SDimitry Andric SDLoc(CFP), MVT::i32); 4525ffd83dbSDimitry Andric return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 4535ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric if (CFP->getValueType(0) == MVT::f64) { 4570b57cec5SDimitry Andric // If this target supports 64-bit registers, do a single 64-bit store. 4580b57cec5SDimitry Andric if (TLI.isTypeLegal(MVT::i64)) { 4590b57cec5SDimitry Andric SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 4600b57cec5SDimitry Andric zextOrTrunc(64), SDLoc(CFP), MVT::i64); 4610b57cec5SDimitry Andric return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 4625ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4630b57cec5SDimitry Andric } 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) { 4660b57cec5SDimitry Andric // Otherwise, if the target supports 32-bit registers, use 2 32-bit 4670b57cec5SDimitry Andric // stores. If the target supports neither 32- nor 64-bits, this 4680b57cec5SDimitry Andric // xform is certainly not worth it. 4690b57cec5SDimitry Andric const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt(); 4700b57cec5SDimitry Andric SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32); 4710b57cec5SDimitry Andric SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32); 4720b57cec5SDimitry Andric if (DAG.getDataLayout().isBigEndian()) 4730b57cec5SDimitry Andric std::swap(Lo, Hi); 4740b57cec5SDimitry Andric 4755ffd83dbSDimitry Andric Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), 4765ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 477e8d8bef9SDimitry Andric Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(4), dl); 4780b57cec5SDimitry Andric Hi = DAG.getStore(Chain, dl, Hi, Ptr, 4790b57cec5SDimitry Andric ST->getPointerInfo().getWithOffset(4), 4805ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric } 486e8d8bef9SDimitry Andric return SDValue(); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) { 4900b57cec5SDimitry Andric StoreSDNode *ST = cast<StoreSDNode>(Node); 4910b57cec5SDimitry Andric SDValue Chain = ST->getChain(); 4920b57cec5SDimitry Andric SDValue Ptr = ST->getBasePtr(); 4930b57cec5SDimitry Andric SDLoc dl(Node); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 4960b57cec5SDimitry Andric AAMDNodes AAInfo = ST->getAAInfo(); 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric if (!ST->isTruncatingStore()) { 4990b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing store operation\n"); 5000b57cec5SDimitry Andric if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { 5010b57cec5SDimitry Andric ReplaceNode(ST, OptStore); 5020b57cec5SDimitry Andric return; 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 5050b57cec5SDimitry Andric SDValue Value = ST->getValue(); 5060b57cec5SDimitry Andric MVT VT = Value.getSimpleValueType(); 5070b57cec5SDimitry Andric switch (TLI.getOperationAction(ISD::STORE, VT)) { 5080b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 5090b57cec5SDimitry Andric case TargetLowering::Legal: { 5100b57cec5SDimitry Andric // If this is an unaligned store and the target doesn't support it, 5110b57cec5SDimitry Andric // expand it. 5120b57cec5SDimitry Andric EVT MemVT = ST->getMemoryVT(); 5130b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 5148bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 5150b57cec5SDimitry Andric *ST->getMemOperand())) { 5160b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n"); 5170b57cec5SDimitry Andric SDValue Result = TLI.expandUnalignedStore(ST, DAG); 5180b57cec5SDimitry Andric ReplaceNode(SDValue(ST, 0), Result); 5190b57cec5SDimitry Andric } else 5200b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legal store\n"); 5210b57cec5SDimitry Andric break; 5220b57cec5SDimitry Andric } 5230b57cec5SDimitry Andric case TargetLowering::Custom: { 5240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying custom lowering\n"); 5250b57cec5SDimitry Andric SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 5260b57cec5SDimitry Andric if (Res && Res != SDValue(Node, 0)) 5270b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 5280b57cec5SDimitry Andric return; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric case TargetLowering::Promote: { 5310b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT); 5320b57cec5SDimitry Andric assert(NVT.getSizeInBits() == VT.getSizeInBits() && 5330b57cec5SDimitry Andric "Can only promote stores to same size type"); 5340b57cec5SDimitry Andric Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value); 5355ffd83dbSDimitry Andric SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 5365ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 5370b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 5380b57cec5SDimitry Andric break; 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric return; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric 5440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n"); 5450b57cec5SDimitry Andric SDValue Value = ST->getValue(); 5460b57cec5SDimitry Andric EVT StVT = ST->getMemoryVT(); 547e8d8bef9SDimitry Andric TypeSize StWidth = StVT.getSizeInBits(); 548e8d8bef9SDimitry Andric TypeSize StSize = StVT.getStoreSizeInBits(); 5490b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 5500b57cec5SDimitry Andric 551e8d8bef9SDimitry Andric if (StWidth != StSize) { 5520b57cec5SDimitry Andric // Promote to a byte-sized store with upper bits zero if not 5530b57cec5SDimitry Andric // storing an integral number of bytes. For example, promote 5540b57cec5SDimitry Andric // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 555e8d8bef9SDimitry Andric EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedSize()); 5560b57cec5SDimitry Andric Value = DAG.getZeroExtendInReg(Value, dl, StVT); 5570b57cec5SDimitry Andric SDValue Result = 5580b57cec5SDimitry Andric DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT, 5595ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 5600b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 561e8d8bef9SDimitry Andric } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedSize())) { 5620b57cec5SDimitry Andric // If not storing a power-of-2 number of bits, expand as two stores. 5630b57cec5SDimitry Andric assert(!StVT.isVector() && "Unsupported truncstore!"); 564e8d8bef9SDimitry Andric unsigned StWidthBits = StWidth.getFixedSize(); 565e8d8bef9SDimitry Andric unsigned LogStWidth = Log2_32(StWidthBits); 5660b57cec5SDimitry Andric assert(LogStWidth < 32); 5670b57cec5SDimitry Andric unsigned RoundWidth = 1 << LogStWidth; 568e8d8bef9SDimitry Andric assert(RoundWidth < StWidthBits); 569e8d8bef9SDimitry Andric unsigned ExtraWidth = StWidthBits - RoundWidth; 5700b57cec5SDimitry Andric assert(ExtraWidth < RoundWidth); 5710b57cec5SDimitry Andric assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 5720b57cec5SDimitry Andric "Store size not an integral number of bytes!"); 5730b57cec5SDimitry Andric EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 5740b57cec5SDimitry Andric EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 5750b57cec5SDimitry Andric SDValue Lo, Hi; 5760b57cec5SDimitry Andric unsigned IncrementSize; 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric if (DL.isLittleEndian()) { 5790b57cec5SDimitry Andric // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) 5800b57cec5SDimitry Andric // Store the bottom RoundWidth bits. 5810b57cec5SDimitry Andric Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 5825ffd83dbSDimitry Andric RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric // Store the remaining ExtraWidth bits. 5850b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 586e8d8bef9SDimitry Andric Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl); 5870b57cec5SDimitry Andric Hi = DAG.getNode( 5880b57cec5SDimitry Andric ISD::SRL, dl, Value.getValueType(), Value, 5890b57cec5SDimitry Andric DAG.getConstant(RoundWidth, dl, 5900b57cec5SDimitry Andric TLI.getShiftAmountTy(Value.getValueType(), DL))); 5915ffd83dbSDimitry Andric Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, 5925ffd83dbSDimitry Andric ST->getPointerInfo().getWithOffset(IncrementSize), 5935ffd83dbSDimitry Andric ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 5940b57cec5SDimitry Andric } else { 5950b57cec5SDimitry Andric // Big endian - avoid unaligned stores. 5960b57cec5SDimitry Andric // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X 5970b57cec5SDimitry Andric // Store the top RoundWidth bits. 5980b57cec5SDimitry Andric Hi = DAG.getNode( 5990b57cec5SDimitry Andric ISD::SRL, dl, Value.getValueType(), Value, 6000b57cec5SDimitry Andric DAG.getConstant(ExtraWidth, dl, 6010b57cec5SDimitry Andric TLI.getShiftAmountTy(Value.getValueType(), DL))); 6025ffd83dbSDimitry Andric Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT, 6035ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric // Store the remaining ExtraWidth bits. 6060b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 6070b57cec5SDimitry Andric Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 6080b57cec5SDimitry Andric DAG.getConstant(IncrementSize, dl, 6090b57cec5SDimitry Andric Ptr.getValueType())); 6105ffd83dbSDimitry Andric Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, 6115ffd83dbSDimitry Andric ST->getPointerInfo().getWithOffset(IncrementSize), 6125ffd83dbSDimitry Andric ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 6130b57cec5SDimitry Andric } 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric // The order of the stores doesn't matter. 6160b57cec5SDimitry Andric SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 6170b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 6180b57cec5SDimitry Andric } else { 6190b57cec5SDimitry Andric switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { 6200b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 6210b57cec5SDimitry Andric case TargetLowering::Legal: { 6220b57cec5SDimitry Andric EVT MemVT = ST->getMemoryVT(); 6230b57cec5SDimitry Andric // If this is an unaligned store and the target doesn't support it, 6240b57cec5SDimitry Andric // expand it. 6258bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 6260b57cec5SDimitry Andric *ST->getMemOperand())) { 6270b57cec5SDimitry Andric SDValue Result = TLI.expandUnalignedStore(ST, DAG); 6280b57cec5SDimitry Andric ReplaceNode(SDValue(ST, 0), Result); 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric break; 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric case TargetLowering::Custom: { 6330b57cec5SDimitry Andric SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 6340b57cec5SDimitry Andric if (Res && Res != SDValue(Node, 0)) 6350b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 6360b57cec5SDimitry Andric return; 6370b57cec5SDimitry Andric } 6380b57cec5SDimitry Andric case TargetLowering::Expand: 6390b57cec5SDimitry Andric assert(!StVT.isVector() && 6400b57cec5SDimitry Andric "Vector Stores are handled in LegalizeVectorOps"); 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric SDValue Result; 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric // TRUNCSTORE:i16 i32 -> STORE i16 6450b57cec5SDimitry Andric if (TLI.isTypeLegal(StVT)) { 6460b57cec5SDimitry Andric Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value); 6470b57cec5SDimitry Andric Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 6485ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6490b57cec5SDimitry Andric } else { 6500b57cec5SDimitry Andric // The in-memory type isn't legal. Truncate to the type it would promote 6510b57cec5SDimitry Andric // to, and then do a truncstore. 6520b57cec5SDimitry Andric Value = DAG.getNode(ISD::TRUNCATE, dl, 6530b57cec5SDimitry Andric TLI.getTypeToTransformTo(*DAG.getContext(), StVT), 6540b57cec5SDimitry Andric Value); 6555ffd83dbSDimitry Andric Result = 6565ffd83dbSDimitry Andric DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT, 6575ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 6610b57cec5SDimitry Andric break; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric } 6640b57cec5SDimitry Andric } 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) { 6670b57cec5SDimitry Andric LoadSDNode *LD = cast<LoadSDNode>(Node); 6680b57cec5SDimitry Andric SDValue Chain = LD->getChain(); // The chain. 6690b57cec5SDimitry Andric SDValue Ptr = LD->getBasePtr(); // The base pointer. 6700b57cec5SDimitry Andric SDValue Value; // The value returned by the load op. 6710b57cec5SDimitry Andric SDLoc dl(Node); 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric ISD::LoadExtType ExtType = LD->getExtensionType(); 6740b57cec5SDimitry Andric if (ExtType == ISD::NON_EXTLOAD) { 6750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n"); 6760b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(0); 6770b57cec5SDimitry Andric SDValue RVal = SDValue(Node, 0); 6780b57cec5SDimitry Andric SDValue RChain = SDValue(Node, 1); 6790b57cec5SDimitry Andric 6800b57cec5SDimitry Andric switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 6810b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 6820b57cec5SDimitry Andric case TargetLowering::Legal: { 6830b57cec5SDimitry Andric EVT MemVT = LD->getMemoryVT(); 6840b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 6850b57cec5SDimitry Andric // If this is an unaligned load and the target doesn't support it, 6860b57cec5SDimitry Andric // expand it. 6878bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 6880b57cec5SDimitry Andric *LD->getMemOperand())) { 6890b57cec5SDimitry Andric std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG); 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric break; 6920b57cec5SDimitry Andric } 6930b57cec5SDimitry Andric case TargetLowering::Custom: 6940b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(RVal, DAG)) { 6950b57cec5SDimitry Andric RVal = Res; 6960b57cec5SDimitry Andric RChain = Res.getValue(1); 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric break; 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric case TargetLowering::Promote: { 7010b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 7020b57cec5SDimitry Andric assert(NVT.getSizeInBits() == VT.getSizeInBits() && 7030b57cec5SDimitry Andric "Can only promote loads to same size type"); 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand()); 7060b57cec5SDimitry Andric RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res); 7070b57cec5SDimitry Andric RChain = Res.getValue(1); 7080b57cec5SDimitry Andric break; 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric if (RChain.getNode() != Node) { 7120b57cec5SDimitry Andric assert(RVal.getNode() != Node && "Load must be completely replaced"); 7130b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal); 7140b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain); 7150b57cec5SDimitry Andric if (UpdatedNodes) { 7160b57cec5SDimitry Andric UpdatedNodes->insert(RVal.getNode()); 7170b57cec5SDimitry Andric UpdatedNodes->insert(RChain.getNode()); 7180b57cec5SDimitry Andric } 7190b57cec5SDimitry Andric ReplacedNode(Node); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric return; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n"); 7250b57cec5SDimitry Andric EVT SrcVT = LD->getMemoryVT(); 726e8d8bef9SDimitry Andric TypeSize SrcWidth = SrcVT.getSizeInBits(); 7270b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags(); 7280b57cec5SDimitry Andric AAMDNodes AAInfo = LD->getAAInfo(); 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric if (SrcWidth != SrcVT.getStoreSizeInBits() && 7310b57cec5SDimitry Andric // Some targets pretend to have an i1 loading operation, and actually 7320b57cec5SDimitry Andric // load an i8. This trick is correct for ZEXTLOAD because the top 7 7330b57cec5SDimitry Andric // bits are guaranteed to be zero; it helps the optimizers understand 7340b57cec5SDimitry Andric // that these bits are zero. It is also useful for EXTLOAD, since it 7350b57cec5SDimitry Andric // tells the optimizers that those bits are undefined. It would be 7360b57cec5SDimitry Andric // nice to have an effective generic way of getting these benefits... 7370b57cec5SDimitry Andric // Until such a way is found, don't insist on promoting i1 here. 7380b57cec5SDimitry Andric (SrcVT != MVT::i1 || 7390b57cec5SDimitry Andric TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) == 7400b57cec5SDimitry Andric TargetLowering::Promote)) { 7410b57cec5SDimitry Andric // Promote to a byte-sized load if not loading an integral number of 7420b57cec5SDimitry Andric // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 7430b57cec5SDimitry Andric unsigned NewWidth = SrcVT.getStoreSizeInBits(); 7440b57cec5SDimitry Andric EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); 7450b57cec5SDimitry Andric SDValue Ch; 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric // The extra bits are guaranteed to be zero, since we stored them that 7480b57cec5SDimitry Andric // way. A zext load from NVT thus automatically gives zext from SrcVT. 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric ISD::LoadExtType NewExtType = 7510b57cec5SDimitry Andric ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; 7520b57cec5SDimitry Andric 7535ffd83dbSDimitry Andric SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), 7545ffd83dbSDimitry Andric Chain, Ptr, LD->getPointerInfo(), NVT, 7555ffd83dbSDimitry Andric LD->getOriginalAlign(), MMOFlags, AAInfo); 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric Ch = Result.getValue(1); // The chain. 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric if (ExtType == ISD::SEXTLOAD) 7600b57cec5SDimitry Andric // Having the top bits zero doesn't help when sign extending. 7610b57cec5SDimitry Andric Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 7620b57cec5SDimitry Andric Result.getValueType(), 7630b57cec5SDimitry Andric Result, DAG.getValueType(SrcVT)); 7640b57cec5SDimitry Andric else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) 7650b57cec5SDimitry Andric // All the top bits are guaranteed to be zero - inform the optimizers. 7660b57cec5SDimitry Andric Result = DAG.getNode(ISD::AssertZext, dl, 7670b57cec5SDimitry Andric Result.getValueType(), Result, 7680b57cec5SDimitry Andric DAG.getValueType(SrcVT)); 7690b57cec5SDimitry Andric 7700b57cec5SDimitry Andric Value = Result; 7710b57cec5SDimitry Andric Chain = Ch; 772e8d8bef9SDimitry Andric } else if (!isPowerOf2_64(SrcWidth.getKnownMinSize())) { 7730b57cec5SDimitry Andric // If not loading a power-of-2 number of bits, expand as two loads. 7740b57cec5SDimitry Andric assert(!SrcVT.isVector() && "Unsupported extload!"); 775e8d8bef9SDimitry Andric unsigned SrcWidthBits = SrcWidth.getFixedSize(); 776e8d8bef9SDimitry Andric unsigned LogSrcWidth = Log2_32(SrcWidthBits); 7770b57cec5SDimitry Andric assert(LogSrcWidth < 32); 7780b57cec5SDimitry Andric unsigned RoundWidth = 1 << LogSrcWidth; 779e8d8bef9SDimitry Andric assert(RoundWidth < SrcWidthBits); 780e8d8bef9SDimitry Andric unsigned ExtraWidth = SrcWidthBits - RoundWidth; 7810b57cec5SDimitry Andric assert(ExtraWidth < RoundWidth); 7820b57cec5SDimitry Andric assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 7830b57cec5SDimitry Andric "Load size not an integral number of bytes!"); 7840b57cec5SDimitry Andric EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 7850b57cec5SDimitry Andric EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 7860b57cec5SDimitry Andric SDValue Lo, Hi, Ch; 7870b57cec5SDimitry Andric unsigned IncrementSize; 7880b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric if (DL.isLittleEndian()) { 7910b57cec5SDimitry Andric // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) 7920b57cec5SDimitry Andric // Load the bottom RoundWidth bits. 7930b57cec5SDimitry Andric Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 7945ffd83dbSDimitry Andric LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(), 7955ffd83dbSDimitry Andric MMOFlags, AAInfo); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric // Load the remaining ExtraWidth bits. 7980b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 799e8d8bef9SDimitry Andric Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl); 8000b57cec5SDimitry Andric Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 8010b57cec5SDimitry Andric LD->getPointerInfo().getWithOffset(IncrementSize), 8025ffd83dbSDimitry Andric ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo); 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric // Build a factor node to remember that this load is independent of 8050b57cec5SDimitry Andric // the other one. 8060b57cec5SDimitry Andric Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 8070b57cec5SDimitry Andric Hi.getValue(1)); 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric // Move the top bits to the right place. 8100b57cec5SDimitry Andric Hi = DAG.getNode( 8110b57cec5SDimitry Andric ISD::SHL, dl, Hi.getValueType(), Hi, 8120b57cec5SDimitry Andric DAG.getConstant(RoundWidth, dl, 8130b57cec5SDimitry Andric TLI.getShiftAmountTy(Hi.getValueType(), DL))); 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric // Join the hi and lo parts. 8160b57cec5SDimitry Andric Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 8170b57cec5SDimitry Andric } else { 8180b57cec5SDimitry Andric // Big endian - avoid unaligned loads. 8190b57cec5SDimitry Andric // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 8200b57cec5SDimitry Andric // Load the top RoundWidth bits. 8210b57cec5SDimitry Andric Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 8225ffd83dbSDimitry Andric LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(), 8235ffd83dbSDimitry Andric MMOFlags, AAInfo); 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric // Load the remaining ExtraWidth bits. 8260b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 827e8d8bef9SDimitry Andric Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::Fixed(IncrementSize), dl); 8280b57cec5SDimitry Andric Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 8290b57cec5SDimitry Andric LD->getPointerInfo().getWithOffset(IncrementSize), 8305ffd83dbSDimitry Andric ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo); 8310b57cec5SDimitry Andric 8320b57cec5SDimitry Andric // Build a factor node to remember that this load is independent of 8330b57cec5SDimitry Andric // the other one. 8340b57cec5SDimitry Andric Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 8350b57cec5SDimitry Andric Hi.getValue(1)); 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric // Move the top bits to the right place. 8380b57cec5SDimitry Andric Hi = DAG.getNode( 8390b57cec5SDimitry Andric ISD::SHL, dl, Hi.getValueType(), Hi, 8400b57cec5SDimitry Andric DAG.getConstant(ExtraWidth, dl, 8410b57cec5SDimitry Andric TLI.getShiftAmountTy(Hi.getValueType(), DL))); 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric // Join the hi and lo parts. 8440b57cec5SDimitry Andric Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andric Chain = Ch; 8480b57cec5SDimitry Andric } else { 8490b57cec5SDimitry Andric bool isCustom = false; 8500b57cec5SDimitry Andric switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0), 8510b57cec5SDimitry Andric SrcVT.getSimpleVT())) { 8520b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 8530b57cec5SDimitry Andric case TargetLowering::Custom: 8540b57cec5SDimitry Andric isCustom = true; 8550b57cec5SDimitry Andric LLVM_FALLTHROUGH; 8560b57cec5SDimitry Andric case TargetLowering::Legal: 8570b57cec5SDimitry Andric Value = SDValue(Node, 0); 8580b57cec5SDimitry Andric Chain = SDValue(Node, 1); 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric if (isCustom) { 8610b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 8620b57cec5SDimitry Andric Value = Res; 8630b57cec5SDimitry Andric Chain = Res.getValue(1); 8640b57cec5SDimitry Andric } 8650b57cec5SDimitry Andric } else { 8660b57cec5SDimitry Andric // If this is an unaligned load and the target doesn't support it, 8670b57cec5SDimitry Andric // expand it. 8680b57cec5SDimitry Andric EVT MemVT = LD->getMemoryVT(); 8690b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 8700b57cec5SDimitry Andric if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, 8710b57cec5SDimitry Andric *LD->getMemOperand())) { 8720b57cec5SDimitry Andric std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG); 8730b57cec5SDimitry Andric } 8740b57cec5SDimitry Andric } 8750b57cec5SDimitry Andric break; 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric case TargetLowering::Expand: { 8780b57cec5SDimitry Andric EVT DestVT = Node->getValueType(0); 8790b57cec5SDimitry Andric if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) { 8800b57cec5SDimitry Andric // If the source type is not legal, see if there is a legal extload to 8810b57cec5SDimitry Andric // an intermediate type that we can then extend further. 8820b57cec5SDimitry Andric EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT()); 8830b57cec5SDimitry Andric if (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT? 8840b57cec5SDimitry Andric TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT)) { 8850b57cec5SDimitry Andric // If we are loading a legal type, this is a non-extload followed by a 8860b57cec5SDimitry Andric // full extend. 8870b57cec5SDimitry Andric ISD::LoadExtType MidExtType = 8880b57cec5SDimitry Andric (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType; 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr, 8910b57cec5SDimitry Andric SrcVT, LD->getMemOperand()); 8920b57cec5SDimitry Andric unsigned ExtendOp = 8930b57cec5SDimitry Andric ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType); 8940b57cec5SDimitry Andric Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load); 8950b57cec5SDimitry Andric Chain = Load.getValue(1); 8960b57cec5SDimitry Andric break; 8970b57cec5SDimitry Andric } 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric // Handle the special case of fp16 extloads. EXTLOAD doesn't have the 9000b57cec5SDimitry Andric // normal undefined upper bits behavior to allow using an in-reg extend 9010b57cec5SDimitry Andric // with the illegal FP type, so load as an integer and do the 9020b57cec5SDimitry Andric // from-integer conversion. 9030b57cec5SDimitry Andric if (SrcVT.getScalarType() == MVT::f16) { 9040b57cec5SDimitry Andric EVT ISrcVT = SrcVT.changeTypeToInteger(); 9050b57cec5SDimitry Andric EVT IDestVT = DestVT.changeTypeToInteger(); 9068bcb0991SDimitry Andric EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT()); 9070b57cec5SDimitry Andric 9088bcb0991SDimitry Andric SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain, 9098bcb0991SDimitry Andric Ptr, ISrcVT, LD->getMemOperand()); 9100b57cec5SDimitry Andric Value = DAG.getNode(ISD::FP16_TO_FP, dl, DestVT, Result); 9110b57cec5SDimitry Andric Chain = Result.getValue(1); 9120b57cec5SDimitry Andric break; 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric } 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric assert(!SrcVT.isVector() && 9170b57cec5SDimitry Andric "Vector Loads are handled in LegalizeVectorOps"); 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric // FIXME: This does not work for vectors on most targets. Sign- 9200b57cec5SDimitry Andric // and zero-extend operations are currently folded into extending 9210b57cec5SDimitry Andric // loads, whether they are legal or not, and then we end up here 9220b57cec5SDimitry Andric // without any support for legalizing them. 9230b57cec5SDimitry Andric assert(ExtType != ISD::EXTLOAD && 9240b57cec5SDimitry Andric "EXTLOAD should always be supported!"); 9250b57cec5SDimitry Andric // Turn the unsupported load into an EXTLOAD followed by an 9260b57cec5SDimitry Andric // explicit zero/sign extend inreg. 9270b57cec5SDimitry Andric SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, 9280b57cec5SDimitry Andric Node->getValueType(0), 9290b57cec5SDimitry Andric Chain, Ptr, SrcVT, 9300b57cec5SDimitry Andric LD->getMemOperand()); 9310b57cec5SDimitry Andric SDValue ValRes; 9320b57cec5SDimitry Andric if (ExtType == ISD::SEXTLOAD) 9330b57cec5SDimitry Andric ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 9340b57cec5SDimitry Andric Result.getValueType(), 9350b57cec5SDimitry Andric Result, DAG.getValueType(SrcVT)); 9360b57cec5SDimitry Andric else 9375ffd83dbSDimitry Andric ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT); 9380b57cec5SDimitry Andric Value = ValRes; 9390b57cec5SDimitry Andric Chain = Result.getValue(1); 9400b57cec5SDimitry Andric break; 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric } 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric // Since loads produce two values, make sure to remember that we legalized 9460b57cec5SDimitry Andric // both of them. 9470b57cec5SDimitry Andric if (Chain.getNode() != Node) { 9480b57cec5SDimitry Andric assert(Value.getNode() != Node && "Load must be completely replaced"); 9490b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value); 9500b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 9510b57cec5SDimitry Andric if (UpdatedNodes) { 9520b57cec5SDimitry Andric UpdatedNodes->insert(Value.getNode()); 9530b57cec5SDimitry Andric UpdatedNodes->insert(Chain.getNode()); 9540b57cec5SDimitry Andric } 9550b57cec5SDimitry Andric ReplacedNode(Node); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric /// Return a legal replacement for the given operation, with all legal operands. 9600b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeOp(SDNode *Node) { 9610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG)); 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric // Allow illegal target nodes and illegal registers. 9640b57cec5SDimitry Andric if (Node->getOpcode() == ISD::TargetConstant || 9650b57cec5SDimitry Andric Node->getOpcode() == ISD::Register) 9660b57cec5SDimitry Andric return; 9670b57cec5SDimitry Andric 9680b57cec5SDimitry Andric #ifndef NDEBUG 9690b57cec5SDimitry Andric for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 9708bcb0991SDimitry Andric assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) == 9718bcb0991SDimitry Andric TargetLowering::TypeLegal && 9720b57cec5SDimitry Andric "Unexpected illegal type!"); 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) 9750b57cec5SDimitry Andric assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) == 9760b57cec5SDimitry Andric TargetLowering::TypeLegal || 9770b57cec5SDimitry Andric Op.getOpcode() == ISD::TargetConstant || 9780b57cec5SDimitry Andric Op.getOpcode() == ISD::Register) && 9790b57cec5SDimitry Andric "Unexpected illegal type!"); 9800b57cec5SDimitry Andric #endif 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric // Figure out the correct action; the way to query this varies by opcode 9830b57cec5SDimitry Andric TargetLowering::LegalizeAction Action = TargetLowering::Legal; 9840b57cec5SDimitry Andric bool SimpleFinishLegalizing = true; 9850b57cec5SDimitry Andric switch (Node->getOpcode()) { 9860b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 9870b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 9880b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 9890b57cec5SDimitry Andric case ISD::STACKSAVE: 9900b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 9910b57cec5SDimitry Andric break; 9920b57cec5SDimitry Andric case ISD::GET_DYNAMIC_AREA_OFFSET: 9930b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 9940b57cec5SDimitry Andric Node->getValueType(0)); 9950b57cec5SDimitry Andric break; 9960b57cec5SDimitry Andric case ISD::VAARG: 9970b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 9980b57cec5SDimitry Andric Node->getValueType(0)); 9990b57cec5SDimitry Andric if (Action != TargetLowering::Promote) 10000b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 10010b57cec5SDimitry Andric break; 10020b57cec5SDimitry Andric case ISD::FP_TO_FP16: 10030b57cec5SDimitry Andric case ISD::SINT_TO_FP: 10040b57cec5SDimitry Andric case ISD::UINT_TO_FP: 10050b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 10060b57cec5SDimitry Andric case ISD::LROUND: 10070b57cec5SDimitry Andric case ISD::LLROUND: 10080b57cec5SDimitry Andric case ISD::LRINT: 10090b57cec5SDimitry Andric case ISD::LLRINT: 10100b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10110b57cec5SDimitry Andric Node->getOperand(0).getValueType()); 10120b57cec5SDimitry Andric break; 10135ffd83dbSDimitry Andric case ISD::STRICT_FP_TO_FP16: 1014480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 1015480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 1016480093f4SDimitry Andric case ISD::STRICT_LRINT: 1017480093f4SDimitry Andric case ISD::STRICT_LLRINT: 1018480093f4SDimitry Andric case ISD::STRICT_LROUND: 1019480093f4SDimitry Andric case ISD::STRICT_LLROUND: 1020480093f4SDimitry Andric // These pseudo-ops are the same as the other STRICT_ ops except 1021480093f4SDimitry Andric // they are registered with setOperationAction() using the input type 1022480093f4SDimitry Andric // instead of the output type. 1023480093f4SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 1024480093f4SDimitry Andric Node->getOperand(1).getValueType()); 1025480093f4SDimitry Andric break; 10260b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: { 10270b57cec5SDimitry Andric EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); 10280b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), InnerType); 10290b57cec5SDimitry Andric break; 10300b57cec5SDimitry Andric } 10310b57cec5SDimitry Andric case ISD::ATOMIC_STORE: 10320b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10330b57cec5SDimitry Andric Node->getOperand(2).getValueType()); 10340b57cec5SDimitry Andric break; 10350b57cec5SDimitry Andric case ISD::SELECT_CC: 1036480093f4SDimitry Andric case ISD::STRICT_FSETCC: 1037480093f4SDimitry Andric case ISD::STRICT_FSETCCS: 10380b57cec5SDimitry Andric case ISD::SETCC: 10390b57cec5SDimitry Andric case ISD::BR_CC: { 10400b57cec5SDimitry Andric unsigned CCOperand = Node->getOpcode() == ISD::SELECT_CC ? 4 : 1041480093f4SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCC ? 3 : 1042480093f4SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS ? 3 : 10430b57cec5SDimitry Andric Node->getOpcode() == ISD::SETCC ? 2 : 1; 1044480093f4SDimitry Andric unsigned CompareOperand = Node->getOpcode() == ISD::BR_CC ? 2 : 1045480093f4SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCC ? 1 : 1046480093f4SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS ? 1 : 0; 10470b57cec5SDimitry Andric MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType(); 10480b57cec5SDimitry Andric ISD::CondCode CCCode = 10490b57cec5SDimitry Andric cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); 10500b57cec5SDimitry Andric Action = TLI.getCondCodeAction(CCCode, OpVT); 10510b57cec5SDimitry Andric if (Action == TargetLowering::Legal) { 10520b57cec5SDimitry Andric if (Node->getOpcode() == ISD::SELECT_CC) 10530b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10540b57cec5SDimitry Andric Node->getValueType(0)); 10550b57cec5SDimitry Andric else 10560b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), OpVT); 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric break; 10590b57cec5SDimitry Andric } 10600b57cec5SDimitry Andric case ISD::LOAD: 10610b57cec5SDimitry Andric case ISD::STORE: 10620b57cec5SDimitry Andric // FIXME: Model these properly. LOAD and STORE are complicated, and 10630b57cec5SDimitry Andric // STORE expects the unlegalized operand in some cases. 10640b57cec5SDimitry Andric SimpleFinishLegalizing = false; 10650b57cec5SDimitry Andric break; 10660b57cec5SDimitry Andric case ISD::CALLSEQ_START: 10670b57cec5SDimitry Andric case ISD::CALLSEQ_END: 10680b57cec5SDimitry Andric // FIXME: This shouldn't be necessary. These nodes have special properties 10690b57cec5SDimitry Andric // dealing with the recursive nature of legalization. Removing this 10700b57cec5SDimitry Andric // special case should be done as part of making LegalizeDAG non-recursive. 10710b57cec5SDimitry Andric SimpleFinishLegalizing = false; 10720b57cec5SDimitry Andric break; 10730b57cec5SDimitry Andric case ISD::EXTRACT_ELEMENT: 10740b57cec5SDimitry Andric case ISD::FLT_ROUNDS_: 10750b57cec5SDimitry Andric case ISD::MERGE_VALUES: 10760b57cec5SDimitry Andric case ISD::EH_RETURN: 10770b57cec5SDimitry Andric case ISD::FRAME_TO_ARGS_OFFSET: 10780b57cec5SDimitry Andric case ISD::EH_DWARF_CFA: 10790b57cec5SDimitry Andric case ISD::EH_SJLJ_SETJMP: 10800b57cec5SDimitry Andric case ISD::EH_SJLJ_LONGJMP: 10810b57cec5SDimitry Andric case ISD::EH_SJLJ_SETUP_DISPATCH: 10820b57cec5SDimitry Andric // These operations lie about being legal: when they claim to be legal, 10830b57cec5SDimitry Andric // they should actually be expanded. 10840b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 10850b57cec5SDimitry Andric if (Action == TargetLowering::Legal) 10860b57cec5SDimitry Andric Action = TargetLowering::Expand; 10870b57cec5SDimitry Andric break; 10880b57cec5SDimitry Andric case ISD::INIT_TRAMPOLINE: 10890b57cec5SDimitry Andric case ISD::ADJUST_TRAMPOLINE: 10900b57cec5SDimitry Andric case ISD::FRAMEADDR: 10910b57cec5SDimitry Andric case ISD::RETURNADDR: 10920b57cec5SDimitry Andric case ISD::ADDROFRETURNADDR: 10930b57cec5SDimitry Andric case ISD::SPONENTRY: 10940b57cec5SDimitry Andric // These operations lie about being legal: when they claim to be legal, 10950b57cec5SDimitry Andric // they should actually be custom-lowered. 10960b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 10970b57cec5SDimitry Andric if (Action == TargetLowering::Legal) 10980b57cec5SDimitry Andric Action = TargetLowering::Custom; 10990b57cec5SDimitry Andric break; 11000b57cec5SDimitry Andric case ISD::READCYCLECOUNTER: 11010b57cec5SDimitry Andric // READCYCLECOUNTER returns an i64, even if type legalization might have 11020b57cec5SDimitry Andric // expanded that to several smaller types. 11030b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64); 11040b57cec5SDimitry Andric break; 11050b57cec5SDimitry Andric case ISD::READ_REGISTER: 11060b57cec5SDimitry Andric case ISD::WRITE_REGISTER: 11070b57cec5SDimitry Andric // Named register is legal in the DAG, but blocked by register name 11080b57cec5SDimitry Andric // selection if not implemented by target (to chose the correct register) 11090b57cec5SDimitry Andric // They'll be converted to Copy(To/From)Reg. 11100b57cec5SDimitry Andric Action = TargetLowering::Legal; 11110b57cec5SDimitry Andric break; 1112e8d8bef9SDimitry Andric case ISD::UBSANTRAP: 1113e8d8bef9SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1114e8d8bef9SDimitry Andric if (Action == TargetLowering::Expand) { 1115e8d8bef9SDimitry Andric // replace ISD::UBSANTRAP with ISD::TRAP 1116e8d8bef9SDimitry Andric SDValue NewVal; 1117e8d8bef9SDimitry Andric NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 1118e8d8bef9SDimitry Andric Node->getOperand(0)); 1119e8d8bef9SDimitry Andric ReplaceNode(Node, NewVal.getNode()); 1120e8d8bef9SDimitry Andric LegalizeOp(NewVal.getNode()); 1121e8d8bef9SDimitry Andric return; 1122e8d8bef9SDimitry Andric } 1123e8d8bef9SDimitry Andric break; 11240b57cec5SDimitry Andric case ISD::DEBUGTRAP: 11250b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11260b57cec5SDimitry Andric if (Action == TargetLowering::Expand) { 11270b57cec5SDimitry Andric // replace ISD::DEBUGTRAP with ISD::TRAP 11280b57cec5SDimitry Andric SDValue NewVal; 11290b57cec5SDimitry Andric NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 11300b57cec5SDimitry Andric Node->getOperand(0)); 11310b57cec5SDimitry Andric ReplaceNode(Node, NewVal.getNode()); 11320b57cec5SDimitry Andric LegalizeOp(NewVal.getNode()); 11330b57cec5SDimitry Andric return; 11340b57cec5SDimitry Andric } 11350b57cec5SDimitry Andric break; 11360b57cec5SDimitry Andric case ISD::SADDSAT: 11370b57cec5SDimitry Andric case ISD::UADDSAT: 11380b57cec5SDimitry Andric case ISD::SSUBSAT: 1139e8d8bef9SDimitry Andric case ISD::USUBSAT: 1140e8d8bef9SDimitry Andric case ISD::SSHLSAT: 1141e8d8bef9SDimitry Andric case ISD::USHLSAT: 1142e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 1143e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 11440b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11450b57cec5SDimitry Andric break; 11460b57cec5SDimitry Andric case ISD::SMULFIX: 11470b57cec5SDimitry Andric case ISD::SMULFIXSAT: 11488bcb0991SDimitry Andric case ISD::UMULFIX: 1149480093f4SDimitry Andric case ISD::UMULFIXSAT: 1150480093f4SDimitry Andric case ISD::SDIVFIX: 11515ffd83dbSDimitry Andric case ISD::SDIVFIXSAT: 11525ffd83dbSDimitry Andric case ISD::UDIVFIX: 11535ffd83dbSDimitry Andric case ISD::UDIVFIXSAT: { 11540b57cec5SDimitry Andric unsigned Scale = Node->getConstantOperandVal(2); 11550b57cec5SDimitry Andric Action = TLI.getFixedPointOperationAction(Node->getOpcode(), 11560b57cec5SDimitry Andric Node->getValueType(0), Scale); 11570b57cec5SDimitry Andric break; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric case ISD::MSCATTER: 11600b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 11610b57cec5SDimitry Andric cast<MaskedScatterSDNode>(Node)->getValue().getValueType()); 11620b57cec5SDimitry Andric break; 11630b57cec5SDimitry Andric case ISD::MSTORE: 11640b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 11650b57cec5SDimitry Andric cast<MaskedStoreSDNode>(Node)->getValue().getValueType()); 11660b57cec5SDimitry Andric break; 1167349cc55cSDimitry Andric case ISD::VP_SCATTER: 1168349cc55cSDimitry Andric Action = TLI.getOperationAction( 1169349cc55cSDimitry Andric Node->getOpcode(), 1170349cc55cSDimitry Andric cast<VPScatterSDNode>(Node)->getValue().getValueType()); 1171349cc55cSDimitry Andric break; 1172349cc55cSDimitry Andric case ISD::VP_STORE: 1173349cc55cSDimitry Andric Action = TLI.getOperationAction( 1174349cc55cSDimitry Andric Node->getOpcode(), 1175349cc55cSDimitry Andric cast<VPStoreSDNode>(Node)->getValue().getValueType()); 1176349cc55cSDimitry Andric break; 11770b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 11780b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 11790b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 11800b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 11810b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 11820b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 11830b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 11840b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 11850b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 11860b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 11870b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 11880b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 11890b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 11900b57cec5SDimitry Andric Action = TLI.getOperationAction( 11910b57cec5SDimitry Andric Node->getOpcode(), Node->getOperand(0).getValueType()); 11920b57cec5SDimitry Andric break; 1193e8d8bef9SDimitry Andric case ISD::VECREDUCE_SEQ_FADD: 1194349cc55cSDimitry Andric case ISD::VECREDUCE_SEQ_FMUL: 1195349cc55cSDimitry Andric case ISD::VP_REDUCE_FADD: 1196349cc55cSDimitry Andric case ISD::VP_REDUCE_FMUL: 1197349cc55cSDimitry Andric case ISD::VP_REDUCE_ADD: 1198349cc55cSDimitry Andric case ISD::VP_REDUCE_MUL: 1199349cc55cSDimitry Andric case ISD::VP_REDUCE_AND: 1200349cc55cSDimitry Andric case ISD::VP_REDUCE_OR: 1201349cc55cSDimitry Andric case ISD::VP_REDUCE_XOR: 1202349cc55cSDimitry Andric case ISD::VP_REDUCE_SMAX: 1203349cc55cSDimitry Andric case ISD::VP_REDUCE_SMIN: 1204349cc55cSDimitry Andric case ISD::VP_REDUCE_UMAX: 1205349cc55cSDimitry Andric case ISD::VP_REDUCE_UMIN: 1206349cc55cSDimitry Andric case ISD::VP_REDUCE_FMAX: 1207349cc55cSDimitry Andric case ISD::VP_REDUCE_FMIN: 1208349cc55cSDimitry Andric case ISD::VP_REDUCE_SEQ_FADD: 1209349cc55cSDimitry Andric case ISD::VP_REDUCE_SEQ_FMUL: 1210e8d8bef9SDimitry Andric Action = TLI.getOperationAction( 1211e8d8bef9SDimitry Andric Node->getOpcode(), Node->getOperand(1).getValueType()); 1212e8d8bef9SDimitry Andric break; 12130b57cec5SDimitry Andric default: 12140b57cec5SDimitry Andric if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 12150b57cec5SDimitry Andric Action = TargetLowering::Legal; 12160b57cec5SDimitry Andric } else { 12170b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric break; 12200b57cec5SDimitry Andric } 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric if (SimpleFinishLegalizing) { 12230b57cec5SDimitry Andric SDNode *NewNode = Node; 12240b57cec5SDimitry Andric switch (Node->getOpcode()) { 12250b57cec5SDimitry Andric default: break; 12260b57cec5SDimitry Andric case ISD::SHL: 12270b57cec5SDimitry Andric case ISD::SRL: 12280b57cec5SDimitry Andric case ISD::SRA: 12290b57cec5SDimitry Andric case ISD::ROTL: 12300b57cec5SDimitry Andric case ISD::ROTR: { 12310b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12320b57cec5SDimitry Andric // to the appropriate width. 12330b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12340b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12350b57cec5SDimitry Andric if (!Op1.getValueType().isVector()) { 12360b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1); 12370b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12380b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12390b57cec5SDimitry Andric // to update the parent node. 12400b57cec5SDimitry Andric // Do not try to legalize SAO here! It will be automatically legalized 12410b57cec5SDimitry Andric // in the next round. 12420b57cec5SDimitry Andric if (SAO != Op1) 12430b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO); 12440b57cec5SDimitry Andric } 12450b57cec5SDimitry Andric } 12460b57cec5SDimitry Andric break; 12470b57cec5SDimitry Andric case ISD::FSHL: 12480b57cec5SDimitry Andric case ISD::FSHR: 12490b57cec5SDimitry Andric case ISD::SRL_PARTS: 12500b57cec5SDimitry Andric case ISD::SRA_PARTS: 12510b57cec5SDimitry Andric case ISD::SHL_PARTS: { 12520b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12530b57cec5SDimitry Andric // to the appropriate width. 12540b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12550b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12560b57cec5SDimitry Andric SDValue Op2 = Node->getOperand(2); 12570b57cec5SDimitry Andric if (!Op2.getValueType().isVector()) { 12580b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2); 12590b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12600b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12610b57cec5SDimitry Andric // to update the parent node. 12620b57cec5SDimitry Andric if (SAO != Op2) 12630b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO); 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric break; 12660b57cec5SDimitry Andric } 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric if (NewNode != Node) { 12700b57cec5SDimitry Andric ReplaceNode(Node, NewNode); 12710b57cec5SDimitry Andric Node = NewNode; 12720b57cec5SDimitry Andric } 12730b57cec5SDimitry Andric switch (Action) { 12740b57cec5SDimitry Andric case TargetLowering::Legal: 12750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 12760b57cec5SDimitry Andric return; 12770b57cec5SDimitry Andric case TargetLowering::Custom: 12780b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 12790b57cec5SDimitry Andric // FIXME: The handling for custom lowering with multiple results is 12800b57cec5SDimitry Andric // a complete mess. 12810b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 12820b57cec5SDimitry Andric if (!(Res.getNode() != Node || Res.getResNo() != 0)) 12830b57cec5SDimitry Andric return; 12840b57cec5SDimitry Andric 12850b57cec5SDimitry Andric if (Node->getNumValues() == 1) { 1286fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1287fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1288fe6060f1SDimitry Andric assert((Res.getValueType() == Node->getValueType(0) || 1289fe6060f1SDimitry Andric Node->getValueType(0) == MVT::Glue) && 1290fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 12910b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 12920b57cec5SDimitry Andric // We can just directly replace this node with the lowered value. 12930b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 12940b57cec5SDimitry Andric return; 12950b57cec5SDimitry Andric } 12960b57cec5SDimitry Andric 12970b57cec5SDimitry Andric SmallVector<SDValue, 8> ResultVals; 1298fe6060f1SDimitry Andric for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 1299fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1300fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1301fe6060f1SDimitry Andric assert((Res->getValueType(i) == Node->getValueType(i) || 1302fe6060f1SDimitry Andric Node->getValueType(i) == MVT::Glue) && 1303fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 13040b57cec5SDimitry Andric ResultVals.push_back(Res.getValue(i)); 1305fe6060f1SDimitry Andric } 13060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 13070b57cec5SDimitry Andric ReplaceNode(Node, ResultVals.data()); 13080b57cec5SDimitry Andric return; 13090b57cec5SDimitry Andric } 13100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 13110b57cec5SDimitry Andric LLVM_FALLTHROUGH; 13120b57cec5SDimitry Andric case TargetLowering::Expand: 13130b57cec5SDimitry Andric if (ExpandNode(Node)) 13140b57cec5SDimitry Andric return; 13150b57cec5SDimitry Andric LLVM_FALLTHROUGH; 13160b57cec5SDimitry Andric case TargetLowering::LibCall: 13170b57cec5SDimitry Andric ConvertNodeToLibcall(Node); 13180b57cec5SDimitry Andric return; 13190b57cec5SDimitry Andric case TargetLowering::Promote: 13200b57cec5SDimitry Andric PromoteNode(Node); 13210b57cec5SDimitry Andric return; 13220b57cec5SDimitry Andric } 13230b57cec5SDimitry Andric } 13240b57cec5SDimitry Andric 13250b57cec5SDimitry Andric switch (Node->getOpcode()) { 13260b57cec5SDimitry Andric default: 13270b57cec5SDimitry Andric #ifndef NDEBUG 13280b57cec5SDimitry Andric dbgs() << "NODE: "; 13290b57cec5SDimitry Andric Node->dump( &DAG); 13300b57cec5SDimitry Andric dbgs() << "\n"; 13310b57cec5SDimitry Andric #endif 13320b57cec5SDimitry Andric llvm_unreachable("Do not know how to legalize this operator!"); 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric case ISD::CALLSEQ_START: 13350b57cec5SDimitry Andric case ISD::CALLSEQ_END: 13360b57cec5SDimitry Andric break; 13370b57cec5SDimitry Andric case ISD::LOAD: 13380b57cec5SDimitry Andric return LegalizeLoadOps(Node); 13390b57cec5SDimitry Andric case ISD::STORE: 13400b57cec5SDimitry Andric return LegalizeStoreOps(Node); 13410b57cec5SDimitry Andric } 13420b57cec5SDimitry Andric } 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 13450b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 13460b57cec5SDimitry Andric SDValue Idx = Op.getOperand(1); 13470b57cec5SDimitry Andric SDLoc dl(Op); 13480b57cec5SDimitry Andric 13490b57cec5SDimitry Andric // Before we generate a new store to a temporary stack slot, see if there is 13500b57cec5SDimitry Andric // already one that we can use. There often is because when we scalarize 13510b57cec5SDimitry Andric // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole 13520b57cec5SDimitry Andric // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in 13530b57cec5SDimitry Andric // the vector. If all are expanded here, we don't want one store per vector 13540b57cec5SDimitry Andric // element. 13550b57cec5SDimitry Andric 13560b57cec5SDimitry Andric // Caches for hasPredecessorHelper 13570b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 32> Visited; 13580b57cec5SDimitry Andric SmallVector<const SDNode *, 16> Worklist; 13590b57cec5SDimitry Andric Visited.insert(Op.getNode()); 13600b57cec5SDimitry Andric Worklist.push_back(Idx.getNode()); 13610b57cec5SDimitry Andric SDValue StackPtr, Ch; 1362349cc55cSDimitry Andric for (SDNode *User : Vec.getNode()->uses()) { 13630b57cec5SDimitry Andric if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) { 13640b57cec5SDimitry Andric if (ST->isIndexed() || ST->isTruncatingStore() || 13650b57cec5SDimitry Andric ST->getValue() != Vec) 13660b57cec5SDimitry Andric continue; 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric // Make sure that nothing else could have stored into the destination of 13690b57cec5SDimitry Andric // this store. 13700b57cec5SDimitry Andric if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode())) 13710b57cec5SDimitry Andric continue; 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric // If the index is dependent on the store we will introduce a cycle when 13740b57cec5SDimitry Andric // creating the load (the load uses the index, and by replacing the chain 13750b57cec5SDimitry Andric // we will make the index dependent on the load). Also, the store might be 13760b57cec5SDimitry Andric // dependent on the extractelement and introduce a cycle when creating 13770b57cec5SDimitry Andric // the load. 13780b57cec5SDimitry Andric if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) || 13790b57cec5SDimitry Andric ST->hasPredecessor(Op.getNode())) 13800b57cec5SDimitry Andric continue; 13810b57cec5SDimitry Andric 13820b57cec5SDimitry Andric StackPtr = ST->getBasePtr(); 13830b57cec5SDimitry Andric Ch = SDValue(ST, 0); 13840b57cec5SDimitry Andric break; 13850b57cec5SDimitry Andric } 13860b57cec5SDimitry Andric } 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric if (!Ch.getNode()) { 13910b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 13920b57cec5SDimitry Andric StackPtr = DAG.CreateStackTemporary(VecVT); 13930b57cec5SDimitry Andric Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, 13940b57cec5SDimitry Andric MachinePointerInfo()); 13950b57cec5SDimitry Andric } 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric SDValue NewLoad; 13980b57cec5SDimitry Andric 1399fe6060f1SDimitry Andric if (Op.getValueType().isVector()) { 1400fe6060f1SDimitry Andric StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, 1401fe6060f1SDimitry Andric Op.getValueType(), Idx); 14020b57cec5SDimitry Andric NewLoad = 14030b57cec5SDimitry Andric DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, MachinePointerInfo()); 1404fe6060f1SDimitry Andric } else { 1405fe6060f1SDimitry Andric StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx); 14060b57cec5SDimitry Andric NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, 14070b57cec5SDimitry Andric MachinePointerInfo(), 14080b57cec5SDimitry Andric VecVT.getVectorElementType()); 1409fe6060f1SDimitry Andric } 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric // Replace the chain going out of the store, by the one out of the load. 14120b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1)); 14130b57cec5SDimitry Andric 14140b57cec5SDimitry Andric // We introduced a cycle though, so update the loads operands, making sure 14150b57cec5SDimitry Andric // to use the original store's chain as an incoming chain. 14160b57cec5SDimitry Andric SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(), 14170b57cec5SDimitry Andric NewLoad->op_end()); 14180b57cec5SDimitry Andric NewLoadOperands[0] = Ch; 14190b57cec5SDimitry Andric NewLoad = 14200b57cec5SDimitry Andric SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0); 14210b57cec5SDimitry Andric return NewLoad; 14220b57cec5SDimitry Andric } 14230b57cec5SDimitry Andric 14240b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { 14250b57cec5SDimitry Andric assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); 14260b57cec5SDimitry Andric 14270b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 14280b57cec5SDimitry Andric SDValue Part = Op.getOperand(1); 14290b57cec5SDimitry Andric SDValue Idx = Op.getOperand(2); 14300b57cec5SDimitry Andric SDLoc dl(Op); 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 14330b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 1434fe6060f1SDimitry Andric EVT SubVecVT = Part.getValueType(); 14350b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(VecVT); 14360b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 14370b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 14380b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric // First store the whole vector. 14410b57cec5SDimitry Andric SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo); 14420b57cec5SDimitry Andric 14430b57cec5SDimitry Andric // Then store the inserted part. 1444fe6060f1SDimitry Andric SDValue SubStackPtr = 1445fe6060f1SDimitry Andric TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx); 14460b57cec5SDimitry Andric 14470b57cec5SDimitry Andric // Store the subvector. 14485ffd83dbSDimitry Andric Ch = DAG.getStore( 14495ffd83dbSDimitry Andric Ch, dl, Part, SubStackPtr, 14505ffd83dbSDimitry Andric MachinePointerInfo::getUnknownStack(DAG.getMachineFunction())); 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric // Finally, load the updated vector. 14530b57cec5SDimitry Andric return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo); 14540b57cec5SDimitry Andric } 14550b57cec5SDimitry Andric 14560b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 14575ffd83dbSDimitry Andric assert((Node->getOpcode() == ISD::BUILD_VECTOR || 14585ffd83dbSDimitry Andric Node->getOpcode() == ISD::CONCAT_VECTORS) && 14595ffd83dbSDimitry Andric "Unexpected opcode!"); 14605ffd83dbSDimitry Andric 14610b57cec5SDimitry Andric // We can't handle this case efficiently. Allocate a sufficiently 14625ffd83dbSDimitry Andric // aligned object on the stack, store each operand into it, then load 14630b57cec5SDimitry Andric // the result as a vector. 14640b57cec5SDimitry Andric // Create the stack frame object. 14650b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 14665ffd83dbSDimitry Andric EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType() 14675ffd83dbSDimitry Andric : Node->getOperand(0).getValueType(); 14680b57cec5SDimitry Andric SDLoc dl(Node); 14690b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(VT); 14700b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 14710b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 14720b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 14730b57cec5SDimitry Andric 14740b57cec5SDimitry Andric // Emit a store of each element to the stack slot. 14750b57cec5SDimitry Andric SmallVector<SDValue, 8> Stores; 14765ffd83dbSDimitry Andric unsigned TypeByteSize = MemVT.getSizeInBits() / 8; 14770b57cec5SDimitry Andric assert(TypeByteSize > 0 && "Vector element type too small for stack store!"); 1478e8d8bef9SDimitry Andric 1479e8d8bef9SDimitry Andric // If the destination vector element type of a BUILD_VECTOR is narrower than 1480e8d8bef9SDimitry Andric // the source element type, only store the bits necessary. 1481e8d8bef9SDimitry Andric bool Truncate = isa<BuildVectorSDNode>(Node) && 1482e8d8bef9SDimitry Andric MemVT.bitsLT(Node->getOperand(0).getValueType()); 1483e8d8bef9SDimitry Andric 14840b57cec5SDimitry Andric // Store (in the right endianness) the elements to memory. 14850b57cec5SDimitry Andric for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 14860b57cec5SDimitry Andric // Ignore undef elements. 14870b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) continue; 14880b57cec5SDimitry Andric 14890b57cec5SDimitry Andric unsigned Offset = TypeByteSize*i; 14900b57cec5SDimitry Andric 1491e8d8bef9SDimitry Andric SDValue Idx = DAG.getMemBasePlusOffset(FIPtr, TypeSize::Fixed(Offset), dl); 14920b57cec5SDimitry Andric 1493e8d8bef9SDimitry Andric if (Truncate) 14940b57cec5SDimitry Andric Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 14950b57cec5SDimitry Andric Node->getOperand(i), Idx, 14965ffd83dbSDimitry Andric PtrInfo.getWithOffset(Offset), MemVT)); 14975ffd83dbSDimitry Andric else 14980b57cec5SDimitry Andric Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i), 14990b57cec5SDimitry Andric Idx, PtrInfo.getWithOffset(Offset))); 15000b57cec5SDimitry Andric } 15010b57cec5SDimitry Andric 15020b57cec5SDimitry Andric SDValue StoreChain; 15030b57cec5SDimitry Andric if (!Stores.empty()) // Not all undef elements? 15040b57cec5SDimitry Andric StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 15050b57cec5SDimitry Andric else 15060b57cec5SDimitry Andric StoreChain = DAG.getEntryNode(); 15070b57cec5SDimitry Andric 15080b57cec5SDimitry Andric // Result is a load from the stack slot. 15090b57cec5SDimitry Andric return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo); 15100b57cec5SDimitry Andric } 15110b57cec5SDimitry Andric 15120b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part 15130b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of 15140b57cec5SDimitry Andric /// holding all bits of the floating-point value. 15150b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State, 15160b57cec5SDimitry Andric const SDLoc &DL, 15170b57cec5SDimitry Andric SDValue Value) const { 15180b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 1519e8d8bef9SDimitry Andric unsigned NumBits = FloatVT.getScalarSizeInBits(); 15200b57cec5SDimitry Andric State.FloatVT = FloatVT; 15210b57cec5SDimitry Andric EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 15220b57cec5SDimitry Andric // Convert to an integer of the same size. 15230b57cec5SDimitry Andric if (TLI.isTypeLegal(IVT)) { 15240b57cec5SDimitry Andric State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value); 15250b57cec5SDimitry Andric State.SignMask = APInt::getSignMask(NumBits); 15260b57cec5SDimitry Andric State.SignBit = NumBits - 1; 15270b57cec5SDimitry Andric return; 15280b57cec5SDimitry Andric } 15290b57cec5SDimitry Andric 15300b57cec5SDimitry Andric auto &DataLayout = DAG.getDataLayout(); 15310b57cec5SDimitry Andric // Store the float to memory, then load the sign part out as an integer. 15320b57cec5SDimitry Andric MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8); 15330b57cec5SDimitry Andric // First create a temporary that is aligned for both the load and store. 15340b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 15350b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 15360b57cec5SDimitry Andric // Then store the float to it. 15370b57cec5SDimitry Andric State.FloatPtr = StackPtr; 15380b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 15390b57cec5SDimitry Andric State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI); 15400b57cec5SDimitry Andric State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr, 15410b57cec5SDimitry Andric State.FloatPointerInfo); 15420b57cec5SDimitry Andric 15430b57cec5SDimitry Andric SDValue IntPtr; 15440b57cec5SDimitry Andric if (DataLayout.isBigEndian()) { 15450b57cec5SDimitry Andric assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 15460b57cec5SDimitry Andric // Load out a legal integer with the same sign bit as the float. 15470b57cec5SDimitry Andric IntPtr = StackPtr; 15480b57cec5SDimitry Andric State.IntPointerInfo = State.FloatPointerInfo; 15490b57cec5SDimitry Andric } else { 15500b57cec5SDimitry Andric // Advance the pointer so that the loaded byte will contain the sign bit. 1551e8d8bef9SDimitry Andric unsigned ByteOffset = (NumBits / 8) - 1; 1552e8d8bef9SDimitry Andric IntPtr = 1553e8d8bef9SDimitry Andric DAG.getMemBasePlusOffset(StackPtr, TypeSize::Fixed(ByteOffset), DL); 15540b57cec5SDimitry Andric State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI, 15550b57cec5SDimitry Andric ByteOffset); 15560b57cec5SDimitry Andric } 15570b57cec5SDimitry Andric 15580b57cec5SDimitry Andric State.IntPtr = IntPtr; 15590b57cec5SDimitry Andric State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr, 15600b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 1561e8d8bef9SDimitry Andric State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7); 15620b57cec5SDimitry Andric State.SignBit = 7; 15630b57cec5SDimitry Andric } 15640b57cec5SDimitry Andric 15650b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value 15660b57cec5SDimitry Andric /// and cast the result back to a floating-point type. 15670b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State, 15680b57cec5SDimitry Andric const SDLoc &DL, 15690b57cec5SDimitry Andric SDValue NewIntValue) const { 15700b57cec5SDimitry Andric if (!State.Chain) 15710b57cec5SDimitry Andric return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue); 15720b57cec5SDimitry Andric 15730b57cec5SDimitry Andric // Override the part containing the sign bit in the value stored on the stack. 15740b57cec5SDimitry Andric SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr, 15750b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 15760b57cec5SDimitry Andric return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr, 15770b57cec5SDimitry Andric State.FloatPointerInfo); 15780b57cec5SDimitry Andric } 15790b57cec5SDimitry Andric 15800b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const { 15810b57cec5SDimitry Andric SDLoc DL(Node); 15820b57cec5SDimitry Andric SDValue Mag = Node->getOperand(0); 15830b57cec5SDimitry Andric SDValue Sign = Node->getOperand(1); 15840b57cec5SDimitry Andric 15850b57cec5SDimitry Andric // Get sign bit into an integer value. 15860b57cec5SDimitry Andric FloatSignAsInt SignAsInt; 15870b57cec5SDimitry Andric getSignAsIntValue(SignAsInt, DL, Sign); 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 15900b57cec5SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 15910b57cec5SDimitry Andric SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue, 15920b57cec5SDimitry Andric SignMask); 15930b57cec5SDimitry Andric 15940b57cec5SDimitry Andric // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X) 15950b57cec5SDimitry Andric EVT FloatVT = Mag.getValueType(); 15960b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) && 15970b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) { 15980b57cec5SDimitry Andric SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag); 15990b57cec5SDimitry Andric SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue); 16000b57cec5SDimitry Andric SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit, 16010b57cec5SDimitry Andric DAG.getConstant(0, DL, IntVT), ISD::SETNE); 16020b57cec5SDimitry Andric return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue); 16030b57cec5SDimitry Andric } 16040b57cec5SDimitry Andric 16050b57cec5SDimitry Andric // Transform Mag value to integer, and clear the sign bit. 16060b57cec5SDimitry Andric FloatSignAsInt MagAsInt; 16070b57cec5SDimitry Andric getSignAsIntValue(MagAsInt, DL, Mag); 16080b57cec5SDimitry Andric EVT MagVT = MagAsInt.IntValue.getValueType(); 16090b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT); 16100b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue, 16110b57cec5SDimitry Andric ClearSignMask); 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric // Get the signbit at the right position for MagAsInt. 16140b57cec5SDimitry Andric int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit; 16150b57cec5SDimitry Andric EVT ShiftVT = IntVT; 1616e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() < 1617e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 16180b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit); 16190b57cec5SDimitry Andric ShiftVT = MagVT; 16200b57cec5SDimitry Andric } 16210b57cec5SDimitry Andric if (ShiftAmount > 0) { 16220b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT); 16230b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst); 16240b57cec5SDimitry Andric } else if (ShiftAmount < 0) { 16250b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT); 16260b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst); 16270b57cec5SDimitry Andric } 1628e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() > 1629e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 16300b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit); 16310b57cec5SDimitry Andric } 16320b57cec5SDimitry Andric 16330b57cec5SDimitry Andric // Store the part with the modified sign and convert back to float. 16340b57cec5SDimitry Andric SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit); 16350b57cec5SDimitry Andric return modifySignAsInt(MagAsInt, DL, CopiedSign); 16360b57cec5SDimitry Andric } 16370b57cec5SDimitry Andric 1638e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const { 1639e8d8bef9SDimitry Andric // Get the sign bit as an integer. 1640e8d8bef9SDimitry Andric SDLoc DL(Node); 1641e8d8bef9SDimitry Andric FloatSignAsInt SignAsInt; 1642e8d8bef9SDimitry Andric getSignAsIntValue(SignAsInt, DL, Node->getOperand(0)); 1643e8d8bef9SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 1644e8d8bef9SDimitry Andric 1645e8d8bef9SDimitry Andric // Flip the sign. 1646e8d8bef9SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 1647e8d8bef9SDimitry Andric SDValue SignFlip = 1648e8d8bef9SDimitry Andric DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask); 1649e8d8bef9SDimitry Andric 1650e8d8bef9SDimitry Andric // Convert back to float. 1651e8d8bef9SDimitry Andric return modifySignAsInt(SignAsInt, DL, SignFlip); 1652e8d8bef9SDimitry Andric } 1653e8d8bef9SDimitry Andric 16540b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const { 16550b57cec5SDimitry Andric SDLoc DL(Node); 16560b57cec5SDimitry Andric SDValue Value = Node->getOperand(0); 16570b57cec5SDimitry Andric 16580b57cec5SDimitry Andric // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal. 16590b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 16600b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) { 16610b57cec5SDimitry Andric SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT); 16620b57cec5SDimitry Andric return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero); 16630b57cec5SDimitry Andric } 16640b57cec5SDimitry Andric 16650b57cec5SDimitry Andric // Transform value to integer, clear the sign bit and transform back. 16660b57cec5SDimitry Andric FloatSignAsInt ValueAsInt; 16670b57cec5SDimitry Andric getSignAsIntValue(ValueAsInt, DL, Value); 16680b57cec5SDimitry Andric EVT IntVT = ValueAsInt.IntValue.getValueType(); 16690b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT); 16700b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue, 16710b57cec5SDimitry Andric ClearSignMask); 16720b57cec5SDimitry Andric return modifySignAsInt(ValueAsInt, DL, ClearedSign); 16730b57cec5SDimitry Andric } 16740b57cec5SDimitry Andric 16750b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 16760b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 1677e8d8bef9SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 16780b57cec5SDimitry Andric assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 16790b57cec5SDimitry Andric " not tell us which reg is the stack pointer!"); 16800b57cec5SDimitry Andric SDLoc dl(Node); 16810b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 16820b57cec5SDimitry Andric SDValue Tmp1 = SDValue(Node, 0); 16830b57cec5SDimitry Andric SDValue Tmp2 = SDValue(Node, 1); 16840b57cec5SDimitry Andric SDValue Tmp3 = Node->getOperand(2); 16850b57cec5SDimitry Andric SDValue Chain = Tmp1.getOperand(0); 16860b57cec5SDimitry Andric 16870b57cec5SDimitry Andric // Chain the dynamic stack allocation so that it doesn't modify the stack 16880b57cec5SDimitry Andric // pointer when other instructions are using the stack. 16890b57cec5SDimitry Andric Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl); 16900b57cec5SDimitry Andric 16910b57cec5SDimitry Andric SDValue Size = Tmp2.getOperand(1); 16920b57cec5SDimitry Andric SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 16930b57cec5SDimitry Andric Chain = SP.getValue(1); 16945ffd83dbSDimitry Andric Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue(); 16955ffd83dbSDimitry Andric const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering(); 16965ffd83dbSDimitry Andric unsigned Opc = 16975ffd83dbSDimitry Andric TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ? 16985ffd83dbSDimitry Andric ISD::ADD : ISD::SUB; 16995ffd83dbSDimitry Andric 17005ffd83dbSDimitry Andric Align StackAlign = TFL->getStackAlign(); 17015ffd83dbSDimitry Andric Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value 17025ffd83dbSDimitry Andric if (Alignment > StackAlign) 17030b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1, 17045ffd83dbSDimitry Andric DAG.getConstant(-Alignment.value(), dl, VT)); 17050b57cec5SDimitry Andric Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 17060b57cec5SDimitry Andric 17070b57cec5SDimitry Andric Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true), 17080b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl, true), SDValue(), dl); 17090b57cec5SDimitry Andric 17100b57cec5SDimitry Andric Results.push_back(Tmp1); 17110b57cec5SDimitry Andric Results.push_back(Tmp2); 17120b57cec5SDimitry Andric } 17130b57cec5SDimitry Andric 17140b57cec5SDimitry Andric /// Emit a store/load combination to the stack. This stores 17150b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 17160b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed. 17170b57cec5SDimitry Andric /// The resultant code need not be legal. 17180b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 17190b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl) { 17200b57cec5SDimitry Andric return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode()); 17210b57cec5SDimitry Andric } 17220b57cec5SDimitry Andric 17230b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 17240b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl, 17250b57cec5SDimitry Andric SDValue Chain) { 1726e8d8bef9SDimitry Andric unsigned SrcSize = SrcOp.getValueSizeInBits(); 1727e8d8bef9SDimitry Andric unsigned SlotSize = SlotVT.getSizeInBits(); 1728e8d8bef9SDimitry Andric unsigned DestSize = DestVT.getSizeInBits(); 1729e8d8bef9SDimitry Andric Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1730e8d8bef9SDimitry Andric Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType); 1731e8d8bef9SDimitry Andric 1732e8d8bef9SDimitry Andric // Don't convert with stack if the load/store is expensive. 1733e8d8bef9SDimitry Andric if ((SrcSize > SlotSize && 1734e8d8bef9SDimitry Andric !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) || 1735e8d8bef9SDimitry Andric (SlotSize < DestSize && 1736e8d8bef9SDimitry Andric !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT))) 1737e8d8bef9SDimitry Andric return SDValue(); 1738e8d8bef9SDimitry Andric 17390b57cec5SDimitry Andric // Create the stack frame object. 1740e8d8bef9SDimitry Andric Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign( 17410b57cec5SDimitry Andric SrcOp.getValueType().getTypeForEVT(*DAG.getContext())); 1742e8d8bef9SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign); 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 17450b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 17460b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 17470b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 17480b57cec5SDimitry Andric 17490b57cec5SDimitry Andric // Emit a store to the stack slot. Use a truncstore if the input value is 17500b57cec5SDimitry Andric // later than DestVT. 17510b57cec5SDimitry Andric SDValue Store; 17520b57cec5SDimitry Andric 17530b57cec5SDimitry Andric if (SrcSize > SlotSize) 17540b57cec5SDimitry Andric Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo, 17550b57cec5SDimitry Andric SlotVT, SrcAlign); 17560b57cec5SDimitry Andric else { 17570b57cec5SDimitry Andric assert(SrcSize == SlotSize && "Invalid store"); 17580b57cec5SDimitry Andric Store = 17590b57cec5SDimitry Andric DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign); 17600b57cec5SDimitry Andric } 17610b57cec5SDimitry Andric 17620b57cec5SDimitry Andric // Result is a load from the stack slot. 17630b57cec5SDimitry Andric if (SlotSize == DestSize) 17640b57cec5SDimitry Andric return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign); 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric assert(SlotSize < DestSize && "Unknown extension!"); 17670b57cec5SDimitry Andric return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT, 17680b57cec5SDimitry Andric DestAlign); 17690b57cec5SDimitry Andric } 17700b57cec5SDimitry Andric 17710b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 17720b57cec5SDimitry Andric SDLoc dl(Node); 17730b57cec5SDimitry Andric // Create a vector sized/aligned stack slot, store the value to element #0, 17740b57cec5SDimitry Andric // then load the whole vector back out. 17750b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 17780b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric SDValue Ch = DAG.getTruncStore( 17810b57cec5SDimitry Andric DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr, 17820b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), 17830b57cec5SDimitry Andric Node->getValueType(0).getVectorElementType()); 17840b57cec5SDimitry Andric return DAG.getLoad( 17850b57cec5SDimitry Andric Node->getValueType(0), dl, Ch, StackPtr, 17860b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 17870b57cec5SDimitry Andric } 17880b57cec5SDimitry Andric 17890b57cec5SDimitry Andric static bool 17900b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, 17910b57cec5SDimitry Andric const TargetLowering &TLI, SDValue &Res) { 17920b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 17930b57cec5SDimitry Andric SDLoc dl(Node); 17940b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric // Try to group the scalars into pairs, shuffle the pairs together, then 17970b57cec5SDimitry Andric // shuffle the pairs of pairs together, etc. until the vector has 17980b57cec5SDimitry Andric // been built. This will work only if all of the necessary shuffle masks 17990b57cec5SDimitry Andric // are legal. 18000b57cec5SDimitry Andric 18010b57cec5SDimitry Andric // We do this in two phases; first to check the legality of the shuffles, 18020b57cec5SDimitry Andric // and next, assuming that all shuffles are legal, to create the new nodes. 18030b57cec5SDimitry Andric for (int Phase = 0; Phase < 2; ++Phase) { 18040b57cec5SDimitry Andric SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals, 18050b57cec5SDimitry Andric NewIntermedVals; 18060b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 18070b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 18080b57cec5SDimitry Andric if (V.isUndef()) 18090b57cec5SDimitry Andric continue; 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric SDValue Vec; 18120b57cec5SDimitry Andric if (Phase) 18130b57cec5SDimitry Andric Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V); 18140b57cec5SDimitry Andric IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i))); 18150b57cec5SDimitry Andric } 18160b57cec5SDimitry Andric 18170b57cec5SDimitry Andric while (IntermedVals.size() > 2) { 18180b57cec5SDimitry Andric NewIntermedVals.clear(); 18190b57cec5SDimitry Andric for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) { 18200b57cec5SDimitry Andric // This vector and the next vector are shuffled together (simply to 18210b57cec5SDimitry Andric // append the one to the other). 18220b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 18230b57cec5SDimitry Andric 18240b57cec5SDimitry Andric SmallVector<int, 16> FinalIndices; 18250b57cec5SDimitry Andric FinalIndices.reserve(IntermedVals[i].second.size() + 18260b57cec5SDimitry Andric IntermedVals[i+1].second.size()); 18270b57cec5SDimitry Andric 18280b57cec5SDimitry Andric int k = 0; 18290b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f; 18300b57cec5SDimitry Andric ++j, ++k) { 18310b57cec5SDimitry Andric ShuffleVec[k] = j; 18320b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i].second[j]); 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f; 18350b57cec5SDimitry Andric ++j, ++k) { 18360b57cec5SDimitry Andric ShuffleVec[k] = NumElems + j; 18370b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i+1].second[j]); 18380b57cec5SDimitry Andric } 18390b57cec5SDimitry Andric 18400b57cec5SDimitry Andric SDValue Shuffle; 18410b57cec5SDimitry Andric if (Phase) 18420b57cec5SDimitry Andric Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first, 18430b57cec5SDimitry Andric IntermedVals[i+1].first, 18440b57cec5SDimitry Andric ShuffleVec); 18450b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 18460b57cec5SDimitry Andric return false; 18470b57cec5SDimitry Andric NewIntermedVals.push_back( 18480b57cec5SDimitry Andric std::make_pair(Shuffle, std::move(FinalIndices))); 18490b57cec5SDimitry Andric } 18500b57cec5SDimitry Andric 18510b57cec5SDimitry Andric // If we had an odd number of defined values, then append the last 18520b57cec5SDimitry Andric // element to the array of new vectors. 18530b57cec5SDimitry Andric if ((IntermedVals.size() & 1) != 0) 18540b57cec5SDimitry Andric NewIntermedVals.push_back(IntermedVals.back()); 18550b57cec5SDimitry Andric 18560b57cec5SDimitry Andric IntermedVals.swap(NewIntermedVals); 18570b57cec5SDimitry Andric } 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 && 18600b57cec5SDimitry Andric "Invalid number of intermediate vectors"); 18610b57cec5SDimitry Andric SDValue Vec1 = IntermedVals[0].first; 18620b57cec5SDimitry Andric SDValue Vec2; 18630b57cec5SDimitry Andric if (IntermedVals.size() > 1) 18640b57cec5SDimitry Andric Vec2 = IntermedVals[1].first; 18650b57cec5SDimitry Andric else if (Phase) 18660b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 18670b57cec5SDimitry Andric 18680b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 18690b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i) 18700b57cec5SDimitry Andric ShuffleVec[IntermedVals[0].second[i]] = i; 18710b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i) 18720b57cec5SDimitry Andric ShuffleVec[IntermedVals[1].second[i]] = NumElems + i; 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric if (Phase) 18750b57cec5SDimitry Andric Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 18760b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 18770b57cec5SDimitry Andric return false; 18780b57cec5SDimitry Andric } 18790b57cec5SDimitry Andric 18800b57cec5SDimitry Andric return true; 18810b57cec5SDimitry Andric } 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't 18840b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type. 18850b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 18860b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 18870b57cec5SDimitry Andric SDValue Value1, Value2; 18880b57cec5SDimitry Andric SDLoc dl(Node); 18890b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 18900b57cec5SDimitry Andric EVT OpVT = Node->getOperand(0).getValueType(); 18910b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 18920b57cec5SDimitry Andric 18930b57cec5SDimitry Andric // If the only non-undef value is the low element, turn this into a 18940b57cec5SDimitry Andric // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 18950b57cec5SDimitry Andric bool isOnlyLowElement = true; 18960b57cec5SDimitry Andric bool MoreThanTwoValues = false; 18970b57cec5SDimitry Andric bool isConstant = true; 18980b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 18990b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 19000b57cec5SDimitry Andric if (V.isUndef()) 19010b57cec5SDimitry Andric continue; 19020b57cec5SDimitry Andric if (i > 0) 19030b57cec5SDimitry Andric isOnlyLowElement = false; 19040b57cec5SDimitry Andric if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 19050b57cec5SDimitry Andric isConstant = false; 19060b57cec5SDimitry Andric 19070b57cec5SDimitry Andric if (!Value1.getNode()) { 19080b57cec5SDimitry Andric Value1 = V; 19090b57cec5SDimitry Andric } else if (!Value2.getNode()) { 19100b57cec5SDimitry Andric if (V != Value1) 19110b57cec5SDimitry Andric Value2 = V; 19120b57cec5SDimitry Andric } else if (V != Value1 && V != Value2) { 19130b57cec5SDimitry Andric MoreThanTwoValues = true; 19140b57cec5SDimitry Andric } 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric if (!Value1.getNode()) 19180b57cec5SDimitry Andric return DAG.getUNDEF(VT); 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric if (isOnlyLowElement) 19210b57cec5SDimitry Andric return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 19220b57cec5SDimitry Andric 19230b57cec5SDimitry Andric // If all elements are constants, create a load from the constant pool. 19240b57cec5SDimitry Andric if (isConstant) { 19250b57cec5SDimitry Andric SmallVector<Constant*, 16> CV; 19260b57cec5SDimitry Andric for (unsigned i = 0, e = NumElems; i != e; ++i) { 19270b57cec5SDimitry Andric if (ConstantFPSDNode *V = 19280b57cec5SDimitry Andric dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 19290b57cec5SDimitry Andric CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 19300b57cec5SDimitry Andric } else if (ConstantSDNode *V = 19310b57cec5SDimitry Andric dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 19320b57cec5SDimitry Andric if (OpVT==EltVT) 19330b57cec5SDimitry Andric CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 19340b57cec5SDimitry Andric else { 19350b57cec5SDimitry Andric // If OpVT and EltVT don't match, EltVT is not legal and the 19360b57cec5SDimitry Andric // element values have been promoted/truncated earlier. Undo this; 19370b57cec5SDimitry Andric // we don't want a v16i8 to become a v16i32 for example. 19380b57cec5SDimitry Andric const ConstantInt *CI = V->getConstantIntValue(); 19390b57cec5SDimitry Andric CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 19400b57cec5SDimitry Andric CI->getZExtValue())); 19410b57cec5SDimitry Andric } 19420b57cec5SDimitry Andric } else { 19430b57cec5SDimitry Andric assert(Node->getOperand(i).isUndef()); 19440b57cec5SDimitry Andric Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 19450b57cec5SDimitry Andric CV.push_back(UndefValue::get(OpNTy)); 19460b57cec5SDimitry Andric } 19470b57cec5SDimitry Andric } 19480b57cec5SDimitry Andric Constant *CP = ConstantVector::get(CV); 19490b57cec5SDimitry Andric SDValue CPIdx = 19500b57cec5SDimitry Andric DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); 19515ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 19520b57cec5SDimitry Andric return DAG.getLoad( 19530b57cec5SDimitry Andric VT, dl, DAG.getEntryNode(), CPIdx, 19540b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 19550b57cec5SDimitry Andric Alignment); 19560b57cec5SDimitry Andric } 19570b57cec5SDimitry Andric 19580b57cec5SDimitry Andric SmallSet<SDValue, 16> DefinedValues; 19590b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19600b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) 19610b57cec5SDimitry Andric continue; 19620b57cec5SDimitry Andric DefinedValues.insert(Node->getOperand(i)); 19630b57cec5SDimitry Andric } 19640b57cec5SDimitry Andric 19650b57cec5SDimitry Andric if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) { 19660b57cec5SDimitry Andric if (!MoreThanTwoValues) { 19670b57cec5SDimitry Andric SmallVector<int, 8> ShuffleVec(NumElems, -1); 19680b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19690b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 19700b57cec5SDimitry Andric if (V.isUndef()) 19710b57cec5SDimitry Andric continue; 19720b57cec5SDimitry Andric ShuffleVec[i] = V == Value1 ? 0 : NumElems; 19730b57cec5SDimitry Andric } 19740b57cec5SDimitry Andric if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 19750b57cec5SDimitry Andric // Get the splatted value into the low element of a vector register. 19760b57cec5SDimitry Andric SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 19770b57cec5SDimitry Andric SDValue Vec2; 19780b57cec5SDimitry Andric if (Value2.getNode()) 19790b57cec5SDimitry Andric Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 19800b57cec5SDimitry Andric else 19810b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 19820b57cec5SDimitry Andric 19830b57cec5SDimitry Andric // Return shuffle(LowValVec, undef, <0,0,0,0>) 19840b57cec5SDimitry Andric return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 19850b57cec5SDimitry Andric } 19860b57cec5SDimitry Andric } else { 19870b57cec5SDimitry Andric SDValue Res; 19880b57cec5SDimitry Andric if (ExpandBVWithShuffles(Node, DAG, TLI, Res)) 19890b57cec5SDimitry Andric return Res; 19900b57cec5SDimitry Andric } 19910b57cec5SDimitry Andric } 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric // Otherwise, we can't handle this case efficiently. 19940b57cec5SDimitry Andric return ExpandVectorBuildThroughStack(Node); 19950b57cec5SDimitry Andric } 19960b57cec5SDimitry Andric 19978bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) { 19988bcb0991SDimitry Andric SDLoc DL(Node); 19998bcb0991SDimitry Andric EVT VT = Node->getValueType(0); 20008bcb0991SDimitry Andric SDValue SplatVal = Node->getOperand(0); 20018bcb0991SDimitry Andric 20028bcb0991SDimitry Andric return DAG.getSplatBuildVector(VT, DL, SplatVal); 20038bcb0991SDimitry Andric } 20048bcb0991SDimitry Andric 20050b57cec5SDimitry Andric // Expand a node into a call to a libcall. If the result value 20060b57cec5SDimitry Andric // does not fit into a register, return the lo part and set the hi part to the 20070b57cec5SDimitry Andric // by-reg argument. If it does fit into a single register, return the result 20080b57cec5SDimitry Andric // and leave the Hi part unset. 20090b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 20100b57cec5SDimitry Andric bool isSigned) { 20110b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 20120b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 20130b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) { 20140b57cec5SDimitry Andric EVT ArgVT = Op.getValueType(); 20150b57cec5SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 20160b57cec5SDimitry Andric Entry.Node = Op; 20170b57cec5SDimitry Andric Entry.Ty = ArgTy; 20180b57cec5SDimitry Andric Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 20190b57cec5SDimitry Andric Entry.IsZExt = !TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 20200b57cec5SDimitry Andric Args.push_back(Entry); 20210b57cec5SDimitry Andric } 20220b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 20230b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 20240b57cec5SDimitry Andric 20250b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 20260b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 20270b57cec5SDimitry Andric 20280b57cec5SDimitry Andric // By default, the input chain to this libcall is the entry node of the 20290b57cec5SDimitry Andric // function. If the libcall is going to be emitted as a tail call then 20300b57cec5SDimitry Andric // TLI.isUsedByReturnOnly will change it to the right chain if the return 20310b57cec5SDimitry Andric // node which is being folded has a non-entry input chain. 20320b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 20330b57cec5SDimitry Andric 20340b57cec5SDimitry Andric // isTailCall may be true since the callee does not reference caller stack 20350b57cec5SDimitry Andric // frame. Check if it's in the right position and that the return types match. 20360b57cec5SDimitry Andric SDValue TCChain = InChain; 20370b57cec5SDimitry Andric const Function &F = DAG.getMachineFunction().getFunction(); 20380b57cec5SDimitry Andric bool isTailCall = 20390b57cec5SDimitry Andric TLI.isInTailCallPosition(DAG, Node, TCChain) && 20400b57cec5SDimitry Andric (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy()); 20410b57cec5SDimitry Andric if (isTailCall) 20420b57cec5SDimitry Andric InChain = TCChain; 20430b57cec5SDimitry Andric 20440b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 20450b57cec5SDimitry Andric bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned); 20460b57cec5SDimitry Andric CLI.setDebugLoc(SDLoc(Node)) 20470b57cec5SDimitry Andric .setChain(InChain) 20480b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 20490b57cec5SDimitry Andric std::move(Args)) 20500b57cec5SDimitry Andric .setTailCall(isTailCall) 20510b57cec5SDimitry Andric .setSExtResult(signExtend) 20520b57cec5SDimitry Andric .setZExtResult(!signExtend) 20530b57cec5SDimitry Andric .setIsPostTypeLegalization(true); 20540b57cec5SDimitry Andric 20550b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 20560b57cec5SDimitry Andric 20570b57cec5SDimitry Andric if (!CallInfo.second.getNode()) { 20588bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG)); 20590b57cec5SDimitry Andric // It's a tailcall, return the chain (which is the DAG root). 20600b57cec5SDimitry Andric return DAG.getRoot(); 20610b57cec5SDimitry Andric } 20620b57cec5SDimitry Andric 20638bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG)); 20640b57cec5SDimitry Andric return CallInfo.first; 20650b57cec5SDimitry Andric } 20660b57cec5SDimitry Andric 2067480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2068fe6060f1SDimitry Andric RTLIB::Libcall LC, 2069480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2070fe6060f1SDimitry Andric if (LC == RTLIB::UNKNOWN_LIBCALL) 2071fe6060f1SDimitry Andric llvm_unreachable("Can't create an unknown libcall!"); 2072480093f4SDimitry Andric 2073480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2074480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 2075e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops(drop_begin(Node->ops())); 2076480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 2077480093f4SDimitry Andric // FIXME: This doesn't support tail calls. 2078480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 2079480093f4SDimitry Andric Ops, CallOptions, 2080480093f4SDimitry Andric SDLoc(Node), 2081480093f4SDimitry Andric Node->getOperand(0)); 2082480093f4SDimitry Andric Results.push_back(Tmp.first); 2083480093f4SDimitry Andric Results.push_back(Tmp.second); 2084480093f4SDimitry Andric } else { 2085480093f4SDimitry Andric SDValue Tmp = ExpandLibCall(LC, Node, false); 2086480093f4SDimitry Andric Results.push_back(Tmp); 2087480093f4SDimitry Andric } 20880b57cec5SDimitry Andric } 20890b57cec5SDimitry Andric 2090fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type. 2091fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2092fe6060f1SDimitry Andric RTLIB::Libcall Call_F32, 2093fe6060f1SDimitry Andric RTLIB::Libcall Call_F64, 2094fe6060f1SDimitry Andric RTLIB::Libcall Call_F80, 2095fe6060f1SDimitry Andric RTLIB::Libcall Call_F128, 2096fe6060f1SDimitry Andric RTLIB::Libcall Call_PPCF128, 2097fe6060f1SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2098fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0), 2099fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2100fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2101fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 2102fe6060f1SDimitry Andric } 2103fe6060f1SDimitry Andric 21040b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 21050b57cec5SDimitry Andric RTLIB::Libcall Call_I8, 21060b57cec5SDimitry Andric RTLIB::Libcall Call_I16, 21070b57cec5SDimitry Andric RTLIB::Libcall Call_I32, 21080b57cec5SDimitry Andric RTLIB::Libcall Call_I64, 21090b57cec5SDimitry Andric RTLIB::Libcall Call_I128) { 21100b57cec5SDimitry Andric RTLIB::Libcall LC; 21110b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 21120b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 21130b57cec5SDimitry Andric case MVT::i8: LC = Call_I8; break; 21140b57cec5SDimitry Andric case MVT::i16: LC = Call_I16; break; 21150b57cec5SDimitry Andric case MVT::i32: LC = Call_I32; break; 21160b57cec5SDimitry Andric case MVT::i64: LC = Call_I64; break; 21170b57cec5SDimitry Andric case MVT::i128: LC = Call_I128; break; 21180b57cec5SDimitry Andric } 21190b57cec5SDimitry Andric return ExpandLibCall(LC, Node, isSigned); 21200b57cec5SDimitry Andric } 21210b57cec5SDimitry Andric 21220b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance 21230b57cec5SDimitry Andric /// lround and its variant). 2124480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node, 21250b57cec5SDimitry Andric RTLIB::Libcall Call_F32, 21260b57cec5SDimitry Andric RTLIB::Libcall Call_F64, 21270b57cec5SDimitry Andric RTLIB::Libcall Call_F80, 21280b57cec5SDimitry Andric RTLIB::Libcall Call_F128, 2129480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 2130480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2131480093f4SDimitry Andric EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType(); 2132fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(), 2133fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2134fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2135fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 21360b57cec5SDimitry Andric } 21370b57cec5SDimitry Andric 21380b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs. 21390b57cec5SDimitry Andric void 21400b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, 21410b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 21420b57cec5SDimitry Andric unsigned Opcode = Node->getOpcode(); 21430b57cec5SDimitry Andric bool isSigned = Opcode == ISD::SDIVREM; 21440b57cec5SDimitry Andric 21450b57cec5SDimitry Andric RTLIB::Libcall LC; 21460b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 21470b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 21480b57cec5SDimitry Andric case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 21490b57cec5SDimitry Andric case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 21500b57cec5SDimitry Andric case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 21510b57cec5SDimitry Andric case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 21520b57cec5SDimitry Andric case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 21530b57cec5SDimitry Andric } 21540b57cec5SDimitry Andric 21550b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 21560b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 21570b57cec5SDimitry Andric // dependence. 21580b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 21590b57cec5SDimitry Andric 21600b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 21610b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 21620b57cec5SDimitry Andric 21630b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 21640b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 21650b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) { 21660b57cec5SDimitry Andric EVT ArgVT = Op.getValueType(); 21670b57cec5SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 21680b57cec5SDimitry Andric Entry.Node = Op; 21690b57cec5SDimitry Andric Entry.Ty = ArgTy; 21700b57cec5SDimitry Andric Entry.IsSExt = isSigned; 21710b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 21720b57cec5SDimitry Andric Args.push_back(Entry); 21730b57cec5SDimitry Andric } 21740b57cec5SDimitry Andric 21750b57cec5SDimitry Andric // Also pass the return address of the remainder. 21760b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(RetVT); 21770b57cec5SDimitry Andric Entry.Node = FIPtr; 21780b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 21790b57cec5SDimitry Andric Entry.IsSExt = isSigned; 21800b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 21810b57cec5SDimitry Andric Args.push_back(Entry); 21820b57cec5SDimitry Andric 21830b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 21840b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 21850b57cec5SDimitry Andric 21860b57cec5SDimitry Andric SDLoc dl(Node); 21870b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 21880b57cec5SDimitry Andric CLI.setDebugLoc(dl) 21890b57cec5SDimitry Andric .setChain(InChain) 21900b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 21910b57cec5SDimitry Andric std::move(Args)) 21920b57cec5SDimitry Andric .setSExtResult(isSigned) 21930b57cec5SDimitry Andric .setZExtResult(!isSigned); 21940b57cec5SDimitry Andric 21950b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric // Remainder is loaded back from the stack frame. 21980b57cec5SDimitry Andric SDValue Rem = 21990b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo()); 22000b57cec5SDimitry Andric Results.push_back(CallInfo.first); 22010b57cec5SDimitry Andric Results.push_back(Rem); 22020b57cec5SDimitry Andric } 22030b57cec5SDimitry Andric 22040b57cec5SDimitry Andric /// Return true if sincos libcall is available. 22050b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) { 22060b57cec5SDimitry Andric RTLIB::Libcall LC; 22070b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 22080b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 22090b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 22100b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 22110b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 22120b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 22130b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 22140b57cec5SDimitry Andric } 22150b57cec5SDimitry Andric return TLI.getLibcallName(LC) != nullptr; 22160b57cec5SDimitry Andric } 22170b57cec5SDimitry Andric 22180b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed. 22190b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) { 22200b57cec5SDimitry Andric unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN 22210b57cec5SDimitry Andric ? ISD::FCOS : ISD::FSIN; 22220b57cec5SDimitry Andric 22230b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 2224349cc55cSDimitry Andric for (const SDNode *User : Op0.getNode()->uses()) { 22250b57cec5SDimitry Andric if (User == Node) 22260b57cec5SDimitry Andric continue; 22270b57cec5SDimitry Andric // The other user might have been turned into sincos already. 22280b57cec5SDimitry Andric if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS) 22290b57cec5SDimitry Andric return true; 22300b57cec5SDimitry Andric } 22310b57cec5SDimitry Andric return false; 22320b57cec5SDimitry Andric } 22330b57cec5SDimitry Andric 22340b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs. 22350b57cec5SDimitry Andric void 22360b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node, 22370b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 22380b57cec5SDimitry Andric RTLIB::Libcall LC; 22390b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 22400b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 22410b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 22420b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 22430b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 22440b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 22450b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 22460b57cec5SDimitry Andric } 22470b57cec5SDimitry Andric 22480b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 22490b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 22500b57cec5SDimitry Andric // dependence. 22510b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 22520b57cec5SDimitry Andric 22530b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 22540b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 22550b57cec5SDimitry Andric 22560b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 22570b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 22580b57cec5SDimitry Andric 22590b57cec5SDimitry Andric // Pass the argument. 22600b57cec5SDimitry Andric Entry.Node = Node->getOperand(0); 22610b57cec5SDimitry Andric Entry.Ty = RetTy; 22620b57cec5SDimitry Andric Entry.IsSExt = false; 22630b57cec5SDimitry Andric Entry.IsZExt = false; 22640b57cec5SDimitry Andric Args.push_back(Entry); 22650b57cec5SDimitry Andric 22660b57cec5SDimitry Andric // Pass the return address of sin. 22670b57cec5SDimitry Andric SDValue SinPtr = DAG.CreateStackTemporary(RetVT); 22680b57cec5SDimitry Andric Entry.Node = SinPtr; 22690b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 22700b57cec5SDimitry Andric Entry.IsSExt = false; 22710b57cec5SDimitry Andric Entry.IsZExt = false; 22720b57cec5SDimitry Andric Args.push_back(Entry); 22730b57cec5SDimitry Andric 22740b57cec5SDimitry Andric // Also pass the return address of the cos. 22750b57cec5SDimitry Andric SDValue CosPtr = DAG.CreateStackTemporary(RetVT); 22760b57cec5SDimitry Andric Entry.Node = CosPtr; 22770b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 22780b57cec5SDimitry Andric Entry.IsSExt = false; 22790b57cec5SDimitry Andric Entry.IsZExt = false; 22800b57cec5SDimitry Andric Args.push_back(Entry); 22810b57cec5SDimitry Andric 22820b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 22830b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 22840b57cec5SDimitry Andric 22850b57cec5SDimitry Andric SDLoc dl(Node); 22860b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 22870b57cec5SDimitry Andric CLI.setDebugLoc(dl).setChain(InChain).setLibCallee( 22880b57cec5SDimitry Andric TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee, 22890b57cec5SDimitry Andric std::move(Args)); 22900b57cec5SDimitry Andric 22910b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 22920b57cec5SDimitry Andric 22930b57cec5SDimitry Andric Results.push_back( 22940b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo())); 22950b57cec5SDimitry Andric Results.push_back( 22960b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo())); 22970b57cec5SDimitry Andric } 22980b57cec5SDimitry Andric 22990b57cec5SDimitry Andric /// This function is responsible for legalizing a 23000b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that 23010b57cec5SDimitry Andric /// we expand it. At this point, we know that the result and operand types are 23020b57cec5SDimitry Andric /// legal for the target. 2303480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node, 2304480093f4SDimitry Andric SDValue &Chain) { 2305480093f4SDimitry Andric bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 2306480093f4SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP); 2307480093f4SDimitry Andric EVT DestVT = Node->getValueType(0); 2308480093f4SDimitry Andric SDLoc dl(Node); 2309480093f4SDimitry Andric unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; 2310480093f4SDimitry Andric SDValue Op0 = Node->getOperand(OpNo); 23110b57cec5SDimitry Andric EVT SrcVT = Op0.getValueType(); 23120b57cec5SDimitry Andric 23130b57cec5SDimitry Andric // TODO: Should any fast-math-flags be set for the created nodes? 23140b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n"); 2315e8d8bef9SDimitry Andric if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) && 2316e8d8bef9SDimitry Andric (DestVT.bitsLE(MVT::f64) || 2317e8d8bef9SDimitry Andric TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND 2318e8d8bef9SDimitry Andric : ISD::FP_EXTEND, 2319e8d8bef9SDimitry Andric DestVT))) { 23200b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double " 23210b57cec5SDimitry Andric "expansion\n"); 23220b57cec5SDimitry Andric 23230b57cec5SDimitry Andric // Get the stack frame index of a 8 byte buffer. 23240b57cec5SDimitry Andric SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 23250b57cec5SDimitry Andric 23265ffd83dbSDimitry Andric SDValue Lo = Op0; 23270b57cec5SDimitry Andric // if signed map to unsigned space 23280b57cec5SDimitry Andric if (isSigned) { 23295ffd83dbSDimitry Andric // Invert sign bit (signed to unsigned mapping). 23305ffd83dbSDimitry Andric Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo, 23315ffd83dbSDimitry Andric DAG.getConstant(0x80000000u, dl, MVT::i32)); 23320b57cec5SDimitry Andric } 23335ffd83dbSDimitry Andric // Initial hi portion of constructed double. 23345ffd83dbSDimitry Andric SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32); 23355ffd83dbSDimitry Andric 23365ffd83dbSDimitry Andric // If this a big endian target, swap the lo and high data. 23375ffd83dbSDimitry Andric if (DAG.getDataLayout().isBigEndian()) 23385ffd83dbSDimitry Andric std::swap(Lo, Hi); 23395ffd83dbSDimitry Andric 23405ffd83dbSDimitry Andric SDValue MemChain = DAG.getEntryNode(); 23415ffd83dbSDimitry Andric 23425ffd83dbSDimitry Andric // Store the lo of the constructed double. 23435ffd83dbSDimitry Andric SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot, 23440b57cec5SDimitry Andric MachinePointerInfo()); 23455ffd83dbSDimitry Andric // Store the hi of the constructed double. 2346e8d8bef9SDimitry Andric SDValue HiPtr = DAG.getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), dl); 23470b57cec5SDimitry Andric SDValue Store2 = 23485ffd83dbSDimitry Andric DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo()); 23495ffd83dbSDimitry Andric MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 23505ffd83dbSDimitry Andric 23510b57cec5SDimitry Andric // load the constructed double 23520b57cec5SDimitry Andric SDValue Load = 23535ffd83dbSDimitry Andric DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo()); 23540b57cec5SDimitry Andric // FP constant to bias correct the final result 23550b57cec5SDimitry Andric SDValue Bias = DAG.getConstantFP(isSigned ? 23560b57cec5SDimitry Andric BitsToDouble(0x4330000080000000ULL) : 23570b57cec5SDimitry Andric BitsToDouble(0x4330000000000000ULL), 23580b57cec5SDimitry Andric dl, MVT::f64); 2359480093f4SDimitry Andric // Subtract the bias and get the final result. 2360480093f4SDimitry Andric SDValue Sub; 2361480093f4SDimitry Andric SDValue Result; 2362480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2363480093f4SDimitry Andric Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other}, 2364480093f4SDimitry Andric {Node->getOperand(0), Load, Bias}); 2365480093f4SDimitry Andric Chain = Sub.getValue(1); 2366480093f4SDimitry Andric if (DestVT != Sub.getValueType()) { 2367480093f4SDimitry Andric std::pair<SDValue, SDValue> ResultPair; 2368480093f4SDimitry Andric ResultPair = 2369480093f4SDimitry Andric DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT); 2370480093f4SDimitry Andric Result = ResultPair.first; 2371480093f4SDimitry Andric Chain = ResultPair.second; 2372480093f4SDimitry Andric } 2373480093f4SDimitry Andric else 2374480093f4SDimitry Andric Result = Sub; 2375480093f4SDimitry Andric } else { 2376480093f4SDimitry Andric Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2377480093f4SDimitry Andric Result = DAG.getFPExtendOrRound(Sub, dl, DestVT); 2378480093f4SDimitry Andric } 23790b57cec5SDimitry Andric return Result; 23800b57cec5SDimitry Andric } 2381e8d8bef9SDimitry Andric 2382e8d8bef9SDimitry Andric if (isSigned) 2383e8d8bef9SDimitry Andric return SDValue(); 23845ffd83dbSDimitry Andric 23855ffd83dbSDimitry Andric // TODO: Generalize this for use with other types. 2386e8d8bef9SDimitry Andric if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) || 2387e8d8bef9SDimitry Andric (SrcVT == MVT::i64 && DestVT == MVT::f64)) { 2388e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n"); 23895ffd83dbSDimitry Andric // For unsigned conversions, convert them to signed conversions using the 23905ffd83dbSDimitry Andric // algorithm from the x86_64 __floatundisf in compiler_rt. That method 23915ffd83dbSDimitry Andric // should be valid for i32->f32 as well. 23925ffd83dbSDimitry Andric 2393e8d8bef9SDimitry Andric // More generally this transform should be valid if there are 3 more bits 2394e8d8bef9SDimitry Andric // in the integer type than the significand. Rounding uses the first bit 2395e8d8bef9SDimitry Andric // after the width of the significand and the OR of all bits after that. So 2396e8d8bef9SDimitry Andric // we need to be able to OR the shifted out bit into one of the bits that 2397e8d8bef9SDimitry Andric // participate in the OR. 2398e8d8bef9SDimitry Andric 23995ffd83dbSDimitry Andric // TODO: This really should be implemented using a branch rather than a 24005ffd83dbSDimitry Andric // select. We happen to get lucky and machinesink does the right 24015ffd83dbSDimitry Andric // thing most of the time. This would be a good candidate for a 24025ffd83dbSDimitry Andric // pseudo-op, or, even better, for whole-function isel. 24035ffd83dbSDimitry Andric EVT SetCCVT = getSetCCResultType(SrcVT); 24045ffd83dbSDimitry Andric 24055ffd83dbSDimitry Andric SDValue SignBitTest = DAG.getSetCC( 24065ffd83dbSDimitry Andric dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 24075ffd83dbSDimitry Andric 24085ffd83dbSDimitry Andric EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout()); 24095ffd83dbSDimitry Andric SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT); 24105ffd83dbSDimitry Andric SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst); 24115ffd83dbSDimitry Andric SDValue AndConst = DAG.getConstant(1, dl, SrcVT); 24125ffd83dbSDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst); 24135ffd83dbSDimitry Andric SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr); 24145ffd83dbSDimitry Andric 24155ffd83dbSDimitry Andric SDValue Slow, Fast; 24165ffd83dbSDimitry Andric if (Node->isStrictFPOpcode()) { 24175ffd83dbSDimitry Andric // In strict mode, we must avoid spurious exceptions, and therefore 24185ffd83dbSDimitry Andric // must make sure to only emit a single STRICT_SINT_TO_FP. 24195ffd83dbSDimitry Andric SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0); 24205ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 24215ffd83dbSDimitry Andric { Node->getOperand(0), InCvt }); 24225ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 24235ffd83dbSDimitry Andric { Fast.getValue(1), Fast, Fast }); 24245ffd83dbSDimitry Andric Chain = Slow.getValue(1); 24255ffd83dbSDimitry Andric // The STRICT_SINT_TO_FP inherits the exception mode from the 24265ffd83dbSDimitry Andric // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can 24275ffd83dbSDimitry Andric // never raise any exception. 24285ffd83dbSDimitry Andric SDNodeFlags Flags; 24295ffd83dbSDimitry Andric Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept()); 24305ffd83dbSDimitry Andric Fast->setFlags(Flags); 24315ffd83dbSDimitry Andric Flags.setNoFPExcept(true); 24325ffd83dbSDimitry Andric Slow->setFlags(Flags); 24335ffd83dbSDimitry Andric } else { 24345ffd83dbSDimitry Andric SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or); 24355ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt); 24365ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 24375ffd83dbSDimitry Andric } 24385ffd83dbSDimitry Andric 24395ffd83dbSDimitry Andric return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast); 24405ffd83dbSDimitry Andric } 24415ffd83dbSDimitry Andric 2442e8d8bef9SDimitry Andric // Don't expand it if there isn't cheap fadd. 2443e8d8bef9SDimitry Andric if (!TLI.isOperationLegalOrCustom( 2444e8d8bef9SDimitry Andric Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT)) 2445e8d8bef9SDimitry Andric return SDValue(); 2446e8d8bef9SDimitry Andric 24475ffd83dbSDimitry Andric // The following optimization is valid only if every value in SrcVT (when 24485ffd83dbSDimitry Andric // treated as signed) is representable in DestVT. Check that the mantissa 24495ffd83dbSDimitry Andric // size of DestVT is >= than the number of bits in SrcVT -1. 24505ffd83dbSDimitry Andric assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >= 24515ffd83dbSDimitry Andric SrcVT.getSizeInBits() - 1 && 24525ffd83dbSDimitry Andric "Cannot perform lossless SINT_TO_FP!"); 24530b57cec5SDimitry Andric 2454480093f4SDimitry Andric SDValue Tmp1; 2455480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2456480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 2457480093f4SDimitry Andric { Node->getOperand(0), Op0 }); 2458480093f4SDimitry Andric } else 2459480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 24600b57cec5SDimitry Andric 24610b57cec5SDimitry Andric SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0, 24620b57cec5SDimitry Andric DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 24630b57cec5SDimitry Andric SDValue Zero = DAG.getIntPtrConstant(0, dl), 24640b57cec5SDimitry Andric Four = DAG.getIntPtrConstant(4, dl); 24650b57cec5SDimitry Andric SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(), 24660b57cec5SDimitry Andric SignSet, Four, Zero); 24670b57cec5SDimitry Andric 24680b57cec5SDimitry Andric // If the sign bit of the integer is set, the large number will be treated 24690b57cec5SDimitry Andric // as a negative number. To counteract this, the dynamic code adds an 24700b57cec5SDimitry Andric // offset depending on the data type. 24710b57cec5SDimitry Andric uint64_t FF; 24720b57cec5SDimitry Andric switch (SrcVT.getSimpleVT().SimpleTy) { 2473e8d8bef9SDimitry Andric default: 2474e8d8bef9SDimitry Andric return SDValue(); 24750b57cec5SDimitry Andric case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 24760b57cec5SDimitry Andric case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 24770b57cec5SDimitry Andric case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 24780b57cec5SDimitry Andric case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 24790b57cec5SDimitry Andric } 24800b57cec5SDimitry Andric if (DAG.getDataLayout().isLittleEndian()) 24810b57cec5SDimitry Andric FF <<= 32; 24820b57cec5SDimitry Andric Constant *FudgeFactor = ConstantInt::get( 24830b57cec5SDimitry Andric Type::getInt64Ty(*DAG.getContext()), FF); 24840b57cec5SDimitry Andric 24850b57cec5SDimitry Andric SDValue CPIdx = 24860b57cec5SDimitry Andric DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); 24875ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 24880b57cec5SDimitry Andric CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); 24895ffd83dbSDimitry Andric Alignment = commonAlignment(Alignment, 4); 24900b57cec5SDimitry Andric SDValue FudgeInReg; 24910b57cec5SDimitry Andric if (DestVT == MVT::f32) 24920b57cec5SDimitry Andric FudgeInReg = DAG.getLoad( 24930b57cec5SDimitry Andric MVT::f32, dl, DAG.getEntryNode(), CPIdx, 24940b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 24950b57cec5SDimitry Andric Alignment); 24960b57cec5SDimitry Andric else { 24970b57cec5SDimitry Andric SDValue Load = DAG.getExtLoad( 24980b57cec5SDimitry Andric ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx, 24990b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, 25000b57cec5SDimitry Andric Alignment); 25010b57cec5SDimitry Andric HandleSDNode Handle(Load); 25020b57cec5SDimitry Andric LegalizeOp(Load.getNode()); 25030b57cec5SDimitry Andric FudgeInReg = Handle.getValue(); 25040b57cec5SDimitry Andric } 25050b57cec5SDimitry Andric 2506480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2507480093f4SDimitry Andric SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 2508480093f4SDimitry Andric { Tmp1.getValue(1), Tmp1, FudgeInReg }); 2509480093f4SDimitry Andric Chain = Result.getValue(1); 2510480093f4SDimitry Andric return Result; 2511480093f4SDimitry Andric } 2512480093f4SDimitry Andric 25130b57cec5SDimitry Andric return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 25140b57cec5SDimitry Andric } 25150b57cec5SDimitry Andric 25160b57cec5SDimitry Andric /// This function is responsible for legalizing a 25170b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that 25180b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 25190b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 25200b57cec5SDimitry Andric /// operation that takes a larger input. 2521480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP( 2522480093f4SDimitry Andric SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) { 2523480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2524480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP || 2525480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_SINT_TO_FP; 2526480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2527480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 2528480093f4SDimitry Andric unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP; 2529480093f4SDimitry Andric unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP; 2530480093f4SDimitry Andric 25310b57cec5SDimitry Andric // First step, figure out the appropriate *INT_TO_FP operation to use. 25320b57cec5SDimitry Andric EVT NewInTy = LegalOp.getValueType(); 25330b57cec5SDimitry Andric 25340b57cec5SDimitry Andric unsigned OpToUse = 0; 25350b57cec5SDimitry Andric 25360b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 25370b57cec5SDimitry Andric while (true) { 25380b57cec5SDimitry Andric NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 25390b57cec5SDimitry Andric assert(NewInTy.isInteger() && "Ran out of possibilities!"); 25400b57cec5SDimitry Andric 25410b57cec5SDimitry Andric // If the target supports SINT_TO_FP of this type, use it. 2542480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) { 2543480093f4SDimitry Andric OpToUse = SIntOp; 25440b57cec5SDimitry Andric break; 25450b57cec5SDimitry Andric } 2546480093f4SDimitry Andric if (IsSigned) 2547480093f4SDimitry Andric continue; 25480b57cec5SDimitry Andric 25490b57cec5SDimitry Andric // If the target supports UINT_TO_FP of this type, use it. 2550480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) { 2551480093f4SDimitry Andric OpToUse = UIntOp; 25520b57cec5SDimitry Andric break; 25530b57cec5SDimitry Andric } 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric // Otherwise, try a larger type. 25560b57cec5SDimitry Andric } 25570b57cec5SDimitry Andric 25580b57cec5SDimitry Andric // Okay, we found the operation and type to use. Zero extend our input to the 25590b57cec5SDimitry Andric // desired type then run the operation on it. 2560480093f4SDimitry Andric if (IsStrict) { 2561480093f4SDimitry Andric SDValue Res = 2562480093f4SDimitry Andric DAG.getNode(OpToUse, dl, {DestVT, MVT::Other}, 2563480093f4SDimitry Andric {N->getOperand(0), 2564480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2565480093f4SDimitry Andric dl, NewInTy, LegalOp)}); 2566480093f4SDimitry Andric Results.push_back(Res); 2567480093f4SDimitry Andric Results.push_back(Res.getValue(1)); 2568480093f4SDimitry Andric return; 2569480093f4SDimitry Andric } 2570480093f4SDimitry Andric 2571480093f4SDimitry Andric Results.push_back( 2572480093f4SDimitry Andric DAG.getNode(OpToUse, dl, DestVT, 2573480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2574480093f4SDimitry Andric dl, NewInTy, LegalOp))); 25750b57cec5SDimitry Andric } 25760b57cec5SDimitry Andric 25770b57cec5SDimitry Andric /// This function is responsible for legalizing a 25780b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that 25790b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 25800b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 25810b57cec5SDimitry Andric /// operation that returns a larger result. 2582480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl, 2583480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2584480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2585480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT || 2586480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_FP_TO_SINT; 2587480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2588480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 25890b57cec5SDimitry Andric // First step, figure out the appropriate FP_TO*INT operation to use. 25900b57cec5SDimitry Andric EVT NewOutTy = DestVT; 25910b57cec5SDimitry Andric 25920b57cec5SDimitry Andric unsigned OpToUse = 0; 25930b57cec5SDimitry Andric 25940b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 25950b57cec5SDimitry Andric while (true) { 25960b57cec5SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 25970b57cec5SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 25980b57cec5SDimitry Andric 25990b57cec5SDimitry Andric // A larger signed type can hold all unsigned values of the requested type, 26000b57cec5SDimitry Andric // so using FP_TO_SINT is valid 2601480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT; 2602480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 26030b57cec5SDimitry Andric break; 26040b57cec5SDimitry Andric 26050b57cec5SDimitry Andric // However, if the value may be < 0.0, we *must* use some FP_TO_SINT. 2606480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT; 2607480093f4SDimitry Andric if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 26080b57cec5SDimitry Andric break; 26090b57cec5SDimitry Andric 26100b57cec5SDimitry Andric // Otherwise, try a larger type. 26110b57cec5SDimitry Andric } 26120b57cec5SDimitry Andric 26130b57cec5SDimitry Andric // Okay, we found the operation and type to use. 2614480093f4SDimitry Andric SDValue Operation; 2615480093f4SDimitry Andric if (IsStrict) { 2616480093f4SDimitry Andric SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other); 2617480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp); 2618480093f4SDimitry Andric } else 2619480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 26200b57cec5SDimitry Andric 26210b57cec5SDimitry Andric // Truncate the result of the extended FP_TO_*INT operation to the desired 26220b57cec5SDimitry Andric // size. 2623480093f4SDimitry Andric SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2624480093f4SDimitry Andric Results.push_back(Trunc); 2625480093f4SDimitry Andric if (IsStrict) 2626480093f4SDimitry Andric Results.push_back(Operation.getValue(1)); 26270b57cec5SDimitry Andric } 26280b57cec5SDimitry Andric 2629e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point 2630e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal 2631e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type. 2632e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node, 2633e8d8bef9SDimitry Andric const SDLoc &dl) { 2634e8d8bef9SDimitry Andric unsigned Opcode = Node->getOpcode(); 2635e8d8bef9SDimitry Andric 2636e8d8bef9SDimitry Andric // Scan for the appropriate larger type to use. 2637e8d8bef9SDimitry Andric EVT NewOutTy = Node->getValueType(0); 2638e8d8bef9SDimitry Andric while (true) { 2639e8d8bef9SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1); 2640e8d8bef9SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2641e8d8bef9SDimitry Andric 2642e8d8bef9SDimitry Andric if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy)) 2643e8d8bef9SDimitry Andric break; 2644e8d8bef9SDimitry Andric } 2645e8d8bef9SDimitry Andric 2646e8d8bef9SDimitry Andric // Saturation width is determined by second operand, so we don't have to 2647e8d8bef9SDimitry Andric // perform any fixup and can directly truncate the result. 2648e8d8bef9SDimitry Andric SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0), 2649e8d8bef9SDimitry Andric Node->getOperand(1)); 2650e8d8bef9SDimitry Andric return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result); 2651e8d8bef9SDimitry Andric } 2652e8d8bef9SDimitry Andric 2653e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation. 2654e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) { 2655e8d8bef9SDimitry Andric EVT VT = Op.getValueType(); 2656e8d8bef9SDimitry Andric EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2657e8d8bef9SDimitry Andric unsigned Sz = VT.getScalarSizeInBits(); 2658e8d8bef9SDimitry Andric 2659e8d8bef9SDimitry Andric // If CTPOP is legal, use it. Otherwise use shifts and xor. 2660e8d8bef9SDimitry Andric SDValue Result; 2661349cc55cSDimitry Andric if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) { 2662e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::CTPOP, dl, VT, Op); 2663e8d8bef9SDimitry Andric } else { 2664e8d8bef9SDimitry Andric Result = Op; 2665e8d8bef9SDimitry Andric for (unsigned i = Log2_32_Ceil(Sz); i != 0;) { 2666e8d8bef9SDimitry Andric SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result, 2667e8d8bef9SDimitry Andric DAG.getConstant(1ULL << (--i), dl, ShVT)); 2668e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift); 2669e8d8bef9SDimitry Andric } 2670e8d8bef9SDimitry Andric } 2671e8d8bef9SDimitry Andric 2672e8d8bef9SDimitry Andric return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT)); 2673e8d8bef9SDimitry Andric } 2674e8d8bef9SDimitry Andric 26750b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { 26760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to expand node\n"); 26770b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 26780b57cec5SDimitry Andric SDLoc dl(Node); 26790b57cec5SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 26800b57cec5SDimitry Andric bool NeedInvert; 26810b57cec5SDimitry Andric switch (Node->getOpcode()) { 26820b57cec5SDimitry Andric case ISD::ABS: 2683349cc55cSDimitry Andric if ((Tmp1 = TLI.expandABS(Node, DAG))) 26840b57cec5SDimitry Andric Results.push_back(Tmp1); 26850b57cec5SDimitry Andric break; 26860b57cec5SDimitry Andric case ISD::CTPOP: 2687349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTPOP(Node, DAG))) 26880b57cec5SDimitry Andric Results.push_back(Tmp1); 26890b57cec5SDimitry Andric break; 26900b57cec5SDimitry Andric case ISD::CTLZ: 26910b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 2692349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTLZ(Node, DAG))) 26930b57cec5SDimitry Andric Results.push_back(Tmp1); 26940b57cec5SDimitry Andric break; 26950b57cec5SDimitry Andric case ISD::CTTZ: 26960b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 2697349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTTZ(Node, DAG))) 26980b57cec5SDimitry Andric Results.push_back(Tmp1); 26990b57cec5SDimitry Andric break; 27000b57cec5SDimitry Andric case ISD::BITREVERSE: 2701fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG))) 2702fe6060f1SDimitry Andric Results.push_back(Tmp1); 27030b57cec5SDimitry Andric break; 27040b57cec5SDimitry Andric case ISD::BSWAP: 2705fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBSWAP(Node, DAG))) 2706fe6060f1SDimitry Andric Results.push_back(Tmp1); 27070b57cec5SDimitry Andric break; 2708e8d8bef9SDimitry Andric case ISD::PARITY: 2709e8d8bef9SDimitry Andric Results.push_back(ExpandPARITY(Node->getOperand(0), dl)); 2710e8d8bef9SDimitry Andric break; 27110b57cec5SDimitry Andric case ISD::FRAMEADDR: 27120b57cec5SDimitry Andric case ISD::RETURNADDR: 27130b57cec5SDimitry Andric case ISD::FRAME_TO_ARGS_OFFSET: 27140b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 27150b57cec5SDimitry Andric break; 27160b57cec5SDimitry Andric case ISD::EH_DWARF_CFA: { 27170b57cec5SDimitry Andric SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl, 27180b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 27190b57cec5SDimitry Andric SDValue Offset = DAG.getNode(ISD::ADD, dl, 27200b57cec5SDimitry Andric CfaArg.getValueType(), 27210b57cec5SDimitry Andric DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, 27220b57cec5SDimitry Andric CfaArg.getValueType()), 27230b57cec5SDimitry Andric CfaArg); 27240b57cec5SDimitry Andric SDValue FA = DAG.getNode( 27250b57cec5SDimitry Andric ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()), 27260b57cec5SDimitry Andric DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()))); 27270b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(), 27280b57cec5SDimitry Andric FA, Offset)); 27290b57cec5SDimitry Andric break; 27300b57cec5SDimitry Andric } 27310b57cec5SDimitry Andric case ISD::FLT_ROUNDS_: 27320b57cec5SDimitry Andric Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0))); 27335ffd83dbSDimitry Andric Results.push_back(Node->getOperand(0)); 27340b57cec5SDimitry Andric break; 27350b57cec5SDimitry Andric case ISD::EH_RETURN: 27360b57cec5SDimitry Andric case ISD::EH_LABEL: 27370b57cec5SDimitry Andric case ISD::PREFETCH: 27380b57cec5SDimitry Andric case ISD::VAEND: 27390b57cec5SDimitry Andric case ISD::EH_SJLJ_LONGJMP: 27400b57cec5SDimitry Andric // If the target didn't expand these, there's nothing to do, so just 27410b57cec5SDimitry Andric // preserve the chain and be done. 27420b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27430b57cec5SDimitry Andric break; 27440b57cec5SDimitry Andric case ISD::READCYCLECOUNTER: 27450b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 27460b57cec5SDimitry Andric // chain. 27470b57cec5SDimitry Andric Results.append(Node->getNumValues() - 1, 27480b57cec5SDimitry Andric DAG.getConstant(0, dl, Node->getValueType(0))); 27490b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27500b57cec5SDimitry Andric break; 27510b57cec5SDimitry Andric case ISD::EH_SJLJ_SETJMP: 27520b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 27530b57cec5SDimitry Andric // chain. 27540b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, MVT::i32)); 27550b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27560b57cec5SDimitry Andric break; 27570b57cec5SDimitry Andric case ISD::ATOMIC_LOAD: { 27580b57cec5SDimitry Andric // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP. 27590b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0)); 27600b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 27610b57cec5SDimitry Andric SDValue Swap = DAG.getAtomicCmpSwap( 27620b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 27630b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Zero, Zero, 27640b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 27650b57cec5SDimitry Andric Results.push_back(Swap.getValue(0)); 27660b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 27670b57cec5SDimitry Andric break; 27680b57cec5SDimitry Andric } 27690b57cec5SDimitry Andric case ISD::ATOMIC_STORE: { 27700b57cec5SDimitry Andric // There is no libcall for atomic store; fake it with ATOMIC_SWAP. 27710b57cec5SDimitry Andric SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, 27720b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemoryVT(), 27730b57cec5SDimitry Andric Node->getOperand(0), 27740b57cec5SDimitry Andric Node->getOperand(1), Node->getOperand(2), 27750b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 27760b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 27770b57cec5SDimitry Andric break; 27780b57cec5SDimitry Andric } 27790b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 27800b57cec5SDimitry Andric // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and 27810b57cec5SDimitry Andric // splits out the success value as a comparison. Expanding the resulting 27820b57cec5SDimitry Andric // ATOMIC_CMP_SWAP will produce a libcall. 27830b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 27840b57cec5SDimitry Andric SDValue Res = DAG.getAtomicCmpSwap( 27850b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 27860b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Node->getOperand(2), 27870b57cec5SDimitry Andric Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand()); 27880b57cec5SDimitry Andric 27890b57cec5SDimitry Andric SDValue ExtRes = Res; 27900b57cec5SDimitry Andric SDValue LHS = Res; 27910b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 27920b57cec5SDimitry Andric 27930b57cec5SDimitry Andric EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT(); 27940b57cec5SDimitry Andric EVT OuterType = Node->getValueType(0); 27950b57cec5SDimitry Andric switch (TLI.getExtendForAtomicOps()) { 27960b57cec5SDimitry Andric case ISD::SIGN_EXTEND: 27970b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res, 27980b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 27990b57cec5SDimitry Andric RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType, 28000b57cec5SDimitry Andric Node->getOperand(2), DAG.getValueType(AtomicType)); 28010b57cec5SDimitry Andric ExtRes = LHS; 28020b57cec5SDimitry Andric break; 28030b57cec5SDimitry Andric case ISD::ZERO_EXTEND: 28040b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, 28050b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 28060b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 28070b57cec5SDimitry Andric ExtRes = LHS; 28080b57cec5SDimitry Andric break; 28090b57cec5SDimitry Andric case ISD::ANY_EXTEND: 28100b57cec5SDimitry Andric LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType); 28110b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 28120b57cec5SDimitry Andric break; 28130b57cec5SDimitry Andric default: 28140b57cec5SDimitry Andric llvm_unreachable("Invalid atomic op extension"); 28150b57cec5SDimitry Andric } 28160b57cec5SDimitry Andric 28170b57cec5SDimitry Andric SDValue Success = 28180b57cec5SDimitry Andric DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ); 28190b57cec5SDimitry Andric 28200b57cec5SDimitry Andric Results.push_back(ExtRes.getValue(0)); 28210b57cec5SDimitry Andric Results.push_back(Success); 28220b57cec5SDimitry Andric Results.push_back(Res.getValue(1)); 28230b57cec5SDimitry Andric break; 28240b57cec5SDimitry Andric } 28250b57cec5SDimitry Andric case ISD::DYNAMIC_STACKALLOC: 28260b57cec5SDimitry Andric ExpandDYNAMIC_STACKALLOC(Node, Results); 28270b57cec5SDimitry Andric break; 28280b57cec5SDimitry Andric case ISD::MERGE_VALUES: 28290b57cec5SDimitry Andric for (unsigned i = 0; i < Node->getNumValues(); i++) 28300b57cec5SDimitry Andric Results.push_back(Node->getOperand(i)); 28310b57cec5SDimitry Andric break; 28320b57cec5SDimitry Andric case ISD::UNDEF: { 28330b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 28340b57cec5SDimitry Andric if (VT.isInteger()) 28350b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, VT)); 28360b57cec5SDimitry Andric else { 28370b57cec5SDimitry Andric assert(VT.isFloatingPoint() && "Unknown value type!"); 28380b57cec5SDimitry Andric Results.push_back(DAG.getConstantFP(0, dl, VT)); 28390b57cec5SDimitry Andric } 28400b57cec5SDimitry Andric break; 28410b57cec5SDimitry Andric } 28420b57cec5SDimitry Andric case ISD::STRICT_FP_ROUND: 2843480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 2844480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 2845480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 2846480093f4SDimitry Andric break; 2847480093f4SDimitry Andric // We might as well mutate to FP_ROUND when FP_ROUND operation is legal 2848480093f4SDimitry Andric // since this operation is more efficient than stack operation. 28498bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 28508bcb0991SDimitry Andric Node->getValueType(0)) 28518bcb0991SDimitry Andric == TargetLowering::Legal) 28528bcb0991SDimitry Andric break; 2853480093f4SDimitry Andric // We fall back to use stack operation when the FP_ROUND operation 2854480093f4SDimitry Andric // isn't available. 2855e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0), 2856e8d8bef9SDimitry Andric Node->getValueType(0), dl, 2857e8d8bef9SDimitry Andric Node->getOperand(0)))) { 28580b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 28590b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n"); 28600b57cec5SDimitry Andric return true; 2861e8d8bef9SDimitry Andric } 2862e8d8bef9SDimitry Andric break; 28630b57cec5SDimitry Andric case ISD::FP_ROUND: 28640b57cec5SDimitry Andric case ISD::BITCAST: 2865e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), 2866e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 28670b57cec5SDimitry Andric Results.push_back(Tmp1); 28680b57cec5SDimitry Andric break; 28690b57cec5SDimitry Andric case ISD::STRICT_FP_EXTEND: 2870480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 2871480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 2872480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 2873480093f4SDimitry Andric break; 2874480093f4SDimitry Andric // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal 2875480093f4SDimitry Andric // since this operation is more efficient than stack operation. 28768bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 28778bcb0991SDimitry Andric Node->getValueType(0)) 28788bcb0991SDimitry Andric == TargetLowering::Legal) 28798bcb0991SDimitry Andric break; 2880480093f4SDimitry Andric // We fall back to use stack operation when the FP_EXTEND operation 2881480093f4SDimitry Andric // isn't available. 2882e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert( 2883e8d8bef9SDimitry Andric Node->getOperand(1), Node->getOperand(1).getValueType(), 2884e8d8bef9SDimitry Andric Node->getValueType(0), dl, Node->getOperand(0)))) { 28850b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 28860b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n"); 28870b57cec5SDimitry Andric return true; 2888e8d8bef9SDimitry Andric } 2889e8d8bef9SDimitry Andric break; 28900b57cec5SDimitry Andric case ISD::FP_EXTEND: 2891e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), 28920b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 2893e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 28940b57cec5SDimitry Andric Results.push_back(Tmp1); 28950b57cec5SDimitry Andric break; 28960b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: { 28970b57cec5SDimitry Andric EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 28980b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 28990b57cec5SDimitry Andric 29000b57cec5SDimitry Andric // An in-register sign-extend of a boolean is a negation: 29010b57cec5SDimitry Andric // 'true' (1) sign-extended is -1. 29020b57cec5SDimitry Andric // 'false' (0) sign-extended is 0. 29030b57cec5SDimitry Andric // However, we must mask the high bits of the source operand because the 29040b57cec5SDimitry Andric // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero. 29050b57cec5SDimitry Andric 29060b57cec5SDimitry Andric // TODO: Do this for vectors too? 29070b57cec5SDimitry Andric if (ExtraVT.getSizeInBits() == 1) { 29080b57cec5SDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 29090b57cec5SDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One); 29100b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 29110b57cec5SDimitry Andric SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And); 29120b57cec5SDimitry Andric Results.push_back(Neg); 29130b57cec5SDimitry Andric break; 29140b57cec5SDimitry Andric } 29150b57cec5SDimitry Andric 29160b57cec5SDimitry Andric // NOTE: we could fall back on load/store here too for targets without 29170b57cec5SDimitry Andric // SRA. However, it is doubtful that any exist. 29180b57cec5SDimitry Andric EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 29190b57cec5SDimitry Andric unsigned BitsDiff = VT.getScalarSizeInBits() - 29200b57cec5SDimitry Andric ExtraVT.getScalarSizeInBits(); 29210b57cec5SDimitry Andric SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy); 29220b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 29230b57cec5SDimitry Andric Node->getOperand(0), ShiftCst); 29240b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 29250b57cec5SDimitry Andric Results.push_back(Tmp1); 29260b57cec5SDimitry Andric break; 29270b57cec5SDimitry Andric } 29280b57cec5SDimitry Andric case ISD::UINT_TO_FP: 2929480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 2930480093f4SDimitry Andric if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) { 29310b57cec5SDimitry Andric Results.push_back(Tmp1); 2932480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 2933480093f4SDimitry Andric Results.push_back(Tmp2); 29340b57cec5SDimitry Andric break; 29350b57cec5SDimitry Andric } 29360b57cec5SDimitry Andric LLVM_FALLTHROUGH; 29370b57cec5SDimitry Andric case ISD::SINT_TO_FP: 2938480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 2939e8d8bef9SDimitry Andric if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) { 29400b57cec5SDimitry Andric Results.push_back(Tmp1); 2941480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 2942480093f4SDimitry Andric Results.push_back(Tmp2); 2943e8d8bef9SDimitry Andric } 29440b57cec5SDimitry Andric break; 29450b57cec5SDimitry Andric case ISD::FP_TO_SINT: 29460b57cec5SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) 29470b57cec5SDimitry Andric Results.push_back(Tmp1); 29480b57cec5SDimitry Andric break; 29498bcb0991SDimitry Andric case ISD::STRICT_FP_TO_SINT: 29508bcb0991SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) { 29518bcb0991SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 29528bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n"); 29538bcb0991SDimitry Andric return true; 29548bcb0991SDimitry Andric } 29558bcb0991SDimitry Andric break; 29560b57cec5SDimitry Andric case ISD::FP_TO_UINT: 29578bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) 29580b57cec5SDimitry Andric Results.push_back(Tmp1); 29590b57cec5SDimitry Andric break; 29608bcb0991SDimitry Andric case ISD::STRICT_FP_TO_UINT: 29618bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) { 29628bcb0991SDimitry Andric // Relink the chain. 29638bcb0991SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2); 29648bcb0991SDimitry Andric // Replace the new UINT result. 29658bcb0991SDimitry Andric ReplaceNodeWithValue(SDValue(Node, 0), Tmp1); 29668bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n"); 29678bcb0991SDimitry Andric return true; 29688bcb0991SDimitry Andric } 29690b57cec5SDimitry Andric break; 2970e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 2971e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 2972e8d8bef9SDimitry Andric Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG)); 2973e8d8bef9SDimitry Andric break; 29740b57cec5SDimitry Andric case ISD::VAARG: 29750b57cec5SDimitry Andric Results.push_back(DAG.expandVAArg(Node)); 29760b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 29770b57cec5SDimitry Andric break; 29780b57cec5SDimitry Andric case ISD::VACOPY: 29790b57cec5SDimitry Andric Results.push_back(DAG.expandVACopy(Node)); 29800b57cec5SDimitry Andric break; 29810b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 29820b57cec5SDimitry Andric if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) 29830b57cec5SDimitry Andric // This must be an access of the only element. Return it. 29840b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), 29850b57cec5SDimitry Andric Node->getOperand(0)); 29860b57cec5SDimitry Andric else 29870b57cec5SDimitry Andric Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 29880b57cec5SDimitry Andric Results.push_back(Tmp1); 29890b57cec5SDimitry Andric break; 29900b57cec5SDimitry Andric case ISD::EXTRACT_SUBVECTOR: 29910b57cec5SDimitry Andric Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 29920b57cec5SDimitry Andric break; 29930b57cec5SDimitry Andric case ISD::INSERT_SUBVECTOR: 29940b57cec5SDimitry Andric Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); 29950b57cec5SDimitry Andric break; 29960b57cec5SDimitry Andric case ISD::CONCAT_VECTORS: 29970b57cec5SDimitry Andric Results.push_back(ExpandVectorBuildThroughStack(Node)); 29980b57cec5SDimitry Andric break; 29990b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: 30000b57cec5SDimitry Andric Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 30010b57cec5SDimitry Andric break; 30020b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: 30030b57cec5SDimitry Andric Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 30040b57cec5SDimitry Andric Node->getOperand(1), 30050b57cec5SDimitry Andric Node->getOperand(2), dl)); 30060b57cec5SDimitry Andric break; 30070b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 30080b57cec5SDimitry Andric SmallVector<int, 32> NewMask; 30090b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 30100b57cec5SDimitry Andric 30110b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 30120b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 30130b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 30140b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 30150b57cec5SDimitry Andric if (!TLI.isTypeLegal(EltVT)) { 30160b57cec5SDimitry Andric EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 30170b57cec5SDimitry Andric 30180b57cec5SDimitry Andric // BUILD_VECTOR operands are allowed to be wider than the element type. 30190b57cec5SDimitry Andric // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept 30200b57cec5SDimitry Andric // it. 30210b57cec5SDimitry Andric if (NewEltVT.bitsLT(EltVT)) { 30220b57cec5SDimitry Andric // Convert shuffle node. 30230b57cec5SDimitry Andric // If original node was v4i64 and the new EltVT is i32, 30240b57cec5SDimitry Andric // cast operands to v8i32 and re-build the mask. 30250b57cec5SDimitry Andric 30260b57cec5SDimitry Andric // Calculate new VT, the size of the new VT should be equal to original. 30270b57cec5SDimitry Andric EVT NewVT = 30280b57cec5SDimitry Andric EVT::getVectorVT(*DAG.getContext(), NewEltVT, 30290b57cec5SDimitry Andric VT.getSizeInBits() / NewEltVT.getSizeInBits()); 30300b57cec5SDimitry Andric assert(NewVT.bitsEq(VT)); 30310b57cec5SDimitry Andric 30320b57cec5SDimitry Andric // cast operands to new VT 30330b57cec5SDimitry Andric Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0); 30340b57cec5SDimitry Andric Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1); 30350b57cec5SDimitry Andric 30360b57cec5SDimitry Andric // Convert the shuffle mask 30370b57cec5SDimitry Andric unsigned int factor = 30380b57cec5SDimitry Andric NewVT.getVectorNumElements()/VT.getVectorNumElements(); 30390b57cec5SDimitry Andric 30400b57cec5SDimitry Andric // EltVT gets smaller 30410b57cec5SDimitry Andric assert(factor > 0); 30420b57cec5SDimitry Andric 30430b57cec5SDimitry Andric for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 30440b57cec5SDimitry Andric if (Mask[i] < 0) { 30450b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 30460b57cec5SDimitry Andric NewMask.push_back(Mask[i]); 30470b57cec5SDimitry Andric } 30480b57cec5SDimitry Andric else { 30490b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 30500b57cec5SDimitry Andric NewMask.push_back(Mask[i]*factor+fi); 30510b57cec5SDimitry Andric } 30520b57cec5SDimitry Andric } 30530b57cec5SDimitry Andric Mask = NewMask; 30540b57cec5SDimitry Andric VT = NewVT; 30550b57cec5SDimitry Andric } 30560b57cec5SDimitry Andric EltVT = NewEltVT; 30570b57cec5SDimitry Andric } 30580b57cec5SDimitry Andric unsigned NumElems = VT.getVectorNumElements(); 30590b57cec5SDimitry Andric SmallVector<SDValue, 16> Ops; 30600b57cec5SDimitry Andric for (unsigned i = 0; i != NumElems; ++i) { 30610b57cec5SDimitry Andric if (Mask[i] < 0) { 30620b57cec5SDimitry Andric Ops.push_back(DAG.getUNDEF(EltVT)); 30630b57cec5SDimitry Andric continue; 30640b57cec5SDimitry Andric } 30650b57cec5SDimitry Andric unsigned Idx = Mask[i]; 30660b57cec5SDimitry Andric if (Idx < NumElems) 30675ffd83dbSDimitry Andric Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0, 30685ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx, dl))); 30690b57cec5SDimitry Andric else 30705ffd83dbSDimitry Andric Ops.push_back( 30715ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1, 30725ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx - NumElems, dl))); 30730b57cec5SDimitry Andric } 30740b57cec5SDimitry Andric 30750b57cec5SDimitry Andric Tmp1 = DAG.getBuildVector(VT, dl, Ops); 30760b57cec5SDimitry Andric // We may have changed the BUILD_VECTOR type. Cast it back to the Node type. 30770b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1); 30780b57cec5SDimitry Andric Results.push_back(Tmp1); 30790b57cec5SDimitry Andric break; 30800b57cec5SDimitry Andric } 3081fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 3082fe6060f1SDimitry Andric Results.push_back(TLI.expandVectorSplice(Node, DAG)); 3083fe6060f1SDimitry Andric break; 3084fe6060f1SDimitry Andric } 30850b57cec5SDimitry Andric case ISD::EXTRACT_ELEMENT: { 30860b57cec5SDimitry Andric EVT OpTy = Node->getOperand(0).getValueType(); 30870b57cec5SDimitry Andric if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 30880b57cec5SDimitry Andric // 1 -> Hi 30890b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 30900b57cec5SDimitry Andric DAG.getConstant(OpTy.getSizeInBits() / 2, dl, 30910b57cec5SDimitry Andric TLI.getShiftAmountTy( 30920b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 30930b57cec5SDimitry Andric DAG.getDataLayout()))); 30940b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 30950b57cec5SDimitry Andric } else { 30960b57cec5SDimitry Andric // 0 -> Lo 30970b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 30980b57cec5SDimitry Andric Node->getOperand(0)); 30990b57cec5SDimitry Andric } 31000b57cec5SDimitry Andric Results.push_back(Tmp1); 31010b57cec5SDimitry Andric break; 31020b57cec5SDimitry Andric } 31030b57cec5SDimitry Andric case ISD::STACKSAVE: 31040b57cec5SDimitry Andric // Expand to CopyFromReg if the target set 31050b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3106e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 31070b57cec5SDimitry Andric Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 31080b57cec5SDimitry Andric Node->getValueType(0))); 31090b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 31100b57cec5SDimitry Andric } else { 31110b57cec5SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 31120b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 31130b57cec5SDimitry Andric } 31140b57cec5SDimitry Andric break; 31150b57cec5SDimitry Andric case ISD::STACKRESTORE: 31160b57cec5SDimitry Andric // Expand to CopyToReg if the target set 31170b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3118e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 31190b57cec5SDimitry Andric Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 31200b57cec5SDimitry Andric Node->getOperand(1))); 31210b57cec5SDimitry Andric } else { 31220b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 31230b57cec5SDimitry Andric } 31240b57cec5SDimitry Andric break; 31250b57cec5SDimitry Andric case ISD::GET_DYNAMIC_AREA_OFFSET: 31260b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 31270b57cec5SDimitry Andric Results.push_back(Results[0].getValue(0)); 31280b57cec5SDimitry Andric break; 31290b57cec5SDimitry Andric case ISD::FCOPYSIGN: 31300b57cec5SDimitry Andric Results.push_back(ExpandFCOPYSIGN(Node)); 31310b57cec5SDimitry Andric break; 31320b57cec5SDimitry Andric case ISD::FNEG: 3133e8d8bef9SDimitry Andric Results.push_back(ExpandFNEG(Node)); 31340b57cec5SDimitry Andric break; 31350b57cec5SDimitry Andric case ISD::FABS: 31360b57cec5SDimitry Andric Results.push_back(ExpandFABS(Node)); 31370b57cec5SDimitry Andric break; 31380b57cec5SDimitry Andric case ISD::SMIN: 31390b57cec5SDimitry Andric case ISD::SMAX: 31400b57cec5SDimitry Andric case ISD::UMIN: 31410b57cec5SDimitry Andric case ISD::UMAX: { 31420b57cec5SDimitry Andric // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 31430b57cec5SDimitry Andric ISD::CondCode Pred; 31440b57cec5SDimitry Andric switch (Node->getOpcode()) { 31450b57cec5SDimitry Andric default: llvm_unreachable("How did we get here?"); 31460b57cec5SDimitry Andric case ISD::SMAX: Pred = ISD::SETGT; break; 31470b57cec5SDimitry Andric case ISD::SMIN: Pred = ISD::SETLT; break; 31480b57cec5SDimitry Andric case ISD::UMAX: Pred = ISD::SETUGT; break; 31490b57cec5SDimitry Andric case ISD::UMIN: Pred = ISD::SETULT; break; 31500b57cec5SDimitry Andric } 31510b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 31520b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 31530b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred); 31540b57cec5SDimitry Andric Results.push_back(Tmp1); 31550b57cec5SDimitry Andric break; 31560b57cec5SDimitry Andric } 31570b57cec5SDimitry Andric case ISD::FMINNUM: 31580b57cec5SDimitry Andric case ISD::FMAXNUM: { 31590b57cec5SDimitry Andric if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) 31600b57cec5SDimitry Andric Results.push_back(Expanded); 31610b57cec5SDimitry Andric break; 31620b57cec5SDimitry Andric } 31630b57cec5SDimitry Andric case ISD::FSIN: 31640b57cec5SDimitry Andric case ISD::FCOS: { 31650b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 31660b57cec5SDimitry Andric // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin / 31670b57cec5SDimitry Andric // fcos which share the same operand and both are used. 31680b57cec5SDimitry Andric if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) || 31690b57cec5SDimitry Andric isSinCosLibcallAvailable(Node, TLI)) 31700b57cec5SDimitry Andric && useSinCos(Node)) { 31710b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 31720b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0)); 31730b57cec5SDimitry Andric if (Node->getOpcode() == ISD::FCOS) 31740b57cec5SDimitry Andric Tmp1 = Tmp1.getValue(1); 31750b57cec5SDimitry Andric Results.push_back(Tmp1); 31760b57cec5SDimitry Andric } 31770b57cec5SDimitry Andric break; 31780b57cec5SDimitry Andric } 31790b57cec5SDimitry Andric case ISD::FMAD: 31800b57cec5SDimitry Andric llvm_unreachable("Illegal fmad should never be formed"); 31810b57cec5SDimitry Andric 31820b57cec5SDimitry Andric case ISD::FP16_TO_FP: 31830b57cec5SDimitry Andric if (Node->getValueType(0) != MVT::f32) { 31840b57cec5SDimitry Andric // We can extend to types bigger than f32 in two steps without changing 31850b57cec5SDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 31860b57cec5SDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 31870b57cec5SDimitry Andric SDValue Res = 31880b57cec5SDimitry Andric DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0)); 31890b57cec5SDimitry Andric Results.push_back( 31900b57cec5SDimitry Andric DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res)); 31910b57cec5SDimitry Andric } 31920b57cec5SDimitry Andric break; 31935ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: 31945ffd83dbSDimitry Andric if (Node->getValueType(0) != MVT::f32) { 31955ffd83dbSDimitry Andric // We can extend to types bigger than f32 in two steps without changing 31965ffd83dbSDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 31975ffd83dbSDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 31985ffd83dbSDimitry Andric SDValue Res = 31995ffd83dbSDimitry Andric DAG.getNode(ISD::STRICT_FP16_TO_FP, dl, {MVT::f32, MVT::Other}, 32005ffd83dbSDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 32015ffd83dbSDimitry Andric Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, 32025ffd83dbSDimitry Andric {Node->getValueType(0), MVT::Other}, 32035ffd83dbSDimitry Andric {Res.getValue(1), Res}); 32045ffd83dbSDimitry Andric Results.push_back(Res); 32055ffd83dbSDimitry Andric Results.push_back(Res.getValue(1)); 32065ffd83dbSDimitry Andric } 32075ffd83dbSDimitry Andric break; 32080b57cec5SDimitry Andric case ISD::FP_TO_FP16: 32090b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n"); 32100b57cec5SDimitry Andric if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) { 32110b57cec5SDimitry Andric SDValue Op = Node->getOperand(0); 32120b57cec5SDimitry Andric MVT SVT = Op.getSimpleValueType(); 32130b57cec5SDimitry Andric if ((SVT == MVT::f64 || SVT == MVT::f80) && 32140b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) { 32150b57cec5SDimitry Andric // Under fastmath, we can expand this node into a fround followed by 32160b57cec5SDimitry Andric // a float-half conversion. 32170b57cec5SDimitry Andric SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 32180b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl)); 32190b57cec5SDimitry Andric Results.push_back( 32200b57cec5SDimitry Andric DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal)); 32210b57cec5SDimitry Andric } 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric break; 32240b57cec5SDimitry Andric case ISD::ConstantFP: { 32250b57cec5SDimitry Andric ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 32260b57cec5SDimitry Andric // Check to see if this FP immediate is already legal. 32270b57cec5SDimitry Andric // If this is a legal constant, turn it into a TargetConstantFP node. 32280b57cec5SDimitry Andric if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0), 3229e8d8bef9SDimitry Andric DAG.shouldOptForSize())) 32300b57cec5SDimitry Andric Results.push_back(ExpandConstantFP(CFP, true)); 32310b57cec5SDimitry Andric break; 32320b57cec5SDimitry Andric } 32330b57cec5SDimitry Andric case ISD::Constant: { 32340b57cec5SDimitry Andric ConstantSDNode *CP = cast<ConstantSDNode>(Node); 32350b57cec5SDimitry Andric Results.push_back(ExpandConstant(CP)); 32360b57cec5SDimitry Andric break; 32370b57cec5SDimitry Andric } 32380b57cec5SDimitry Andric case ISD::FSUB: { 32390b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32400b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) && 32410b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) { 32420b57cec5SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 32430b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1)); 32440b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags); 32450b57cec5SDimitry Andric Results.push_back(Tmp1); 32460b57cec5SDimitry Andric } 32470b57cec5SDimitry Andric break; 32480b57cec5SDimitry Andric } 32490b57cec5SDimitry Andric case ISD::SUB: { 32500b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32510b57cec5SDimitry Andric assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 32520b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 32530b57cec5SDimitry Andric "Don't know how to expand this subtraction!"); 3254349cc55cSDimitry Andric Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT); 32550b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT)); 32560b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 32570b57cec5SDimitry Andric break; 32580b57cec5SDimitry Andric } 32590b57cec5SDimitry Andric case ISD::UREM: 32605ffd83dbSDimitry Andric case ISD::SREM: 32615ffd83dbSDimitry Andric if (TLI.expandREM(Node, Tmp1, DAG)) 32620b57cec5SDimitry Andric Results.push_back(Tmp1); 32630b57cec5SDimitry Andric break; 32640b57cec5SDimitry Andric case ISD::UDIV: 32650b57cec5SDimitry Andric case ISD::SDIV: { 32660b57cec5SDimitry Andric bool isSigned = Node->getOpcode() == ISD::SDIV; 32670b57cec5SDimitry Andric unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 32680b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32690b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 32700b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 32710b57cec5SDimitry Andric Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 32720b57cec5SDimitry Andric Node->getOperand(1)); 32730b57cec5SDimitry Andric Results.push_back(Tmp1); 32740b57cec5SDimitry Andric } 32750b57cec5SDimitry Andric break; 32760b57cec5SDimitry Andric } 32770b57cec5SDimitry Andric case ISD::MULHU: 32780b57cec5SDimitry Andric case ISD::MULHS: { 32790b57cec5SDimitry Andric unsigned ExpandOpcode = 32800b57cec5SDimitry Andric Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI; 32810b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32820b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 32830b57cec5SDimitry Andric 32840b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 32850b57cec5SDimitry Andric Node->getOperand(1)); 32860b57cec5SDimitry Andric Results.push_back(Tmp1.getValue(1)); 32870b57cec5SDimitry Andric break; 32880b57cec5SDimitry Andric } 32890b57cec5SDimitry Andric case ISD::UMUL_LOHI: 32900b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 32910b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 32920b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 32930b57cec5SDimitry Andric MVT VT = LHS.getSimpleValueType(); 32940b57cec5SDimitry Andric unsigned MULHOpcode = 32950b57cec5SDimitry Andric Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS; 32960b57cec5SDimitry Andric 32970b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) { 32980b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS)); 32990b57cec5SDimitry Andric Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS)); 33000b57cec5SDimitry Andric break; 33010b57cec5SDimitry Andric } 33020b57cec5SDimitry Andric 33030b57cec5SDimitry Andric SmallVector<SDValue, 4> Halves; 33040b57cec5SDimitry Andric EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext()); 33050b57cec5SDimitry Andric assert(TLI.isTypeLegal(HalfType)); 3306e8d8bef9SDimitry Andric if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves, 33070b57cec5SDimitry Andric HalfType, DAG, 33080b57cec5SDimitry Andric TargetLowering::MulExpansionKind::Always)) { 33090b57cec5SDimitry Andric for (unsigned i = 0; i < 2; ++i) { 33100b57cec5SDimitry Andric SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]); 33110b57cec5SDimitry Andric SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]); 33120b57cec5SDimitry Andric SDValue Shift = DAG.getConstant( 33130b57cec5SDimitry Andric HalfType.getScalarSizeInBits(), dl, 33140b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 33150b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 33160b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 33170b57cec5SDimitry Andric } 33180b57cec5SDimitry Andric break; 33190b57cec5SDimitry Andric } 33200b57cec5SDimitry Andric break; 33210b57cec5SDimitry Andric } 33220b57cec5SDimitry Andric case ISD::MUL: { 33230b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 33240b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 33250b57cec5SDimitry Andric // See if multiply or divide can be lowered using two-result operations. 33260b57cec5SDimitry Andric // We just need the low half of the multiply; try both the signed 33270b57cec5SDimitry Andric // and unsigned forms. If the target supports both SMUL_LOHI and 33280b57cec5SDimitry Andric // UMUL_LOHI, form a preference by checking which forms of plain 33290b57cec5SDimitry Andric // MULH it supports. 33300b57cec5SDimitry Andric bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 33310b57cec5SDimitry Andric bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 33320b57cec5SDimitry Andric bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 33330b57cec5SDimitry Andric bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 33340b57cec5SDimitry Andric unsigned OpToUse = 0; 33350b57cec5SDimitry Andric if (HasSMUL_LOHI && !HasMULHS) { 33360b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 33370b57cec5SDimitry Andric } else if (HasUMUL_LOHI && !HasMULHU) { 33380b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 33390b57cec5SDimitry Andric } else if (HasSMUL_LOHI) { 33400b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 33410b57cec5SDimitry Andric } else if (HasUMUL_LOHI) { 33420b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 33430b57cec5SDimitry Andric } 33440b57cec5SDimitry Andric if (OpToUse) { 33450b57cec5SDimitry Andric Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 33460b57cec5SDimitry Andric Node->getOperand(1))); 33470b57cec5SDimitry Andric break; 33480b57cec5SDimitry Andric } 33490b57cec5SDimitry Andric 33500b57cec5SDimitry Andric SDValue Lo, Hi; 33510b57cec5SDimitry Andric EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext()); 33520b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) && 33530b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) && 33540b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 33550b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::OR, VT) && 33560b57cec5SDimitry Andric TLI.expandMUL(Node, Lo, Hi, HalfType, DAG, 33570b57cec5SDimitry Andric TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) { 33580b57cec5SDimitry Andric Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 33590b57cec5SDimitry Andric Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi); 33600b57cec5SDimitry Andric SDValue Shift = 33610b57cec5SDimitry Andric DAG.getConstant(HalfType.getSizeInBits(), dl, 33620b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 33630b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 33640b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 33650b57cec5SDimitry Andric } 33660b57cec5SDimitry Andric break; 33670b57cec5SDimitry Andric } 33680b57cec5SDimitry Andric case ISD::FSHL: 33690b57cec5SDimitry Andric case ISD::FSHR: 33700b57cec5SDimitry Andric if (TLI.expandFunnelShift(Node, Tmp1, DAG)) 33710b57cec5SDimitry Andric Results.push_back(Tmp1); 33720b57cec5SDimitry Andric break; 33730b57cec5SDimitry Andric case ISD::ROTL: 33740b57cec5SDimitry Andric case ISD::ROTR: 3375e8d8bef9SDimitry Andric if (TLI.expandROT(Node, true /*AllowVectorOps*/, Tmp1, DAG)) 33760b57cec5SDimitry Andric Results.push_back(Tmp1); 33770b57cec5SDimitry Andric break; 33780b57cec5SDimitry Andric case ISD::SADDSAT: 33790b57cec5SDimitry Andric case ISD::UADDSAT: 33800b57cec5SDimitry Andric case ISD::SSUBSAT: 33810b57cec5SDimitry Andric case ISD::USUBSAT: 33820b57cec5SDimitry Andric Results.push_back(TLI.expandAddSubSat(Node, DAG)); 33830b57cec5SDimitry Andric break; 3384e8d8bef9SDimitry Andric case ISD::SSHLSAT: 3385e8d8bef9SDimitry Andric case ISD::USHLSAT: 3386e8d8bef9SDimitry Andric Results.push_back(TLI.expandShlSat(Node, DAG)); 3387e8d8bef9SDimitry Andric break; 33880b57cec5SDimitry Andric case ISD::SMULFIX: 33890b57cec5SDimitry Andric case ISD::SMULFIXSAT: 33900b57cec5SDimitry Andric case ISD::UMULFIX: 33918bcb0991SDimitry Andric case ISD::UMULFIXSAT: 33920b57cec5SDimitry Andric Results.push_back(TLI.expandFixedPointMul(Node, DAG)); 33930b57cec5SDimitry Andric break; 3394480093f4SDimitry Andric case ISD::SDIVFIX: 33955ffd83dbSDimitry Andric case ISD::SDIVFIXSAT: 3396480093f4SDimitry Andric case ISD::UDIVFIX: 33975ffd83dbSDimitry Andric case ISD::UDIVFIXSAT: 3398480093f4SDimitry Andric if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node), 3399480093f4SDimitry Andric Node->getOperand(0), 3400480093f4SDimitry Andric Node->getOperand(1), 3401480093f4SDimitry Andric Node->getConstantOperandVal(2), 3402480093f4SDimitry Andric DAG)) { 3403480093f4SDimitry Andric Results.push_back(V); 3404480093f4SDimitry Andric break; 3405480093f4SDimitry Andric } 3406480093f4SDimitry Andric // FIXME: We might want to retry here with a wider type if we fail, if that 3407480093f4SDimitry Andric // type is legal. 3408480093f4SDimitry Andric // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is 3409480093f4SDimitry Andric // <= 128 (which is the case for all of the default Embedded-C types), 3410480093f4SDimitry Andric // we will only get here with types and scales that we could always expand 3411480093f4SDimitry Andric // if we were allowed to generate libcalls to division functions of illegal 3412480093f4SDimitry Andric // type. But we cannot do that. 3413480093f4SDimitry Andric llvm_unreachable("Cannot expand DIVFIX!"); 34140b57cec5SDimitry Andric case ISD::ADDCARRY: 34150b57cec5SDimitry Andric case ISD::SUBCARRY: { 34160b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 34170b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 34180b57cec5SDimitry Andric SDValue Carry = Node->getOperand(2); 34190b57cec5SDimitry Andric 34200b57cec5SDimitry Andric bool IsAdd = Node->getOpcode() == ISD::ADDCARRY; 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric // Initial add of the 2 operands. 34230b57cec5SDimitry Andric unsigned Op = IsAdd ? ISD::ADD : ISD::SUB; 34240b57cec5SDimitry Andric EVT VT = LHS.getValueType(); 34250b57cec5SDimitry Andric SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS); 34260b57cec5SDimitry Andric 34270b57cec5SDimitry Andric // Initial check for overflow. 34280b57cec5SDimitry Andric EVT CarryType = Node->getValueType(1); 34290b57cec5SDimitry Andric EVT SetCCType = getSetCCResultType(Node->getValueType(0)); 34300b57cec5SDimitry Andric ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; 34310b57cec5SDimitry Andric SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC); 34320b57cec5SDimitry Andric 34330b57cec5SDimitry Andric // Add of the sum and the carry. 34345ffd83dbSDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 34350b57cec5SDimitry Andric SDValue CarryExt = 34365ffd83dbSDimitry Andric DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One); 34370b57cec5SDimitry Andric SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt); 34380b57cec5SDimitry Andric 34390b57cec5SDimitry Andric // Second check for overflow. If we are adding, we can only overflow if the 34400b57cec5SDimitry Andric // initial sum is all 1s ang the carry is set, resulting in a new sum of 0. 34410b57cec5SDimitry Andric // If we are subtracting, we can only overflow if the initial sum is 0 and 34420b57cec5SDimitry Andric // the carry is set, resulting in a new sum of all 1s. 34430b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 34440b57cec5SDimitry Andric SDValue Overflow2 = 34450b57cec5SDimitry Andric IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ) 34460b57cec5SDimitry Andric : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ); 34470b57cec5SDimitry Andric Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2, 34480b57cec5SDimitry Andric DAG.getZExtOrTrunc(Carry, dl, SetCCType)); 34490b57cec5SDimitry Andric 34500b57cec5SDimitry Andric SDValue ResultCarry = 34510b57cec5SDimitry Andric DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2); 34520b57cec5SDimitry Andric 34530b57cec5SDimitry Andric Results.push_back(Sum2); 34540b57cec5SDimitry Andric Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT)); 34550b57cec5SDimitry Andric break; 34560b57cec5SDimitry Andric } 34570b57cec5SDimitry Andric case ISD::SADDO: 34580b57cec5SDimitry Andric case ISD::SSUBO: { 34590b57cec5SDimitry Andric SDValue Result, Overflow; 34600b57cec5SDimitry Andric TLI.expandSADDSUBO(Node, Result, Overflow, DAG); 34610b57cec5SDimitry Andric Results.push_back(Result); 34620b57cec5SDimitry Andric Results.push_back(Overflow); 34630b57cec5SDimitry Andric break; 34640b57cec5SDimitry Andric } 34650b57cec5SDimitry Andric case ISD::UADDO: 34660b57cec5SDimitry Andric case ISD::USUBO: { 34670b57cec5SDimitry Andric SDValue Result, Overflow; 34680b57cec5SDimitry Andric TLI.expandUADDSUBO(Node, Result, Overflow, DAG); 34690b57cec5SDimitry Andric Results.push_back(Result); 34700b57cec5SDimitry Andric Results.push_back(Overflow); 34710b57cec5SDimitry Andric break; 34720b57cec5SDimitry Andric } 34730b57cec5SDimitry Andric case ISD::UMULO: 34740b57cec5SDimitry Andric case ISD::SMULO: { 34750b57cec5SDimitry Andric SDValue Result, Overflow; 34760b57cec5SDimitry Andric if (TLI.expandMULO(Node, Result, Overflow, DAG)) { 34770b57cec5SDimitry Andric Results.push_back(Result); 34780b57cec5SDimitry Andric Results.push_back(Overflow); 34790b57cec5SDimitry Andric } 34800b57cec5SDimitry Andric break; 34810b57cec5SDimitry Andric } 34820b57cec5SDimitry Andric case ISD::BUILD_PAIR: { 34830b57cec5SDimitry Andric EVT PairTy = Node->getValueType(0); 34840b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 34850b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 34860b57cec5SDimitry Andric Tmp2 = DAG.getNode( 34870b57cec5SDimitry Andric ISD::SHL, dl, PairTy, Tmp2, 34880b57cec5SDimitry Andric DAG.getConstant(PairTy.getSizeInBits() / 2, dl, 34890b57cec5SDimitry Andric TLI.getShiftAmountTy(PairTy, DAG.getDataLayout()))); 34900b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 34910b57cec5SDimitry Andric break; 34920b57cec5SDimitry Andric } 34930b57cec5SDimitry Andric case ISD::SELECT: 34940b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 34950b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 34960b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); 34970b57cec5SDimitry Andric if (Tmp1.getOpcode() == ISD::SETCC) { 34980b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 34990b57cec5SDimitry Andric Tmp2, Tmp3, 35000b57cec5SDimitry Andric cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 35010b57cec5SDimitry Andric } else { 35020b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, 35030b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp1.getValueType()), 35040b57cec5SDimitry Andric Tmp2, Tmp3, ISD::SETNE); 35050b57cec5SDimitry Andric } 35060b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 35070b57cec5SDimitry Andric Results.push_back(Tmp1); 35080b57cec5SDimitry Andric break; 35090b57cec5SDimitry Andric case ISD::BR_JT: { 35100b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); 35110b57cec5SDimitry Andric SDValue Table = Node->getOperand(1); 35120b57cec5SDimitry Andric SDValue Index = Node->getOperand(2); 35130b57cec5SDimitry Andric 35140b57cec5SDimitry Andric const DataLayout &TD = DAG.getDataLayout(); 35150b57cec5SDimitry Andric EVT PTy = TLI.getPointerTy(TD); 35160b57cec5SDimitry Andric 35170b57cec5SDimitry Andric unsigned EntrySize = 35180b57cec5SDimitry Andric DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 35190b57cec5SDimitry Andric 35200b57cec5SDimitry Andric // For power-of-two jumptable entry sizes convert multiplication to a shift. 35210b57cec5SDimitry Andric // This transformation needs to be done here since otherwise the MIPS 35220b57cec5SDimitry Andric // backend will end up emitting a three instruction multiply sequence 35230b57cec5SDimitry Andric // instead of a single shift and MSP430 will call a runtime function. 35240b57cec5SDimitry Andric if (llvm::isPowerOf2_32(EntrySize)) 35250b57cec5SDimitry Andric Index = DAG.getNode( 35260b57cec5SDimitry Andric ISD::SHL, dl, Index.getValueType(), Index, 35270b57cec5SDimitry Andric DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType())); 35280b57cec5SDimitry Andric else 35290b57cec5SDimitry Andric Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index, 35300b57cec5SDimitry Andric DAG.getConstant(EntrySize, dl, Index.getValueType())); 35310b57cec5SDimitry Andric SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(), 35320b57cec5SDimitry Andric Index, Table); 35330b57cec5SDimitry Andric 35340b57cec5SDimitry Andric EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 35350b57cec5SDimitry Andric SDValue LD = DAG.getExtLoad( 35360b57cec5SDimitry Andric ISD::SEXTLOAD, dl, PTy, Chain, Addr, 35370b57cec5SDimitry Andric MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT); 35380b57cec5SDimitry Andric Addr = LD; 35390b57cec5SDimitry Andric if (TLI.isJumpTableRelative()) { 35400b57cec5SDimitry Andric // For PIC, the sequence is: 35410b57cec5SDimitry Andric // BRIND(load(Jumptable + index) + RelocBase) 35420b57cec5SDimitry Andric // RelocBase can be JumpTable, GOT or some sort of global base. 35430b57cec5SDimitry Andric Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 35440b57cec5SDimitry Andric TLI.getPICJumpTableRelocBase(Table, DAG)); 35450b57cec5SDimitry Andric } 35460b57cec5SDimitry Andric 35470b57cec5SDimitry Andric Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, DAG); 35480b57cec5SDimitry Andric Results.push_back(Tmp1); 35490b57cec5SDimitry Andric break; 35500b57cec5SDimitry Andric } 35510b57cec5SDimitry Andric case ISD::BRCOND: 35520b57cec5SDimitry Andric // Expand brcond's setcc into its constituent parts and create a BR_CC 35530b57cec5SDimitry Andric // Node. 35540b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 35550b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 3556*4824e7fdSDimitry Andric if (Tmp2.getOpcode() == ISD::SETCC && 3557*4824e7fdSDimitry Andric TLI.isOperationLegalOrCustom(ISD::BR_CC, 3558*4824e7fdSDimitry Andric Tmp2.getOperand(0).getValueType())) { 3559*4824e7fdSDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2), 35600b57cec5SDimitry Andric Tmp2.getOperand(0), Tmp2.getOperand(1), 35610b57cec5SDimitry Andric Node->getOperand(2)); 35620b57cec5SDimitry Andric } else { 35630b57cec5SDimitry Andric // We test only the i1 bit. Skip the AND if UNDEF or another AND. 35640b57cec5SDimitry Andric if (Tmp2.isUndef() || 35650b57cec5SDimitry Andric (Tmp2.getOpcode() == ISD::AND && 35660b57cec5SDimitry Andric isa<ConstantSDNode>(Tmp2.getOperand(1)) && 35670b57cec5SDimitry Andric cast<ConstantSDNode>(Tmp2.getOperand(1))->getZExtValue() == 1)) 35680b57cec5SDimitry Andric Tmp3 = Tmp2; 35690b57cec5SDimitry Andric else 35700b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, 35710b57cec5SDimitry Andric DAG.getConstant(1, dl, Tmp2.getValueType())); 35720b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 35730b57cec5SDimitry Andric DAG.getCondCode(ISD::SETNE), Tmp3, 35740b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp3.getValueType()), 35750b57cec5SDimitry Andric Node->getOperand(2)); 35760b57cec5SDimitry Andric } 35770b57cec5SDimitry Andric Results.push_back(Tmp1); 35780b57cec5SDimitry Andric break; 3579480093f4SDimitry Andric case ISD::SETCC: 3580480093f4SDimitry Andric case ISD::STRICT_FSETCC: 3581480093f4SDimitry Andric case ISD::STRICT_FSETCCS: { 3582480093f4SDimitry Andric bool IsStrict = Node->getOpcode() != ISD::SETCC; 3583480093f4SDimitry Andric bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS; 3584480093f4SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 3585480093f4SDimitry Andric unsigned Offset = IsStrict ? 1 : 0; 3586480093f4SDimitry Andric Tmp1 = Node->getOperand(0 + Offset); 3587480093f4SDimitry Andric Tmp2 = Node->getOperand(1 + Offset); 3588480093f4SDimitry Andric Tmp3 = Node->getOperand(2 + Offset); 3589480093f4SDimitry Andric bool Legalized = 3590fe6060f1SDimitry Andric TLI.LegalizeSetCCCondCode(DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, 3591480093f4SDimitry Andric NeedInvert, dl, Chain, IsSignaling); 35920b57cec5SDimitry Andric 35930b57cec5SDimitry Andric if (Legalized) { 35940b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 35950b57cec5SDimitry Andric // condition code, create a new SETCC node. 35960b57cec5SDimitry Andric if (Tmp3.getNode()) 35970b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 35980b57cec5SDimitry Andric Tmp1, Tmp2, Tmp3, Node->getFlags()); 35990b57cec5SDimitry Andric 36000b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then wrap 36010b57cec5SDimitry Andric // the existing SETCC in a NOT to restore the intended condition. 36020b57cec5SDimitry Andric if (NeedInvert) 36030b57cec5SDimitry Andric Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0)); 36040b57cec5SDimitry Andric 36050b57cec5SDimitry Andric Results.push_back(Tmp1); 3606480093f4SDimitry Andric if (IsStrict) 3607480093f4SDimitry Andric Results.push_back(Chain); 3608480093f4SDimitry Andric 36090b57cec5SDimitry Andric break; 36100b57cec5SDimitry Andric } 36110b57cec5SDimitry Andric 3612480093f4SDimitry Andric // FIXME: It seems Legalized is false iff CCCode is Legal. I don't 3613480093f4SDimitry Andric // understand if this code is useful for strict nodes. 3614480093f4SDimitry Andric assert(!IsStrict && "Don't know how to expand for strict nodes."); 3615480093f4SDimitry Andric 36160b57cec5SDimitry Andric // Otherwise, SETCC for the given comparison type must be completely 36170b57cec5SDimitry Andric // illegal; expand it into a SELECT_CC. 36180b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 36190b57cec5SDimitry Andric int TrueValue; 36200b57cec5SDimitry Andric switch (TLI.getBooleanContents(Tmp1.getValueType())) { 36210b57cec5SDimitry Andric case TargetLowering::ZeroOrOneBooleanContent: 36220b57cec5SDimitry Andric case TargetLowering::UndefinedBooleanContent: 36230b57cec5SDimitry Andric TrueValue = 1; 36240b57cec5SDimitry Andric break; 36250b57cec5SDimitry Andric case TargetLowering::ZeroOrNegativeOneBooleanContent: 36260b57cec5SDimitry Andric TrueValue = -1; 36270b57cec5SDimitry Andric break; 36280b57cec5SDimitry Andric } 36290b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 36300b57cec5SDimitry Andric DAG.getConstant(TrueValue, dl, VT), 36310b57cec5SDimitry Andric DAG.getConstant(0, dl, VT), 36320b57cec5SDimitry Andric Tmp3); 36330b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36340b57cec5SDimitry Andric Results.push_back(Tmp1); 36350b57cec5SDimitry Andric break; 36360b57cec5SDimitry Andric } 36370b57cec5SDimitry Andric case ISD::SELECT_CC: { 3638480093f4SDimitry Andric // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS 36390b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // LHS 36400b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); // RHS 36410b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); // True 36420b57cec5SDimitry Andric Tmp4 = Node->getOperand(3); // False 36430b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 3644480093f4SDimitry Andric SDValue Chain; 36450b57cec5SDimitry Andric SDValue CC = Node->getOperand(4); 36460b57cec5SDimitry Andric ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get(); 36470b57cec5SDimitry Andric 36480b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) { 36490b57cec5SDimitry Andric // If the condition code is legal, then we need to expand this 36500b57cec5SDimitry Andric // node using SETCC and SELECT. 36510b57cec5SDimitry Andric EVT CmpVT = Tmp1.getValueType(); 36520b57cec5SDimitry Andric assert(!TLI.isOperationExpand(ISD::SELECT, VT) && 36530b57cec5SDimitry Andric "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be " 36540b57cec5SDimitry Andric "expanded."); 36550b57cec5SDimitry Andric EVT CCVT = getSetCCResultType(CmpVT); 36560b57cec5SDimitry Andric SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags()); 36570b57cec5SDimitry Andric Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4)); 36580b57cec5SDimitry Andric break; 36590b57cec5SDimitry Andric } 36600b57cec5SDimitry Andric 36610b57cec5SDimitry Andric // SELECT_CC is legal, so the condition code must not be. 36620b57cec5SDimitry Andric bool Legalized = false; 36630b57cec5SDimitry Andric // Try to legalize by inverting the condition. This is for targets that 36640b57cec5SDimitry Andric // might support an ordered version of a condition, but not the unordered 36650b57cec5SDimitry Andric // version (or vice versa). 3666480093f4SDimitry Andric ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType()); 36670b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) { 36680b57cec5SDimitry Andric // Use the new condition code and swap true and false 36690b57cec5SDimitry Andric Legalized = true; 36700b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC); 36710b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36720b57cec5SDimitry Andric } else { 36730b57cec5SDimitry Andric // If The inverse is not legal, then try to swap the arguments using 36740b57cec5SDimitry Andric // the inverse condition code. 36750b57cec5SDimitry Andric ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC); 36760b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) { 36770b57cec5SDimitry Andric // The swapped inverse condition is legal, so swap true and false, 36780b57cec5SDimitry Andric // lhs and rhs. 36790b57cec5SDimitry Andric Legalized = true; 36800b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC); 36810b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36820b57cec5SDimitry Andric } 36830b57cec5SDimitry Andric } 36840b57cec5SDimitry Andric 36850b57cec5SDimitry Andric if (!Legalized) { 3686fe6060f1SDimitry Andric Legalized = TLI.LegalizeSetCCCondCode( 3687fe6060f1SDimitry Andric DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, 3688fe6060f1SDimitry Andric NeedInvert, dl, Chain); 36890b57cec5SDimitry Andric 36900b57cec5SDimitry Andric assert(Legalized && "Can't legalize SELECT_CC with legal condition!"); 36910b57cec5SDimitry Andric 36920b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then swap 36930b57cec5SDimitry Andric // the True/False operands to match. 36940b57cec5SDimitry Andric if (NeedInvert) 36950b57cec5SDimitry Andric std::swap(Tmp3, Tmp4); 36960b57cec5SDimitry Andric 36970b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 36980b57cec5SDimitry Andric // condition code, create a new SELECT_CC node. 36990b57cec5SDimitry Andric if (CC.getNode()) { 37000b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), 37010b57cec5SDimitry Andric Tmp1, Tmp2, Tmp3, Tmp4, CC); 37020b57cec5SDimitry Andric } else { 37030b57cec5SDimitry Andric Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType()); 37040b57cec5SDimitry Andric CC = DAG.getCondCode(ISD::SETNE); 37050b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, 37060b57cec5SDimitry Andric Tmp2, Tmp3, Tmp4, CC); 37070b57cec5SDimitry Andric } 37080b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 37090b57cec5SDimitry Andric } 37100b57cec5SDimitry Andric Results.push_back(Tmp1); 37110b57cec5SDimitry Andric break; 37120b57cec5SDimitry Andric } 37130b57cec5SDimitry Andric case ISD::BR_CC: { 3714480093f4SDimitry Andric // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS 3715480093f4SDimitry Andric SDValue Chain; 37160b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // Chain 37170b57cec5SDimitry Andric Tmp2 = Node->getOperand(2); // LHS 37180b57cec5SDimitry Andric Tmp3 = Node->getOperand(3); // RHS 37190b57cec5SDimitry Andric Tmp4 = Node->getOperand(1); // CC 37200b57cec5SDimitry Andric 3721480093f4SDimitry Andric bool Legalized = 3722fe6060f1SDimitry Andric TLI.LegalizeSetCCCondCode(DAG, getSetCCResultType(Tmp2.getValueType()), 3723fe6060f1SDimitry Andric Tmp2, Tmp3, Tmp4, NeedInvert, dl, Chain); 37240b57cec5SDimitry Andric (void)Legalized; 37250b57cec5SDimitry Andric assert(Legalized && "Can't legalize BR_CC with legal condition!"); 37260b57cec5SDimitry Andric 37270b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC 37280b57cec5SDimitry Andric // node. 37290b57cec5SDimitry Andric if (Tmp4.getNode()) { 3730e8d8bef9SDimitry Andric assert(!NeedInvert && "Don't know how to invert BR_CC!"); 3731e8d8bef9SDimitry Andric 37320b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, 37330b57cec5SDimitry Andric Tmp4, Tmp2, Tmp3, Node->getOperand(4)); 37340b57cec5SDimitry Andric } else { 37350b57cec5SDimitry Andric Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType()); 3736e8d8bef9SDimitry Andric Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE); 37370b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, 37380b57cec5SDimitry Andric Tmp2, Tmp3, Node->getOperand(4)); 37390b57cec5SDimitry Andric } 37400b57cec5SDimitry Andric Results.push_back(Tmp1); 37410b57cec5SDimitry Andric break; 37420b57cec5SDimitry Andric } 37430b57cec5SDimitry Andric case ISD::BUILD_VECTOR: 37440b57cec5SDimitry Andric Results.push_back(ExpandBUILD_VECTOR(Node)); 37450b57cec5SDimitry Andric break; 37468bcb0991SDimitry Andric case ISD::SPLAT_VECTOR: 37478bcb0991SDimitry Andric Results.push_back(ExpandSPLAT_VECTOR(Node)); 37488bcb0991SDimitry Andric break; 37490b57cec5SDimitry Andric case ISD::SRA: 37500b57cec5SDimitry Andric case ISD::SRL: 37510b57cec5SDimitry Andric case ISD::SHL: { 37520b57cec5SDimitry Andric // Scalarize vector SRA/SRL/SHL. 37530b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 37540b57cec5SDimitry Andric assert(VT.isVector() && "Unable to legalize non-vector shift"); 37550b57cec5SDimitry Andric assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal"); 37560b57cec5SDimitry Andric unsigned NumElem = VT.getVectorNumElements(); 37570b57cec5SDimitry Andric 37580b57cec5SDimitry Andric SmallVector<SDValue, 8> Scalars; 37590b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < NumElem; Idx++) { 37605ffd83dbSDimitry Andric SDValue Ex = 37615ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 37625ffd83dbSDimitry Andric Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl)); 37635ffd83dbSDimitry Andric SDValue Sh = 37645ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 37655ffd83dbSDimitry Andric Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl)); 37660b57cec5SDimitry Andric Scalars.push_back(DAG.getNode(Node->getOpcode(), dl, 37670b57cec5SDimitry Andric VT.getScalarType(), Ex, Sh)); 37680b57cec5SDimitry Andric } 37690b57cec5SDimitry Andric 37700b57cec5SDimitry Andric SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars); 3771480093f4SDimitry Andric Results.push_back(Result); 37720b57cec5SDimitry Andric break; 37730b57cec5SDimitry Andric } 37740b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 37750b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 37760b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 37770b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 37780b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 37790b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 37800b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 37810b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 37820b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 37830b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 37840b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 37850b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 37860b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 37870b57cec5SDimitry Andric Results.push_back(TLI.expandVecReduce(Node, DAG)); 37880b57cec5SDimitry Andric break; 37890b57cec5SDimitry Andric case ISD::GLOBAL_OFFSET_TABLE: 37900b57cec5SDimitry Andric case ISD::GlobalAddress: 37910b57cec5SDimitry Andric case ISD::GlobalTLSAddress: 37920b57cec5SDimitry Andric case ISD::ExternalSymbol: 37930b57cec5SDimitry Andric case ISD::ConstantPool: 37940b57cec5SDimitry Andric case ISD::JumpTable: 37950b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 37960b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 37970b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 37980b57cec5SDimitry Andric // FIXME: Custom lowering for these operations shouldn't return null! 3799480093f4SDimitry Andric // Return true so that we don't call ConvertNodeToLibcall which also won't 3800480093f4SDimitry Andric // do anything. 3801480093f4SDimitry Andric return true; 38020b57cec5SDimitry Andric } 38030b57cec5SDimitry Andric 3804480093f4SDimitry Andric if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) { 38058bcb0991SDimitry Andric // FIXME: We were asked to expand a strict floating-point operation, 38068bcb0991SDimitry Andric // but there is currently no expansion implemented that would preserve 38078bcb0991SDimitry Andric // the "strict" properties. For now, we just fall back to the non-strict 38088bcb0991SDimitry Andric // version if that is legal on the target. The actual mutation of the 38098bcb0991SDimitry Andric // operation will happen in SelectionDAGISel::DoInstructionSelection. 38108bcb0991SDimitry Andric switch (Node->getOpcode()) { 38118bcb0991SDimitry Andric default: 38128bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 38138bcb0991SDimitry Andric Node->getValueType(0)) 38148bcb0991SDimitry Andric == TargetLowering::Legal) 38158bcb0991SDimitry Andric return true; 38168bcb0991SDimitry Andric break; 3817e8d8bef9SDimitry Andric case ISD::STRICT_FSUB: { 3818e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 3819e8d8bef9SDimitry Andric ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal) 3820e8d8bef9SDimitry Andric return true; 3821e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 3822e8d8bef9SDimitry Andric ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal) 3823e8d8bef9SDimitry Andric break; 3824e8d8bef9SDimitry Andric 3825e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 3826e8d8bef9SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 3827e8d8bef9SDimitry Andric SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags); 3828e8d8bef9SDimitry Andric SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(), 3829e8d8bef9SDimitry Andric {Node->getOperand(0), Node->getOperand(1), Neg}, 3830e8d8bef9SDimitry Andric Flags); 3831e8d8bef9SDimitry Andric 3832e8d8bef9SDimitry Andric Results.push_back(Fadd); 3833e8d8bef9SDimitry Andric Results.push_back(Fadd.getValue(1)); 3834e8d8bef9SDimitry Andric break; 3835e8d8bef9SDimitry Andric } 3836e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 3837e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 38388bcb0991SDimitry Andric case ISD::STRICT_LRINT: 38398bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 38408bcb0991SDimitry Andric case ISD::STRICT_LROUND: 38418bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 38428bcb0991SDimitry Andric // These are registered by the operand type instead of the value 38438bcb0991SDimitry Andric // type. Reflect that here. 38448bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 38458bcb0991SDimitry Andric Node->getOperand(1).getValueType()) 38468bcb0991SDimitry Andric == TargetLowering::Legal) 38478bcb0991SDimitry Andric return true; 38488bcb0991SDimitry Andric break; 38498bcb0991SDimitry Andric } 38508bcb0991SDimitry Andric } 38518bcb0991SDimitry Andric 38520b57cec5SDimitry Andric // Replace the original node with the legalized result. 38530b57cec5SDimitry Andric if (Results.empty()) { 38540b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Cannot expand node\n"); 38550b57cec5SDimitry Andric return false; 38560b57cec5SDimitry Andric } 38570b57cec5SDimitry Andric 38580b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded node\n"); 38590b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 38600b57cec5SDimitry Andric return true; 38610b57cec5SDimitry Andric } 38620b57cec5SDimitry Andric 38630b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) { 38640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n"); 38650b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 38660b57cec5SDimitry Andric SDLoc dl(Node); 38670b57cec5SDimitry Andric // FIXME: Check flags on the node to see if we can use a finite call. 38680b57cec5SDimitry Andric unsigned Opc = Node->getOpcode(); 38690b57cec5SDimitry Andric switch (Opc) { 38700b57cec5SDimitry Andric case ISD::ATOMIC_FENCE: { 38710b57cec5SDimitry Andric // If the target didn't lower this, lower it to '__sync_synchronize()' call 38720b57cec5SDimitry Andric // FIXME: handle "fence singlethread" more efficiently. 38730b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 38740b57cec5SDimitry Andric 38750b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 38760b57cec5SDimitry Andric CLI.setDebugLoc(dl) 38770b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 38780b57cec5SDimitry Andric .setLibCallee( 38790b57cec5SDimitry Andric CallingConv::C, Type::getVoidTy(*DAG.getContext()), 38800b57cec5SDimitry Andric DAG.getExternalSymbol("__sync_synchronize", 38810b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())), 38820b57cec5SDimitry Andric std::move(Args)); 38830b57cec5SDimitry Andric 38840b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 38850b57cec5SDimitry Andric 38860b57cec5SDimitry Andric Results.push_back(CallResult.second); 38870b57cec5SDimitry Andric break; 38880b57cec5SDimitry Andric } 38890b57cec5SDimitry Andric // By default, atomic intrinsics are marked Legal and lowered. Targets 38900b57cec5SDimitry Andric // which don't support them directly, however, may want libcalls, in which 38910b57cec5SDimitry Andric // case they mark them Expand, and we get here. 38920b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: 38930b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_ADD: 38940b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_SUB: 38950b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_AND: 38960b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_CLR: 38970b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_OR: 38980b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_XOR: 38990b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_NAND: 39000b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MIN: 39010b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MAX: 39020b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMIN: 39030b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMAX: 39040b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP: { 39050b57cec5SDimitry Andric MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 3906fe6060f1SDimitry Andric AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering(); 3907e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT); 3908480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 3909480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 3910e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops; 3911e8d8bef9SDimitry Andric if (TLI.getLibcallName(LC)) { 3912e8d8bef9SDimitry Andric // If outline atomic available, prepare its arguments and expand. 3913e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 2, Node->op_end()); 3914e8d8bef9SDimitry Andric Ops.push_back(Node->getOperand(1)); 3915e8d8bef9SDimitry Andric 3916e8d8bef9SDimitry Andric } else { 3917e8d8bef9SDimitry Andric LC = RTLIB::getSYNC(Opc, VT); 3918e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && 3919e8d8bef9SDimitry Andric "Unexpected atomic op or value type!"); 3920e8d8bef9SDimitry Andric // Arguments for expansion to sync libcall 3921e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 1, Node->op_end()); 3922e8d8bef9SDimitry Andric } 3923480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 3924480093f4SDimitry Andric Ops, CallOptions, 3925480093f4SDimitry Andric SDLoc(Node), 3926480093f4SDimitry Andric Node->getOperand(0)); 39270b57cec5SDimitry Andric Results.push_back(Tmp.first); 39280b57cec5SDimitry Andric Results.push_back(Tmp.second); 39290b57cec5SDimitry Andric break; 39300b57cec5SDimitry Andric } 39310b57cec5SDimitry Andric case ISD::TRAP: { 39320b57cec5SDimitry Andric // If this operation is not supported, lower it to 'abort()' call 39330b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 39340b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 39350b57cec5SDimitry Andric CLI.setDebugLoc(dl) 39360b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 39370b57cec5SDimitry Andric .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 39380b57cec5SDimitry Andric DAG.getExternalSymbol( 39390b57cec5SDimitry Andric "abort", TLI.getPointerTy(DAG.getDataLayout())), 39400b57cec5SDimitry Andric std::move(Args)); 39410b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 39420b57cec5SDimitry Andric 39430b57cec5SDimitry Andric Results.push_back(CallResult.second); 39440b57cec5SDimitry Andric break; 39450b57cec5SDimitry Andric } 39460b57cec5SDimitry Andric case ISD::FMINNUM: 39470b57cec5SDimitry Andric case ISD::STRICT_FMINNUM: 3948480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64, 39490b57cec5SDimitry Andric RTLIB::FMIN_F80, RTLIB::FMIN_F128, 3950480093f4SDimitry Andric RTLIB::FMIN_PPCF128, Results); 39510b57cec5SDimitry Andric break; 39520b57cec5SDimitry Andric case ISD::FMAXNUM: 39530b57cec5SDimitry Andric case ISD::STRICT_FMAXNUM: 3954480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64, 39550b57cec5SDimitry Andric RTLIB::FMAX_F80, RTLIB::FMAX_F128, 3956480093f4SDimitry Andric RTLIB::FMAX_PPCF128, Results); 39570b57cec5SDimitry Andric break; 39580b57cec5SDimitry Andric case ISD::FSQRT: 39590b57cec5SDimitry Andric case ISD::STRICT_FSQRT: 3960480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 39610b57cec5SDimitry Andric RTLIB::SQRT_F80, RTLIB::SQRT_F128, 3962480093f4SDimitry Andric RTLIB::SQRT_PPCF128, Results); 39630b57cec5SDimitry Andric break; 39640b57cec5SDimitry Andric case ISD::FCBRT: 3965480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64, 39660b57cec5SDimitry Andric RTLIB::CBRT_F80, RTLIB::CBRT_F128, 3967480093f4SDimitry Andric RTLIB::CBRT_PPCF128, Results); 39680b57cec5SDimitry Andric break; 39690b57cec5SDimitry Andric case ISD::FSIN: 39700b57cec5SDimitry Andric case ISD::STRICT_FSIN: 3971480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 39720b57cec5SDimitry Andric RTLIB::SIN_F80, RTLIB::SIN_F128, 3973480093f4SDimitry Andric RTLIB::SIN_PPCF128, Results); 39740b57cec5SDimitry Andric break; 39750b57cec5SDimitry Andric case ISD::FCOS: 39760b57cec5SDimitry Andric case ISD::STRICT_FCOS: 3977480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 39780b57cec5SDimitry Andric RTLIB::COS_F80, RTLIB::COS_F128, 3979480093f4SDimitry Andric RTLIB::COS_PPCF128, Results); 39800b57cec5SDimitry Andric break; 39810b57cec5SDimitry Andric case ISD::FSINCOS: 39820b57cec5SDimitry Andric // Expand into sincos libcall. 39830b57cec5SDimitry Andric ExpandSinCosLibCall(Node, Results); 39840b57cec5SDimitry Andric break; 39850b57cec5SDimitry Andric case ISD::FLOG: 39860b57cec5SDimitry Andric case ISD::STRICT_FLOG: 39878c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80, 39888c27c554SDimitry Andric RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results); 39890b57cec5SDimitry Andric break; 39900b57cec5SDimitry Andric case ISD::FLOG2: 39910b57cec5SDimitry Andric case ISD::STRICT_FLOG2: 39928c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80, 39938c27c554SDimitry Andric RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results); 39940b57cec5SDimitry Andric break; 39950b57cec5SDimitry Andric case ISD::FLOG10: 39960b57cec5SDimitry Andric case ISD::STRICT_FLOG10: 39978c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80, 39988c27c554SDimitry Andric RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results); 39990b57cec5SDimitry Andric break; 40000b57cec5SDimitry Andric case ISD::FEXP: 40010b57cec5SDimitry Andric case ISD::STRICT_FEXP: 40028c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80, 40038c27c554SDimitry Andric RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results); 40040b57cec5SDimitry Andric break; 40050b57cec5SDimitry Andric case ISD::FEXP2: 40060b57cec5SDimitry Andric case ISD::STRICT_FEXP2: 40078c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80, 40088c27c554SDimitry Andric RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results); 40090b57cec5SDimitry Andric break; 40100b57cec5SDimitry Andric case ISD::FTRUNC: 40110b57cec5SDimitry Andric case ISD::STRICT_FTRUNC: 4012480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 40130b57cec5SDimitry Andric RTLIB::TRUNC_F80, RTLIB::TRUNC_F128, 4014480093f4SDimitry Andric RTLIB::TRUNC_PPCF128, Results); 40150b57cec5SDimitry Andric break; 40160b57cec5SDimitry Andric case ISD::FFLOOR: 40170b57cec5SDimitry Andric case ISD::STRICT_FFLOOR: 4018480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 40190b57cec5SDimitry Andric RTLIB::FLOOR_F80, RTLIB::FLOOR_F128, 4020480093f4SDimitry Andric RTLIB::FLOOR_PPCF128, Results); 40210b57cec5SDimitry Andric break; 40220b57cec5SDimitry Andric case ISD::FCEIL: 40230b57cec5SDimitry Andric case ISD::STRICT_FCEIL: 4024480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 40250b57cec5SDimitry Andric RTLIB::CEIL_F80, RTLIB::CEIL_F128, 4026480093f4SDimitry Andric RTLIB::CEIL_PPCF128, Results); 40270b57cec5SDimitry Andric break; 40280b57cec5SDimitry Andric case ISD::FRINT: 40290b57cec5SDimitry Andric case ISD::STRICT_FRINT: 4030480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 40310b57cec5SDimitry Andric RTLIB::RINT_F80, RTLIB::RINT_F128, 4032480093f4SDimitry Andric RTLIB::RINT_PPCF128, Results); 40330b57cec5SDimitry Andric break; 40340b57cec5SDimitry Andric case ISD::FNEARBYINT: 40350b57cec5SDimitry Andric case ISD::STRICT_FNEARBYINT: 4036480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 40370b57cec5SDimitry Andric RTLIB::NEARBYINT_F64, 40380b57cec5SDimitry Andric RTLIB::NEARBYINT_F80, 40390b57cec5SDimitry Andric RTLIB::NEARBYINT_F128, 4040480093f4SDimitry Andric RTLIB::NEARBYINT_PPCF128, Results); 40410b57cec5SDimitry Andric break; 40420b57cec5SDimitry Andric case ISD::FROUND: 40430b57cec5SDimitry Andric case ISD::STRICT_FROUND: 4044480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUND_F32, 40450b57cec5SDimitry Andric RTLIB::ROUND_F64, 40460b57cec5SDimitry Andric RTLIB::ROUND_F80, 40470b57cec5SDimitry Andric RTLIB::ROUND_F128, 4048480093f4SDimitry Andric RTLIB::ROUND_PPCF128, Results); 40490b57cec5SDimitry Andric break; 40505ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 40515ffd83dbSDimitry Andric case ISD::STRICT_FROUNDEVEN: 40525ffd83dbSDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32, 40535ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F64, 40545ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F80, 40555ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F128, 40565ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_PPCF128, Results); 40575ffd83dbSDimitry Andric break; 40580b57cec5SDimitry Andric case ISD::FPOWI: 4059480093f4SDimitry Andric case ISD::STRICT_FPOWI: { 4060fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0)); 4061fe6060f1SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi."); 4062480093f4SDimitry Andric if (!TLI.getLibcallName(LC)) { 4063480093f4SDimitry Andric // Some targets don't have a powi libcall; use pow instead. 4064480093f4SDimitry Andric SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), 4065480093f4SDimitry Andric Node->getValueType(0), 4066480093f4SDimitry Andric Node->getOperand(1)); 4067480093f4SDimitry Andric Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node), 4068480093f4SDimitry Andric Node->getValueType(0), Node->getOperand(0), 4069480093f4SDimitry Andric Exponent)); 40700b57cec5SDimitry Andric break; 4071480093f4SDimitry Andric } 4072fe6060f1SDimitry Andric unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0; 4073fe6060f1SDimitry Andric bool ExponentHasSizeOfInt = 4074fe6060f1SDimitry Andric DAG.getLibInfo().getIntSize() == 4075fe6060f1SDimitry Andric Node->getOperand(1 + Offset).getValueType().getSizeInBits(); 4076fe6060f1SDimitry Andric if (!ExponentHasSizeOfInt) { 4077fe6060f1SDimitry Andric // If the exponent does not match with sizeof(int) a libcall to 4078fe6060f1SDimitry Andric // RTLIB::POWI would use the wrong type for the argument. 4079fe6060f1SDimitry Andric DAG.getContext()->emitError("POWI exponent does not match sizeof(int)"); 4080fe6060f1SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 4081fe6060f1SDimitry Andric break; 4082fe6060f1SDimitry Andric } 4083fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 4084480093f4SDimitry Andric break; 4085480093f4SDimitry Andric } 40860b57cec5SDimitry Andric case ISD::FPOW: 40870b57cec5SDimitry Andric case ISD::STRICT_FPOW: 40888c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, 40898c27c554SDimitry Andric RTLIB::POW_F128, RTLIB::POW_PPCF128, Results); 40900b57cec5SDimitry Andric break; 40918bcb0991SDimitry Andric case ISD::LROUND: 40928bcb0991SDimitry Andric case ISD::STRICT_LROUND: 4093480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LROUND_F32, 40948bcb0991SDimitry Andric RTLIB::LROUND_F64, RTLIB::LROUND_F80, 40958bcb0991SDimitry Andric RTLIB::LROUND_F128, 4096480093f4SDimitry Andric RTLIB::LROUND_PPCF128, Results); 40978bcb0991SDimitry Andric break; 40988bcb0991SDimitry Andric case ISD::LLROUND: 40998bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 4100480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32, 41018bcb0991SDimitry Andric RTLIB::LLROUND_F64, RTLIB::LLROUND_F80, 41028bcb0991SDimitry Andric RTLIB::LLROUND_F128, 4103480093f4SDimitry Andric RTLIB::LLROUND_PPCF128, Results); 41048bcb0991SDimitry Andric break; 41058bcb0991SDimitry Andric case ISD::LRINT: 41068bcb0991SDimitry Andric case ISD::STRICT_LRINT: 4107480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LRINT_F32, 41088bcb0991SDimitry Andric RTLIB::LRINT_F64, RTLIB::LRINT_F80, 41098bcb0991SDimitry Andric RTLIB::LRINT_F128, 4110480093f4SDimitry Andric RTLIB::LRINT_PPCF128, Results); 41118bcb0991SDimitry Andric break; 41128bcb0991SDimitry Andric case ISD::LLRINT: 41138bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 4114480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32, 41158bcb0991SDimitry Andric RTLIB::LLRINT_F64, RTLIB::LLRINT_F80, 41168bcb0991SDimitry Andric RTLIB::LLRINT_F128, 4117480093f4SDimitry Andric RTLIB::LLRINT_PPCF128, Results); 41188bcb0991SDimitry Andric break; 41190b57cec5SDimitry Andric case ISD::FDIV: 4120480093f4SDimitry Andric case ISD::STRICT_FDIV: 4121480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 41220b57cec5SDimitry Andric RTLIB::DIV_F80, RTLIB::DIV_F128, 4123480093f4SDimitry Andric RTLIB::DIV_PPCF128, Results); 41240b57cec5SDimitry Andric break; 41250b57cec5SDimitry Andric case ISD::FREM: 41260b57cec5SDimitry Andric case ISD::STRICT_FREM: 4127480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 41280b57cec5SDimitry Andric RTLIB::REM_F80, RTLIB::REM_F128, 4129480093f4SDimitry Andric RTLIB::REM_PPCF128, Results); 41300b57cec5SDimitry Andric break; 41310b57cec5SDimitry Andric case ISD::FMA: 41320b57cec5SDimitry Andric case ISD::STRICT_FMA: 4133480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, 41340b57cec5SDimitry Andric RTLIB::FMA_F80, RTLIB::FMA_F128, 4135480093f4SDimitry Andric RTLIB::FMA_PPCF128, Results); 41360b57cec5SDimitry Andric break; 41370b57cec5SDimitry Andric case ISD::FADD: 4138480093f4SDimitry Andric case ISD::STRICT_FADD: 4139480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64, 41400b57cec5SDimitry Andric RTLIB::ADD_F80, RTLIB::ADD_F128, 4141480093f4SDimitry Andric RTLIB::ADD_PPCF128, Results); 41420b57cec5SDimitry Andric break; 41430b57cec5SDimitry Andric case ISD::FMUL: 4144480093f4SDimitry Andric case ISD::STRICT_FMUL: 4145480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64, 41460b57cec5SDimitry Andric RTLIB::MUL_F80, RTLIB::MUL_F128, 4147480093f4SDimitry Andric RTLIB::MUL_PPCF128, Results); 41480b57cec5SDimitry Andric break; 41490b57cec5SDimitry Andric case ISD::FP16_TO_FP: 41500b57cec5SDimitry Andric if (Node->getValueType(0) == MVT::f32) { 41510b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); 41520b57cec5SDimitry Andric } 41530b57cec5SDimitry Andric break; 41545ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: { 41555ffd83dbSDimitry Andric if (Node->getValueType(0) == MVT::f32) { 41565ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 41575ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall( 41585ffd83dbSDimitry Andric DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions, 41595ffd83dbSDimitry Andric SDLoc(Node), Node->getOperand(0)); 41605ffd83dbSDimitry Andric Results.push_back(Tmp.first); 41615ffd83dbSDimitry Andric Results.push_back(Tmp.second); 41625ffd83dbSDimitry Andric } 41635ffd83dbSDimitry Andric break; 41645ffd83dbSDimitry Andric } 41650b57cec5SDimitry Andric case ISD::FP_TO_FP16: { 41660b57cec5SDimitry Andric RTLIB::Libcall LC = 41670b57cec5SDimitry Andric RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16); 41680b57cec5SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16"); 41690b57cec5SDimitry Andric Results.push_back(ExpandLibCall(LC, Node, false)); 41700b57cec5SDimitry Andric break; 41710b57cec5SDimitry Andric } 4172e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4173e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 4174e8d8bef9SDimitry Andric case ISD::SINT_TO_FP: 4175e8d8bef9SDimitry Andric case ISD::UINT_TO_FP: { 4176e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP 4177e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4178e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::SINT_TO_FP || 4179e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP; 4180e8d8bef9SDimitry Andric EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType(); 4181e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4182e8d8bef9SDimitry Andric EVT NVT = EVT(); 4183e8d8bef9SDimitry Andric SDLoc dl(Node); 4184e8d8bef9SDimitry Andric 4185e8d8bef9SDimitry Andric // Even if the input is legal, no libcall may exactly match, eg. we don't 4186e8d8bef9SDimitry Andric // have i1 -> fp conversions. So, it needs to be promoted to a larger type, 4187e8d8bef9SDimitry Andric // eg: i13 -> fp. Then, look for an appropriate libcall. 4188e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4189e8d8bef9SDimitry Andric for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE; 4190e8d8bef9SDimitry Andric t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4191e8d8bef9SDimitry Andric ++t) { 4192e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)t; 4193e8d8bef9SDimitry Andric // The source needs to big enough to hold the operand. 4194e8d8bef9SDimitry Andric if (NVT.bitsGE(SVT)) 4195e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT) 4196e8d8bef9SDimitry Andric : RTLIB::getUINTTOFP(NVT, RVT); 4197e8d8bef9SDimitry Andric } 4198e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4199e8d8bef9SDimitry Andric 4200e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4201e8d8bef9SDimitry Andric // Sign/zero extend the argument if the libcall takes a larger type. 4202e8d8bef9SDimitry Andric SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl, 4203e8d8bef9SDimitry Andric NVT, Node->getOperand(IsStrict ? 1 : 0)); 4204e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4205e8d8bef9SDimitry Andric CallOptions.setSExt(Signed); 4206e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4207e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain); 4208e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4209e8d8bef9SDimitry Andric if (IsStrict) 4210e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4211e8d8bef9SDimitry Andric break; 4212e8d8bef9SDimitry Andric } 4213e8d8bef9SDimitry Andric case ISD::FP_TO_SINT: 4214e8d8bef9SDimitry Andric case ISD::FP_TO_UINT: 4215e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4216e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_UINT: { 4217e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT. 4218e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4219e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::FP_TO_SINT || 4220e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_SINT; 4221e8d8bef9SDimitry Andric 4222e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4223e8d8bef9SDimitry Andric EVT SVT = Op.getValueType(); 4224e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4225e8d8bef9SDimitry Andric EVT NVT = EVT(); 4226e8d8bef9SDimitry Andric SDLoc dl(Node); 4227e8d8bef9SDimitry Andric 4228e8d8bef9SDimitry Andric // Even if the result is legal, no libcall may exactly match, eg. we don't 4229e8d8bef9SDimitry Andric // have fp -> i1 conversions. So, it needs to be promoted to a larger type, 4230e8d8bef9SDimitry Andric // eg: fp -> i32. Then, look for an appropriate libcall. 4231e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4232e8d8bef9SDimitry Andric for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE; 4233e8d8bef9SDimitry Andric IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4234e8d8bef9SDimitry Andric ++IntVT) { 4235e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)IntVT; 4236e8d8bef9SDimitry Andric // The type needs to big enough to hold the result. 4237e8d8bef9SDimitry Andric if (NVT.bitsGE(RVT)) 4238e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT) 4239e8d8bef9SDimitry Andric : RTLIB::getFPTOUINT(SVT, NVT); 4240e8d8bef9SDimitry Andric } 4241e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4242e8d8bef9SDimitry Andric 4243e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4244e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4245e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4246e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain); 4247e8d8bef9SDimitry Andric 4248e8d8bef9SDimitry Andric // Truncate the result if the libcall returns a larger type. 4249e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first)); 4250e8d8bef9SDimitry Andric if (IsStrict) 4251e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4252e8d8bef9SDimitry Andric break; 4253e8d8bef9SDimitry Andric } 4254e8d8bef9SDimitry Andric 4255e8d8bef9SDimitry Andric case ISD::FP_ROUND: 4256e8d8bef9SDimitry Andric case ISD::STRICT_FP_ROUND: { 4257e8d8bef9SDimitry Andric // X = FP_ROUND(Y, TRUNC) 4258e8d8bef9SDimitry Andric // TRUNC is a flag, which is always an integer that is zero or one. 4259e8d8bef9SDimitry Andric // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND 4260e8d8bef9SDimitry Andric // is known to not change the value of Y. 4261e8d8bef9SDimitry Andric // We can only expand it into libcall if the TRUNC is 0. 4262e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4263e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4264e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4265e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 4266349cc55cSDimitry Andric assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() && 4267e8d8bef9SDimitry Andric "Unable to expand as libcall if it is not normal rounding"); 4268e8d8bef9SDimitry Andric 4269e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT); 4270e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4271e8d8bef9SDimitry Andric 4272e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4273e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4274e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain); 4275e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4276e8d8bef9SDimitry Andric if (IsStrict) 4277e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4278e8d8bef9SDimitry Andric break; 4279e8d8bef9SDimitry Andric } 4280e8d8bef9SDimitry Andric case ISD::FP_EXTEND: { 4281e8d8bef9SDimitry Andric Results.push_back( 4282e8d8bef9SDimitry Andric ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(), 4283e8d8bef9SDimitry Andric Node->getValueType(0)), 4284e8d8bef9SDimitry Andric Node, false)); 4285e8d8bef9SDimitry Andric break; 4286e8d8bef9SDimitry Andric } 4287e8d8bef9SDimitry Andric case ISD::STRICT_FP_EXTEND: 42885ffd83dbSDimitry Andric case ISD::STRICT_FP_TO_FP16: { 42895ffd83dbSDimitry Andric RTLIB::Libcall LC = 4290e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_FP16 4291e8d8bef9SDimitry Andric ? RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16) 4292e8d8bef9SDimitry Andric : RTLIB::getFPEXT(Node->getOperand(1).getValueType(), 4293e8d8bef9SDimitry Andric Node->getValueType(0)); 4294e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4295e8d8bef9SDimitry Andric 42965ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 42975ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = 42985ffd83dbSDimitry Andric TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1), 42995ffd83dbSDimitry Andric CallOptions, SDLoc(Node), Node->getOperand(0)); 43005ffd83dbSDimitry Andric Results.push_back(Tmp.first); 43015ffd83dbSDimitry Andric Results.push_back(Tmp.second); 43025ffd83dbSDimitry Andric break; 43035ffd83dbSDimitry Andric } 43040b57cec5SDimitry Andric case ISD::FSUB: 4305480093f4SDimitry Andric case ISD::STRICT_FSUB: 4306480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64, 43070b57cec5SDimitry Andric RTLIB::SUB_F80, RTLIB::SUB_F128, 4308480093f4SDimitry Andric RTLIB::SUB_PPCF128, Results); 43090b57cec5SDimitry Andric break; 43100b57cec5SDimitry Andric case ISD::SREM: 43110b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 43120b57cec5SDimitry Andric RTLIB::SREM_I8, 43130b57cec5SDimitry Andric RTLIB::SREM_I16, RTLIB::SREM_I32, 43140b57cec5SDimitry Andric RTLIB::SREM_I64, RTLIB::SREM_I128)); 43150b57cec5SDimitry Andric break; 43160b57cec5SDimitry Andric case ISD::UREM: 43170b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 43180b57cec5SDimitry Andric RTLIB::UREM_I8, 43190b57cec5SDimitry Andric RTLIB::UREM_I16, RTLIB::UREM_I32, 43200b57cec5SDimitry Andric RTLIB::UREM_I64, RTLIB::UREM_I128)); 43210b57cec5SDimitry Andric break; 43220b57cec5SDimitry Andric case ISD::SDIV: 43230b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 43240b57cec5SDimitry Andric RTLIB::SDIV_I8, 43250b57cec5SDimitry Andric RTLIB::SDIV_I16, RTLIB::SDIV_I32, 43260b57cec5SDimitry Andric RTLIB::SDIV_I64, RTLIB::SDIV_I128)); 43270b57cec5SDimitry Andric break; 43280b57cec5SDimitry Andric case ISD::UDIV: 43290b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 43300b57cec5SDimitry Andric RTLIB::UDIV_I8, 43310b57cec5SDimitry Andric RTLIB::UDIV_I16, RTLIB::UDIV_I32, 43320b57cec5SDimitry Andric RTLIB::UDIV_I64, RTLIB::UDIV_I128)); 43330b57cec5SDimitry Andric break; 43340b57cec5SDimitry Andric case ISD::SDIVREM: 43350b57cec5SDimitry Andric case ISD::UDIVREM: 43360b57cec5SDimitry Andric // Expand into divrem libcall 43370b57cec5SDimitry Andric ExpandDivRemLibCall(Node, Results); 43380b57cec5SDimitry Andric break; 43390b57cec5SDimitry Andric case ISD::MUL: 43400b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 43410b57cec5SDimitry Andric RTLIB::MUL_I8, 43420b57cec5SDimitry Andric RTLIB::MUL_I16, RTLIB::MUL_I32, 43430b57cec5SDimitry Andric RTLIB::MUL_I64, RTLIB::MUL_I128)); 43440b57cec5SDimitry Andric break; 43450b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 43460b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 43470b57cec5SDimitry Andric default: 43480b57cec5SDimitry Andric llvm_unreachable("LibCall explicitly requested, but not available"); 43490b57cec5SDimitry Andric case MVT::i32: 43500b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false)); 43510b57cec5SDimitry Andric break; 43520b57cec5SDimitry Andric case MVT::i64: 43530b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false)); 43540b57cec5SDimitry Andric break; 43550b57cec5SDimitry Andric case MVT::i128: 43560b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false)); 43570b57cec5SDimitry Andric break; 43580b57cec5SDimitry Andric } 43590b57cec5SDimitry Andric break; 43600b57cec5SDimitry Andric } 43610b57cec5SDimitry Andric 43620b57cec5SDimitry Andric // Replace the original node with the legalized result. 43630b57cec5SDimitry Andric if (!Results.empty()) { 43640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n"); 43650b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 43660b57cec5SDimitry Andric } else 43670b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n"); 43680b57cec5SDimitry Andric } 43690b57cec5SDimitry Andric 43700b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when 43710b57cec5SDimitry Andric // promoting equally sized vectors. 43720b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI, 43730b57cec5SDimitry Andric MVT EltVT, MVT NewEltVT) { 43740b57cec5SDimitry Andric unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits(); 43750b57cec5SDimitry Andric MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt); 43760b57cec5SDimitry Andric assert(TLI.isTypeLegal(MidVT) && "unexpected"); 43770b57cec5SDimitry Andric return MidVT; 43780b57cec5SDimitry Andric } 43790b57cec5SDimitry Andric 43800b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) { 43810b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to promote node\n"); 43820b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 43830b57cec5SDimitry Andric MVT OVT = Node->getSimpleValueType(0); 43840b57cec5SDimitry Andric if (Node->getOpcode() == ISD::UINT_TO_FP || 43850b57cec5SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP || 43860b57cec5SDimitry Andric Node->getOpcode() == ISD::SETCC || 43870b57cec5SDimitry Andric Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT || 43880b57cec5SDimitry Andric Node->getOpcode() == ISD::INSERT_VECTOR_ELT) { 43890b57cec5SDimitry Andric OVT = Node->getOperand(0).getSimpleValueType(); 43900b57cec5SDimitry Andric } 4391480093f4SDimitry Andric if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP || 4392e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 4393e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCC || 4394e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS) 4395480093f4SDimitry Andric OVT = Node->getOperand(1).getSimpleValueType(); 4396fe6060f1SDimitry Andric if (Node->getOpcode() == ISD::BR_CC || 4397fe6060f1SDimitry Andric Node->getOpcode() == ISD::SELECT_CC) 43980b57cec5SDimitry Andric OVT = Node->getOperand(2).getSimpleValueType(); 43990b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 44000b57cec5SDimitry Andric SDLoc dl(Node); 4401fe6060f1SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 44020b57cec5SDimitry Andric switch (Node->getOpcode()) { 44030b57cec5SDimitry Andric case ISD::CTTZ: 44040b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 44050b57cec5SDimitry Andric case ISD::CTLZ: 44060b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 44070b57cec5SDimitry Andric case ISD::CTPOP: 44085ffd83dbSDimitry Andric // Zero extend the argument unless its cttz, then use any_extend. 44095ffd83dbSDimitry Andric if (Node->getOpcode() == ISD::CTTZ || 44105ffd83dbSDimitry Andric Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF) 44115ffd83dbSDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 44125ffd83dbSDimitry Andric else 44130b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 44145ffd83dbSDimitry Andric 44150b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTTZ) { 44160b57cec5SDimitry Andric // The count is the same in the promoted type except if the original 44170b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 44180b57cec5SDimitry Andric // the top of the original type. 44190b57cec5SDimitry Andric auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(), 44200b57cec5SDimitry Andric OVT.getSizeInBits()); 44210b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1, 44220b57cec5SDimitry Andric DAG.getConstant(TopBit, dl, NVT)); 44230b57cec5SDimitry Andric } 44240b57cec5SDimitry Andric // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is 44250b57cec5SDimitry Andric // already the correct result. 44260b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 44270b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTLZ || 44280b57cec5SDimitry Andric Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) { 44290b57cec5SDimitry Andric // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 44300b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 44310b57cec5SDimitry Andric DAG.getConstant(NVT.getSizeInBits() - 44320b57cec5SDimitry Andric OVT.getSizeInBits(), dl, NVT)); 44330b57cec5SDimitry Andric } 44340b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 44350b57cec5SDimitry Andric break; 44360b57cec5SDimitry Andric case ISD::BITREVERSE: 44370b57cec5SDimitry Andric case ISD::BSWAP: { 44380b57cec5SDimitry Andric unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 44390b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 44400b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 44410b57cec5SDimitry Andric Tmp1 = DAG.getNode( 44420b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 44430b57cec5SDimitry Andric DAG.getConstant(DiffBits, dl, 44440b57cec5SDimitry Andric TLI.getShiftAmountTy(NVT, DAG.getDataLayout()))); 44450b57cec5SDimitry Andric 44460b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 44470b57cec5SDimitry Andric break; 44480b57cec5SDimitry Andric } 44490b57cec5SDimitry Andric case ISD::FP_TO_UINT: 4450480093f4SDimitry Andric case ISD::STRICT_FP_TO_UINT: 44510b57cec5SDimitry Andric case ISD::FP_TO_SINT: 4452480093f4SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4453480093f4SDimitry Andric PromoteLegalFP_TO_INT(Node, dl, Results); 44540b57cec5SDimitry Andric break; 4455e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 4456e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 4457e8d8bef9SDimitry Andric Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl)); 4458e8d8bef9SDimitry Andric break; 44590b57cec5SDimitry Andric case ISD::UINT_TO_FP: 4460480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 44610b57cec5SDimitry Andric case ISD::SINT_TO_FP: 4462480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4463480093f4SDimitry Andric PromoteLegalINT_TO_FP(Node, dl, Results); 44640b57cec5SDimitry Andric break; 44650b57cec5SDimitry Andric case ISD::VAARG: { 44660b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); // Get the chain. 44670b57cec5SDimitry Andric SDValue Ptr = Node->getOperand(1); // Get the pointer. 44680b57cec5SDimitry Andric 44690b57cec5SDimitry Andric unsigned TruncOp; 44700b57cec5SDimitry Andric if (OVT.isVector()) { 44710b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 44720b57cec5SDimitry Andric } else { 44730b57cec5SDimitry Andric assert(OVT.isInteger() 44740b57cec5SDimitry Andric && "VAARG promotion is supported only for vectors or integer types"); 44750b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 44760b57cec5SDimitry Andric } 44770b57cec5SDimitry Andric 44780b57cec5SDimitry Andric // Perform the larger operation, then convert back 44790b57cec5SDimitry Andric Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2), 44800b57cec5SDimitry Andric Node->getConstantOperandVal(3)); 44810b57cec5SDimitry Andric Chain = Tmp1.getValue(1); 44820b57cec5SDimitry Andric 44830b57cec5SDimitry Andric Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1); 44840b57cec5SDimitry Andric 44850b57cec5SDimitry Andric // Modified the chain result - switch anything that used the old chain to 44860b57cec5SDimitry Andric // use the new one. 44870b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2); 44880b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 44890b57cec5SDimitry Andric if (UpdatedNodes) { 44900b57cec5SDimitry Andric UpdatedNodes->insert(Tmp2.getNode()); 44910b57cec5SDimitry Andric UpdatedNodes->insert(Chain.getNode()); 44920b57cec5SDimitry Andric } 44930b57cec5SDimitry Andric ReplacedNode(Node); 44940b57cec5SDimitry Andric break; 44950b57cec5SDimitry Andric } 44960b57cec5SDimitry Andric case ISD::MUL: 44970b57cec5SDimitry Andric case ISD::SDIV: 44980b57cec5SDimitry Andric case ISD::SREM: 44990b57cec5SDimitry Andric case ISD::UDIV: 45000b57cec5SDimitry Andric case ISD::UREM: 45010b57cec5SDimitry Andric case ISD::AND: 45020b57cec5SDimitry Andric case ISD::OR: 45030b57cec5SDimitry Andric case ISD::XOR: { 45040b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 45050b57cec5SDimitry Andric if (OVT.isVector()) { 45060b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 45070b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 45080b57cec5SDimitry Andric } else { 45090b57cec5SDimitry Andric assert(OVT.isInteger() && "Cannot promote logic operation"); 45100b57cec5SDimitry Andric 45110b57cec5SDimitry Andric switch (Node->getOpcode()) { 45120b57cec5SDimitry Andric default: 45130b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 45140b57cec5SDimitry Andric break; 45150b57cec5SDimitry Andric case ISD::SDIV: 45160b57cec5SDimitry Andric case ISD::SREM: 45170b57cec5SDimitry Andric ExtOp = ISD::SIGN_EXTEND; 45180b57cec5SDimitry Andric break; 45190b57cec5SDimitry Andric case ISD::UDIV: 45200b57cec5SDimitry Andric case ISD::UREM: 45210b57cec5SDimitry Andric ExtOp = ISD::ZERO_EXTEND; 45220b57cec5SDimitry Andric break; 45230b57cec5SDimitry Andric } 45240b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 45250b57cec5SDimitry Andric } 45260b57cec5SDimitry Andric // Promote each of the values to the new type. 45270b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 45280b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45290b57cec5SDimitry Andric // Perform the larger operation, then convert back 45300b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 45310b57cec5SDimitry Andric Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 45320b57cec5SDimitry Andric break; 45330b57cec5SDimitry Andric } 45340b57cec5SDimitry Andric case ISD::UMUL_LOHI: 45350b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 45360b57cec5SDimitry Andric // Promote to a multiply in a wider integer type. 45370b57cec5SDimitry Andric unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND 45380b57cec5SDimitry Andric : ISD::SIGN_EXTEND; 45390b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 45400b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45410b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2); 45420b57cec5SDimitry Andric 45430b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 45440b57cec5SDimitry Andric unsigned OriginalSize = OVT.getScalarSizeInBits(); 45450b57cec5SDimitry Andric Tmp2 = DAG.getNode( 45460b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 45470b57cec5SDimitry Andric DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT))); 45480b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 45490b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2)); 45500b57cec5SDimitry Andric break; 45510b57cec5SDimitry Andric } 45520b57cec5SDimitry Andric case ISD::SELECT: { 45530b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 45540b57cec5SDimitry Andric if (Node->getValueType(0).isVector() || 45550b57cec5SDimitry Andric Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) { 45560b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 45570b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 45580b57cec5SDimitry Andric } else if (Node->getValueType(0).isInteger()) { 45590b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 45600b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 45610b57cec5SDimitry Andric } else { 45620b57cec5SDimitry Andric ExtOp = ISD::FP_EXTEND; 45630b57cec5SDimitry Andric TruncOp = ISD::FP_ROUND; 45640b57cec5SDimitry Andric } 45650b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 45660b57cec5SDimitry Andric // Promote each of the values to the new type. 45670b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45680b57cec5SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 45690b57cec5SDimitry Andric // Perform the larger operation, then round down. 45700b57cec5SDimitry Andric Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3); 45710b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 45720b57cec5SDimitry Andric if (TruncOp != ISD::FP_ROUND) 45730b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 45740b57cec5SDimitry Andric else 45750b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 45760b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl)); 45770b57cec5SDimitry Andric Results.push_back(Tmp1); 45780b57cec5SDimitry Andric break; 45790b57cec5SDimitry Andric } 45800b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 45810b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 45820b57cec5SDimitry Andric 45830b57cec5SDimitry Andric // Cast the two input vectors. 45840b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); 45850b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); 45860b57cec5SDimitry Andric 45870b57cec5SDimitry Andric // Convert the shuffle mask to the right # elements. 45880b57cec5SDimitry Andric Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 45890b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); 45900b57cec5SDimitry Andric Results.push_back(Tmp1); 45910b57cec5SDimitry Andric break; 45920b57cec5SDimitry Andric } 4593fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 4594fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 4595fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1)); 4596fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2, 4597fe6060f1SDimitry Andric Node->getOperand(2)); 4598fe6060f1SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3)); 4599fe6060f1SDimitry Andric break; 4600fe6060f1SDimitry Andric } 4601fe6060f1SDimitry Andric case ISD::SELECT_CC: { 4602fe6060f1SDimitry Andric SDValue Cond = Node->getOperand(4); 4603fe6060f1SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get(); 4604fe6060f1SDimitry Andric // Type of the comparison operands. 4605fe6060f1SDimitry Andric MVT CVT = Node->getSimpleValueType(0); 4606fe6060f1SDimitry Andric assert(CVT == OVT && "not handled"); 4607fe6060f1SDimitry Andric 4608fe6060f1SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 4609fe6060f1SDimitry Andric if (NVT.isInteger()) { 4610fe6060f1SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4611fe6060f1SDimitry Andric } 4612fe6060f1SDimitry Andric 4613fe6060f1SDimitry Andric // Promote the comparison operands, if needed. 4614fe6060f1SDimitry Andric if (TLI.isCondCodeLegal(CCCode, CVT)) { 4615fe6060f1SDimitry Andric Tmp1 = Node->getOperand(0); 4616fe6060f1SDimitry Andric Tmp2 = Node->getOperand(1); 4617fe6060f1SDimitry Andric } else { 4618fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4619fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4620fe6060f1SDimitry Andric } 4621fe6060f1SDimitry Andric // Cast the true/false operands. 4622fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4623fe6060f1SDimitry Andric Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 4624fe6060f1SDimitry Andric 4625fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond}, 4626fe6060f1SDimitry Andric Node->getFlags()); 4627fe6060f1SDimitry Andric 4628fe6060f1SDimitry Andric // Cast the result back to the original type. 4629fe6060f1SDimitry Andric if (ExtOp != ISD::FP_EXTEND) 4630fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1); 4631fe6060f1SDimitry Andric else 4632fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1, 4633fe6060f1SDimitry Andric DAG.getIntPtrConstant(0, dl)); 4634fe6060f1SDimitry Andric 4635fe6060f1SDimitry Andric Results.push_back(Tmp1); 4636fe6060f1SDimitry Andric break; 4637fe6060f1SDimitry Andric } 4638e8d8bef9SDimitry Andric case ISD::SETCC: 4639e8d8bef9SDimitry Andric case ISD::STRICT_FSETCC: 4640e8d8bef9SDimitry Andric case ISD::STRICT_FSETCCS: { 46410b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 46420b57cec5SDimitry Andric if (NVT.isInteger()) { 4643e8d8bef9SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get(); 46440b57cec5SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 46450b57cec5SDimitry Andric } 4646e8d8bef9SDimitry Andric if (Node->isStrictFPOpcode()) { 4647e8d8bef9SDimitry Andric SDValue InChain = Node->getOperand(0); 4648e8d8bef9SDimitry Andric std::tie(Tmp1, std::ignore) = 4649e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT); 4650e8d8bef9SDimitry Andric std::tie(Tmp2, std::ignore) = 4651e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT); 4652e8d8bef9SDimitry Andric SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)}; 4653e8d8bef9SDimitry Andric SDValue OutChain = DAG.getTokenFactor(dl, TmpChains); 4654e8d8bef9SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 4655e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs, 4656e8d8bef9SDimitry Andric {OutChain, Tmp1, Tmp2, Node->getOperand(3)}, 4657e8d8bef9SDimitry Andric Node->getFlags())); 4658e8d8bef9SDimitry Andric Results.push_back(Results.back().getValue(1)); 4659e8d8bef9SDimitry Andric break; 4660e8d8bef9SDimitry Andric } 46610b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 46620b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 46630b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1, 46640b57cec5SDimitry Andric Tmp2, Node->getOperand(2), Node->getFlags())); 46650b57cec5SDimitry Andric break; 46660b57cec5SDimitry Andric } 46670b57cec5SDimitry Andric case ISD::BR_CC: { 46680b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 46690b57cec5SDimitry Andric if (NVT.isInteger()) { 46700b57cec5SDimitry Andric ISD::CondCode CCCode = 46710b57cec5SDimitry Andric cast<CondCodeSDNode>(Node->getOperand(1))->get(); 46720b57cec5SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 46730b57cec5SDimitry Andric } 46740b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 46750b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 46760b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), 46770b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), 46780b57cec5SDimitry Andric Tmp1, Tmp2, Node->getOperand(4))); 46790b57cec5SDimitry Andric break; 46800b57cec5SDimitry Andric } 46810b57cec5SDimitry Andric case ISD::FADD: 46820b57cec5SDimitry Andric case ISD::FSUB: 46830b57cec5SDimitry Andric case ISD::FMUL: 46840b57cec5SDimitry Andric case ISD::FDIV: 46850b57cec5SDimitry Andric case ISD::FREM: 46860b57cec5SDimitry Andric case ISD::FMINNUM: 46870b57cec5SDimitry Andric case ISD::FMAXNUM: 46880b57cec5SDimitry Andric case ISD::FPOW: 46890b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 46900b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 46910b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, 46920b57cec5SDimitry Andric Node->getFlags()); 46930b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 46940b57cec5SDimitry Andric Tmp3, DAG.getIntPtrConstant(0, dl))); 46950b57cec5SDimitry Andric break; 4696480093f4SDimitry Andric case ISD::STRICT_FREM: 4697480093f4SDimitry Andric case ISD::STRICT_FPOW: 4698480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4699480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 4700480093f4SDimitry Andric Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4701480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(2)}); 4702480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1), 4703480093f4SDimitry Andric Tmp2.getValue(1)); 4704480093f4SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 4705480093f4SDimitry Andric {Tmp3, Tmp1, Tmp2}); 4706480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 4707480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)}); 4708480093f4SDimitry Andric Results.push_back(Tmp1); 4709480093f4SDimitry Andric Results.push_back(Tmp1.getValue(1)); 4710480093f4SDimitry Andric break; 47110b57cec5SDimitry Andric case ISD::FMA: 47120b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 47130b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 47140b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2)); 47150b57cec5SDimitry Andric Results.push_back( 47160b57cec5SDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, 47170b57cec5SDimitry Andric DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3), 47180b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl))); 47190b57cec5SDimitry Andric break; 47200b57cec5SDimitry Andric case ISD::FCOPYSIGN: 47210b57cec5SDimitry Andric case ISD::FPOWI: { 47220b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 47230b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 47240b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 47250b57cec5SDimitry Andric 47260b57cec5SDimitry Andric // fcopysign doesn't change anything but the sign bit, so 47270b57cec5SDimitry Andric // (fp_round (fcopysign (fpext a), b)) 47280b57cec5SDimitry Andric // is as precise as 47290b57cec5SDimitry Andric // (fp_round (fpext a)) 47300b57cec5SDimitry Andric // which is a no-op. Mark it as a TRUNCating FP_ROUND. 47310b57cec5SDimitry Andric const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN); 47320b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 47330b57cec5SDimitry Andric Tmp3, DAG.getIntPtrConstant(isTrunc, dl))); 47340b57cec5SDimitry Andric break; 47350b57cec5SDimitry Andric } 47360b57cec5SDimitry Andric case ISD::FFLOOR: 47370b57cec5SDimitry Andric case ISD::FCEIL: 47380b57cec5SDimitry Andric case ISD::FRINT: 47390b57cec5SDimitry Andric case ISD::FNEARBYINT: 47400b57cec5SDimitry Andric case ISD::FROUND: 47415ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 47420b57cec5SDimitry Andric case ISD::FTRUNC: 47430b57cec5SDimitry Andric case ISD::FNEG: 47440b57cec5SDimitry Andric case ISD::FSQRT: 47450b57cec5SDimitry Andric case ISD::FSIN: 47460b57cec5SDimitry Andric case ISD::FCOS: 47470b57cec5SDimitry Andric case ISD::FLOG: 47480b57cec5SDimitry Andric case ISD::FLOG2: 47490b57cec5SDimitry Andric case ISD::FLOG10: 47500b57cec5SDimitry Andric case ISD::FABS: 47510b57cec5SDimitry Andric case ISD::FEXP: 47520b57cec5SDimitry Andric case ISD::FEXP2: 47530b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 47540b57cec5SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 47550b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 47560b57cec5SDimitry Andric Tmp2, DAG.getIntPtrConstant(0, dl))); 47570b57cec5SDimitry Andric break; 4758480093f4SDimitry Andric case ISD::STRICT_FFLOOR: 4759480093f4SDimitry Andric case ISD::STRICT_FCEIL: 4760349cc55cSDimitry Andric case ISD::STRICT_FROUND: 4761480093f4SDimitry Andric case ISD::STRICT_FSIN: 4762480093f4SDimitry Andric case ISD::STRICT_FCOS: 4763480093f4SDimitry Andric case ISD::STRICT_FLOG: 4764480093f4SDimitry Andric case ISD::STRICT_FLOG10: 4765480093f4SDimitry Andric case ISD::STRICT_FEXP: 4766480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4767480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 4768480093f4SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 4769480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1}); 4770480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 4771480093f4SDimitry Andric {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)}); 4772480093f4SDimitry Andric Results.push_back(Tmp3); 4773480093f4SDimitry Andric Results.push_back(Tmp3.getValue(1)); 4774480093f4SDimitry Andric break; 47750b57cec5SDimitry Andric case ISD::BUILD_VECTOR: { 47760b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 47770b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 47780b57cec5SDimitry Andric 47790b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 47800b57cec5SDimitry Andric // 47810b57cec5SDimitry Andric // e.g. v2i64 = build_vector i64:x, i64:y => v4i32 47820b57cec5SDimitry Andric // => 47830b57cec5SDimitry Andric // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y)) 47840b57cec5SDimitry Andric 47850b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 47860b57cec5SDimitry Andric "Invalid promote type for build_vector"); 47870b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 47880b57cec5SDimitry Andric 47890b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 47900b57cec5SDimitry Andric 47910b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 47920b57cec5SDimitry Andric for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) { 47930b57cec5SDimitry Andric SDValue Op = Node->getOperand(I); 47940b57cec5SDimitry Andric NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op)); 47950b57cec5SDimitry Andric } 47960b57cec5SDimitry Andric 47970b57cec5SDimitry Andric SDLoc SL(Node); 47980b57cec5SDimitry Andric SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps); 47990b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 48000b57cec5SDimitry Andric Results.push_back(CvtVec); 48010b57cec5SDimitry Andric break; 48020b57cec5SDimitry Andric } 48030b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: { 48040b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 48050b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 48060b57cec5SDimitry Andric 48070b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size. 48080b57cec5SDimitry Andric // 48090b57cec5SDimitry Andric // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32 48100b57cec5SDimitry Andric // => 48110b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 48120b57cec5SDimitry Andric // 48130b57cec5SDimitry Andric // i64 = bitcast 48140b57cec5SDimitry Andric // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 48150b57cec5SDimitry Andric // (i32 (extract_vector_elt castx, (2 * y + 1))) 48160b57cec5SDimitry Andric // 48170b57cec5SDimitry Andric 48180b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 48190b57cec5SDimitry Andric "Invalid promote type for extract_vector_elt"); 48200b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 48210b57cec5SDimitry Andric 48220b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 48230b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 48240b57cec5SDimitry Andric 48250b57cec5SDimitry Andric SDValue Idx = Node->getOperand(1); 48260b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 48270b57cec5SDimitry Andric SDLoc SL(Node); 48280b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT); 48290b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 48300b57cec5SDimitry Andric 48310b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 48320b57cec5SDimitry Andric 48330b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 48340b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 48350b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 48360b57cec5SDimitry Andric SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 48370b57cec5SDimitry Andric 48380b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 48390b57cec5SDimitry Andric CastVec, TmpIdx); 48400b57cec5SDimitry Andric NewOps.push_back(Elt); 48410b57cec5SDimitry Andric } 48420b57cec5SDimitry Andric 48430b57cec5SDimitry Andric SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps); 48440b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec)); 48450b57cec5SDimitry Andric break; 48460b57cec5SDimitry Andric } 48470b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: { 48480b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 48490b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 48500b57cec5SDimitry Andric 48510b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 48520b57cec5SDimitry Andric // 48530b57cec5SDimitry Andric // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32 48540b57cec5SDimitry Andric // => 48550b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 48560b57cec5SDimitry Andric // v2i32:casty = bitcast y:i64 48570b57cec5SDimitry Andric // 48580b57cec5SDimitry Andric // v2i64 = bitcast 48590b57cec5SDimitry Andric // (v4i32 insert_vector_elt 48600b57cec5SDimitry Andric // (v4i32 insert_vector_elt v4i32:castx, 48610b57cec5SDimitry Andric // (extract_vector_elt casty, 0), 2 * z), 48620b57cec5SDimitry Andric // (extract_vector_elt casty, 1), (2 * z + 1)) 48630b57cec5SDimitry Andric 48640b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 48650b57cec5SDimitry Andric "Invalid promote type for insert_vector_elt"); 48660b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 48670b57cec5SDimitry Andric 48680b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 48690b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 48700b57cec5SDimitry Andric 48710b57cec5SDimitry Andric SDValue Val = Node->getOperand(1); 48720b57cec5SDimitry Andric SDValue Idx = Node->getOperand(2); 48730b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 48740b57cec5SDimitry Andric SDLoc SL(Node); 48750b57cec5SDimitry Andric 48760b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT); 48770b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 48780b57cec5SDimitry Andric 48790b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 48800b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 48810b57cec5SDimitry Andric 48820b57cec5SDimitry Andric SDValue NewVec = CastVec; 48830b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 48840b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 48850b57cec5SDimitry Andric SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 48860b57cec5SDimitry Andric 48870b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 48880b57cec5SDimitry Andric CastVal, IdxOffset); 48890b57cec5SDimitry Andric 48900b57cec5SDimitry Andric NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT, 48910b57cec5SDimitry Andric NewVec, Elt, InEltIdx); 48920b57cec5SDimitry Andric } 48930b57cec5SDimitry Andric 48940b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec)); 48950b57cec5SDimitry Andric break; 48960b57cec5SDimitry Andric } 48970b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: { 48980b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 48990b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 49000b57cec5SDimitry Andric 49010b57cec5SDimitry Andric // Handle bitcasts to different vector type with the same total bit size. 49020b57cec5SDimitry Andric // 49030b57cec5SDimitry Andric // e.g. v2i64 = scalar_to_vector x:i64 49040b57cec5SDimitry Andric // => 49050b57cec5SDimitry Andric // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef) 49060b57cec5SDimitry Andric // 49070b57cec5SDimitry Andric 49080b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 49090b57cec5SDimitry Andric SDValue Val = Node->getOperand(0); 49100b57cec5SDimitry Andric SDLoc SL(Node); 49110b57cec5SDimitry Andric 49120b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 49130b57cec5SDimitry Andric SDValue Undef = DAG.getUNDEF(MidVT); 49140b57cec5SDimitry Andric 49150b57cec5SDimitry Andric SmallVector<SDValue, 8> NewElts; 49160b57cec5SDimitry Andric NewElts.push_back(CastVal); 49170b57cec5SDimitry Andric for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I) 49180b57cec5SDimitry Andric NewElts.push_back(Undef); 49190b57cec5SDimitry Andric 49200b57cec5SDimitry Andric SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts); 49210b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 49220b57cec5SDimitry Andric Results.push_back(CvtVec); 49230b57cec5SDimitry Andric break; 49240b57cec5SDimitry Andric } 49250b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: { 49260b57cec5SDimitry Andric AtomicSDNode *AM = cast<AtomicSDNode>(Node); 49270b57cec5SDimitry Andric SDLoc SL(Node); 49280b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal()); 49290b57cec5SDimitry Andric assert(NVT.getSizeInBits() == OVT.getSizeInBits() && 49300b57cec5SDimitry Andric "unexpected promotion type"); 49310b57cec5SDimitry Andric assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() && 49320b57cec5SDimitry Andric "unexpected atomic_swap with illegal type"); 49330b57cec5SDimitry Andric 49340b57cec5SDimitry Andric SDValue NewAtomic 49350b57cec5SDimitry Andric = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT, 49360b57cec5SDimitry Andric DAG.getVTList(NVT, MVT::Other), 49370b57cec5SDimitry Andric { AM->getChain(), AM->getBasePtr(), CastVal }, 49380b57cec5SDimitry Andric AM->getMemOperand()); 49390b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic)); 49400b57cec5SDimitry Andric Results.push_back(NewAtomic.getValue(1)); 49410b57cec5SDimitry Andric break; 49420b57cec5SDimitry Andric } 49430b57cec5SDimitry Andric } 49440b57cec5SDimitry Andric 49450b57cec5SDimitry Andric // Replace the original node with the legalized result. 49460b57cec5SDimitry Andric if (!Results.empty()) { 49470b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully promoted node\n"); 49480b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 49490b57cec5SDimitry Andric } else 49500b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not promote node\n"); 49510b57cec5SDimitry Andric } 49520b57cec5SDimitry Andric 49530b57cec5SDimitry Andric /// This is the entry point for the file. 49540b57cec5SDimitry Andric void SelectionDAG::Legalize() { 49550b57cec5SDimitry Andric AssignTopologicalOrder(); 49560b57cec5SDimitry Andric 49570b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 49580b57cec5SDimitry Andric // Use a delete listener to remove nodes which were deleted during 49590b57cec5SDimitry Andric // legalization from LegalizeNodes. This is needed to handle the situation 49600b57cec5SDimitry Andric // where a new node is allocated by the object pool to the same address of a 49610b57cec5SDimitry Andric // previously deleted node. 49620b57cec5SDimitry Andric DAGNodeDeletedListener DeleteListener( 49630b57cec5SDimitry Andric *this, 49640b57cec5SDimitry Andric [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); }); 49650b57cec5SDimitry Andric 49660b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes); 49670b57cec5SDimitry Andric 49680b57cec5SDimitry Andric // Visit all the nodes. We start in topological order, so that we see 49690b57cec5SDimitry Andric // nodes with their original operands intact. Legalization can produce 49700b57cec5SDimitry Andric // new nodes which may themselves need to be legalized. Iterate until all 49710b57cec5SDimitry Andric // nodes have been legalized. 49720b57cec5SDimitry Andric while (true) { 49730b57cec5SDimitry Andric bool AnyLegalized = false; 49740b57cec5SDimitry Andric for (auto NI = allnodes_end(); NI != allnodes_begin();) { 49750b57cec5SDimitry Andric --NI; 49760b57cec5SDimitry Andric 49770b57cec5SDimitry Andric SDNode *N = &*NI; 49780b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 49790b57cec5SDimitry Andric ++NI; 49800b57cec5SDimitry Andric DeleteNode(N); 49810b57cec5SDimitry Andric continue; 49820b57cec5SDimitry Andric } 49830b57cec5SDimitry Andric 49840b57cec5SDimitry Andric if (LegalizedNodes.insert(N).second) { 49850b57cec5SDimitry Andric AnyLegalized = true; 49860b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 49870b57cec5SDimitry Andric 49880b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 49890b57cec5SDimitry Andric ++NI; 49900b57cec5SDimitry Andric DeleteNode(N); 49910b57cec5SDimitry Andric } 49920b57cec5SDimitry Andric } 49930b57cec5SDimitry Andric } 49940b57cec5SDimitry Andric if (!AnyLegalized) 49950b57cec5SDimitry Andric break; 49960b57cec5SDimitry Andric 49970b57cec5SDimitry Andric } 49980b57cec5SDimitry Andric 49990b57cec5SDimitry Andric // Remove dead nodes now. 50000b57cec5SDimitry Andric RemoveDeadNodes(); 50010b57cec5SDimitry Andric } 50020b57cec5SDimitry Andric 50030b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N, 50040b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> &UpdatedNodes) { 50050b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 50060b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes); 50070b57cec5SDimitry Andric 50080b57cec5SDimitry Andric // Directly insert the node in question, and legalize it. This will recurse 50090b57cec5SDimitry Andric // as needed through operands. 50100b57cec5SDimitry Andric LegalizedNodes.insert(N); 50110b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 50120b57cec5SDimitry Andric 50130b57cec5SDimitry Andric return LegalizedNodes.count(N); 50140b57cec5SDimitry Andric } 5015