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 138*fe6060f1SDimitry Andric void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC, 139*fe6060f1SDimitry 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; 11670b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 11680b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 11690b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 11700b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 11710b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 11720b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 11730b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 11740b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 11750b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 11760b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 11770b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 11780b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 11790b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 11800b57cec5SDimitry Andric Action = TLI.getOperationAction( 11810b57cec5SDimitry Andric Node->getOpcode(), Node->getOperand(0).getValueType()); 11820b57cec5SDimitry Andric break; 1183e8d8bef9SDimitry Andric case ISD::VECREDUCE_SEQ_FADD: 1184e8d8bef9SDimitry Andric Action = TLI.getOperationAction( 1185e8d8bef9SDimitry Andric Node->getOpcode(), Node->getOperand(1).getValueType()); 1186e8d8bef9SDimitry Andric break; 11870b57cec5SDimitry Andric default: 11880b57cec5SDimitry Andric if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 11890b57cec5SDimitry Andric Action = TargetLowering::Legal; 11900b57cec5SDimitry Andric } else { 11910b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11920b57cec5SDimitry Andric } 11930b57cec5SDimitry Andric break; 11940b57cec5SDimitry Andric } 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric if (SimpleFinishLegalizing) { 11970b57cec5SDimitry Andric SDNode *NewNode = Node; 11980b57cec5SDimitry Andric switch (Node->getOpcode()) { 11990b57cec5SDimitry Andric default: break; 12000b57cec5SDimitry Andric case ISD::SHL: 12010b57cec5SDimitry Andric case ISD::SRL: 12020b57cec5SDimitry Andric case ISD::SRA: 12030b57cec5SDimitry Andric case ISD::ROTL: 12040b57cec5SDimitry Andric case ISD::ROTR: { 12050b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12060b57cec5SDimitry Andric // to the appropriate width. 12070b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12080b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12090b57cec5SDimitry Andric if (!Op1.getValueType().isVector()) { 12100b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1); 12110b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12120b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12130b57cec5SDimitry Andric // to update the parent node. 12140b57cec5SDimitry Andric // Do not try to legalize SAO here! It will be automatically legalized 12150b57cec5SDimitry Andric // in the next round. 12160b57cec5SDimitry Andric if (SAO != Op1) 12170b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric } 12200b57cec5SDimitry Andric break; 12210b57cec5SDimitry Andric case ISD::FSHL: 12220b57cec5SDimitry Andric case ISD::FSHR: 12230b57cec5SDimitry Andric case ISD::SRL_PARTS: 12240b57cec5SDimitry Andric case ISD::SRA_PARTS: 12250b57cec5SDimitry Andric case ISD::SHL_PARTS: { 12260b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12270b57cec5SDimitry Andric // to the appropriate width. 12280b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12290b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12300b57cec5SDimitry Andric SDValue Op2 = Node->getOperand(2); 12310b57cec5SDimitry Andric if (!Op2.getValueType().isVector()) { 12320b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2); 12330b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12340b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12350b57cec5SDimitry Andric // to update the parent node. 12360b57cec5SDimitry Andric if (SAO != Op2) 12370b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO); 12380b57cec5SDimitry Andric } 12390b57cec5SDimitry Andric break; 12400b57cec5SDimitry Andric } 12410b57cec5SDimitry Andric } 12420b57cec5SDimitry Andric 12430b57cec5SDimitry Andric if (NewNode != Node) { 12440b57cec5SDimitry Andric ReplaceNode(Node, NewNode); 12450b57cec5SDimitry Andric Node = NewNode; 12460b57cec5SDimitry Andric } 12470b57cec5SDimitry Andric switch (Action) { 12480b57cec5SDimitry Andric case TargetLowering::Legal: 12490b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 12500b57cec5SDimitry Andric return; 12510b57cec5SDimitry Andric case TargetLowering::Custom: 12520b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 12530b57cec5SDimitry Andric // FIXME: The handling for custom lowering with multiple results is 12540b57cec5SDimitry Andric // a complete mess. 12550b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 12560b57cec5SDimitry Andric if (!(Res.getNode() != Node || Res.getResNo() != 0)) 12570b57cec5SDimitry Andric return; 12580b57cec5SDimitry Andric 12590b57cec5SDimitry Andric if (Node->getNumValues() == 1) { 1260*fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1261*fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1262*fe6060f1SDimitry Andric assert((Res.getValueType() == Node->getValueType(0) || 1263*fe6060f1SDimitry Andric Node->getValueType(0) == MVT::Glue) && 1264*fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 12650b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 12660b57cec5SDimitry Andric // We can just directly replace this node with the lowered value. 12670b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 12680b57cec5SDimitry Andric return; 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric SmallVector<SDValue, 8> ResultVals; 1272*fe6060f1SDimitry Andric for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 1273*fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1274*fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1275*fe6060f1SDimitry Andric assert((Res->getValueType(i) == Node->getValueType(i) || 1276*fe6060f1SDimitry Andric Node->getValueType(i) == MVT::Glue) && 1277*fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 12780b57cec5SDimitry Andric ResultVals.push_back(Res.getValue(i)); 1279*fe6060f1SDimitry Andric } 12800b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 12810b57cec5SDimitry Andric ReplaceNode(Node, ResultVals.data()); 12820b57cec5SDimitry Andric return; 12830b57cec5SDimitry Andric } 12840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 12850b57cec5SDimitry Andric LLVM_FALLTHROUGH; 12860b57cec5SDimitry Andric case TargetLowering::Expand: 12870b57cec5SDimitry Andric if (ExpandNode(Node)) 12880b57cec5SDimitry Andric return; 12890b57cec5SDimitry Andric LLVM_FALLTHROUGH; 12900b57cec5SDimitry Andric case TargetLowering::LibCall: 12910b57cec5SDimitry Andric ConvertNodeToLibcall(Node); 12920b57cec5SDimitry Andric return; 12930b57cec5SDimitry Andric case TargetLowering::Promote: 12940b57cec5SDimitry Andric PromoteNode(Node); 12950b57cec5SDimitry Andric return; 12960b57cec5SDimitry Andric } 12970b57cec5SDimitry Andric } 12980b57cec5SDimitry Andric 12990b57cec5SDimitry Andric switch (Node->getOpcode()) { 13000b57cec5SDimitry Andric default: 13010b57cec5SDimitry Andric #ifndef NDEBUG 13020b57cec5SDimitry Andric dbgs() << "NODE: "; 13030b57cec5SDimitry Andric Node->dump( &DAG); 13040b57cec5SDimitry Andric dbgs() << "\n"; 13050b57cec5SDimitry Andric #endif 13060b57cec5SDimitry Andric llvm_unreachable("Do not know how to legalize this operator!"); 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric case ISD::CALLSEQ_START: 13090b57cec5SDimitry Andric case ISD::CALLSEQ_END: 13100b57cec5SDimitry Andric break; 13110b57cec5SDimitry Andric case ISD::LOAD: 13120b57cec5SDimitry Andric return LegalizeLoadOps(Node); 13130b57cec5SDimitry Andric case ISD::STORE: 13140b57cec5SDimitry Andric return LegalizeStoreOps(Node); 13150b57cec5SDimitry Andric } 13160b57cec5SDimitry Andric } 13170b57cec5SDimitry Andric 13180b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 13190b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 13200b57cec5SDimitry Andric SDValue Idx = Op.getOperand(1); 13210b57cec5SDimitry Andric SDLoc dl(Op); 13220b57cec5SDimitry Andric 13230b57cec5SDimitry Andric // Before we generate a new store to a temporary stack slot, see if there is 13240b57cec5SDimitry Andric // already one that we can use. There often is because when we scalarize 13250b57cec5SDimitry Andric // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole 13260b57cec5SDimitry Andric // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in 13270b57cec5SDimitry Andric // the vector. If all are expanded here, we don't want one store per vector 13280b57cec5SDimitry Andric // element. 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric // Caches for hasPredecessorHelper 13310b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 32> Visited; 13320b57cec5SDimitry Andric SmallVector<const SDNode *, 16> Worklist; 13330b57cec5SDimitry Andric Visited.insert(Op.getNode()); 13340b57cec5SDimitry Andric Worklist.push_back(Idx.getNode()); 13350b57cec5SDimitry Andric SDValue StackPtr, Ch; 13360b57cec5SDimitry Andric for (SDNode::use_iterator UI = Vec.getNode()->use_begin(), 13370b57cec5SDimitry Andric UE = Vec.getNode()->use_end(); UI != UE; ++UI) { 13380b57cec5SDimitry Andric SDNode *User = *UI; 13390b57cec5SDimitry Andric if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) { 13400b57cec5SDimitry Andric if (ST->isIndexed() || ST->isTruncatingStore() || 13410b57cec5SDimitry Andric ST->getValue() != Vec) 13420b57cec5SDimitry Andric continue; 13430b57cec5SDimitry Andric 13440b57cec5SDimitry Andric // Make sure that nothing else could have stored into the destination of 13450b57cec5SDimitry Andric // this store. 13460b57cec5SDimitry Andric if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode())) 13470b57cec5SDimitry Andric continue; 13480b57cec5SDimitry Andric 13490b57cec5SDimitry Andric // If the index is dependent on the store we will introduce a cycle when 13500b57cec5SDimitry Andric // creating the load (the load uses the index, and by replacing the chain 13510b57cec5SDimitry Andric // we will make the index dependent on the load). Also, the store might be 13520b57cec5SDimitry Andric // dependent on the extractelement and introduce a cycle when creating 13530b57cec5SDimitry Andric // the load. 13540b57cec5SDimitry Andric if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) || 13550b57cec5SDimitry Andric ST->hasPredecessor(Op.getNode())) 13560b57cec5SDimitry Andric continue; 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric StackPtr = ST->getBasePtr(); 13590b57cec5SDimitry Andric Ch = SDValue(ST, 0); 13600b57cec5SDimitry Andric break; 13610b57cec5SDimitry Andric } 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric 13640b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 13650b57cec5SDimitry Andric 13660b57cec5SDimitry Andric if (!Ch.getNode()) { 13670b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 13680b57cec5SDimitry Andric StackPtr = DAG.CreateStackTemporary(VecVT); 13690b57cec5SDimitry Andric Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, 13700b57cec5SDimitry Andric MachinePointerInfo()); 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric 13730b57cec5SDimitry Andric SDValue NewLoad; 13740b57cec5SDimitry Andric 1375*fe6060f1SDimitry Andric if (Op.getValueType().isVector()) { 1376*fe6060f1SDimitry Andric StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, 1377*fe6060f1SDimitry Andric Op.getValueType(), Idx); 13780b57cec5SDimitry Andric NewLoad = 13790b57cec5SDimitry Andric DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, MachinePointerInfo()); 1380*fe6060f1SDimitry Andric } else { 1381*fe6060f1SDimitry Andric StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx); 13820b57cec5SDimitry Andric NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, 13830b57cec5SDimitry Andric MachinePointerInfo(), 13840b57cec5SDimitry Andric VecVT.getVectorElementType()); 1385*fe6060f1SDimitry Andric } 13860b57cec5SDimitry Andric 13870b57cec5SDimitry Andric // Replace the chain going out of the store, by the one out of the load. 13880b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1)); 13890b57cec5SDimitry Andric 13900b57cec5SDimitry Andric // We introduced a cycle though, so update the loads operands, making sure 13910b57cec5SDimitry Andric // to use the original store's chain as an incoming chain. 13920b57cec5SDimitry Andric SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(), 13930b57cec5SDimitry Andric NewLoad->op_end()); 13940b57cec5SDimitry Andric NewLoadOperands[0] = Ch; 13950b57cec5SDimitry Andric NewLoad = 13960b57cec5SDimitry Andric SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0); 13970b57cec5SDimitry Andric return NewLoad; 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { 14010b57cec5SDimitry Andric assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); 14020b57cec5SDimitry Andric 14030b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 14040b57cec5SDimitry Andric SDValue Part = Op.getOperand(1); 14050b57cec5SDimitry Andric SDValue Idx = Op.getOperand(2); 14060b57cec5SDimitry Andric SDLoc dl(Op); 14070b57cec5SDimitry Andric 14080b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 14090b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 1410*fe6060f1SDimitry Andric EVT SubVecVT = Part.getValueType(); 14110b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(VecVT); 14120b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 14130b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 14140b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 14150b57cec5SDimitry Andric 14160b57cec5SDimitry Andric // First store the whole vector. 14170b57cec5SDimitry Andric SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo); 14180b57cec5SDimitry Andric 14190b57cec5SDimitry Andric // Then store the inserted part. 1420*fe6060f1SDimitry Andric SDValue SubStackPtr = 1421*fe6060f1SDimitry Andric TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx); 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric // Store the subvector. 14245ffd83dbSDimitry Andric Ch = DAG.getStore( 14255ffd83dbSDimitry Andric Ch, dl, Part, SubStackPtr, 14265ffd83dbSDimitry Andric MachinePointerInfo::getUnknownStack(DAG.getMachineFunction())); 14270b57cec5SDimitry Andric 14280b57cec5SDimitry Andric // Finally, load the updated vector. 14290b57cec5SDimitry Andric return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo); 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 14335ffd83dbSDimitry Andric assert((Node->getOpcode() == ISD::BUILD_VECTOR || 14345ffd83dbSDimitry Andric Node->getOpcode() == ISD::CONCAT_VECTORS) && 14355ffd83dbSDimitry Andric "Unexpected opcode!"); 14365ffd83dbSDimitry Andric 14370b57cec5SDimitry Andric // We can't handle this case efficiently. Allocate a sufficiently 14385ffd83dbSDimitry Andric // aligned object on the stack, store each operand into it, then load 14390b57cec5SDimitry Andric // the result as a vector. 14400b57cec5SDimitry Andric // Create the stack frame object. 14410b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 14425ffd83dbSDimitry Andric EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType() 14435ffd83dbSDimitry Andric : Node->getOperand(0).getValueType(); 14440b57cec5SDimitry Andric SDLoc dl(Node); 14450b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(VT); 14460b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 14470b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 14480b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 14490b57cec5SDimitry Andric 14500b57cec5SDimitry Andric // Emit a store of each element to the stack slot. 14510b57cec5SDimitry Andric SmallVector<SDValue, 8> Stores; 14525ffd83dbSDimitry Andric unsigned TypeByteSize = MemVT.getSizeInBits() / 8; 14530b57cec5SDimitry Andric assert(TypeByteSize > 0 && "Vector element type too small for stack store!"); 1454e8d8bef9SDimitry Andric 1455e8d8bef9SDimitry Andric // If the destination vector element type of a BUILD_VECTOR is narrower than 1456e8d8bef9SDimitry Andric // the source element type, only store the bits necessary. 1457e8d8bef9SDimitry Andric bool Truncate = isa<BuildVectorSDNode>(Node) && 1458e8d8bef9SDimitry Andric MemVT.bitsLT(Node->getOperand(0).getValueType()); 1459e8d8bef9SDimitry Andric 14600b57cec5SDimitry Andric // Store (in the right endianness) the elements to memory. 14610b57cec5SDimitry Andric for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 14620b57cec5SDimitry Andric // Ignore undef elements. 14630b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) continue; 14640b57cec5SDimitry Andric 14650b57cec5SDimitry Andric unsigned Offset = TypeByteSize*i; 14660b57cec5SDimitry Andric 1467e8d8bef9SDimitry Andric SDValue Idx = DAG.getMemBasePlusOffset(FIPtr, TypeSize::Fixed(Offset), dl); 14680b57cec5SDimitry Andric 1469e8d8bef9SDimitry Andric if (Truncate) 14700b57cec5SDimitry Andric Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 14710b57cec5SDimitry Andric Node->getOperand(i), Idx, 14725ffd83dbSDimitry Andric PtrInfo.getWithOffset(Offset), MemVT)); 14735ffd83dbSDimitry Andric else 14740b57cec5SDimitry Andric Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i), 14750b57cec5SDimitry Andric Idx, PtrInfo.getWithOffset(Offset))); 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric SDValue StoreChain; 14790b57cec5SDimitry Andric if (!Stores.empty()) // Not all undef elements? 14800b57cec5SDimitry Andric StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 14810b57cec5SDimitry Andric else 14820b57cec5SDimitry Andric StoreChain = DAG.getEntryNode(); 14830b57cec5SDimitry Andric 14840b57cec5SDimitry Andric // Result is a load from the stack slot. 14850b57cec5SDimitry Andric return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo); 14860b57cec5SDimitry Andric } 14870b57cec5SDimitry Andric 14880b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part 14890b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of 14900b57cec5SDimitry Andric /// holding all bits of the floating-point value. 14910b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State, 14920b57cec5SDimitry Andric const SDLoc &DL, 14930b57cec5SDimitry Andric SDValue Value) const { 14940b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 1495e8d8bef9SDimitry Andric unsigned NumBits = FloatVT.getScalarSizeInBits(); 14960b57cec5SDimitry Andric State.FloatVT = FloatVT; 14970b57cec5SDimitry Andric EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 14980b57cec5SDimitry Andric // Convert to an integer of the same size. 14990b57cec5SDimitry Andric if (TLI.isTypeLegal(IVT)) { 15000b57cec5SDimitry Andric State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value); 15010b57cec5SDimitry Andric State.SignMask = APInt::getSignMask(NumBits); 15020b57cec5SDimitry Andric State.SignBit = NumBits - 1; 15030b57cec5SDimitry Andric return; 15040b57cec5SDimitry Andric } 15050b57cec5SDimitry Andric 15060b57cec5SDimitry Andric auto &DataLayout = DAG.getDataLayout(); 15070b57cec5SDimitry Andric // Store the float to memory, then load the sign part out as an integer. 15080b57cec5SDimitry Andric MVT LoadTy = TLI.getRegisterType(*DAG.getContext(), MVT::i8); 15090b57cec5SDimitry Andric // First create a temporary that is aligned for both the load and store. 15100b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 15110b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 15120b57cec5SDimitry Andric // Then store the float to it. 15130b57cec5SDimitry Andric State.FloatPtr = StackPtr; 15140b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 15150b57cec5SDimitry Andric State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI); 15160b57cec5SDimitry Andric State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr, 15170b57cec5SDimitry Andric State.FloatPointerInfo); 15180b57cec5SDimitry Andric 15190b57cec5SDimitry Andric SDValue IntPtr; 15200b57cec5SDimitry Andric if (DataLayout.isBigEndian()) { 15210b57cec5SDimitry Andric assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 15220b57cec5SDimitry Andric // Load out a legal integer with the same sign bit as the float. 15230b57cec5SDimitry Andric IntPtr = StackPtr; 15240b57cec5SDimitry Andric State.IntPointerInfo = State.FloatPointerInfo; 15250b57cec5SDimitry Andric } else { 15260b57cec5SDimitry Andric // Advance the pointer so that the loaded byte will contain the sign bit. 1527e8d8bef9SDimitry Andric unsigned ByteOffset = (NumBits / 8) - 1; 1528e8d8bef9SDimitry Andric IntPtr = 1529e8d8bef9SDimitry Andric DAG.getMemBasePlusOffset(StackPtr, TypeSize::Fixed(ByteOffset), DL); 15300b57cec5SDimitry Andric State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI, 15310b57cec5SDimitry Andric ByteOffset); 15320b57cec5SDimitry Andric } 15330b57cec5SDimitry Andric 15340b57cec5SDimitry Andric State.IntPtr = IntPtr; 15350b57cec5SDimitry Andric State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr, 15360b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 1537e8d8bef9SDimitry Andric State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7); 15380b57cec5SDimitry Andric State.SignBit = 7; 15390b57cec5SDimitry Andric } 15400b57cec5SDimitry Andric 15410b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value 15420b57cec5SDimitry Andric /// and cast the result back to a floating-point type. 15430b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State, 15440b57cec5SDimitry Andric const SDLoc &DL, 15450b57cec5SDimitry Andric SDValue NewIntValue) const { 15460b57cec5SDimitry Andric if (!State.Chain) 15470b57cec5SDimitry Andric return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue); 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andric // Override the part containing the sign bit in the value stored on the stack. 15500b57cec5SDimitry Andric SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr, 15510b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 15520b57cec5SDimitry Andric return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr, 15530b57cec5SDimitry Andric State.FloatPointerInfo); 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 15560b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const { 15570b57cec5SDimitry Andric SDLoc DL(Node); 15580b57cec5SDimitry Andric SDValue Mag = Node->getOperand(0); 15590b57cec5SDimitry Andric SDValue Sign = Node->getOperand(1); 15600b57cec5SDimitry Andric 15610b57cec5SDimitry Andric // Get sign bit into an integer value. 15620b57cec5SDimitry Andric FloatSignAsInt SignAsInt; 15630b57cec5SDimitry Andric getSignAsIntValue(SignAsInt, DL, Sign); 15640b57cec5SDimitry Andric 15650b57cec5SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 15660b57cec5SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 15670b57cec5SDimitry Andric SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue, 15680b57cec5SDimitry Andric SignMask); 15690b57cec5SDimitry Andric 15700b57cec5SDimitry Andric // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X) 15710b57cec5SDimitry Andric EVT FloatVT = Mag.getValueType(); 15720b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) && 15730b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) { 15740b57cec5SDimitry Andric SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag); 15750b57cec5SDimitry Andric SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue); 15760b57cec5SDimitry Andric SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit, 15770b57cec5SDimitry Andric DAG.getConstant(0, DL, IntVT), ISD::SETNE); 15780b57cec5SDimitry Andric return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue); 15790b57cec5SDimitry Andric } 15800b57cec5SDimitry Andric 15810b57cec5SDimitry Andric // Transform Mag value to integer, and clear the sign bit. 15820b57cec5SDimitry Andric FloatSignAsInt MagAsInt; 15830b57cec5SDimitry Andric getSignAsIntValue(MagAsInt, DL, Mag); 15840b57cec5SDimitry Andric EVT MagVT = MagAsInt.IntValue.getValueType(); 15850b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT); 15860b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue, 15870b57cec5SDimitry Andric ClearSignMask); 15880b57cec5SDimitry Andric 15890b57cec5SDimitry Andric // Get the signbit at the right position for MagAsInt. 15900b57cec5SDimitry Andric int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit; 15910b57cec5SDimitry Andric EVT ShiftVT = IntVT; 1592e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() < 1593e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 15940b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit); 15950b57cec5SDimitry Andric ShiftVT = MagVT; 15960b57cec5SDimitry Andric } 15970b57cec5SDimitry Andric if (ShiftAmount > 0) { 15980b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT); 15990b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst); 16000b57cec5SDimitry Andric } else if (ShiftAmount < 0) { 16010b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT); 16020b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst); 16030b57cec5SDimitry Andric } 1604e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() > 1605e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 16060b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit); 16070b57cec5SDimitry Andric } 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric // Store the part with the modified sign and convert back to float. 16100b57cec5SDimitry Andric SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit); 16110b57cec5SDimitry Andric return modifySignAsInt(MagAsInt, DL, CopiedSign); 16120b57cec5SDimitry Andric } 16130b57cec5SDimitry Andric 1614e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const { 1615e8d8bef9SDimitry Andric // Get the sign bit as an integer. 1616e8d8bef9SDimitry Andric SDLoc DL(Node); 1617e8d8bef9SDimitry Andric FloatSignAsInt SignAsInt; 1618e8d8bef9SDimitry Andric getSignAsIntValue(SignAsInt, DL, Node->getOperand(0)); 1619e8d8bef9SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 1620e8d8bef9SDimitry Andric 1621e8d8bef9SDimitry Andric // Flip the sign. 1622e8d8bef9SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 1623e8d8bef9SDimitry Andric SDValue SignFlip = 1624e8d8bef9SDimitry Andric DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask); 1625e8d8bef9SDimitry Andric 1626e8d8bef9SDimitry Andric // Convert back to float. 1627e8d8bef9SDimitry Andric return modifySignAsInt(SignAsInt, DL, SignFlip); 1628e8d8bef9SDimitry Andric } 1629e8d8bef9SDimitry Andric 16300b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const { 16310b57cec5SDimitry Andric SDLoc DL(Node); 16320b57cec5SDimitry Andric SDValue Value = Node->getOperand(0); 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal. 16350b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 16360b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) { 16370b57cec5SDimitry Andric SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT); 16380b57cec5SDimitry Andric return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero); 16390b57cec5SDimitry Andric } 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric // Transform value to integer, clear the sign bit and transform back. 16420b57cec5SDimitry Andric FloatSignAsInt ValueAsInt; 16430b57cec5SDimitry Andric getSignAsIntValue(ValueAsInt, DL, Value); 16440b57cec5SDimitry Andric EVT IntVT = ValueAsInt.IntValue.getValueType(); 16450b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT); 16460b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue, 16470b57cec5SDimitry Andric ClearSignMask); 16480b57cec5SDimitry Andric return modifySignAsInt(ValueAsInt, DL, ClearedSign); 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 16520b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 1653e8d8bef9SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 16540b57cec5SDimitry Andric assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 16550b57cec5SDimitry Andric " not tell us which reg is the stack pointer!"); 16560b57cec5SDimitry Andric SDLoc dl(Node); 16570b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 16580b57cec5SDimitry Andric SDValue Tmp1 = SDValue(Node, 0); 16590b57cec5SDimitry Andric SDValue Tmp2 = SDValue(Node, 1); 16600b57cec5SDimitry Andric SDValue Tmp3 = Node->getOperand(2); 16610b57cec5SDimitry Andric SDValue Chain = Tmp1.getOperand(0); 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric // Chain the dynamic stack allocation so that it doesn't modify the stack 16640b57cec5SDimitry Andric // pointer when other instructions are using the stack. 16650b57cec5SDimitry Andric Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl); 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric SDValue Size = Tmp2.getOperand(1); 16680b57cec5SDimitry Andric SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 16690b57cec5SDimitry Andric Chain = SP.getValue(1); 16705ffd83dbSDimitry Andric Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue(); 16715ffd83dbSDimitry Andric const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering(); 16725ffd83dbSDimitry Andric unsigned Opc = 16735ffd83dbSDimitry Andric TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ? 16745ffd83dbSDimitry Andric ISD::ADD : ISD::SUB; 16755ffd83dbSDimitry Andric 16765ffd83dbSDimitry Andric Align StackAlign = TFL->getStackAlign(); 16775ffd83dbSDimitry Andric Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value 16785ffd83dbSDimitry Andric if (Alignment > StackAlign) 16790b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1, 16805ffd83dbSDimitry Andric DAG.getConstant(-Alignment.value(), dl, VT)); 16810b57cec5SDimitry Andric Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 16820b57cec5SDimitry Andric 16830b57cec5SDimitry Andric Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true), 16840b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl, true), SDValue(), dl); 16850b57cec5SDimitry Andric 16860b57cec5SDimitry Andric Results.push_back(Tmp1); 16870b57cec5SDimitry Andric Results.push_back(Tmp2); 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric /// Emit a store/load combination to the stack. This stores 16910b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 16920b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed. 16930b57cec5SDimitry Andric /// The resultant code need not be legal. 16940b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 16950b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl) { 16960b57cec5SDimitry Andric return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode()); 16970b57cec5SDimitry Andric } 16980b57cec5SDimitry Andric 16990b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 17000b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl, 17010b57cec5SDimitry Andric SDValue Chain) { 1702e8d8bef9SDimitry Andric unsigned SrcSize = SrcOp.getValueSizeInBits(); 1703e8d8bef9SDimitry Andric unsigned SlotSize = SlotVT.getSizeInBits(); 1704e8d8bef9SDimitry Andric unsigned DestSize = DestVT.getSizeInBits(); 1705e8d8bef9SDimitry Andric Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1706e8d8bef9SDimitry Andric Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType); 1707e8d8bef9SDimitry Andric 1708e8d8bef9SDimitry Andric // Don't convert with stack if the load/store is expensive. 1709e8d8bef9SDimitry Andric if ((SrcSize > SlotSize && 1710e8d8bef9SDimitry Andric !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) || 1711e8d8bef9SDimitry Andric (SlotSize < DestSize && 1712e8d8bef9SDimitry Andric !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT))) 1713e8d8bef9SDimitry Andric return SDValue(); 1714e8d8bef9SDimitry Andric 17150b57cec5SDimitry Andric // Create the stack frame object. 1716e8d8bef9SDimitry Andric Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign( 17170b57cec5SDimitry Andric SrcOp.getValueType().getTypeForEVT(*DAG.getContext())); 1718e8d8bef9SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign); 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 17210b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 17220b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 17230b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric // Emit a store to the stack slot. Use a truncstore if the input value is 17260b57cec5SDimitry Andric // later than DestVT. 17270b57cec5SDimitry Andric SDValue Store; 17280b57cec5SDimitry Andric 17290b57cec5SDimitry Andric if (SrcSize > SlotSize) 17300b57cec5SDimitry Andric Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo, 17310b57cec5SDimitry Andric SlotVT, SrcAlign); 17320b57cec5SDimitry Andric else { 17330b57cec5SDimitry Andric assert(SrcSize == SlotSize && "Invalid store"); 17340b57cec5SDimitry Andric Store = 17350b57cec5SDimitry Andric DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign); 17360b57cec5SDimitry Andric } 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric // Result is a load from the stack slot. 17390b57cec5SDimitry Andric if (SlotSize == DestSize) 17400b57cec5SDimitry Andric return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign); 17410b57cec5SDimitry Andric 17420b57cec5SDimitry Andric assert(SlotSize < DestSize && "Unknown extension!"); 17430b57cec5SDimitry Andric return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT, 17440b57cec5SDimitry Andric DestAlign); 17450b57cec5SDimitry Andric } 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 17480b57cec5SDimitry Andric SDLoc dl(Node); 17490b57cec5SDimitry Andric // Create a vector sized/aligned stack slot, store the value to element #0, 17500b57cec5SDimitry Andric // then load the whole vector back out. 17510b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 17520b57cec5SDimitry Andric 17530b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 17540b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 17550b57cec5SDimitry Andric 17560b57cec5SDimitry Andric SDValue Ch = DAG.getTruncStore( 17570b57cec5SDimitry Andric DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr, 17580b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), 17590b57cec5SDimitry Andric Node->getValueType(0).getVectorElementType()); 17600b57cec5SDimitry Andric return DAG.getLoad( 17610b57cec5SDimitry Andric Node->getValueType(0), dl, Ch, StackPtr, 17620b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 17630b57cec5SDimitry Andric } 17640b57cec5SDimitry Andric 17650b57cec5SDimitry Andric static bool 17660b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, 17670b57cec5SDimitry Andric const TargetLowering &TLI, SDValue &Res) { 17680b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 17690b57cec5SDimitry Andric SDLoc dl(Node); 17700b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 17710b57cec5SDimitry Andric 17720b57cec5SDimitry Andric // Try to group the scalars into pairs, shuffle the pairs together, then 17730b57cec5SDimitry Andric // shuffle the pairs of pairs together, etc. until the vector has 17740b57cec5SDimitry Andric // been built. This will work only if all of the necessary shuffle masks 17750b57cec5SDimitry Andric // are legal. 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric // We do this in two phases; first to check the legality of the shuffles, 17780b57cec5SDimitry Andric // and next, assuming that all shuffles are legal, to create the new nodes. 17790b57cec5SDimitry Andric for (int Phase = 0; Phase < 2; ++Phase) { 17800b57cec5SDimitry Andric SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals, 17810b57cec5SDimitry Andric NewIntermedVals; 17820b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 17830b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 17840b57cec5SDimitry Andric if (V.isUndef()) 17850b57cec5SDimitry Andric continue; 17860b57cec5SDimitry Andric 17870b57cec5SDimitry Andric SDValue Vec; 17880b57cec5SDimitry Andric if (Phase) 17890b57cec5SDimitry Andric Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V); 17900b57cec5SDimitry Andric IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i))); 17910b57cec5SDimitry Andric } 17920b57cec5SDimitry Andric 17930b57cec5SDimitry Andric while (IntermedVals.size() > 2) { 17940b57cec5SDimitry Andric NewIntermedVals.clear(); 17950b57cec5SDimitry Andric for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) { 17960b57cec5SDimitry Andric // This vector and the next vector are shuffled together (simply to 17970b57cec5SDimitry Andric // append the one to the other). 17980b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 17990b57cec5SDimitry Andric 18000b57cec5SDimitry Andric SmallVector<int, 16> FinalIndices; 18010b57cec5SDimitry Andric FinalIndices.reserve(IntermedVals[i].second.size() + 18020b57cec5SDimitry Andric IntermedVals[i+1].second.size()); 18030b57cec5SDimitry Andric 18040b57cec5SDimitry Andric int k = 0; 18050b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f; 18060b57cec5SDimitry Andric ++j, ++k) { 18070b57cec5SDimitry Andric ShuffleVec[k] = j; 18080b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i].second[j]); 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f; 18110b57cec5SDimitry Andric ++j, ++k) { 18120b57cec5SDimitry Andric ShuffleVec[k] = NumElems + j; 18130b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i+1].second[j]); 18140b57cec5SDimitry Andric } 18150b57cec5SDimitry Andric 18160b57cec5SDimitry Andric SDValue Shuffle; 18170b57cec5SDimitry Andric if (Phase) 18180b57cec5SDimitry Andric Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first, 18190b57cec5SDimitry Andric IntermedVals[i+1].first, 18200b57cec5SDimitry Andric ShuffleVec); 18210b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 18220b57cec5SDimitry Andric return false; 18230b57cec5SDimitry Andric NewIntermedVals.push_back( 18240b57cec5SDimitry Andric std::make_pair(Shuffle, std::move(FinalIndices))); 18250b57cec5SDimitry Andric } 18260b57cec5SDimitry Andric 18270b57cec5SDimitry Andric // If we had an odd number of defined values, then append the last 18280b57cec5SDimitry Andric // element to the array of new vectors. 18290b57cec5SDimitry Andric if ((IntermedVals.size() & 1) != 0) 18300b57cec5SDimitry Andric NewIntermedVals.push_back(IntermedVals.back()); 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric IntermedVals.swap(NewIntermedVals); 18330b57cec5SDimitry Andric } 18340b57cec5SDimitry Andric 18350b57cec5SDimitry Andric assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 && 18360b57cec5SDimitry Andric "Invalid number of intermediate vectors"); 18370b57cec5SDimitry Andric SDValue Vec1 = IntermedVals[0].first; 18380b57cec5SDimitry Andric SDValue Vec2; 18390b57cec5SDimitry Andric if (IntermedVals.size() > 1) 18400b57cec5SDimitry Andric Vec2 = IntermedVals[1].first; 18410b57cec5SDimitry Andric else if (Phase) 18420b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 18430b57cec5SDimitry Andric 18440b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 18450b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i) 18460b57cec5SDimitry Andric ShuffleVec[IntermedVals[0].second[i]] = i; 18470b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i) 18480b57cec5SDimitry Andric ShuffleVec[IntermedVals[1].second[i]] = NumElems + i; 18490b57cec5SDimitry Andric 18500b57cec5SDimitry Andric if (Phase) 18510b57cec5SDimitry Andric Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 18520b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 18530b57cec5SDimitry Andric return false; 18540b57cec5SDimitry Andric } 18550b57cec5SDimitry Andric 18560b57cec5SDimitry Andric return true; 18570b57cec5SDimitry Andric } 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't 18600b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type. 18610b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 18620b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 18630b57cec5SDimitry Andric SDValue Value1, Value2; 18640b57cec5SDimitry Andric SDLoc dl(Node); 18650b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 18660b57cec5SDimitry Andric EVT OpVT = Node->getOperand(0).getValueType(); 18670b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 18680b57cec5SDimitry Andric 18690b57cec5SDimitry Andric // If the only non-undef value is the low element, turn this into a 18700b57cec5SDimitry Andric // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 18710b57cec5SDimitry Andric bool isOnlyLowElement = true; 18720b57cec5SDimitry Andric bool MoreThanTwoValues = false; 18730b57cec5SDimitry Andric bool isConstant = true; 18740b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 18750b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 18760b57cec5SDimitry Andric if (V.isUndef()) 18770b57cec5SDimitry Andric continue; 18780b57cec5SDimitry Andric if (i > 0) 18790b57cec5SDimitry Andric isOnlyLowElement = false; 18800b57cec5SDimitry Andric if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 18810b57cec5SDimitry Andric isConstant = false; 18820b57cec5SDimitry Andric 18830b57cec5SDimitry Andric if (!Value1.getNode()) { 18840b57cec5SDimitry Andric Value1 = V; 18850b57cec5SDimitry Andric } else if (!Value2.getNode()) { 18860b57cec5SDimitry Andric if (V != Value1) 18870b57cec5SDimitry Andric Value2 = V; 18880b57cec5SDimitry Andric } else if (V != Value1 && V != Value2) { 18890b57cec5SDimitry Andric MoreThanTwoValues = true; 18900b57cec5SDimitry Andric } 18910b57cec5SDimitry Andric } 18920b57cec5SDimitry Andric 18930b57cec5SDimitry Andric if (!Value1.getNode()) 18940b57cec5SDimitry Andric return DAG.getUNDEF(VT); 18950b57cec5SDimitry Andric 18960b57cec5SDimitry Andric if (isOnlyLowElement) 18970b57cec5SDimitry Andric return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 18980b57cec5SDimitry Andric 18990b57cec5SDimitry Andric // If all elements are constants, create a load from the constant pool. 19000b57cec5SDimitry Andric if (isConstant) { 19010b57cec5SDimitry Andric SmallVector<Constant*, 16> CV; 19020b57cec5SDimitry Andric for (unsigned i = 0, e = NumElems; i != e; ++i) { 19030b57cec5SDimitry Andric if (ConstantFPSDNode *V = 19040b57cec5SDimitry Andric dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 19050b57cec5SDimitry Andric CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 19060b57cec5SDimitry Andric } else if (ConstantSDNode *V = 19070b57cec5SDimitry Andric dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 19080b57cec5SDimitry Andric if (OpVT==EltVT) 19090b57cec5SDimitry Andric CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 19100b57cec5SDimitry Andric else { 19110b57cec5SDimitry Andric // If OpVT and EltVT don't match, EltVT is not legal and the 19120b57cec5SDimitry Andric // element values have been promoted/truncated earlier. Undo this; 19130b57cec5SDimitry Andric // we don't want a v16i8 to become a v16i32 for example. 19140b57cec5SDimitry Andric const ConstantInt *CI = V->getConstantIntValue(); 19150b57cec5SDimitry Andric CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 19160b57cec5SDimitry Andric CI->getZExtValue())); 19170b57cec5SDimitry Andric } 19180b57cec5SDimitry Andric } else { 19190b57cec5SDimitry Andric assert(Node->getOperand(i).isUndef()); 19200b57cec5SDimitry Andric Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 19210b57cec5SDimitry Andric CV.push_back(UndefValue::get(OpNTy)); 19220b57cec5SDimitry Andric } 19230b57cec5SDimitry Andric } 19240b57cec5SDimitry Andric Constant *CP = ConstantVector::get(CV); 19250b57cec5SDimitry Andric SDValue CPIdx = 19260b57cec5SDimitry Andric DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); 19275ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 19280b57cec5SDimitry Andric return DAG.getLoad( 19290b57cec5SDimitry Andric VT, dl, DAG.getEntryNode(), CPIdx, 19300b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 19310b57cec5SDimitry Andric Alignment); 19320b57cec5SDimitry Andric } 19330b57cec5SDimitry Andric 19340b57cec5SDimitry Andric SmallSet<SDValue, 16> DefinedValues; 19350b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19360b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) 19370b57cec5SDimitry Andric continue; 19380b57cec5SDimitry Andric DefinedValues.insert(Node->getOperand(i)); 19390b57cec5SDimitry Andric } 19400b57cec5SDimitry Andric 19410b57cec5SDimitry Andric if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) { 19420b57cec5SDimitry Andric if (!MoreThanTwoValues) { 19430b57cec5SDimitry Andric SmallVector<int, 8> ShuffleVec(NumElems, -1); 19440b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19450b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 19460b57cec5SDimitry Andric if (V.isUndef()) 19470b57cec5SDimitry Andric continue; 19480b57cec5SDimitry Andric ShuffleVec[i] = V == Value1 ? 0 : NumElems; 19490b57cec5SDimitry Andric } 19500b57cec5SDimitry Andric if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 19510b57cec5SDimitry Andric // Get the splatted value into the low element of a vector register. 19520b57cec5SDimitry Andric SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 19530b57cec5SDimitry Andric SDValue Vec2; 19540b57cec5SDimitry Andric if (Value2.getNode()) 19550b57cec5SDimitry Andric Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 19560b57cec5SDimitry Andric else 19570b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 19580b57cec5SDimitry Andric 19590b57cec5SDimitry Andric // Return shuffle(LowValVec, undef, <0,0,0,0>) 19600b57cec5SDimitry Andric return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 19610b57cec5SDimitry Andric } 19620b57cec5SDimitry Andric } else { 19630b57cec5SDimitry Andric SDValue Res; 19640b57cec5SDimitry Andric if (ExpandBVWithShuffles(Node, DAG, TLI, Res)) 19650b57cec5SDimitry Andric return Res; 19660b57cec5SDimitry Andric } 19670b57cec5SDimitry Andric } 19680b57cec5SDimitry Andric 19690b57cec5SDimitry Andric // Otherwise, we can't handle this case efficiently. 19700b57cec5SDimitry Andric return ExpandVectorBuildThroughStack(Node); 19710b57cec5SDimitry Andric } 19720b57cec5SDimitry Andric 19738bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) { 19748bcb0991SDimitry Andric SDLoc DL(Node); 19758bcb0991SDimitry Andric EVT VT = Node->getValueType(0); 19768bcb0991SDimitry Andric SDValue SplatVal = Node->getOperand(0); 19778bcb0991SDimitry Andric 19788bcb0991SDimitry Andric return DAG.getSplatBuildVector(VT, DL, SplatVal); 19798bcb0991SDimitry Andric } 19808bcb0991SDimitry Andric 19810b57cec5SDimitry Andric // Expand a node into a call to a libcall. If the result value 19820b57cec5SDimitry Andric // does not fit into a register, return the lo part and set the hi part to the 19830b57cec5SDimitry Andric // by-reg argument. If it does fit into a single register, return the result 19840b57cec5SDimitry Andric // and leave the Hi part unset. 19850b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 19860b57cec5SDimitry Andric bool isSigned) { 19870b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 19880b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 19890b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) { 19900b57cec5SDimitry Andric EVT ArgVT = Op.getValueType(); 19910b57cec5SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 19920b57cec5SDimitry Andric Entry.Node = Op; 19930b57cec5SDimitry Andric Entry.Ty = ArgTy; 19940b57cec5SDimitry Andric Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 19950b57cec5SDimitry Andric Entry.IsZExt = !TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 19960b57cec5SDimitry Andric Args.push_back(Entry); 19970b57cec5SDimitry Andric } 19980b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 19990b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 20000b57cec5SDimitry Andric 20010b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 20020b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 20030b57cec5SDimitry Andric 20040b57cec5SDimitry Andric // By default, the input chain to this libcall is the entry node of the 20050b57cec5SDimitry Andric // function. If the libcall is going to be emitted as a tail call then 20060b57cec5SDimitry Andric // TLI.isUsedByReturnOnly will change it to the right chain if the return 20070b57cec5SDimitry Andric // node which is being folded has a non-entry input chain. 20080b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 20090b57cec5SDimitry Andric 20100b57cec5SDimitry Andric // isTailCall may be true since the callee does not reference caller stack 20110b57cec5SDimitry Andric // frame. Check if it's in the right position and that the return types match. 20120b57cec5SDimitry Andric SDValue TCChain = InChain; 20130b57cec5SDimitry Andric const Function &F = DAG.getMachineFunction().getFunction(); 20140b57cec5SDimitry Andric bool isTailCall = 20150b57cec5SDimitry Andric TLI.isInTailCallPosition(DAG, Node, TCChain) && 20160b57cec5SDimitry Andric (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy()); 20170b57cec5SDimitry Andric if (isTailCall) 20180b57cec5SDimitry Andric InChain = TCChain; 20190b57cec5SDimitry Andric 20200b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 20210b57cec5SDimitry Andric bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned); 20220b57cec5SDimitry Andric CLI.setDebugLoc(SDLoc(Node)) 20230b57cec5SDimitry Andric .setChain(InChain) 20240b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 20250b57cec5SDimitry Andric std::move(Args)) 20260b57cec5SDimitry Andric .setTailCall(isTailCall) 20270b57cec5SDimitry Andric .setSExtResult(signExtend) 20280b57cec5SDimitry Andric .setZExtResult(!signExtend) 20290b57cec5SDimitry Andric .setIsPostTypeLegalization(true); 20300b57cec5SDimitry Andric 20310b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 20320b57cec5SDimitry Andric 20330b57cec5SDimitry Andric if (!CallInfo.second.getNode()) { 20348bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG)); 20350b57cec5SDimitry Andric // It's a tailcall, return the chain (which is the DAG root). 20360b57cec5SDimitry Andric return DAG.getRoot(); 20370b57cec5SDimitry Andric } 20380b57cec5SDimitry Andric 20398bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG)); 20400b57cec5SDimitry Andric return CallInfo.first; 20410b57cec5SDimitry Andric } 20420b57cec5SDimitry Andric 2043480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2044*fe6060f1SDimitry Andric RTLIB::Libcall LC, 2045480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2046*fe6060f1SDimitry Andric if (LC == RTLIB::UNKNOWN_LIBCALL) 2047*fe6060f1SDimitry Andric llvm_unreachable("Can't create an unknown libcall!"); 2048480093f4SDimitry Andric 2049480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2050480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 2051e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops(drop_begin(Node->ops())); 2052480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 2053480093f4SDimitry Andric // FIXME: This doesn't support tail calls. 2054480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 2055480093f4SDimitry Andric Ops, CallOptions, 2056480093f4SDimitry Andric SDLoc(Node), 2057480093f4SDimitry Andric Node->getOperand(0)); 2058480093f4SDimitry Andric Results.push_back(Tmp.first); 2059480093f4SDimitry Andric Results.push_back(Tmp.second); 2060480093f4SDimitry Andric } else { 2061480093f4SDimitry Andric SDValue Tmp = ExpandLibCall(LC, Node, false); 2062480093f4SDimitry Andric Results.push_back(Tmp); 2063480093f4SDimitry Andric } 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric 2066*fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type. 2067*fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2068*fe6060f1SDimitry Andric RTLIB::Libcall Call_F32, 2069*fe6060f1SDimitry Andric RTLIB::Libcall Call_F64, 2070*fe6060f1SDimitry Andric RTLIB::Libcall Call_F80, 2071*fe6060f1SDimitry Andric RTLIB::Libcall Call_F128, 2072*fe6060f1SDimitry Andric RTLIB::Libcall Call_PPCF128, 2073*fe6060f1SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2074*fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0), 2075*fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2076*fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2077*fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 2078*fe6060f1SDimitry Andric } 2079*fe6060f1SDimitry Andric 20800b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 20810b57cec5SDimitry Andric RTLIB::Libcall Call_I8, 20820b57cec5SDimitry Andric RTLIB::Libcall Call_I16, 20830b57cec5SDimitry Andric RTLIB::Libcall Call_I32, 20840b57cec5SDimitry Andric RTLIB::Libcall Call_I64, 20850b57cec5SDimitry Andric RTLIB::Libcall Call_I128) { 20860b57cec5SDimitry Andric RTLIB::Libcall LC; 20870b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 20880b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 20890b57cec5SDimitry Andric case MVT::i8: LC = Call_I8; break; 20900b57cec5SDimitry Andric case MVT::i16: LC = Call_I16; break; 20910b57cec5SDimitry Andric case MVT::i32: LC = Call_I32; break; 20920b57cec5SDimitry Andric case MVT::i64: LC = Call_I64; break; 20930b57cec5SDimitry Andric case MVT::i128: LC = Call_I128; break; 20940b57cec5SDimitry Andric } 20950b57cec5SDimitry Andric return ExpandLibCall(LC, Node, isSigned); 20960b57cec5SDimitry Andric } 20970b57cec5SDimitry Andric 20980b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance 20990b57cec5SDimitry Andric /// lround and its variant). 2100480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node, 21010b57cec5SDimitry Andric RTLIB::Libcall Call_F32, 21020b57cec5SDimitry Andric RTLIB::Libcall Call_F64, 21030b57cec5SDimitry Andric RTLIB::Libcall Call_F80, 21040b57cec5SDimitry Andric RTLIB::Libcall Call_F128, 2105480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 2106480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2107480093f4SDimitry Andric EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType(); 2108*fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(), 2109*fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2110*fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2111*fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 21120b57cec5SDimitry Andric } 21130b57cec5SDimitry Andric 21140b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs. 21150b57cec5SDimitry Andric void 21160b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, 21170b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 21180b57cec5SDimitry Andric unsigned Opcode = Node->getOpcode(); 21190b57cec5SDimitry Andric bool isSigned = Opcode == ISD::SDIVREM; 21200b57cec5SDimitry Andric 21210b57cec5SDimitry Andric RTLIB::Libcall LC; 21220b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 21230b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 21240b57cec5SDimitry Andric case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 21250b57cec5SDimitry Andric case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 21260b57cec5SDimitry Andric case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 21270b57cec5SDimitry Andric case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 21280b57cec5SDimitry Andric case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 21290b57cec5SDimitry Andric } 21300b57cec5SDimitry Andric 21310b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 21320b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 21330b57cec5SDimitry Andric // dependence. 21340b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 21350b57cec5SDimitry Andric 21360b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 21370b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 21380b57cec5SDimitry Andric 21390b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 21400b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 21410b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) { 21420b57cec5SDimitry Andric EVT ArgVT = Op.getValueType(); 21430b57cec5SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 21440b57cec5SDimitry Andric Entry.Node = Op; 21450b57cec5SDimitry Andric Entry.Ty = ArgTy; 21460b57cec5SDimitry Andric Entry.IsSExt = isSigned; 21470b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 21480b57cec5SDimitry Andric Args.push_back(Entry); 21490b57cec5SDimitry Andric } 21500b57cec5SDimitry Andric 21510b57cec5SDimitry Andric // Also pass the return address of the remainder. 21520b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(RetVT); 21530b57cec5SDimitry Andric Entry.Node = FIPtr; 21540b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 21550b57cec5SDimitry Andric Entry.IsSExt = isSigned; 21560b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 21570b57cec5SDimitry Andric Args.push_back(Entry); 21580b57cec5SDimitry Andric 21590b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 21600b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 21610b57cec5SDimitry Andric 21620b57cec5SDimitry Andric SDLoc dl(Node); 21630b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 21640b57cec5SDimitry Andric CLI.setDebugLoc(dl) 21650b57cec5SDimitry Andric .setChain(InChain) 21660b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 21670b57cec5SDimitry Andric std::move(Args)) 21680b57cec5SDimitry Andric .setSExtResult(isSigned) 21690b57cec5SDimitry Andric .setZExtResult(!isSigned); 21700b57cec5SDimitry Andric 21710b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 21720b57cec5SDimitry Andric 21730b57cec5SDimitry Andric // Remainder is loaded back from the stack frame. 21740b57cec5SDimitry Andric SDValue Rem = 21750b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo()); 21760b57cec5SDimitry Andric Results.push_back(CallInfo.first); 21770b57cec5SDimitry Andric Results.push_back(Rem); 21780b57cec5SDimitry Andric } 21790b57cec5SDimitry Andric 21800b57cec5SDimitry Andric /// Return true if sincos libcall is available. 21810b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) { 21820b57cec5SDimitry Andric RTLIB::Libcall LC; 21830b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 21840b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 21850b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 21860b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 21870b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 21880b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 21890b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 21900b57cec5SDimitry Andric } 21910b57cec5SDimitry Andric return TLI.getLibcallName(LC) != nullptr; 21920b57cec5SDimitry Andric } 21930b57cec5SDimitry Andric 21940b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed. 21950b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) { 21960b57cec5SDimitry Andric unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN 21970b57cec5SDimitry Andric ? ISD::FCOS : ISD::FSIN; 21980b57cec5SDimitry Andric 21990b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 22000b57cec5SDimitry Andric for (SDNode::use_iterator UI = Op0.getNode()->use_begin(), 22010b57cec5SDimitry Andric UE = Op0.getNode()->use_end(); UI != UE; ++UI) { 22020b57cec5SDimitry Andric SDNode *User = *UI; 22030b57cec5SDimitry Andric if (User == Node) 22040b57cec5SDimitry Andric continue; 22050b57cec5SDimitry Andric // The other user might have been turned into sincos already. 22060b57cec5SDimitry Andric if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS) 22070b57cec5SDimitry Andric return true; 22080b57cec5SDimitry Andric } 22090b57cec5SDimitry Andric return false; 22100b57cec5SDimitry Andric } 22110b57cec5SDimitry Andric 22120b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs. 22130b57cec5SDimitry Andric void 22140b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node, 22150b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 22160b57cec5SDimitry Andric RTLIB::Libcall LC; 22170b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 22180b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 22190b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 22200b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 22210b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 22220b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 22230b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 22240b57cec5SDimitry Andric } 22250b57cec5SDimitry Andric 22260b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 22270b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 22280b57cec5SDimitry Andric // dependence. 22290b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 22300b57cec5SDimitry Andric 22310b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 22320b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 22330b57cec5SDimitry Andric 22340b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 22350b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 22360b57cec5SDimitry Andric 22370b57cec5SDimitry Andric // Pass the argument. 22380b57cec5SDimitry Andric Entry.Node = Node->getOperand(0); 22390b57cec5SDimitry Andric Entry.Ty = RetTy; 22400b57cec5SDimitry Andric Entry.IsSExt = false; 22410b57cec5SDimitry Andric Entry.IsZExt = false; 22420b57cec5SDimitry Andric Args.push_back(Entry); 22430b57cec5SDimitry Andric 22440b57cec5SDimitry Andric // Pass the return address of sin. 22450b57cec5SDimitry Andric SDValue SinPtr = DAG.CreateStackTemporary(RetVT); 22460b57cec5SDimitry Andric Entry.Node = SinPtr; 22470b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 22480b57cec5SDimitry Andric Entry.IsSExt = false; 22490b57cec5SDimitry Andric Entry.IsZExt = false; 22500b57cec5SDimitry Andric Args.push_back(Entry); 22510b57cec5SDimitry Andric 22520b57cec5SDimitry Andric // Also pass the return address of the cos. 22530b57cec5SDimitry Andric SDValue CosPtr = DAG.CreateStackTemporary(RetVT); 22540b57cec5SDimitry Andric Entry.Node = CosPtr; 22550b57cec5SDimitry Andric Entry.Ty = RetTy->getPointerTo(); 22560b57cec5SDimitry Andric Entry.IsSExt = false; 22570b57cec5SDimitry Andric Entry.IsZExt = false; 22580b57cec5SDimitry Andric Args.push_back(Entry); 22590b57cec5SDimitry Andric 22600b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 22610b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 22620b57cec5SDimitry Andric 22630b57cec5SDimitry Andric SDLoc dl(Node); 22640b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 22650b57cec5SDimitry Andric CLI.setDebugLoc(dl).setChain(InChain).setLibCallee( 22660b57cec5SDimitry Andric TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee, 22670b57cec5SDimitry Andric std::move(Args)); 22680b57cec5SDimitry Andric 22690b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 22700b57cec5SDimitry Andric 22710b57cec5SDimitry Andric Results.push_back( 22720b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo())); 22730b57cec5SDimitry Andric Results.push_back( 22740b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo())); 22750b57cec5SDimitry Andric } 22760b57cec5SDimitry Andric 22770b57cec5SDimitry Andric /// This function is responsible for legalizing a 22780b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that 22790b57cec5SDimitry Andric /// we expand it. At this point, we know that the result and operand types are 22800b57cec5SDimitry Andric /// legal for the target. 2281480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node, 2282480093f4SDimitry Andric SDValue &Chain) { 2283480093f4SDimitry Andric bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 2284480093f4SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP); 2285480093f4SDimitry Andric EVT DestVT = Node->getValueType(0); 2286480093f4SDimitry Andric SDLoc dl(Node); 2287480093f4SDimitry Andric unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; 2288480093f4SDimitry Andric SDValue Op0 = Node->getOperand(OpNo); 22890b57cec5SDimitry Andric EVT SrcVT = Op0.getValueType(); 22900b57cec5SDimitry Andric 22910b57cec5SDimitry Andric // TODO: Should any fast-math-flags be set for the created nodes? 22920b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n"); 2293e8d8bef9SDimitry Andric if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) && 2294e8d8bef9SDimitry Andric (DestVT.bitsLE(MVT::f64) || 2295e8d8bef9SDimitry Andric TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND 2296e8d8bef9SDimitry Andric : ISD::FP_EXTEND, 2297e8d8bef9SDimitry Andric DestVT))) { 22980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double " 22990b57cec5SDimitry Andric "expansion\n"); 23000b57cec5SDimitry Andric 23010b57cec5SDimitry Andric // Get the stack frame index of a 8 byte buffer. 23020b57cec5SDimitry Andric SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 23030b57cec5SDimitry Andric 23045ffd83dbSDimitry Andric SDValue Lo = Op0; 23050b57cec5SDimitry Andric // if signed map to unsigned space 23060b57cec5SDimitry Andric if (isSigned) { 23075ffd83dbSDimitry Andric // Invert sign bit (signed to unsigned mapping). 23085ffd83dbSDimitry Andric Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo, 23095ffd83dbSDimitry Andric DAG.getConstant(0x80000000u, dl, MVT::i32)); 23100b57cec5SDimitry Andric } 23115ffd83dbSDimitry Andric // Initial hi portion of constructed double. 23125ffd83dbSDimitry Andric SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32); 23135ffd83dbSDimitry Andric 23145ffd83dbSDimitry Andric // If this a big endian target, swap the lo and high data. 23155ffd83dbSDimitry Andric if (DAG.getDataLayout().isBigEndian()) 23165ffd83dbSDimitry Andric std::swap(Lo, Hi); 23175ffd83dbSDimitry Andric 23185ffd83dbSDimitry Andric SDValue MemChain = DAG.getEntryNode(); 23195ffd83dbSDimitry Andric 23205ffd83dbSDimitry Andric // Store the lo of the constructed double. 23215ffd83dbSDimitry Andric SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot, 23220b57cec5SDimitry Andric MachinePointerInfo()); 23235ffd83dbSDimitry Andric // Store the hi of the constructed double. 2324e8d8bef9SDimitry Andric SDValue HiPtr = DAG.getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), dl); 23250b57cec5SDimitry Andric SDValue Store2 = 23265ffd83dbSDimitry Andric DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo()); 23275ffd83dbSDimitry Andric MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 23285ffd83dbSDimitry Andric 23290b57cec5SDimitry Andric // load the constructed double 23300b57cec5SDimitry Andric SDValue Load = 23315ffd83dbSDimitry Andric DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo()); 23320b57cec5SDimitry Andric // FP constant to bias correct the final result 23330b57cec5SDimitry Andric SDValue Bias = DAG.getConstantFP(isSigned ? 23340b57cec5SDimitry Andric BitsToDouble(0x4330000080000000ULL) : 23350b57cec5SDimitry Andric BitsToDouble(0x4330000000000000ULL), 23360b57cec5SDimitry Andric dl, MVT::f64); 2337480093f4SDimitry Andric // Subtract the bias and get the final result. 2338480093f4SDimitry Andric SDValue Sub; 2339480093f4SDimitry Andric SDValue Result; 2340480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2341480093f4SDimitry Andric Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other}, 2342480093f4SDimitry Andric {Node->getOperand(0), Load, Bias}); 2343480093f4SDimitry Andric Chain = Sub.getValue(1); 2344480093f4SDimitry Andric if (DestVT != Sub.getValueType()) { 2345480093f4SDimitry Andric std::pair<SDValue, SDValue> ResultPair; 2346480093f4SDimitry Andric ResultPair = 2347480093f4SDimitry Andric DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT); 2348480093f4SDimitry Andric Result = ResultPair.first; 2349480093f4SDimitry Andric Chain = ResultPair.second; 2350480093f4SDimitry Andric } 2351480093f4SDimitry Andric else 2352480093f4SDimitry Andric Result = Sub; 2353480093f4SDimitry Andric } else { 2354480093f4SDimitry Andric Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2355480093f4SDimitry Andric Result = DAG.getFPExtendOrRound(Sub, dl, DestVT); 2356480093f4SDimitry Andric } 23570b57cec5SDimitry Andric return Result; 23580b57cec5SDimitry Andric } 2359e8d8bef9SDimitry Andric 2360e8d8bef9SDimitry Andric if (isSigned) 2361e8d8bef9SDimitry Andric return SDValue(); 23625ffd83dbSDimitry Andric 23635ffd83dbSDimitry Andric // TODO: Generalize this for use with other types. 2364e8d8bef9SDimitry Andric if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) || 2365e8d8bef9SDimitry Andric (SrcVT == MVT::i64 && DestVT == MVT::f64)) { 2366e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n"); 23675ffd83dbSDimitry Andric // For unsigned conversions, convert them to signed conversions using the 23685ffd83dbSDimitry Andric // algorithm from the x86_64 __floatundisf in compiler_rt. That method 23695ffd83dbSDimitry Andric // should be valid for i32->f32 as well. 23705ffd83dbSDimitry Andric 2371e8d8bef9SDimitry Andric // More generally this transform should be valid if there are 3 more bits 2372e8d8bef9SDimitry Andric // in the integer type than the significand. Rounding uses the first bit 2373e8d8bef9SDimitry Andric // after the width of the significand and the OR of all bits after that. So 2374e8d8bef9SDimitry Andric // we need to be able to OR the shifted out bit into one of the bits that 2375e8d8bef9SDimitry Andric // participate in the OR. 2376e8d8bef9SDimitry Andric 23775ffd83dbSDimitry Andric // TODO: This really should be implemented using a branch rather than a 23785ffd83dbSDimitry Andric // select. We happen to get lucky and machinesink does the right 23795ffd83dbSDimitry Andric // thing most of the time. This would be a good candidate for a 23805ffd83dbSDimitry Andric // pseudo-op, or, even better, for whole-function isel. 23815ffd83dbSDimitry Andric EVT SetCCVT = getSetCCResultType(SrcVT); 23825ffd83dbSDimitry Andric 23835ffd83dbSDimitry Andric SDValue SignBitTest = DAG.getSetCC( 23845ffd83dbSDimitry Andric dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 23855ffd83dbSDimitry Andric 23865ffd83dbSDimitry Andric EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout()); 23875ffd83dbSDimitry Andric SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT); 23885ffd83dbSDimitry Andric SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst); 23895ffd83dbSDimitry Andric SDValue AndConst = DAG.getConstant(1, dl, SrcVT); 23905ffd83dbSDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst); 23915ffd83dbSDimitry Andric SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr); 23925ffd83dbSDimitry Andric 23935ffd83dbSDimitry Andric SDValue Slow, Fast; 23945ffd83dbSDimitry Andric if (Node->isStrictFPOpcode()) { 23955ffd83dbSDimitry Andric // In strict mode, we must avoid spurious exceptions, and therefore 23965ffd83dbSDimitry Andric // must make sure to only emit a single STRICT_SINT_TO_FP. 23975ffd83dbSDimitry Andric SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0); 23985ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 23995ffd83dbSDimitry Andric { Node->getOperand(0), InCvt }); 24005ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 24015ffd83dbSDimitry Andric { Fast.getValue(1), Fast, Fast }); 24025ffd83dbSDimitry Andric Chain = Slow.getValue(1); 24035ffd83dbSDimitry Andric // The STRICT_SINT_TO_FP inherits the exception mode from the 24045ffd83dbSDimitry Andric // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can 24055ffd83dbSDimitry Andric // never raise any exception. 24065ffd83dbSDimitry Andric SDNodeFlags Flags; 24075ffd83dbSDimitry Andric Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept()); 24085ffd83dbSDimitry Andric Fast->setFlags(Flags); 24095ffd83dbSDimitry Andric Flags.setNoFPExcept(true); 24105ffd83dbSDimitry Andric Slow->setFlags(Flags); 24115ffd83dbSDimitry Andric } else { 24125ffd83dbSDimitry Andric SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or); 24135ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt); 24145ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 24155ffd83dbSDimitry Andric } 24165ffd83dbSDimitry Andric 24175ffd83dbSDimitry Andric return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast); 24185ffd83dbSDimitry Andric } 24195ffd83dbSDimitry Andric 2420e8d8bef9SDimitry Andric // Don't expand it if there isn't cheap fadd. 2421e8d8bef9SDimitry Andric if (!TLI.isOperationLegalOrCustom( 2422e8d8bef9SDimitry Andric Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT)) 2423e8d8bef9SDimitry Andric return SDValue(); 2424e8d8bef9SDimitry Andric 24255ffd83dbSDimitry Andric // The following optimization is valid only if every value in SrcVT (when 24265ffd83dbSDimitry Andric // treated as signed) is representable in DestVT. Check that the mantissa 24275ffd83dbSDimitry Andric // size of DestVT is >= than the number of bits in SrcVT -1. 24285ffd83dbSDimitry Andric assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >= 24295ffd83dbSDimitry Andric SrcVT.getSizeInBits() - 1 && 24305ffd83dbSDimitry Andric "Cannot perform lossless SINT_TO_FP!"); 24310b57cec5SDimitry Andric 2432480093f4SDimitry Andric SDValue Tmp1; 2433480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2434480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 2435480093f4SDimitry Andric { Node->getOperand(0), Op0 }); 2436480093f4SDimitry Andric } else 2437480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 24380b57cec5SDimitry Andric 24390b57cec5SDimitry Andric SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0, 24400b57cec5SDimitry Andric DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 24410b57cec5SDimitry Andric SDValue Zero = DAG.getIntPtrConstant(0, dl), 24420b57cec5SDimitry Andric Four = DAG.getIntPtrConstant(4, dl); 24430b57cec5SDimitry Andric SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(), 24440b57cec5SDimitry Andric SignSet, Four, Zero); 24450b57cec5SDimitry Andric 24460b57cec5SDimitry Andric // If the sign bit of the integer is set, the large number will be treated 24470b57cec5SDimitry Andric // as a negative number. To counteract this, the dynamic code adds an 24480b57cec5SDimitry Andric // offset depending on the data type. 24490b57cec5SDimitry Andric uint64_t FF; 24500b57cec5SDimitry Andric switch (SrcVT.getSimpleVT().SimpleTy) { 2451e8d8bef9SDimitry Andric default: 2452e8d8bef9SDimitry Andric return SDValue(); 24530b57cec5SDimitry Andric case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 24540b57cec5SDimitry Andric case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 24550b57cec5SDimitry Andric case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 24560b57cec5SDimitry Andric case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 24570b57cec5SDimitry Andric } 24580b57cec5SDimitry Andric if (DAG.getDataLayout().isLittleEndian()) 24590b57cec5SDimitry Andric FF <<= 32; 24600b57cec5SDimitry Andric Constant *FudgeFactor = ConstantInt::get( 24610b57cec5SDimitry Andric Type::getInt64Ty(*DAG.getContext()), FF); 24620b57cec5SDimitry Andric 24630b57cec5SDimitry Andric SDValue CPIdx = 24640b57cec5SDimitry Andric DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); 24655ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 24660b57cec5SDimitry Andric CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); 24675ffd83dbSDimitry Andric Alignment = commonAlignment(Alignment, 4); 24680b57cec5SDimitry Andric SDValue FudgeInReg; 24690b57cec5SDimitry Andric if (DestVT == MVT::f32) 24700b57cec5SDimitry Andric FudgeInReg = DAG.getLoad( 24710b57cec5SDimitry Andric MVT::f32, dl, DAG.getEntryNode(), CPIdx, 24720b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 24730b57cec5SDimitry Andric Alignment); 24740b57cec5SDimitry Andric else { 24750b57cec5SDimitry Andric SDValue Load = DAG.getExtLoad( 24760b57cec5SDimitry Andric ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx, 24770b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, 24780b57cec5SDimitry Andric Alignment); 24790b57cec5SDimitry Andric HandleSDNode Handle(Load); 24800b57cec5SDimitry Andric LegalizeOp(Load.getNode()); 24810b57cec5SDimitry Andric FudgeInReg = Handle.getValue(); 24820b57cec5SDimitry Andric } 24830b57cec5SDimitry Andric 2484480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2485480093f4SDimitry Andric SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 2486480093f4SDimitry Andric { Tmp1.getValue(1), Tmp1, FudgeInReg }); 2487480093f4SDimitry Andric Chain = Result.getValue(1); 2488480093f4SDimitry Andric return Result; 2489480093f4SDimitry Andric } 2490480093f4SDimitry Andric 24910b57cec5SDimitry Andric return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 24920b57cec5SDimitry Andric } 24930b57cec5SDimitry Andric 24940b57cec5SDimitry Andric /// This function is responsible for legalizing a 24950b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that 24960b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 24970b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 24980b57cec5SDimitry Andric /// operation that takes a larger input. 2499480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP( 2500480093f4SDimitry Andric SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) { 2501480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2502480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP || 2503480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_SINT_TO_FP; 2504480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2505480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 2506480093f4SDimitry Andric unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP; 2507480093f4SDimitry Andric unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP; 2508480093f4SDimitry Andric 25090b57cec5SDimitry Andric // First step, figure out the appropriate *INT_TO_FP operation to use. 25100b57cec5SDimitry Andric EVT NewInTy = LegalOp.getValueType(); 25110b57cec5SDimitry Andric 25120b57cec5SDimitry Andric unsigned OpToUse = 0; 25130b57cec5SDimitry Andric 25140b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 25150b57cec5SDimitry Andric while (true) { 25160b57cec5SDimitry Andric NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 25170b57cec5SDimitry Andric assert(NewInTy.isInteger() && "Ran out of possibilities!"); 25180b57cec5SDimitry Andric 25190b57cec5SDimitry Andric // If the target supports SINT_TO_FP of this type, use it. 2520480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) { 2521480093f4SDimitry Andric OpToUse = SIntOp; 25220b57cec5SDimitry Andric break; 25230b57cec5SDimitry Andric } 2524480093f4SDimitry Andric if (IsSigned) 2525480093f4SDimitry Andric continue; 25260b57cec5SDimitry Andric 25270b57cec5SDimitry Andric // If the target supports UINT_TO_FP of this type, use it. 2528480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) { 2529480093f4SDimitry Andric OpToUse = UIntOp; 25300b57cec5SDimitry Andric break; 25310b57cec5SDimitry Andric } 25320b57cec5SDimitry Andric 25330b57cec5SDimitry Andric // Otherwise, try a larger type. 25340b57cec5SDimitry Andric } 25350b57cec5SDimitry Andric 25360b57cec5SDimitry Andric // Okay, we found the operation and type to use. Zero extend our input to the 25370b57cec5SDimitry Andric // desired type then run the operation on it. 2538480093f4SDimitry Andric if (IsStrict) { 2539480093f4SDimitry Andric SDValue Res = 2540480093f4SDimitry Andric DAG.getNode(OpToUse, dl, {DestVT, MVT::Other}, 2541480093f4SDimitry Andric {N->getOperand(0), 2542480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2543480093f4SDimitry Andric dl, NewInTy, LegalOp)}); 2544480093f4SDimitry Andric Results.push_back(Res); 2545480093f4SDimitry Andric Results.push_back(Res.getValue(1)); 2546480093f4SDimitry Andric return; 2547480093f4SDimitry Andric } 2548480093f4SDimitry Andric 2549480093f4SDimitry Andric Results.push_back( 2550480093f4SDimitry Andric DAG.getNode(OpToUse, dl, DestVT, 2551480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2552480093f4SDimitry Andric dl, NewInTy, LegalOp))); 25530b57cec5SDimitry Andric } 25540b57cec5SDimitry Andric 25550b57cec5SDimitry Andric /// This function is responsible for legalizing a 25560b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that 25570b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 25580b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 25590b57cec5SDimitry Andric /// operation that returns a larger result. 2560480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl, 2561480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2562480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2563480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT || 2564480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_FP_TO_SINT; 2565480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2566480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 25670b57cec5SDimitry Andric // First step, figure out the appropriate FP_TO*INT operation to use. 25680b57cec5SDimitry Andric EVT NewOutTy = DestVT; 25690b57cec5SDimitry Andric 25700b57cec5SDimitry Andric unsigned OpToUse = 0; 25710b57cec5SDimitry Andric 25720b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 25730b57cec5SDimitry Andric while (true) { 25740b57cec5SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 25750b57cec5SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 25760b57cec5SDimitry Andric 25770b57cec5SDimitry Andric // A larger signed type can hold all unsigned values of the requested type, 25780b57cec5SDimitry Andric // so using FP_TO_SINT is valid 2579480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT; 2580480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 25810b57cec5SDimitry Andric break; 25820b57cec5SDimitry Andric 25830b57cec5SDimitry Andric // However, if the value may be < 0.0, we *must* use some FP_TO_SINT. 2584480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT; 2585480093f4SDimitry Andric if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 25860b57cec5SDimitry Andric break; 25870b57cec5SDimitry Andric 25880b57cec5SDimitry Andric // Otherwise, try a larger type. 25890b57cec5SDimitry Andric } 25900b57cec5SDimitry Andric 25910b57cec5SDimitry Andric // Okay, we found the operation and type to use. 2592480093f4SDimitry Andric SDValue Operation; 2593480093f4SDimitry Andric if (IsStrict) { 2594480093f4SDimitry Andric SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other); 2595480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp); 2596480093f4SDimitry Andric } else 2597480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 25980b57cec5SDimitry Andric 25990b57cec5SDimitry Andric // Truncate the result of the extended FP_TO_*INT operation to the desired 26000b57cec5SDimitry Andric // size. 2601480093f4SDimitry Andric SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2602480093f4SDimitry Andric Results.push_back(Trunc); 2603480093f4SDimitry Andric if (IsStrict) 2604480093f4SDimitry Andric Results.push_back(Operation.getValue(1)); 26050b57cec5SDimitry Andric } 26060b57cec5SDimitry Andric 2607e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point 2608e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal 2609e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type. 2610e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node, 2611e8d8bef9SDimitry Andric const SDLoc &dl) { 2612e8d8bef9SDimitry Andric unsigned Opcode = Node->getOpcode(); 2613e8d8bef9SDimitry Andric 2614e8d8bef9SDimitry Andric // Scan for the appropriate larger type to use. 2615e8d8bef9SDimitry Andric EVT NewOutTy = Node->getValueType(0); 2616e8d8bef9SDimitry Andric while (true) { 2617e8d8bef9SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1); 2618e8d8bef9SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2619e8d8bef9SDimitry Andric 2620e8d8bef9SDimitry Andric if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy)) 2621e8d8bef9SDimitry Andric break; 2622e8d8bef9SDimitry Andric } 2623e8d8bef9SDimitry Andric 2624e8d8bef9SDimitry Andric // Saturation width is determined by second operand, so we don't have to 2625e8d8bef9SDimitry Andric // perform any fixup and can directly truncate the result. 2626e8d8bef9SDimitry Andric SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0), 2627e8d8bef9SDimitry Andric Node->getOperand(1)); 2628e8d8bef9SDimitry Andric return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result); 2629e8d8bef9SDimitry Andric } 2630e8d8bef9SDimitry Andric 2631e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation. 2632e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) { 2633e8d8bef9SDimitry Andric EVT VT = Op.getValueType(); 2634e8d8bef9SDimitry Andric EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2635e8d8bef9SDimitry Andric unsigned Sz = VT.getScalarSizeInBits(); 2636e8d8bef9SDimitry Andric 2637e8d8bef9SDimitry Andric // If CTPOP is legal, use it. Otherwise use shifts and xor. 2638e8d8bef9SDimitry Andric SDValue Result; 2639e8d8bef9SDimitry Andric if (TLI.isOperationLegal(ISD::CTPOP, VT)) { 2640e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::CTPOP, dl, VT, Op); 2641e8d8bef9SDimitry Andric } else { 2642e8d8bef9SDimitry Andric Result = Op; 2643e8d8bef9SDimitry Andric for (unsigned i = Log2_32_Ceil(Sz); i != 0;) { 2644e8d8bef9SDimitry Andric SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result, 2645e8d8bef9SDimitry Andric DAG.getConstant(1ULL << (--i), dl, ShVT)); 2646e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift); 2647e8d8bef9SDimitry Andric } 2648e8d8bef9SDimitry Andric } 2649e8d8bef9SDimitry Andric 2650e8d8bef9SDimitry Andric return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT)); 2651e8d8bef9SDimitry Andric } 2652e8d8bef9SDimitry Andric 26530b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { 26540b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to expand node\n"); 26550b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 26560b57cec5SDimitry Andric SDLoc dl(Node); 26570b57cec5SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 26580b57cec5SDimitry Andric bool NeedInvert; 26590b57cec5SDimitry Andric switch (Node->getOpcode()) { 26600b57cec5SDimitry Andric case ISD::ABS: 26610b57cec5SDimitry Andric if (TLI.expandABS(Node, Tmp1, DAG)) 26620b57cec5SDimitry Andric Results.push_back(Tmp1); 26630b57cec5SDimitry Andric break; 26640b57cec5SDimitry Andric case ISD::CTPOP: 26650b57cec5SDimitry Andric if (TLI.expandCTPOP(Node, Tmp1, DAG)) 26660b57cec5SDimitry Andric Results.push_back(Tmp1); 26670b57cec5SDimitry Andric break; 26680b57cec5SDimitry Andric case ISD::CTLZ: 26690b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 26700b57cec5SDimitry Andric if (TLI.expandCTLZ(Node, Tmp1, DAG)) 26710b57cec5SDimitry Andric Results.push_back(Tmp1); 26720b57cec5SDimitry Andric break; 26730b57cec5SDimitry Andric case ISD::CTTZ: 26740b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 26750b57cec5SDimitry Andric if (TLI.expandCTTZ(Node, Tmp1, DAG)) 26760b57cec5SDimitry Andric Results.push_back(Tmp1); 26770b57cec5SDimitry Andric break; 26780b57cec5SDimitry Andric case ISD::BITREVERSE: 2679*fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG))) 2680*fe6060f1SDimitry Andric Results.push_back(Tmp1); 26810b57cec5SDimitry Andric break; 26820b57cec5SDimitry Andric case ISD::BSWAP: 2683*fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBSWAP(Node, DAG))) 2684*fe6060f1SDimitry Andric Results.push_back(Tmp1); 26850b57cec5SDimitry Andric break; 2686e8d8bef9SDimitry Andric case ISD::PARITY: 2687e8d8bef9SDimitry Andric Results.push_back(ExpandPARITY(Node->getOperand(0), dl)); 2688e8d8bef9SDimitry Andric break; 26890b57cec5SDimitry Andric case ISD::FRAMEADDR: 26900b57cec5SDimitry Andric case ISD::RETURNADDR: 26910b57cec5SDimitry Andric case ISD::FRAME_TO_ARGS_OFFSET: 26920b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 26930b57cec5SDimitry Andric break; 26940b57cec5SDimitry Andric case ISD::EH_DWARF_CFA: { 26950b57cec5SDimitry Andric SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl, 26960b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 26970b57cec5SDimitry Andric SDValue Offset = DAG.getNode(ISD::ADD, dl, 26980b57cec5SDimitry Andric CfaArg.getValueType(), 26990b57cec5SDimitry Andric DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, 27000b57cec5SDimitry Andric CfaArg.getValueType()), 27010b57cec5SDimitry Andric CfaArg); 27020b57cec5SDimitry Andric SDValue FA = DAG.getNode( 27030b57cec5SDimitry Andric ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()), 27040b57cec5SDimitry Andric DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()))); 27050b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(), 27060b57cec5SDimitry Andric FA, Offset)); 27070b57cec5SDimitry Andric break; 27080b57cec5SDimitry Andric } 27090b57cec5SDimitry Andric case ISD::FLT_ROUNDS_: 27100b57cec5SDimitry Andric Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0))); 27115ffd83dbSDimitry Andric Results.push_back(Node->getOperand(0)); 27120b57cec5SDimitry Andric break; 27130b57cec5SDimitry Andric case ISD::EH_RETURN: 27140b57cec5SDimitry Andric case ISD::EH_LABEL: 27150b57cec5SDimitry Andric case ISD::PREFETCH: 27160b57cec5SDimitry Andric case ISD::VAEND: 27170b57cec5SDimitry Andric case ISD::EH_SJLJ_LONGJMP: 27180b57cec5SDimitry Andric // If the target didn't expand these, there's nothing to do, so just 27190b57cec5SDimitry Andric // preserve the chain and be done. 27200b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27210b57cec5SDimitry Andric break; 27220b57cec5SDimitry Andric case ISD::READCYCLECOUNTER: 27230b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 27240b57cec5SDimitry Andric // chain. 27250b57cec5SDimitry Andric Results.append(Node->getNumValues() - 1, 27260b57cec5SDimitry Andric DAG.getConstant(0, dl, Node->getValueType(0))); 27270b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27280b57cec5SDimitry Andric break; 27290b57cec5SDimitry Andric case ISD::EH_SJLJ_SETJMP: 27300b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 27310b57cec5SDimitry Andric // chain. 27320b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, MVT::i32)); 27330b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 27340b57cec5SDimitry Andric break; 27350b57cec5SDimitry Andric case ISD::ATOMIC_LOAD: { 27360b57cec5SDimitry Andric // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP. 27370b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0)); 27380b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 27390b57cec5SDimitry Andric SDValue Swap = DAG.getAtomicCmpSwap( 27400b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 27410b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Zero, Zero, 27420b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 27430b57cec5SDimitry Andric Results.push_back(Swap.getValue(0)); 27440b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 27450b57cec5SDimitry Andric break; 27460b57cec5SDimitry Andric } 27470b57cec5SDimitry Andric case ISD::ATOMIC_STORE: { 27480b57cec5SDimitry Andric // There is no libcall for atomic store; fake it with ATOMIC_SWAP. 27490b57cec5SDimitry Andric SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, 27500b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemoryVT(), 27510b57cec5SDimitry Andric Node->getOperand(0), 27520b57cec5SDimitry Andric Node->getOperand(1), Node->getOperand(2), 27530b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 27540b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 27550b57cec5SDimitry Andric break; 27560b57cec5SDimitry Andric } 27570b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 27580b57cec5SDimitry Andric // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and 27590b57cec5SDimitry Andric // splits out the success value as a comparison. Expanding the resulting 27600b57cec5SDimitry Andric // ATOMIC_CMP_SWAP will produce a libcall. 27610b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 27620b57cec5SDimitry Andric SDValue Res = DAG.getAtomicCmpSwap( 27630b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 27640b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Node->getOperand(2), 27650b57cec5SDimitry Andric Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand()); 27660b57cec5SDimitry Andric 27670b57cec5SDimitry Andric SDValue ExtRes = Res; 27680b57cec5SDimitry Andric SDValue LHS = Res; 27690b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 27700b57cec5SDimitry Andric 27710b57cec5SDimitry Andric EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT(); 27720b57cec5SDimitry Andric EVT OuterType = Node->getValueType(0); 27730b57cec5SDimitry Andric switch (TLI.getExtendForAtomicOps()) { 27740b57cec5SDimitry Andric case ISD::SIGN_EXTEND: 27750b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res, 27760b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 27770b57cec5SDimitry Andric RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType, 27780b57cec5SDimitry Andric Node->getOperand(2), DAG.getValueType(AtomicType)); 27790b57cec5SDimitry Andric ExtRes = LHS; 27800b57cec5SDimitry Andric break; 27810b57cec5SDimitry Andric case ISD::ZERO_EXTEND: 27820b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, 27830b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 27840b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 27850b57cec5SDimitry Andric ExtRes = LHS; 27860b57cec5SDimitry Andric break; 27870b57cec5SDimitry Andric case ISD::ANY_EXTEND: 27880b57cec5SDimitry Andric LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType); 27890b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 27900b57cec5SDimitry Andric break; 27910b57cec5SDimitry Andric default: 27920b57cec5SDimitry Andric llvm_unreachable("Invalid atomic op extension"); 27930b57cec5SDimitry Andric } 27940b57cec5SDimitry Andric 27950b57cec5SDimitry Andric SDValue Success = 27960b57cec5SDimitry Andric DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ); 27970b57cec5SDimitry Andric 27980b57cec5SDimitry Andric Results.push_back(ExtRes.getValue(0)); 27990b57cec5SDimitry Andric Results.push_back(Success); 28000b57cec5SDimitry Andric Results.push_back(Res.getValue(1)); 28010b57cec5SDimitry Andric break; 28020b57cec5SDimitry Andric } 28030b57cec5SDimitry Andric case ISD::DYNAMIC_STACKALLOC: 28040b57cec5SDimitry Andric ExpandDYNAMIC_STACKALLOC(Node, Results); 28050b57cec5SDimitry Andric break; 28060b57cec5SDimitry Andric case ISD::MERGE_VALUES: 28070b57cec5SDimitry Andric for (unsigned i = 0; i < Node->getNumValues(); i++) 28080b57cec5SDimitry Andric Results.push_back(Node->getOperand(i)); 28090b57cec5SDimitry Andric break; 28100b57cec5SDimitry Andric case ISD::UNDEF: { 28110b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 28120b57cec5SDimitry Andric if (VT.isInteger()) 28130b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, VT)); 28140b57cec5SDimitry Andric else { 28150b57cec5SDimitry Andric assert(VT.isFloatingPoint() && "Unknown value type!"); 28160b57cec5SDimitry Andric Results.push_back(DAG.getConstantFP(0, dl, VT)); 28170b57cec5SDimitry Andric } 28180b57cec5SDimitry Andric break; 28190b57cec5SDimitry Andric } 28200b57cec5SDimitry Andric case ISD::STRICT_FP_ROUND: 2821480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 2822480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 2823480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 2824480093f4SDimitry Andric break; 2825480093f4SDimitry Andric // We might as well mutate to FP_ROUND when FP_ROUND operation is legal 2826480093f4SDimitry Andric // since this operation is more efficient than stack operation. 28278bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 28288bcb0991SDimitry Andric Node->getValueType(0)) 28298bcb0991SDimitry Andric == TargetLowering::Legal) 28308bcb0991SDimitry Andric break; 2831480093f4SDimitry Andric // We fall back to use stack operation when the FP_ROUND operation 2832480093f4SDimitry Andric // isn't available. 2833e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0), 2834e8d8bef9SDimitry Andric Node->getValueType(0), dl, 2835e8d8bef9SDimitry Andric Node->getOperand(0)))) { 28360b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 28370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n"); 28380b57cec5SDimitry Andric return true; 2839e8d8bef9SDimitry Andric } 2840e8d8bef9SDimitry Andric break; 28410b57cec5SDimitry Andric case ISD::FP_ROUND: 28420b57cec5SDimitry Andric case ISD::BITCAST: 2843e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), 2844e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 28450b57cec5SDimitry Andric Results.push_back(Tmp1); 28460b57cec5SDimitry Andric break; 28470b57cec5SDimitry Andric case ISD::STRICT_FP_EXTEND: 2848480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 2849480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 2850480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 2851480093f4SDimitry Andric break; 2852480093f4SDimitry Andric // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal 2853480093f4SDimitry Andric // since this operation is more efficient than stack operation. 28548bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 28558bcb0991SDimitry Andric Node->getValueType(0)) 28568bcb0991SDimitry Andric == TargetLowering::Legal) 28578bcb0991SDimitry Andric break; 2858480093f4SDimitry Andric // We fall back to use stack operation when the FP_EXTEND operation 2859480093f4SDimitry Andric // isn't available. 2860e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert( 2861e8d8bef9SDimitry Andric Node->getOperand(1), Node->getOperand(1).getValueType(), 2862e8d8bef9SDimitry Andric Node->getValueType(0), dl, Node->getOperand(0)))) { 28630b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 28640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n"); 28650b57cec5SDimitry Andric return true; 2866e8d8bef9SDimitry Andric } 2867e8d8bef9SDimitry Andric break; 28680b57cec5SDimitry Andric case ISD::FP_EXTEND: 2869e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), 28700b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 2871e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 28720b57cec5SDimitry Andric Results.push_back(Tmp1); 28730b57cec5SDimitry Andric break; 28740b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: { 28750b57cec5SDimitry Andric EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 28760b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 28770b57cec5SDimitry Andric 28780b57cec5SDimitry Andric // An in-register sign-extend of a boolean is a negation: 28790b57cec5SDimitry Andric // 'true' (1) sign-extended is -1. 28800b57cec5SDimitry Andric // 'false' (0) sign-extended is 0. 28810b57cec5SDimitry Andric // However, we must mask the high bits of the source operand because the 28820b57cec5SDimitry Andric // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero. 28830b57cec5SDimitry Andric 28840b57cec5SDimitry Andric // TODO: Do this for vectors too? 28850b57cec5SDimitry Andric if (ExtraVT.getSizeInBits() == 1) { 28860b57cec5SDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 28870b57cec5SDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One); 28880b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 28890b57cec5SDimitry Andric SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And); 28900b57cec5SDimitry Andric Results.push_back(Neg); 28910b57cec5SDimitry Andric break; 28920b57cec5SDimitry Andric } 28930b57cec5SDimitry Andric 28940b57cec5SDimitry Andric // NOTE: we could fall back on load/store here too for targets without 28950b57cec5SDimitry Andric // SRA. However, it is doubtful that any exist. 28960b57cec5SDimitry Andric EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 28970b57cec5SDimitry Andric unsigned BitsDiff = VT.getScalarSizeInBits() - 28980b57cec5SDimitry Andric ExtraVT.getScalarSizeInBits(); 28990b57cec5SDimitry Andric SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy); 29000b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 29010b57cec5SDimitry Andric Node->getOperand(0), ShiftCst); 29020b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 29030b57cec5SDimitry Andric Results.push_back(Tmp1); 29040b57cec5SDimitry Andric break; 29050b57cec5SDimitry Andric } 29060b57cec5SDimitry Andric case ISD::UINT_TO_FP: 2907480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 2908480093f4SDimitry Andric if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) { 29090b57cec5SDimitry Andric Results.push_back(Tmp1); 2910480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 2911480093f4SDimitry Andric Results.push_back(Tmp2); 29120b57cec5SDimitry Andric break; 29130b57cec5SDimitry Andric } 29140b57cec5SDimitry Andric LLVM_FALLTHROUGH; 29150b57cec5SDimitry Andric case ISD::SINT_TO_FP: 2916480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 2917e8d8bef9SDimitry Andric if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) { 29180b57cec5SDimitry Andric Results.push_back(Tmp1); 2919480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 2920480093f4SDimitry Andric Results.push_back(Tmp2); 2921e8d8bef9SDimitry Andric } 29220b57cec5SDimitry Andric break; 29230b57cec5SDimitry Andric case ISD::FP_TO_SINT: 29240b57cec5SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) 29250b57cec5SDimitry Andric Results.push_back(Tmp1); 29260b57cec5SDimitry Andric break; 29278bcb0991SDimitry Andric case ISD::STRICT_FP_TO_SINT: 29288bcb0991SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) { 29298bcb0991SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 29308bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n"); 29318bcb0991SDimitry Andric return true; 29328bcb0991SDimitry Andric } 29338bcb0991SDimitry Andric break; 29340b57cec5SDimitry Andric case ISD::FP_TO_UINT: 29358bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) 29360b57cec5SDimitry Andric Results.push_back(Tmp1); 29370b57cec5SDimitry Andric break; 29388bcb0991SDimitry Andric case ISD::STRICT_FP_TO_UINT: 29398bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) { 29408bcb0991SDimitry Andric // Relink the chain. 29418bcb0991SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2); 29428bcb0991SDimitry Andric // Replace the new UINT result. 29438bcb0991SDimitry Andric ReplaceNodeWithValue(SDValue(Node, 0), Tmp1); 29448bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n"); 29458bcb0991SDimitry Andric return true; 29468bcb0991SDimitry Andric } 29470b57cec5SDimitry Andric break; 2948e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 2949e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 2950e8d8bef9SDimitry Andric Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG)); 2951e8d8bef9SDimitry Andric break; 29520b57cec5SDimitry Andric case ISD::VAARG: 29530b57cec5SDimitry Andric Results.push_back(DAG.expandVAArg(Node)); 29540b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 29550b57cec5SDimitry Andric break; 29560b57cec5SDimitry Andric case ISD::VACOPY: 29570b57cec5SDimitry Andric Results.push_back(DAG.expandVACopy(Node)); 29580b57cec5SDimitry Andric break; 29590b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 29600b57cec5SDimitry Andric if (Node->getOperand(0).getValueType().getVectorNumElements() == 1) 29610b57cec5SDimitry Andric // This must be an access of the only element. Return it. 29620b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), 29630b57cec5SDimitry Andric Node->getOperand(0)); 29640b57cec5SDimitry Andric else 29650b57cec5SDimitry Andric Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 29660b57cec5SDimitry Andric Results.push_back(Tmp1); 29670b57cec5SDimitry Andric break; 29680b57cec5SDimitry Andric case ISD::EXTRACT_SUBVECTOR: 29690b57cec5SDimitry Andric Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 29700b57cec5SDimitry Andric break; 29710b57cec5SDimitry Andric case ISD::INSERT_SUBVECTOR: 29720b57cec5SDimitry Andric Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); 29730b57cec5SDimitry Andric break; 29740b57cec5SDimitry Andric case ISD::CONCAT_VECTORS: 29750b57cec5SDimitry Andric Results.push_back(ExpandVectorBuildThroughStack(Node)); 29760b57cec5SDimitry Andric break; 29770b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: 29780b57cec5SDimitry Andric Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 29790b57cec5SDimitry Andric break; 29800b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: 29810b57cec5SDimitry Andric Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 29820b57cec5SDimitry Andric Node->getOperand(1), 29830b57cec5SDimitry Andric Node->getOperand(2), dl)); 29840b57cec5SDimitry Andric break; 29850b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 29860b57cec5SDimitry Andric SmallVector<int, 32> NewMask; 29870b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 29880b57cec5SDimitry Andric 29890b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 29900b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 29910b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 29920b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 29930b57cec5SDimitry Andric if (!TLI.isTypeLegal(EltVT)) { 29940b57cec5SDimitry Andric EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 29950b57cec5SDimitry Andric 29960b57cec5SDimitry Andric // BUILD_VECTOR operands are allowed to be wider than the element type. 29970b57cec5SDimitry Andric // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept 29980b57cec5SDimitry Andric // it. 29990b57cec5SDimitry Andric if (NewEltVT.bitsLT(EltVT)) { 30000b57cec5SDimitry Andric // Convert shuffle node. 30010b57cec5SDimitry Andric // If original node was v4i64 and the new EltVT is i32, 30020b57cec5SDimitry Andric // cast operands to v8i32 and re-build the mask. 30030b57cec5SDimitry Andric 30040b57cec5SDimitry Andric // Calculate new VT, the size of the new VT should be equal to original. 30050b57cec5SDimitry Andric EVT NewVT = 30060b57cec5SDimitry Andric EVT::getVectorVT(*DAG.getContext(), NewEltVT, 30070b57cec5SDimitry Andric VT.getSizeInBits() / NewEltVT.getSizeInBits()); 30080b57cec5SDimitry Andric assert(NewVT.bitsEq(VT)); 30090b57cec5SDimitry Andric 30100b57cec5SDimitry Andric // cast operands to new VT 30110b57cec5SDimitry Andric Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0); 30120b57cec5SDimitry Andric Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1); 30130b57cec5SDimitry Andric 30140b57cec5SDimitry Andric // Convert the shuffle mask 30150b57cec5SDimitry Andric unsigned int factor = 30160b57cec5SDimitry Andric NewVT.getVectorNumElements()/VT.getVectorNumElements(); 30170b57cec5SDimitry Andric 30180b57cec5SDimitry Andric // EltVT gets smaller 30190b57cec5SDimitry Andric assert(factor > 0); 30200b57cec5SDimitry Andric 30210b57cec5SDimitry Andric for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 30220b57cec5SDimitry Andric if (Mask[i] < 0) { 30230b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 30240b57cec5SDimitry Andric NewMask.push_back(Mask[i]); 30250b57cec5SDimitry Andric } 30260b57cec5SDimitry Andric else { 30270b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 30280b57cec5SDimitry Andric NewMask.push_back(Mask[i]*factor+fi); 30290b57cec5SDimitry Andric } 30300b57cec5SDimitry Andric } 30310b57cec5SDimitry Andric Mask = NewMask; 30320b57cec5SDimitry Andric VT = NewVT; 30330b57cec5SDimitry Andric } 30340b57cec5SDimitry Andric EltVT = NewEltVT; 30350b57cec5SDimitry Andric } 30360b57cec5SDimitry Andric unsigned NumElems = VT.getVectorNumElements(); 30370b57cec5SDimitry Andric SmallVector<SDValue, 16> Ops; 30380b57cec5SDimitry Andric for (unsigned i = 0; i != NumElems; ++i) { 30390b57cec5SDimitry Andric if (Mask[i] < 0) { 30400b57cec5SDimitry Andric Ops.push_back(DAG.getUNDEF(EltVT)); 30410b57cec5SDimitry Andric continue; 30420b57cec5SDimitry Andric } 30430b57cec5SDimitry Andric unsigned Idx = Mask[i]; 30440b57cec5SDimitry Andric if (Idx < NumElems) 30455ffd83dbSDimitry Andric Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0, 30465ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx, dl))); 30470b57cec5SDimitry Andric else 30485ffd83dbSDimitry Andric Ops.push_back( 30495ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1, 30505ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx - NumElems, dl))); 30510b57cec5SDimitry Andric } 30520b57cec5SDimitry Andric 30530b57cec5SDimitry Andric Tmp1 = DAG.getBuildVector(VT, dl, Ops); 30540b57cec5SDimitry Andric // We may have changed the BUILD_VECTOR type. Cast it back to the Node type. 30550b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1); 30560b57cec5SDimitry Andric Results.push_back(Tmp1); 30570b57cec5SDimitry Andric break; 30580b57cec5SDimitry Andric } 3059*fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 3060*fe6060f1SDimitry Andric Results.push_back(TLI.expandVectorSplice(Node, DAG)); 3061*fe6060f1SDimitry Andric break; 3062*fe6060f1SDimitry Andric } 30630b57cec5SDimitry Andric case ISD::EXTRACT_ELEMENT: { 30640b57cec5SDimitry Andric EVT OpTy = Node->getOperand(0).getValueType(); 30650b57cec5SDimitry Andric if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) { 30660b57cec5SDimitry Andric // 1 -> Hi 30670b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 30680b57cec5SDimitry Andric DAG.getConstant(OpTy.getSizeInBits() / 2, dl, 30690b57cec5SDimitry Andric TLI.getShiftAmountTy( 30700b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 30710b57cec5SDimitry Andric DAG.getDataLayout()))); 30720b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 30730b57cec5SDimitry Andric } else { 30740b57cec5SDimitry Andric // 0 -> Lo 30750b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 30760b57cec5SDimitry Andric Node->getOperand(0)); 30770b57cec5SDimitry Andric } 30780b57cec5SDimitry Andric Results.push_back(Tmp1); 30790b57cec5SDimitry Andric break; 30800b57cec5SDimitry Andric } 30810b57cec5SDimitry Andric case ISD::STACKSAVE: 30820b57cec5SDimitry Andric // Expand to CopyFromReg if the target set 30830b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3084e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 30850b57cec5SDimitry Andric Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 30860b57cec5SDimitry Andric Node->getValueType(0))); 30870b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 30880b57cec5SDimitry Andric } else { 30890b57cec5SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 30900b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 30910b57cec5SDimitry Andric } 30920b57cec5SDimitry Andric break; 30930b57cec5SDimitry Andric case ISD::STACKRESTORE: 30940b57cec5SDimitry Andric // Expand to CopyToReg if the target set 30950b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3096e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 30970b57cec5SDimitry Andric Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 30980b57cec5SDimitry Andric Node->getOperand(1))); 30990b57cec5SDimitry Andric } else { 31000b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 31010b57cec5SDimitry Andric } 31020b57cec5SDimitry Andric break; 31030b57cec5SDimitry Andric case ISD::GET_DYNAMIC_AREA_OFFSET: 31040b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 31050b57cec5SDimitry Andric Results.push_back(Results[0].getValue(0)); 31060b57cec5SDimitry Andric break; 31070b57cec5SDimitry Andric case ISD::FCOPYSIGN: 31080b57cec5SDimitry Andric Results.push_back(ExpandFCOPYSIGN(Node)); 31090b57cec5SDimitry Andric break; 31100b57cec5SDimitry Andric case ISD::FNEG: 3111e8d8bef9SDimitry Andric Results.push_back(ExpandFNEG(Node)); 31120b57cec5SDimitry Andric break; 31130b57cec5SDimitry Andric case ISD::FABS: 31140b57cec5SDimitry Andric Results.push_back(ExpandFABS(Node)); 31150b57cec5SDimitry Andric break; 31160b57cec5SDimitry Andric case ISD::SMIN: 31170b57cec5SDimitry Andric case ISD::SMAX: 31180b57cec5SDimitry Andric case ISD::UMIN: 31190b57cec5SDimitry Andric case ISD::UMAX: { 31200b57cec5SDimitry Andric // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 31210b57cec5SDimitry Andric ISD::CondCode Pred; 31220b57cec5SDimitry Andric switch (Node->getOpcode()) { 31230b57cec5SDimitry Andric default: llvm_unreachable("How did we get here?"); 31240b57cec5SDimitry Andric case ISD::SMAX: Pred = ISD::SETGT; break; 31250b57cec5SDimitry Andric case ISD::SMIN: Pred = ISD::SETLT; break; 31260b57cec5SDimitry Andric case ISD::UMAX: Pred = ISD::SETUGT; break; 31270b57cec5SDimitry Andric case ISD::UMIN: Pred = ISD::SETULT; break; 31280b57cec5SDimitry Andric } 31290b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 31300b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 31310b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred); 31320b57cec5SDimitry Andric Results.push_back(Tmp1); 31330b57cec5SDimitry Andric break; 31340b57cec5SDimitry Andric } 31350b57cec5SDimitry Andric case ISD::FMINNUM: 31360b57cec5SDimitry Andric case ISD::FMAXNUM: { 31370b57cec5SDimitry Andric if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) 31380b57cec5SDimitry Andric Results.push_back(Expanded); 31390b57cec5SDimitry Andric break; 31400b57cec5SDimitry Andric } 31410b57cec5SDimitry Andric case ISD::FSIN: 31420b57cec5SDimitry Andric case ISD::FCOS: { 31430b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 31440b57cec5SDimitry Andric // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin / 31450b57cec5SDimitry Andric // fcos which share the same operand and both are used. 31460b57cec5SDimitry Andric if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) || 31470b57cec5SDimitry Andric isSinCosLibcallAvailable(Node, TLI)) 31480b57cec5SDimitry Andric && useSinCos(Node)) { 31490b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 31500b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0)); 31510b57cec5SDimitry Andric if (Node->getOpcode() == ISD::FCOS) 31520b57cec5SDimitry Andric Tmp1 = Tmp1.getValue(1); 31530b57cec5SDimitry Andric Results.push_back(Tmp1); 31540b57cec5SDimitry Andric } 31550b57cec5SDimitry Andric break; 31560b57cec5SDimitry Andric } 31570b57cec5SDimitry Andric case ISD::FMAD: 31580b57cec5SDimitry Andric llvm_unreachable("Illegal fmad should never be formed"); 31590b57cec5SDimitry Andric 31600b57cec5SDimitry Andric case ISD::FP16_TO_FP: 31610b57cec5SDimitry Andric if (Node->getValueType(0) != MVT::f32) { 31620b57cec5SDimitry Andric // We can extend to types bigger than f32 in two steps without changing 31630b57cec5SDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 31640b57cec5SDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 31650b57cec5SDimitry Andric SDValue Res = 31660b57cec5SDimitry Andric DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0)); 31670b57cec5SDimitry Andric Results.push_back( 31680b57cec5SDimitry Andric DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res)); 31690b57cec5SDimitry Andric } 31700b57cec5SDimitry Andric break; 31715ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: 31725ffd83dbSDimitry Andric if (Node->getValueType(0) != MVT::f32) { 31735ffd83dbSDimitry Andric // We can extend to types bigger than f32 in two steps without changing 31745ffd83dbSDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 31755ffd83dbSDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 31765ffd83dbSDimitry Andric SDValue Res = 31775ffd83dbSDimitry Andric DAG.getNode(ISD::STRICT_FP16_TO_FP, dl, {MVT::f32, MVT::Other}, 31785ffd83dbSDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 31795ffd83dbSDimitry Andric Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, 31805ffd83dbSDimitry Andric {Node->getValueType(0), MVT::Other}, 31815ffd83dbSDimitry Andric {Res.getValue(1), Res}); 31825ffd83dbSDimitry Andric Results.push_back(Res); 31835ffd83dbSDimitry Andric Results.push_back(Res.getValue(1)); 31845ffd83dbSDimitry Andric } 31855ffd83dbSDimitry Andric break; 31860b57cec5SDimitry Andric case ISD::FP_TO_FP16: 31870b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n"); 31880b57cec5SDimitry Andric if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) { 31890b57cec5SDimitry Andric SDValue Op = Node->getOperand(0); 31900b57cec5SDimitry Andric MVT SVT = Op.getSimpleValueType(); 31910b57cec5SDimitry Andric if ((SVT == MVT::f64 || SVT == MVT::f80) && 31920b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) { 31930b57cec5SDimitry Andric // Under fastmath, we can expand this node into a fround followed by 31940b57cec5SDimitry Andric // a float-half conversion. 31950b57cec5SDimitry Andric SDValue FloatVal = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 31960b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl)); 31970b57cec5SDimitry Andric Results.push_back( 31980b57cec5SDimitry Andric DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal)); 31990b57cec5SDimitry Andric } 32000b57cec5SDimitry Andric } 32010b57cec5SDimitry Andric break; 32020b57cec5SDimitry Andric case ISD::ConstantFP: { 32030b57cec5SDimitry Andric ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 32040b57cec5SDimitry Andric // Check to see if this FP immediate is already legal. 32050b57cec5SDimitry Andric // If this is a legal constant, turn it into a TargetConstantFP node. 32060b57cec5SDimitry Andric if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0), 3207e8d8bef9SDimitry Andric DAG.shouldOptForSize())) 32080b57cec5SDimitry Andric Results.push_back(ExpandConstantFP(CFP, true)); 32090b57cec5SDimitry Andric break; 32100b57cec5SDimitry Andric } 32110b57cec5SDimitry Andric case ISD::Constant: { 32120b57cec5SDimitry Andric ConstantSDNode *CP = cast<ConstantSDNode>(Node); 32130b57cec5SDimitry Andric Results.push_back(ExpandConstant(CP)); 32140b57cec5SDimitry Andric break; 32150b57cec5SDimitry Andric } 32160b57cec5SDimitry Andric case ISD::FSUB: { 32170b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32180b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) && 32190b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) { 32200b57cec5SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 32210b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1)); 32220b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags); 32230b57cec5SDimitry Andric Results.push_back(Tmp1); 32240b57cec5SDimitry Andric } 32250b57cec5SDimitry Andric break; 32260b57cec5SDimitry Andric } 32270b57cec5SDimitry Andric case ISD::SUB: { 32280b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32290b57cec5SDimitry Andric assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 32300b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 32310b57cec5SDimitry Andric "Don't know how to expand this subtraction!"); 32320b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::XOR, dl, VT, Node->getOperand(1), 32330b57cec5SDimitry Andric DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), dl, 32340b57cec5SDimitry Andric VT)); 32350b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT)); 32360b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 32370b57cec5SDimitry Andric break; 32380b57cec5SDimitry Andric } 32390b57cec5SDimitry Andric case ISD::UREM: 32405ffd83dbSDimitry Andric case ISD::SREM: 32415ffd83dbSDimitry Andric if (TLI.expandREM(Node, Tmp1, DAG)) 32420b57cec5SDimitry Andric Results.push_back(Tmp1); 32430b57cec5SDimitry Andric break; 32440b57cec5SDimitry Andric case ISD::UDIV: 32450b57cec5SDimitry Andric case ISD::SDIV: { 32460b57cec5SDimitry Andric bool isSigned = Node->getOpcode() == ISD::SDIV; 32470b57cec5SDimitry Andric unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 32480b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32490b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 32500b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 32510b57cec5SDimitry Andric Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 32520b57cec5SDimitry Andric Node->getOperand(1)); 32530b57cec5SDimitry Andric Results.push_back(Tmp1); 32540b57cec5SDimitry Andric } 32550b57cec5SDimitry Andric break; 32560b57cec5SDimitry Andric } 32570b57cec5SDimitry Andric case ISD::MULHU: 32580b57cec5SDimitry Andric case ISD::MULHS: { 32590b57cec5SDimitry Andric unsigned ExpandOpcode = 32600b57cec5SDimitry Andric Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI; 32610b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32620b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 32630b57cec5SDimitry Andric 32640b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 32650b57cec5SDimitry Andric Node->getOperand(1)); 32660b57cec5SDimitry Andric Results.push_back(Tmp1.getValue(1)); 32670b57cec5SDimitry Andric break; 32680b57cec5SDimitry Andric } 32690b57cec5SDimitry Andric case ISD::UMUL_LOHI: 32700b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 32710b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 32720b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 32730b57cec5SDimitry Andric MVT VT = LHS.getSimpleValueType(); 32740b57cec5SDimitry Andric unsigned MULHOpcode = 32750b57cec5SDimitry Andric Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS; 32760b57cec5SDimitry Andric 32770b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) { 32780b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS)); 32790b57cec5SDimitry Andric Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS)); 32800b57cec5SDimitry Andric break; 32810b57cec5SDimitry Andric } 32820b57cec5SDimitry Andric 32830b57cec5SDimitry Andric SmallVector<SDValue, 4> Halves; 32840b57cec5SDimitry Andric EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext()); 32850b57cec5SDimitry Andric assert(TLI.isTypeLegal(HalfType)); 3286e8d8bef9SDimitry Andric if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves, 32870b57cec5SDimitry Andric HalfType, DAG, 32880b57cec5SDimitry Andric TargetLowering::MulExpansionKind::Always)) { 32890b57cec5SDimitry Andric for (unsigned i = 0; i < 2; ++i) { 32900b57cec5SDimitry Andric SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]); 32910b57cec5SDimitry Andric SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]); 32920b57cec5SDimitry Andric SDValue Shift = DAG.getConstant( 32930b57cec5SDimitry Andric HalfType.getScalarSizeInBits(), dl, 32940b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 32950b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 32960b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 32970b57cec5SDimitry Andric } 32980b57cec5SDimitry Andric break; 32990b57cec5SDimitry Andric } 33000b57cec5SDimitry Andric break; 33010b57cec5SDimitry Andric } 33020b57cec5SDimitry Andric case ISD::MUL: { 33030b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 33040b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 33050b57cec5SDimitry Andric // See if multiply or divide can be lowered using two-result operations. 33060b57cec5SDimitry Andric // We just need the low half of the multiply; try both the signed 33070b57cec5SDimitry Andric // and unsigned forms. If the target supports both SMUL_LOHI and 33080b57cec5SDimitry Andric // UMUL_LOHI, form a preference by checking which forms of plain 33090b57cec5SDimitry Andric // MULH it supports. 33100b57cec5SDimitry Andric bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 33110b57cec5SDimitry Andric bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 33120b57cec5SDimitry Andric bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 33130b57cec5SDimitry Andric bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 33140b57cec5SDimitry Andric unsigned OpToUse = 0; 33150b57cec5SDimitry Andric if (HasSMUL_LOHI && !HasMULHS) { 33160b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 33170b57cec5SDimitry Andric } else if (HasUMUL_LOHI && !HasMULHU) { 33180b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 33190b57cec5SDimitry Andric } else if (HasSMUL_LOHI) { 33200b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 33210b57cec5SDimitry Andric } else if (HasUMUL_LOHI) { 33220b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 33230b57cec5SDimitry Andric } 33240b57cec5SDimitry Andric if (OpToUse) { 33250b57cec5SDimitry Andric Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 33260b57cec5SDimitry Andric Node->getOperand(1))); 33270b57cec5SDimitry Andric break; 33280b57cec5SDimitry Andric } 33290b57cec5SDimitry Andric 33300b57cec5SDimitry Andric SDValue Lo, Hi; 33310b57cec5SDimitry Andric EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext()); 33320b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) && 33330b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) && 33340b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 33350b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::OR, VT) && 33360b57cec5SDimitry Andric TLI.expandMUL(Node, Lo, Hi, HalfType, DAG, 33370b57cec5SDimitry Andric TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) { 33380b57cec5SDimitry Andric Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 33390b57cec5SDimitry Andric Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi); 33400b57cec5SDimitry Andric SDValue Shift = 33410b57cec5SDimitry Andric DAG.getConstant(HalfType.getSizeInBits(), dl, 33420b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 33430b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 33440b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 33450b57cec5SDimitry Andric } 33460b57cec5SDimitry Andric break; 33470b57cec5SDimitry Andric } 33480b57cec5SDimitry Andric case ISD::FSHL: 33490b57cec5SDimitry Andric case ISD::FSHR: 33500b57cec5SDimitry Andric if (TLI.expandFunnelShift(Node, Tmp1, DAG)) 33510b57cec5SDimitry Andric Results.push_back(Tmp1); 33520b57cec5SDimitry Andric break; 33530b57cec5SDimitry Andric case ISD::ROTL: 33540b57cec5SDimitry Andric case ISD::ROTR: 3355e8d8bef9SDimitry Andric if (TLI.expandROT(Node, true /*AllowVectorOps*/, Tmp1, DAG)) 33560b57cec5SDimitry Andric Results.push_back(Tmp1); 33570b57cec5SDimitry Andric break; 33580b57cec5SDimitry Andric case ISD::SADDSAT: 33590b57cec5SDimitry Andric case ISD::UADDSAT: 33600b57cec5SDimitry Andric case ISD::SSUBSAT: 33610b57cec5SDimitry Andric case ISD::USUBSAT: 33620b57cec5SDimitry Andric Results.push_back(TLI.expandAddSubSat(Node, DAG)); 33630b57cec5SDimitry Andric break; 3364e8d8bef9SDimitry Andric case ISD::SSHLSAT: 3365e8d8bef9SDimitry Andric case ISD::USHLSAT: 3366e8d8bef9SDimitry Andric Results.push_back(TLI.expandShlSat(Node, DAG)); 3367e8d8bef9SDimitry Andric break; 33680b57cec5SDimitry Andric case ISD::SMULFIX: 33690b57cec5SDimitry Andric case ISD::SMULFIXSAT: 33700b57cec5SDimitry Andric case ISD::UMULFIX: 33718bcb0991SDimitry Andric case ISD::UMULFIXSAT: 33720b57cec5SDimitry Andric Results.push_back(TLI.expandFixedPointMul(Node, DAG)); 33730b57cec5SDimitry Andric break; 3374480093f4SDimitry Andric case ISD::SDIVFIX: 33755ffd83dbSDimitry Andric case ISD::SDIVFIXSAT: 3376480093f4SDimitry Andric case ISD::UDIVFIX: 33775ffd83dbSDimitry Andric case ISD::UDIVFIXSAT: 3378480093f4SDimitry Andric if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node), 3379480093f4SDimitry Andric Node->getOperand(0), 3380480093f4SDimitry Andric Node->getOperand(1), 3381480093f4SDimitry Andric Node->getConstantOperandVal(2), 3382480093f4SDimitry Andric DAG)) { 3383480093f4SDimitry Andric Results.push_back(V); 3384480093f4SDimitry Andric break; 3385480093f4SDimitry Andric } 3386480093f4SDimitry Andric // FIXME: We might want to retry here with a wider type if we fail, if that 3387480093f4SDimitry Andric // type is legal. 3388480093f4SDimitry Andric // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is 3389480093f4SDimitry Andric // <= 128 (which is the case for all of the default Embedded-C types), 3390480093f4SDimitry Andric // we will only get here with types and scales that we could always expand 3391480093f4SDimitry Andric // if we were allowed to generate libcalls to division functions of illegal 3392480093f4SDimitry Andric // type. But we cannot do that. 3393480093f4SDimitry Andric llvm_unreachable("Cannot expand DIVFIX!"); 33940b57cec5SDimitry Andric case ISD::ADDCARRY: 33950b57cec5SDimitry Andric case ISD::SUBCARRY: { 33960b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 33970b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 33980b57cec5SDimitry Andric SDValue Carry = Node->getOperand(2); 33990b57cec5SDimitry Andric 34000b57cec5SDimitry Andric bool IsAdd = Node->getOpcode() == ISD::ADDCARRY; 34010b57cec5SDimitry Andric 34020b57cec5SDimitry Andric // Initial add of the 2 operands. 34030b57cec5SDimitry Andric unsigned Op = IsAdd ? ISD::ADD : ISD::SUB; 34040b57cec5SDimitry Andric EVT VT = LHS.getValueType(); 34050b57cec5SDimitry Andric SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS); 34060b57cec5SDimitry Andric 34070b57cec5SDimitry Andric // Initial check for overflow. 34080b57cec5SDimitry Andric EVT CarryType = Node->getValueType(1); 34090b57cec5SDimitry Andric EVT SetCCType = getSetCCResultType(Node->getValueType(0)); 34100b57cec5SDimitry Andric ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; 34110b57cec5SDimitry Andric SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC); 34120b57cec5SDimitry Andric 34130b57cec5SDimitry Andric // Add of the sum and the carry. 34145ffd83dbSDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 34150b57cec5SDimitry Andric SDValue CarryExt = 34165ffd83dbSDimitry Andric DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One); 34170b57cec5SDimitry Andric SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt); 34180b57cec5SDimitry Andric 34190b57cec5SDimitry Andric // Second check for overflow. If we are adding, we can only overflow if the 34200b57cec5SDimitry Andric // initial sum is all 1s ang the carry is set, resulting in a new sum of 0. 34210b57cec5SDimitry Andric // If we are subtracting, we can only overflow if the initial sum is 0 and 34220b57cec5SDimitry Andric // the carry is set, resulting in a new sum of all 1s. 34230b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 34240b57cec5SDimitry Andric SDValue Overflow2 = 34250b57cec5SDimitry Andric IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ) 34260b57cec5SDimitry Andric : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ); 34270b57cec5SDimitry Andric Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2, 34280b57cec5SDimitry Andric DAG.getZExtOrTrunc(Carry, dl, SetCCType)); 34290b57cec5SDimitry Andric 34300b57cec5SDimitry Andric SDValue ResultCarry = 34310b57cec5SDimitry Andric DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2); 34320b57cec5SDimitry Andric 34330b57cec5SDimitry Andric Results.push_back(Sum2); 34340b57cec5SDimitry Andric Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT)); 34350b57cec5SDimitry Andric break; 34360b57cec5SDimitry Andric } 34370b57cec5SDimitry Andric case ISD::SADDO: 34380b57cec5SDimitry Andric case ISD::SSUBO: { 34390b57cec5SDimitry Andric SDValue Result, Overflow; 34400b57cec5SDimitry Andric TLI.expandSADDSUBO(Node, Result, Overflow, DAG); 34410b57cec5SDimitry Andric Results.push_back(Result); 34420b57cec5SDimitry Andric Results.push_back(Overflow); 34430b57cec5SDimitry Andric break; 34440b57cec5SDimitry Andric } 34450b57cec5SDimitry Andric case ISD::UADDO: 34460b57cec5SDimitry Andric case ISD::USUBO: { 34470b57cec5SDimitry Andric SDValue Result, Overflow; 34480b57cec5SDimitry Andric TLI.expandUADDSUBO(Node, Result, Overflow, DAG); 34490b57cec5SDimitry Andric Results.push_back(Result); 34500b57cec5SDimitry Andric Results.push_back(Overflow); 34510b57cec5SDimitry Andric break; 34520b57cec5SDimitry Andric } 34530b57cec5SDimitry Andric case ISD::UMULO: 34540b57cec5SDimitry Andric case ISD::SMULO: { 34550b57cec5SDimitry Andric SDValue Result, Overflow; 34560b57cec5SDimitry Andric if (TLI.expandMULO(Node, Result, Overflow, DAG)) { 34570b57cec5SDimitry Andric Results.push_back(Result); 34580b57cec5SDimitry Andric Results.push_back(Overflow); 34590b57cec5SDimitry Andric } 34600b57cec5SDimitry Andric break; 34610b57cec5SDimitry Andric } 34620b57cec5SDimitry Andric case ISD::BUILD_PAIR: { 34630b57cec5SDimitry Andric EVT PairTy = Node->getValueType(0); 34640b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 34650b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 34660b57cec5SDimitry Andric Tmp2 = DAG.getNode( 34670b57cec5SDimitry Andric ISD::SHL, dl, PairTy, Tmp2, 34680b57cec5SDimitry Andric DAG.getConstant(PairTy.getSizeInBits() / 2, dl, 34690b57cec5SDimitry Andric TLI.getShiftAmountTy(PairTy, DAG.getDataLayout()))); 34700b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 34710b57cec5SDimitry Andric break; 34720b57cec5SDimitry Andric } 34730b57cec5SDimitry Andric case ISD::SELECT: 34740b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 34750b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 34760b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); 34770b57cec5SDimitry Andric if (Tmp1.getOpcode() == ISD::SETCC) { 34780b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 34790b57cec5SDimitry Andric Tmp2, Tmp3, 34800b57cec5SDimitry Andric cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 34810b57cec5SDimitry Andric } else { 34820b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, 34830b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp1.getValueType()), 34840b57cec5SDimitry Andric Tmp2, Tmp3, ISD::SETNE); 34850b57cec5SDimitry Andric } 34860b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 34870b57cec5SDimitry Andric Results.push_back(Tmp1); 34880b57cec5SDimitry Andric break; 34890b57cec5SDimitry Andric case ISD::BR_JT: { 34900b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); 34910b57cec5SDimitry Andric SDValue Table = Node->getOperand(1); 34920b57cec5SDimitry Andric SDValue Index = Node->getOperand(2); 34930b57cec5SDimitry Andric 34940b57cec5SDimitry Andric const DataLayout &TD = DAG.getDataLayout(); 34950b57cec5SDimitry Andric EVT PTy = TLI.getPointerTy(TD); 34960b57cec5SDimitry Andric 34970b57cec5SDimitry Andric unsigned EntrySize = 34980b57cec5SDimitry Andric DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 34990b57cec5SDimitry Andric 35000b57cec5SDimitry Andric // For power-of-two jumptable entry sizes convert multiplication to a shift. 35010b57cec5SDimitry Andric // This transformation needs to be done here since otherwise the MIPS 35020b57cec5SDimitry Andric // backend will end up emitting a three instruction multiply sequence 35030b57cec5SDimitry Andric // instead of a single shift and MSP430 will call a runtime function. 35040b57cec5SDimitry Andric if (llvm::isPowerOf2_32(EntrySize)) 35050b57cec5SDimitry Andric Index = DAG.getNode( 35060b57cec5SDimitry Andric ISD::SHL, dl, Index.getValueType(), Index, 35070b57cec5SDimitry Andric DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType())); 35080b57cec5SDimitry Andric else 35090b57cec5SDimitry Andric Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index, 35100b57cec5SDimitry Andric DAG.getConstant(EntrySize, dl, Index.getValueType())); 35110b57cec5SDimitry Andric SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(), 35120b57cec5SDimitry Andric Index, Table); 35130b57cec5SDimitry Andric 35140b57cec5SDimitry Andric EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 35150b57cec5SDimitry Andric SDValue LD = DAG.getExtLoad( 35160b57cec5SDimitry Andric ISD::SEXTLOAD, dl, PTy, Chain, Addr, 35170b57cec5SDimitry Andric MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT); 35180b57cec5SDimitry Andric Addr = LD; 35190b57cec5SDimitry Andric if (TLI.isJumpTableRelative()) { 35200b57cec5SDimitry Andric // For PIC, the sequence is: 35210b57cec5SDimitry Andric // BRIND(load(Jumptable + index) + RelocBase) 35220b57cec5SDimitry Andric // RelocBase can be JumpTable, GOT or some sort of global base. 35230b57cec5SDimitry Andric Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 35240b57cec5SDimitry Andric TLI.getPICJumpTableRelocBase(Table, DAG)); 35250b57cec5SDimitry Andric } 35260b57cec5SDimitry Andric 35270b57cec5SDimitry Andric Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, DAG); 35280b57cec5SDimitry Andric Results.push_back(Tmp1); 35290b57cec5SDimitry Andric break; 35300b57cec5SDimitry Andric } 35310b57cec5SDimitry Andric case ISD::BRCOND: 35320b57cec5SDimitry Andric // Expand brcond's setcc into its constituent parts and create a BR_CC 35330b57cec5SDimitry Andric // Node. 35340b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 35350b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 35360b57cec5SDimitry Andric if (Tmp2.getOpcode() == ISD::SETCC) { 35370b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, 35380b57cec5SDimitry Andric Tmp1, Tmp2.getOperand(2), 35390b57cec5SDimitry Andric Tmp2.getOperand(0), Tmp2.getOperand(1), 35400b57cec5SDimitry Andric Node->getOperand(2)); 35410b57cec5SDimitry Andric } else { 35420b57cec5SDimitry Andric // We test only the i1 bit. Skip the AND if UNDEF or another AND. 35430b57cec5SDimitry Andric if (Tmp2.isUndef() || 35440b57cec5SDimitry Andric (Tmp2.getOpcode() == ISD::AND && 35450b57cec5SDimitry Andric isa<ConstantSDNode>(Tmp2.getOperand(1)) && 35460b57cec5SDimitry Andric cast<ConstantSDNode>(Tmp2.getOperand(1))->getZExtValue() == 1)) 35470b57cec5SDimitry Andric Tmp3 = Tmp2; 35480b57cec5SDimitry Andric else 35490b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, 35500b57cec5SDimitry Andric DAG.getConstant(1, dl, Tmp2.getValueType())); 35510b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 35520b57cec5SDimitry Andric DAG.getCondCode(ISD::SETNE), Tmp3, 35530b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp3.getValueType()), 35540b57cec5SDimitry Andric Node->getOperand(2)); 35550b57cec5SDimitry Andric } 35560b57cec5SDimitry Andric Results.push_back(Tmp1); 35570b57cec5SDimitry Andric break; 3558480093f4SDimitry Andric case ISD::SETCC: 3559480093f4SDimitry Andric case ISD::STRICT_FSETCC: 3560480093f4SDimitry Andric case ISD::STRICT_FSETCCS: { 3561480093f4SDimitry Andric bool IsStrict = Node->getOpcode() != ISD::SETCC; 3562480093f4SDimitry Andric bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS; 3563480093f4SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 3564480093f4SDimitry Andric unsigned Offset = IsStrict ? 1 : 0; 3565480093f4SDimitry Andric Tmp1 = Node->getOperand(0 + Offset); 3566480093f4SDimitry Andric Tmp2 = Node->getOperand(1 + Offset); 3567480093f4SDimitry Andric Tmp3 = Node->getOperand(2 + Offset); 3568480093f4SDimitry Andric bool Legalized = 3569*fe6060f1SDimitry Andric TLI.LegalizeSetCCCondCode(DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, 3570480093f4SDimitry Andric NeedInvert, dl, Chain, IsSignaling); 35710b57cec5SDimitry Andric 35720b57cec5SDimitry Andric if (Legalized) { 35730b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 35740b57cec5SDimitry Andric // condition code, create a new SETCC node. 35750b57cec5SDimitry Andric if (Tmp3.getNode()) 35760b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), 35770b57cec5SDimitry Andric Tmp1, Tmp2, Tmp3, Node->getFlags()); 35780b57cec5SDimitry Andric 35790b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then wrap 35800b57cec5SDimitry Andric // the existing SETCC in a NOT to restore the intended condition. 35810b57cec5SDimitry Andric if (NeedInvert) 35820b57cec5SDimitry Andric Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0)); 35830b57cec5SDimitry Andric 35840b57cec5SDimitry Andric Results.push_back(Tmp1); 3585480093f4SDimitry Andric if (IsStrict) 3586480093f4SDimitry Andric Results.push_back(Chain); 3587480093f4SDimitry Andric 35880b57cec5SDimitry Andric break; 35890b57cec5SDimitry Andric } 35900b57cec5SDimitry Andric 3591480093f4SDimitry Andric // FIXME: It seems Legalized is false iff CCCode is Legal. I don't 3592480093f4SDimitry Andric // understand if this code is useful for strict nodes. 3593480093f4SDimitry Andric assert(!IsStrict && "Don't know how to expand for strict nodes."); 3594480093f4SDimitry Andric 35950b57cec5SDimitry Andric // Otherwise, SETCC for the given comparison type must be completely 35960b57cec5SDimitry Andric // illegal; expand it into a SELECT_CC. 35970b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 35980b57cec5SDimitry Andric int TrueValue; 35990b57cec5SDimitry Andric switch (TLI.getBooleanContents(Tmp1.getValueType())) { 36000b57cec5SDimitry Andric case TargetLowering::ZeroOrOneBooleanContent: 36010b57cec5SDimitry Andric case TargetLowering::UndefinedBooleanContent: 36020b57cec5SDimitry Andric TrueValue = 1; 36030b57cec5SDimitry Andric break; 36040b57cec5SDimitry Andric case TargetLowering::ZeroOrNegativeOneBooleanContent: 36050b57cec5SDimitry Andric TrueValue = -1; 36060b57cec5SDimitry Andric break; 36070b57cec5SDimitry Andric } 36080b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 36090b57cec5SDimitry Andric DAG.getConstant(TrueValue, dl, VT), 36100b57cec5SDimitry Andric DAG.getConstant(0, dl, VT), 36110b57cec5SDimitry Andric Tmp3); 36120b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36130b57cec5SDimitry Andric Results.push_back(Tmp1); 36140b57cec5SDimitry Andric break; 36150b57cec5SDimitry Andric } 36160b57cec5SDimitry Andric case ISD::SELECT_CC: { 3617480093f4SDimitry Andric // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS 36180b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // LHS 36190b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); // RHS 36200b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); // True 36210b57cec5SDimitry Andric Tmp4 = Node->getOperand(3); // False 36220b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 3623480093f4SDimitry Andric SDValue Chain; 36240b57cec5SDimitry Andric SDValue CC = Node->getOperand(4); 36250b57cec5SDimitry Andric ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get(); 36260b57cec5SDimitry Andric 36270b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) { 36280b57cec5SDimitry Andric // If the condition code is legal, then we need to expand this 36290b57cec5SDimitry Andric // node using SETCC and SELECT. 36300b57cec5SDimitry Andric EVT CmpVT = Tmp1.getValueType(); 36310b57cec5SDimitry Andric assert(!TLI.isOperationExpand(ISD::SELECT, VT) && 36320b57cec5SDimitry Andric "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be " 36330b57cec5SDimitry Andric "expanded."); 36340b57cec5SDimitry Andric EVT CCVT = getSetCCResultType(CmpVT); 36350b57cec5SDimitry Andric SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags()); 36360b57cec5SDimitry Andric Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4)); 36370b57cec5SDimitry Andric break; 36380b57cec5SDimitry Andric } 36390b57cec5SDimitry Andric 36400b57cec5SDimitry Andric // SELECT_CC is legal, so the condition code must not be. 36410b57cec5SDimitry Andric bool Legalized = false; 36420b57cec5SDimitry Andric // Try to legalize by inverting the condition. This is for targets that 36430b57cec5SDimitry Andric // might support an ordered version of a condition, but not the unordered 36440b57cec5SDimitry Andric // version (or vice versa). 3645480093f4SDimitry Andric ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType()); 36460b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) { 36470b57cec5SDimitry Andric // Use the new condition code and swap true and false 36480b57cec5SDimitry Andric Legalized = true; 36490b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC); 36500b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36510b57cec5SDimitry Andric } else { 36520b57cec5SDimitry Andric // If The inverse is not legal, then try to swap the arguments using 36530b57cec5SDimitry Andric // the inverse condition code. 36540b57cec5SDimitry Andric ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC); 36550b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) { 36560b57cec5SDimitry Andric // The swapped inverse condition is legal, so swap true and false, 36570b57cec5SDimitry Andric // lhs and rhs. 36580b57cec5SDimitry Andric Legalized = true; 36590b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC); 36600b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36610b57cec5SDimitry Andric } 36620b57cec5SDimitry Andric } 36630b57cec5SDimitry Andric 36640b57cec5SDimitry Andric if (!Legalized) { 3665*fe6060f1SDimitry Andric Legalized = TLI.LegalizeSetCCCondCode( 3666*fe6060f1SDimitry Andric DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, 3667*fe6060f1SDimitry Andric NeedInvert, dl, Chain); 36680b57cec5SDimitry Andric 36690b57cec5SDimitry Andric assert(Legalized && "Can't legalize SELECT_CC with legal condition!"); 36700b57cec5SDimitry Andric 36710b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then swap 36720b57cec5SDimitry Andric // the True/False operands to match. 36730b57cec5SDimitry Andric if (NeedInvert) 36740b57cec5SDimitry Andric std::swap(Tmp3, Tmp4); 36750b57cec5SDimitry Andric 36760b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 36770b57cec5SDimitry Andric // condition code, create a new SELECT_CC node. 36780b57cec5SDimitry Andric if (CC.getNode()) { 36790b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), 36800b57cec5SDimitry Andric Tmp1, Tmp2, Tmp3, Tmp4, CC); 36810b57cec5SDimitry Andric } else { 36820b57cec5SDimitry Andric Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType()); 36830b57cec5SDimitry Andric CC = DAG.getCondCode(ISD::SETNE); 36840b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, 36850b57cec5SDimitry Andric Tmp2, Tmp3, Tmp4, CC); 36860b57cec5SDimitry Andric } 36870b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 36880b57cec5SDimitry Andric } 36890b57cec5SDimitry Andric Results.push_back(Tmp1); 36900b57cec5SDimitry Andric break; 36910b57cec5SDimitry Andric } 36920b57cec5SDimitry Andric case ISD::BR_CC: { 3693480093f4SDimitry Andric // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS 3694480093f4SDimitry Andric SDValue Chain; 36950b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // Chain 36960b57cec5SDimitry Andric Tmp2 = Node->getOperand(2); // LHS 36970b57cec5SDimitry Andric Tmp3 = Node->getOperand(3); // RHS 36980b57cec5SDimitry Andric Tmp4 = Node->getOperand(1); // CC 36990b57cec5SDimitry Andric 3700480093f4SDimitry Andric bool Legalized = 3701*fe6060f1SDimitry Andric TLI.LegalizeSetCCCondCode(DAG, getSetCCResultType(Tmp2.getValueType()), 3702*fe6060f1SDimitry Andric Tmp2, Tmp3, Tmp4, NeedInvert, dl, Chain); 37030b57cec5SDimitry Andric (void)Legalized; 37040b57cec5SDimitry Andric assert(Legalized && "Can't legalize BR_CC with legal condition!"); 37050b57cec5SDimitry Andric 37060b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC 37070b57cec5SDimitry Andric // node. 37080b57cec5SDimitry Andric if (Tmp4.getNode()) { 3709e8d8bef9SDimitry Andric assert(!NeedInvert && "Don't know how to invert BR_CC!"); 3710e8d8bef9SDimitry Andric 37110b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, 37120b57cec5SDimitry Andric Tmp4, Tmp2, Tmp3, Node->getOperand(4)); 37130b57cec5SDimitry Andric } else { 37140b57cec5SDimitry Andric Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType()); 3715e8d8bef9SDimitry Andric Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE); 37160b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, 37170b57cec5SDimitry Andric Tmp2, Tmp3, Node->getOperand(4)); 37180b57cec5SDimitry Andric } 37190b57cec5SDimitry Andric Results.push_back(Tmp1); 37200b57cec5SDimitry Andric break; 37210b57cec5SDimitry Andric } 37220b57cec5SDimitry Andric case ISD::BUILD_VECTOR: 37230b57cec5SDimitry Andric Results.push_back(ExpandBUILD_VECTOR(Node)); 37240b57cec5SDimitry Andric break; 37258bcb0991SDimitry Andric case ISD::SPLAT_VECTOR: 37268bcb0991SDimitry Andric Results.push_back(ExpandSPLAT_VECTOR(Node)); 37278bcb0991SDimitry Andric break; 37280b57cec5SDimitry Andric case ISD::SRA: 37290b57cec5SDimitry Andric case ISD::SRL: 37300b57cec5SDimitry Andric case ISD::SHL: { 37310b57cec5SDimitry Andric // Scalarize vector SRA/SRL/SHL. 37320b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 37330b57cec5SDimitry Andric assert(VT.isVector() && "Unable to legalize non-vector shift"); 37340b57cec5SDimitry Andric assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal"); 37350b57cec5SDimitry Andric unsigned NumElem = VT.getVectorNumElements(); 37360b57cec5SDimitry Andric 37370b57cec5SDimitry Andric SmallVector<SDValue, 8> Scalars; 37380b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < NumElem; Idx++) { 37395ffd83dbSDimitry Andric SDValue Ex = 37405ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 37415ffd83dbSDimitry Andric Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl)); 37425ffd83dbSDimitry Andric SDValue Sh = 37435ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 37445ffd83dbSDimitry Andric Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl)); 37450b57cec5SDimitry Andric Scalars.push_back(DAG.getNode(Node->getOpcode(), dl, 37460b57cec5SDimitry Andric VT.getScalarType(), Ex, Sh)); 37470b57cec5SDimitry Andric } 37480b57cec5SDimitry Andric 37490b57cec5SDimitry Andric SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars); 3750480093f4SDimitry Andric Results.push_back(Result); 37510b57cec5SDimitry Andric break; 37520b57cec5SDimitry Andric } 37530b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 37540b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 37550b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 37560b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 37570b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 37580b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 37590b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 37600b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 37610b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 37620b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 37630b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 37640b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 37650b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 37660b57cec5SDimitry Andric Results.push_back(TLI.expandVecReduce(Node, DAG)); 37670b57cec5SDimitry Andric break; 37680b57cec5SDimitry Andric case ISD::GLOBAL_OFFSET_TABLE: 37690b57cec5SDimitry Andric case ISD::GlobalAddress: 37700b57cec5SDimitry Andric case ISD::GlobalTLSAddress: 37710b57cec5SDimitry Andric case ISD::ExternalSymbol: 37720b57cec5SDimitry Andric case ISD::ConstantPool: 37730b57cec5SDimitry Andric case ISD::JumpTable: 37740b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 37750b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 37760b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 37770b57cec5SDimitry Andric // FIXME: Custom lowering for these operations shouldn't return null! 3778480093f4SDimitry Andric // Return true so that we don't call ConvertNodeToLibcall which also won't 3779480093f4SDimitry Andric // do anything. 3780480093f4SDimitry Andric return true; 37810b57cec5SDimitry Andric } 37820b57cec5SDimitry Andric 3783480093f4SDimitry Andric if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) { 37848bcb0991SDimitry Andric // FIXME: We were asked to expand a strict floating-point operation, 37858bcb0991SDimitry Andric // but there is currently no expansion implemented that would preserve 37868bcb0991SDimitry Andric // the "strict" properties. For now, we just fall back to the non-strict 37878bcb0991SDimitry Andric // version if that is legal on the target. The actual mutation of the 37888bcb0991SDimitry Andric // operation will happen in SelectionDAGISel::DoInstructionSelection. 37898bcb0991SDimitry Andric switch (Node->getOpcode()) { 37908bcb0991SDimitry Andric default: 37918bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 37928bcb0991SDimitry Andric Node->getValueType(0)) 37938bcb0991SDimitry Andric == TargetLowering::Legal) 37948bcb0991SDimitry Andric return true; 37958bcb0991SDimitry Andric break; 3796e8d8bef9SDimitry Andric case ISD::STRICT_FSUB: { 3797e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 3798e8d8bef9SDimitry Andric ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal) 3799e8d8bef9SDimitry Andric return true; 3800e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 3801e8d8bef9SDimitry Andric ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal) 3802e8d8bef9SDimitry Andric break; 3803e8d8bef9SDimitry Andric 3804e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 3805e8d8bef9SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 3806e8d8bef9SDimitry Andric SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags); 3807e8d8bef9SDimitry Andric SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(), 3808e8d8bef9SDimitry Andric {Node->getOperand(0), Node->getOperand(1), Neg}, 3809e8d8bef9SDimitry Andric Flags); 3810e8d8bef9SDimitry Andric 3811e8d8bef9SDimitry Andric Results.push_back(Fadd); 3812e8d8bef9SDimitry Andric Results.push_back(Fadd.getValue(1)); 3813e8d8bef9SDimitry Andric break; 3814e8d8bef9SDimitry Andric } 3815e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 3816e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 38178bcb0991SDimitry Andric case ISD::STRICT_LRINT: 38188bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 38198bcb0991SDimitry Andric case ISD::STRICT_LROUND: 38208bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 38218bcb0991SDimitry Andric // These are registered by the operand type instead of the value 38228bcb0991SDimitry Andric // type. Reflect that here. 38238bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 38248bcb0991SDimitry Andric Node->getOperand(1).getValueType()) 38258bcb0991SDimitry Andric == TargetLowering::Legal) 38268bcb0991SDimitry Andric return true; 38278bcb0991SDimitry Andric break; 38288bcb0991SDimitry Andric } 38298bcb0991SDimitry Andric } 38308bcb0991SDimitry Andric 38310b57cec5SDimitry Andric // Replace the original node with the legalized result. 38320b57cec5SDimitry Andric if (Results.empty()) { 38330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Cannot expand node\n"); 38340b57cec5SDimitry Andric return false; 38350b57cec5SDimitry Andric } 38360b57cec5SDimitry Andric 38370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded node\n"); 38380b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 38390b57cec5SDimitry Andric return true; 38400b57cec5SDimitry Andric } 38410b57cec5SDimitry Andric 38420b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) { 38430b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n"); 38440b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 38450b57cec5SDimitry Andric SDLoc dl(Node); 38460b57cec5SDimitry Andric // FIXME: Check flags on the node to see if we can use a finite call. 38470b57cec5SDimitry Andric unsigned Opc = Node->getOpcode(); 38480b57cec5SDimitry Andric switch (Opc) { 38490b57cec5SDimitry Andric case ISD::ATOMIC_FENCE: { 38500b57cec5SDimitry Andric // If the target didn't lower this, lower it to '__sync_synchronize()' call 38510b57cec5SDimitry Andric // FIXME: handle "fence singlethread" more efficiently. 38520b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 38530b57cec5SDimitry Andric 38540b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 38550b57cec5SDimitry Andric CLI.setDebugLoc(dl) 38560b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 38570b57cec5SDimitry Andric .setLibCallee( 38580b57cec5SDimitry Andric CallingConv::C, Type::getVoidTy(*DAG.getContext()), 38590b57cec5SDimitry Andric DAG.getExternalSymbol("__sync_synchronize", 38600b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())), 38610b57cec5SDimitry Andric std::move(Args)); 38620b57cec5SDimitry Andric 38630b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 38640b57cec5SDimitry Andric 38650b57cec5SDimitry Andric Results.push_back(CallResult.second); 38660b57cec5SDimitry Andric break; 38670b57cec5SDimitry Andric } 38680b57cec5SDimitry Andric // By default, atomic intrinsics are marked Legal and lowered. Targets 38690b57cec5SDimitry Andric // which don't support them directly, however, may want libcalls, in which 38700b57cec5SDimitry Andric // case they mark them Expand, and we get here. 38710b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: 38720b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_ADD: 38730b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_SUB: 38740b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_AND: 38750b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_CLR: 38760b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_OR: 38770b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_XOR: 38780b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_NAND: 38790b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MIN: 38800b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MAX: 38810b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMIN: 38820b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMAX: 38830b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP: { 38840b57cec5SDimitry Andric MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 3885*fe6060f1SDimitry Andric AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering(); 3886e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT); 3887480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 3888480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 3889e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops; 3890e8d8bef9SDimitry Andric if (TLI.getLibcallName(LC)) { 3891e8d8bef9SDimitry Andric // If outline atomic available, prepare its arguments and expand. 3892e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 2, Node->op_end()); 3893e8d8bef9SDimitry Andric Ops.push_back(Node->getOperand(1)); 3894e8d8bef9SDimitry Andric 3895e8d8bef9SDimitry Andric } else { 3896e8d8bef9SDimitry Andric LC = RTLIB::getSYNC(Opc, VT); 3897e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && 3898e8d8bef9SDimitry Andric "Unexpected atomic op or value type!"); 3899e8d8bef9SDimitry Andric // Arguments for expansion to sync libcall 3900e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 1, Node->op_end()); 3901e8d8bef9SDimitry Andric } 3902480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 3903480093f4SDimitry Andric Ops, CallOptions, 3904480093f4SDimitry Andric SDLoc(Node), 3905480093f4SDimitry Andric Node->getOperand(0)); 39060b57cec5SDimitry Andric Results.push_back(Tmp.first); 39070b57cec5SDimitry Andric Results.push_back(Tmp.second); 39080b57cec5SDimitry Andric break; 39090b57cec5SDimitry Andric } 39100b57cec5SDimitry Andric case ISD::TRAP: { 39110b57cec5SDimitry Andric // If this operation is not supported, lower it to 'abort()' call 39120b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 39130b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 39140b57cec5SDimitry Andric CLI.setDebugLoc(dl) 39150b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 39160b57cec5SDimitry Andric .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 39170b57cec5SDimitry Andric DAG.getExternalSymbol( 39180b57cec5SDimitry Andric "abort", TLI.getPointerTy(DAG.getDataLayout())), 39190b57cec5SDimitry Andric std::move(Args)); 39200b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 39210b57cec5SDimitry Andric 39220b57cec5SDimitry Andric Results.push_back(CallResult.second); 39230b57cec5SDimitry Andric break; 39240b57cec5SDimitry Andric } 39250b57cec5SDimitry Andric case ISD::FMINNUM: 39260b57cec5SDimitry Andric case ISD::STRICT_FMINNUM: 3927480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64, 39280b57cec5SDimitry Andric RTLIB::FMIN_F80, RTLIB::FMIN_F128, 3929480093f4SDimitry Andric RTLIB::FMIN_PPCF128, Results); 39300b57cec5SDimitry Andric break; 39310b57cec5SDimitry Andric case ISD::FMAXNUM: 39320b57cec5SDimitry Andric case ISD::STRICT_FMAXNUM: 3933480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64, 39340b57cec5SDimitry Andric RTLIB::FMAX_F80, RTLIB::FMAX_F128, 3935480093f4SDimitry Andric RTLIB::FMAX_PPCF128, Results); 39360b57cec5SDimitry Andric break; 39370b57cec5SDimitry Andric case ISD::FSQRT: 39380b57cec5SDimitry Andric case ISD::STRICT_FSQRT: 3939480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 39400b57cec5SDimitry Andric RTLIB::SQRT_F80, RTLIB::SQRT_F128, 3941480093f4SDimitry Andric RTLIB::SQRT_PPCF128, Results); 39420b57cec5SDimitry Andric break; 39430b57cec5SDimitry Andric case ISD::FCBRT: 3944480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64, 39450b57cec5SDimitry Andric RTLIB::CBRT_F80, RTLIB::CBRT_F128, 3946480093f4SDimitry Andric RTLIB::CBRT_PPCF128, Results); 39470b57cec5SDimitry Andric break; 39480b57cec5SDimitry Andric case ISD::FSIN: 39490b57cec5SDimitry Andric case ISD::STRICT_FSIN: 3950480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 39510b57cec5SDimitry Andric RTLIB::SIN_F80, RTLIB::SIN_F128, 3952480093f4SDimitry Andric RTLIB::SIN_PPCF128, Results); 39530b57cec5SDimitry Andric break; 39540b57cec5SDimitry Andric case ISD::FCOS: 39550b57cec5SDimitry Andric case ISD::STRICT_FCOS: 3956480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 39570b57cec5SDimitry Andric RTLIB::COS_F80, RTLIB::COS_F128, 3958480093f4SDimitry Andric RTLIB::COS_PPCF128, Results); 39590b57cec5SDimitry Andric break; 39600b57cec5SDimitry Andric case ISD::FSINCOS: 39610b57cec5SDimitry Andric // Expand into sincos libcall. 39620b57cec5SDimitry Andric ExpandSinCosLibCall(Node, Results); 39630b57cec5SDimitry Andric break; 39640b57cec5SDimitry Andric case ISD::FLOG: 39650b57cec5SDimitry Andric case ISD::STRICT_FLOG: 39668c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80, 39678c27c554SDimitry Andric RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results); 39680b57cec5SDimitry Andric break; 39690b57cec5SDimitry Andric case ISD::FLOG2: 39700b57cec5SDimitry Andric case ISD::STRICT_FLOG2: 39718c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80, 39728c27c554SDimitry Andric RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results); 39730b57cec5SDimitry Andric break; 39740b57cec5SDimitry Andric case ISD::FLOG10: 39750b57cec5SDimitry Andric case ISD::STRICT_FLOG10: 39768c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80, 39778c27c554SDimitry Andric RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results); 39780b57cec5SDimitry Andric break; 39790b57cec5SDimitry Andric case ISD::FEXP: 39800b57cec5SDimitry Andric case ISD::STRICT_FEXP: 39818c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80, 39828c27c554SDimitry Andric RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results); 39830b57cec5SDimitry Andric break; 39840b57cec5SDimitry Andric case ISD::FEXP2: 39850b57cec5SDimitry Andric case ISD::STRICT_FEXP2: 39868c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80, 39878c27c554SDimitry Andric RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results); 39880b57cec5SDimitry Andric break; 39890b57cec5SDimitry Andric case ISD::FTRUNC: 39900b57cec5SDimitry Andric case ISD::STRICT_FTRUNC: 3991480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 39920b57cec5SDimitry Andric RTLIB::TRUNC_F80, RTLIB::TRUNC_F128, 3993480093f4SDimitry Andric RTLIB::TRUNC_PPCF128, Results); 39940b57cec5SDimitry Andric break; 39950b57cec5SDimitry Andric case ISD::FFLOOR: 39960b57cec5SDimitry Andric case ISD::STRICT_FFLOOR: 3997480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 39980b57cec5SDimitry Andric RTLIB::FLOOR_F80, RTLIB::FLOOR_F128, 3999480093f4SDimitry Andric RTLIB::FLOOR_PPCF128, Results); 40000b57cec5SDimitry Andric break; 40010b57cec5SDimitry Andric case ISD::FCEIL: 40020b57cec5SDimitry Andric case ISD::STRICT_FCEIL: 4003480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 40040b57cec5SDimitry Andric RTLIB::CEIL_F80, RTLIB::CEIL_F128, 4005480093f4SDimitry Andric RTLIB::CEIL_PPCF128, Results); 40060b57cec5SDimitry Andric break; 40070b57cec5SDimitry Andric case ISD::FRINT: 40080b57cec5SDimitry Andric case ISD::STRICT_FRINT: 4009480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 40100b57cec5SDimitry Andric RTLIB::RINT_F80, RTLIB::RINT_F128, 4011480093f4SDimitry Andric RTLIB::RINT_PPCF128, Results); 40120b57cec5SDimitry Andric break; 40130b57cec5SDimitry Andric case ISD::FNEARBYINT: 40140b57cec5SDimitry Andric case ISD::STRICT_FNEARBYINT: 4015480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 40160b57cec5SDimitry Andric RTLIB::NEARBYINT_F64, 40170b57cec5SDimitry Andric RTLIB::NEARBYINT_F80, 40180b57cec5SDimitry Andric RTLIB::NEARBYINT_F128, 4019480093f4SDimitry Andric RTLIB::NEARBYINT_PPCF128, Results); 40200b57cec5SDimitry Andric break; 40210b57cec5SDimitry Andric case ISD::FROUND: 40220b57cec5SDimitry Andric case ISD::STRICT_FROUND: 4023480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUND_F32, 40240b57cec5SDimitry Andric RTLIB::ROUND_F64, 40250b57cec5SDimitry Andric RTLIB::ROUND_F80, 40260b57cec5SDimitry Andric RTLIB::ROUND_F128, 4027480093f4SDimitry Andric RTLIB::ROUND_PPCF128, Results); 40280b57cec5SDimitry Andric break; 40295ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 40305ffd83dbSDimitry Andric case ISD::STRICT_FROUNDEVEN: 40315ffd83dbSDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32, 40325ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F64, 40335ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F80, 40345ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F128, 40355ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_PPCF128, Results); 40365ffd83dbSDimitry Andric break; 40370b57cec5SDimitry Andric case ISD::FPOWI: 4038480093f4SDimitry Andric case ISD::STRICT_FPOWI: { 4039*fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0)); 4040*fe6060f1SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi."); 4041480093f4SDimitry Andric if (!TLI.getLibcallName(LC)) { 4042480093f4SDimitry Andric // Some targets don't have a powi libcall; use pow instead. 4043480093f4SDimitry Andric SDValue Exponent = DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), 4044480093f4SDimitry Andric Node->getValueType(0), 4045480093f4SDimitry Andric Node->getOperand(1)); 4046480093f4SDimitry Andric Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node), 4047480093f4SDimitry Andric Node->getValueType(0), Node->getOperand(0), 4048480093f4SDimitry Andric Exponent)); 40490b57cec5SDimitry Andric break; 4050480093f4SDimitry Andric } 4051*fe6060f1SDimitry Andric unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0; 4052*fe6060f1SDimitry Andric bool ExponentHasSizeOfInt = 4053*fe6060f1SDimitry Andric DAG.getLibInfo().getIntSize() == 4054*fe6060f1SDimitry Andric Node->getOperand(1 + Offset).getValueType().getSizeInBits(); 4055*fe6060f1SDimitry Andric if (!ExponentHasSizeOfInt) { 4056*fe6060f1SDimitry Andric // If the exponent does not match with sizeof(int) a libcall to 4057*fe6060f1SDimitry Andric // RTLIB::POWI would use the wrong type for the argument. 4058*fe6060f1SDimitry Andric DAG.getContext()->emitError("POWI exponent does not match sizeof(int)"); 4059*fe6060f1SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 4060*fe6060f1SDimitry Andric break; 4061*fe6060f1SDimitry Andric } 4062*fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 4063480093f4SDimitry Andric break; 4064480093f4SDimitry Andric } 40650b57cec5SDimitry Andric case ISD::FPOW: 40660b57cec5SDimitry Andric case ISD::STRICT_FPOW: 40678c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, 40688c27c554SDimitry Andric RTLIB::POW_F128, RTLIB::POW_PPCF128, Results); 40690b57cec5SDimitry Andric break; 40708bcb0991SDimitry Andric case ISD::LROUND: 40718bcb0991SDimitry Andric case ISD::STRICT_LROUND: 4072480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LROUND_F32, 40738bcb0991SDimitry Andric RTLIB::LROUND_F64, RTLIB::LROUND_F80, 40748bcb0991SDimitry Andric RTLIB::LROUND_F128, 4075480093f4SDimitry Andric RTLIB::LROUND_PPCF128, Results); 40768bcb0991SDimitry Andric break; 40778bcb0991SDimitry Andric case ISD::LLROUND: 40788bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 4079480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32, 40808bcb0991SDimitry Andric RTLIB::LLROUND_F64, RTLIB::LLROUND_F80, 40818bcb0991SDimitry Andric RTLIB::LLROUND_F128, 4082480093f4SDimitry Andric RTLIB::LLROUND_PPCF128, Results); 40838bcb0991SDimitry Andric break; 40848bcb0991SDimitry Andric case ISD::LRINT: 40858bcb0991SDimitry Andric case ISD::STRICT_LRINT: 4086480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LRINT_F32, 40878bcb0991SDimitry Andric RTLIB::LRINT_F64, RTLIB::LRINT_F80, 40888bcb0991SDimitry Andric RTLIB::LRINT_F128, 4089480093f4SDimitry Andric RTLIB::LRINT_PPCF128, Results); 40908bcb0991SDimitry Andric break; 40918bcb0991SDimitry Andric case ISD::LLRINT: 40928bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 4093480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32, 40948bcb0991SDimitry Andric RTLIB::LLRINT_F64, RTLIB::LLRINT_F80, 40958bcb0991SDimitry Andric RTLIB::LLRINT_F128, 4096480093f4SDimitry Andric RTLIB::LLRINT_PPCF128, Results); 40978bcb0991SDimitry Andric break; 40980b57cec5SDimitry Andric case ISD::FDIV: 4099480093f4SDimitry Andric case ISD::STRICT_FDIV: 4100480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 41010b57cec5SDimitry Andric RTLIB::DIV_F80, RTLIB::DIV_F128, 4102480093f4SDimitry Andric RTLIB::DIV_PPCF128, Results); 41030b57cec5SDimitry Andric break; 41040b57cec5SDimitry Andric case ISD::FREM: 41050b57cec5SDimitry Andric case ISD::STRICT_FREM: 4106480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 41070b57cec5SDimitry Andric RTLIB::REM_F80, RTLIB::REM_F128, 4108480093f4SDimitry Andric RTLIB::REM_PPCF128, Results); 41090b57cec5SDimitry Andric break; 41100b57cec5SDimitry Andric case ISD::FMA: 41110b57cec5SDimitry Andric case ISD::STRICT_FMA: 4112480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, 41130b57cec5SDimitry Andric RTLIB::FMA_F80, RTLIB::FMA_F128, 4114480093f4SDimitry Andric RTLIB::FMA_PPCF128, Results); 41150b57cec5SDimitry Andric break; 41160b57cec5SDimitry Andric case ISD::FADD: 4117480093f4SDimitry Andric case ISD::STRICT_FADD: 4118480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64, 41190b57cec5SDimitry Andric RTLIB::ADD_F80, RTLIB::ADD_F128, 4120480093f4SDimitry Andric RTLIB::ADD_PPCF128, Results); 41210b57cec5SDimitry Andric break; 41220b57cec5SDimitry Andric case ISD::FMUL: 4123480093f4SDimitry Andric case ISD::STRICT_FMUL: 4124480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64, 41250b57cec5SDimitry Andric RTLIB::MUL_F80, RTLIB::MUL_F128, 4126480093f4SDimitry Andric RTLIB::MUL_PPCF128, Results); 41270b57cec5SDimitry Andric break; 41280b57cec5SDimitry Andric case ISD::FP16_TO_FP: 41290b57cec5SDimitry Andric if (Node->getValueType(0) == MVT::f32) { 41300b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false)); 41310b57cec5SDimitry Andric } 41320b57cec5SDimitry Andric break; 41335ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: { 41345ffd83dbSDimitry Andric if (Node->getValueType(0) == MVT::f32) { 41355ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 41365ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall( 41375ffd83dbSDimitry Andric DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions, 41385ffd83dbSDimitry Andric SDLoc(Node), Node->getOperand(0)); 41395ffd83dbSDimitry Andric Results.push_back(Tmp.first); 41405ffd83dbSDimitry Andric Results.push_back(Tmp.second); 41415ffd83dbSDimitry Andric } 41425ffd83dbSDimitry Andric break; 41435ffd83dbSDimitry Andric } 41440b57cec5SDimitry Andric case ISD::FP_TO_FP16: { 41450b57cec5SDimitry Andric RTLIB::Libcall LC = 41460b57cec5SDimitry Andric RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16); 41470b57cec5SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16"); 41480b57cec5SDimitry Andric Results.push_back(ExpandLibCall(LC, Node, false)); 41490b57cec5SDimitry Andric break; 41500b57cec5SDimitry Andric } 4151e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4152e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 4153e8d8bef9SDimitry Andric case ISD::SINT_TO_FP: 4154e8d8bef9SDimitry Andric case ISD::UINT_TO_FP: { 4155e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP 4156e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4157e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::SINT_TO_FP || 4158e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP; 4159e8d8bef9SDimitry Andric EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType(); 4160e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4161e8d8bef9SDimitry Andric EVT NVT = EVT(); 4162e8d8bef9SDimitry Andric SDLoc dl(Node); 4163e8d8bef9SDimitry Andric 4164e8d8bef9SDimitry Andric // Even if the input is legal, no libcall may exactly match, eg. we don't 4165e8d8bef9SDimitry Andric // have i1 -> fp conversions. So, it needs to be promoted to a larger type, 4166e8d8bef9SDimitry Andric // eg: i13 -> fp. Then, look for an appropriate libcall. 4167e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4168e8d8bef9SDimitry Andric for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE; 4169e8d8bef9SDimitry Andric t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4170e8d8bef9SDimitry Andric ++t) { 4171e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)t; 4172e8d8bef9SDimitry Andric // The source needs to big enough to hold the operand. 4173e8d8bef9SDimitry Andric if (NVT.bitsGE(SVT)) 4174e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT) 4175e8d8bef9SDimitry Andric : RTLIB::getUINTTOFP(NVT, RVT); 4176e8d8bef9SDimitry Andric } 4177e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4178e8d8bef9SDimitry Andric 4179e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4180e8d8bef9SDimitry Andric // Sign/zero extend the argument if the libcall takes a larger type. 4181e8d8bef9SDimitry Andric SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl, 4182e8d8bef9SDimitry Andric NVT, Node->getOperand(IsStrict ? 1 : 0)); 4183e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4184e8d8bef9SDimitry Andric CallOptions.setSExt(Signed); 4185e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4186e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain); 4187e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4188e8d8bef9SDimitry Andric if (IsStrict) 4189e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4190e8d8bef9SDimitry Andric break; 4191e8d8bef9SDimitry Andric } 4192e8d8bef9SDimitry Andric case ISD::FP_TO_SINT: 4193e8d8bef9SDimitry Andric case ISD::FP_TO_UINT: 4194e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4195e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_UINT: { 4196e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT. 4197e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4198e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::FP_TO_SINT || 4199e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_SINT; 4200e8d8bef9SDimitry Andric 4201e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4202e8d8bef9SDimitry Andric EVT SVT = Op.getValueType(); 4203e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4204e8d8bef9SDimitry Andric EVT NVT = EVT(); 4205e8d8bef9SDimitry Andric SDLoc dl(Node); 4206e8d8bef9SDimitry Andric 4207e8d8bef9SDimitry Andric // Even if the result is legal, no libcall may exactly match, eg. we don't 4208e8d8bef9SDimitry Andric // have fp -> i1 conversions. So, it needs to be promoted to a larger type, 4209e8d8bef9SDimitry Andric // eg: fp -> i32. Then, look for an appropriate libcall. 4210e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4211e8d8bef9SDimitry Andric for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE; 4212e8d8bef9SDimitry Andric IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4213e8d8bef9SDimitry Andric ++IntVT) { 4214e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)IntVT; 4215e8d8bef9SDimitry Andric // The type needs to big enough to hold the result. 4216e8d8bef9SDimitry Andric if (NVT.bitsGE(RVT)) 4217e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT) 4218e8d8bef9SDimitry Andric : RTLIB::getFPTOUINT(SVT, NVT); 4219e8d8bef9SDimitry Andric } 4220e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4221e8d8bef9SDimitry Andric 4222e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4223e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4224e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4225e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain); 4226e8d8bef9SDimitry Andric 4227e8d8bef9SDimitry Andric // Truncate the result if the libcall returns a larger type. 4228e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first)); 4229e8d8bef9SDimitry Andric if (IsStrict) 4230e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4231e8d8bef9SDimitry Andric break; 4232e8d8bef9SDimitry Andric } 4233e8d8bef9SDimitry Andric 4234e8d8bef9SDimitry Andric case ISD::FP_ROUND: 4235e8d8bef9SDimitry Andric case ISD::STRICT_FP_ROUND: { 4236e8d8bef9SDimitry Andric // X = FP_ROUND(Y, TRUNC) 4237e8d8bef9SDimitry Andric // TRUNC is a flag, which is always an integer that is zero or one. 4238e8d8bef9SDimitry Andric // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND 4239e8d8bef9SDimitry Andric // is known to not change the value of Y. 4240e8d8bef9SDimitry Andric // We can only expand it into libcall if the TRUNC is 0. 4241e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4242e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4243e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4244e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 4245e8d8bef9SDimitry Andric assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1)) 4246e8d8bef9SDimitry Andric ->isNullValue() && 4247e8d8bef9SDimitry Andric "Unable to expand as libcall if it is not normal rounding"); 4248e8d8bef9SDimitry Andric 4249e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT); 4250e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4251e8d8bef9SDimitry Andric 4252e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4253e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4254e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain); 4255e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4256e8d8bef9SDimitry Andric if (IsStrict) 4257e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4258e8d8bef9SDimitry Andric break; 4259e8d8bef9SDimitry Andric } 4260e8d8bef9SDimitry Andric case ISD::FP_EXTEND: { 4261e8d8bef9SDimitry Andric Results.push_back( 4262e8d8bef9SDimitry Andric ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(), 4263e8d8bef9SDimitry Andric Node->getValueType(0)), 4264e8d8bef9SDimitry Andric Node, false)); 4265e8d8bef9SDimitry Andric break; 4266e8d8bef9SDimitry Andric } 4267e8d8bef9SDimitry Andric case ISD::STRICT_FP_EXTEND: 42685ffd83dbSDimitry Andric case ISD::STRICT_FP_TO_FP16: { 42695ffd83dbSDimitry Andric RTLIB::Libcall LC = 4270e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_FP16 4271e8d8bef9SDimitry Andric ? RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16) 4272e8d8bef9SDimitry Andric : RTLIB::getFPEXT(Node->getOperand(1).getValueType(), 4273e8d8bef9SDimitry Andric Node->getValueType(0)); 4274e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4275e8d8bef9SDimitry Andric 42765ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 42775ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = 42785ffd83dbSDimitry Andric TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1), 42795ffd83dbSDimitry Andric CallOptions, SDLoc(Node), Node->getOperand(0)); 42805ffd83dbSDimitry Andric Results.push_back(Tmp.first); 42815ffd83dbSDimitry Andric Results.push_back(Tmp.second); 42825ffd83dbSDimitry Andric break; 42835ffd83dbSDimitry Andric } 42840b57cec5SDimitry Andric case ISD::FSUB: 4285480093f4SDimitry Andric case ISD::STRICT_FSUB: 4286480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64, 42870b57cec5SDimitry Andric RTLIB::SUB_F80, RTLIB::SUB_F128, 4288480093f4SDimitry Andric RTLIB::SUB_PPCF128, Results); 42890b57cec5SDimitry Andric break; 42900b57cec5SDimitry Andric case ISD::SREM: 42910b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 42920b57cec5SDimitry Andric RTLIB::SREM_I8, 42930b57cec5SDimitry Andric RTLIB::SREM_I16, RTLIB::SREM_I32, 42940b57cec5SDimitry Andric RTLIB::SREM_I64, RTLIB::SREM_I128)); 42950b57cec5SDimitry Andric break; 42960b57cec5SDimitry Andric case ISD::UREM: 42970b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 42980b57cec5SDimitry Andric RTLIB::UREM_I8, 42990b57cec5SDimitry Andric RTLIB::UREM_I16, RTLIB::UREM_I32, 43000b57cec5SDimitry Andric RTLIB::UREM_I64, RTLIB::UREM_I128)); 43010b57cec5SDimitry Andric break; 43020b57cec5SDimitry Andric case ISD::SDIV: 43030b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 43040b57cec5SDimitry Andric RTLIB::SDIV_I8, 43050b57cec5SDimitry Andric RTLIB::SDIV_I16, RTLIB::SDIV_I32, 43060b57cec5SDimitry Andric RTLIB::SDIV_I64, RTLIB::SDIV_I128)); 43070b57cec5SDimitry Andric break; 43080b57cec5SDimitry Andric case ISD::UDIV: 43090b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 43100b57cec5SDimitry Andric RTLIB::UDIV_I8, 43110b57cec5SDimitry Andric RTLIB::UDIV_I16, RTLIB::UDIV_I32, 43120b57cec5SDimitry Andric RTLIB::UDIV_I64, RTLIB::UDIV_I128)); 43130b57cec5SDimitry Andric break; 43140b57cec5SDimitry Andric case ISD::SDIVREM: 43150b57cec5SDimitry Andric case ISD::UDIVREM: 43160b57cec5SDimitry Andric // Expand into divrem libcall 43170b57cec5SDimitry Andric ExpandDivRemLibCall(Node, Results); 43180b57cec5SDimitry Andric break; 43190b57cec5SDimitry Andric case ISD::MUL: 43200b57cec5SDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 43210b57cec5SDimitry Andric RTLIB::MUL_I8, 43220b57cec5SDimitry Andric RTLIB::MUL_I16, RTLIB::MUL_I32, 43230b57cec5SDimitry Andric RTLIB::MUL_I64, RTLIB::MUL_I128)); 43240b57cec5SDimitry Andric break; 43250b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 43260b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 43270b57cec5SDimitry Andric default: 43280b57cec5SDimitry Andric llvm_unreachable("LibCall explicitly requested, but not available"); 43290b57cec5SDimitry Andric case MVT::i32: 43300b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false)); 43310b57cec5SDimitry Andric break; 43320b57cec5SDimitry Andric case MVT::i64: 43330b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false)); 43340b57cec5SDimitry Andric break; 43350b57cec5SDimitry Andric case MVT::i128: 43360b57cec5SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false)); 43370b57cec5SDimitry Andric break; 43380b57cec5SDimitry Andric } 43390b57cec5SDimitry Andric break; 43400b57cec5SDimitry Andric } 43410b57cec5SDimitry Andric 43420b57cec5SDimitry Andric // Replace the original node with the legalized result. 43430b57cec5SDimitry Andric if (!Results.empty()) { 43440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n"); 43450b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 43460b57cec5SDimitry Andric } else 43470b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n"); 43480b57cec5SDimitry Andric } 43490b57cec5SDimitry Andric 43500b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when 43510b57cec5SDimitry Andric // promoting equally sized vectors. 43520b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI, 43530b57cec5SDimitry Andric MVT EltVT, MVT NewEltVT) { 43540b57cec5SDimitry Andric unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits(); 43550b57cec5SDimitry Andric MVT MidVT = MVT::getVectorVT(NewEltVT, OldEltsPerNewElt); 43560b57cec5SDimitry Andric assert(TLI.isTypeLegal(MidVT) && "unexpected"); 43570b57cec5SDimitry Andric return MidVT; 43580b57cec5SDimitry Andric } 43590b57cec5SDimitry Andric 43600b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) { 43610b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to promote node\n"); 43620b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 43630b57cec5SDimitry Andric MVT OVT = Node->getSimpleValueType(0); 43640b57cec5SDimitry Andric if (Node->getOpcode() == ISD::UINT_TO_FP || 43650b57cec5SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP || 43660b57cec5SDimitry Andric Node->getOpcode() == ISD::SETCC || 43670b57cec5SDimitry Andric Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT || 43680b57cec5SDimitry Andric Node->getOpcode() == ISD::INSERT_VECTOR_ELT) { 43690b57cec5SDimitry Andric OVT = Node->getOperand(0).getSimpleValueType(); 43700b57cec5SDimitry Andric } 4371480093f4SDimitry Andric if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP || 4372e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 4373e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCC || 4374e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS) 4375480093f4SDimitry Andric OVT = Node->getOperand(1).getSimpleValueType(); 4376*fe6060f1SDimitry Andric if (Node->getOpcode() == ISD::BR_CC || 4377*fe6060f1SDimitry Andric Node->getOpcode() == ISD::SELECT_CC) 43780b57cec5SDimitry Andric OVT = Node->getOperand(2).getSimpleValueType(); 43790b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 43800b57cec5SDimitry Andric SDLoc dl(Node); 4381*fe6060f1SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 43820b57cec5SDimitry Andric switch (Node->getOpcode()) { 43830b57cec5SDimitry Andric case ISD::CTTZ: 43840b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 43850b57cec5SDimitry Andric case ISD::CTLZ: 43860b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 43870b57cec5SDimitry Andric case ISD::CTPOP: 43885ffd83dbSDimitry Andric // Zero extend the argument unless its cttz, then use any_extend. 43895ffd83dbSDimitry Andric if (Node->getOpcode() == ISD::CTTZ || 43905ffd83dbSDimitry Andric Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF) 43915ffd83dbSDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 43925ffd83dbSDimitry Andric else 43930b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 43945ffd83dbSDimitry Andric 43950b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTTZ) { 43960b57cec5SDimitry Andric // The count is the same in the promoted type except if the original 43970b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 43980b57cec5SDimitry Andric // the top of the original type. 43990b57cec5SDimitry Andric auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(), 44000b57cec5SDimitry Andric OVT.getSizeInBits()); 44010b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1, 44020b57cec5SDimitry Andric DAG.getConstant(TopBit, dl, NVT)); 44030b57cec5SDimitry Andric } 44040b57cec5SDimitry Andric // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is 44050b57cec5SDimitry Andric // already the correct result. 44060b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 44070b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTLZ || 44080b57cec5SDimitry Andric Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) { 44090b57cec5SDimitry Andric // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 44100b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 44110b57cec5SDimitry Andric DAG.getConstant(NVT.getSizeInBits() - 44120b57cec5SDimitry Andric OVT.getSizeInBits(), dl, NVT)); 44130b57cec5SDimitry Andric } 44140b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 44150b57cec5SDimitry Andric break; 44160b57cec5SDimitry Andric case ISD::BITREVERSE: 44170b57cec5SDimitry Andric case ISD::BSWAP: { 44180b57cec5SDimitry Andric unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 44190b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 44200b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 44210b57cec5SDimitry Andric Tmp1 = DAG.getNode( 44220b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 44230b57cec5SDimitry Andric DAG.getConstant(DiffBits, dl, 44240b57cec5SDimitry Andric TLI.getShiftAmountTy(NVT, DAG.getDataLayout()))); 44250b57cec5SDimitry Andric 44260b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 44270b57cec5SDimitry Andric break; 44280b57cec5SDimitry Andric } 44290b57cec5SDimitry Andric case ISD::FP_TO_UINT: 4430480093f4SDimitry Andric case ISD::STRICT_FP_TO_UINT: 44310b57cec5SDimitry Andric case ISD::FP_TO_SINT: 4432480093f4SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4433480093f4SDimitry Andric PromoteLegalFP_TO_INT(Node, dl, Results); 44340b57cec5SDimitry Andric break; 4435e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 4436e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 4437e8d8bef9SDimitry Andric Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl)); 4438e8d8bef9SDimitry Andric break; 44390b57cec5SDimitry Andric case ISD::UINT_TO_FP: 4440480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 44410b57cec5SDimitry Andric case ISD::SINT_TO_FP: 4442480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4443480093f4SDimitry Andric PromoteLegalINT_TO_FP(Node, dl, Results); 44440b57cec5SDimitry Andric break; 44450b57cec5SDimitry Andric case ISD::VAARG: { 44460b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); // Get the chain. 44470b57cec5SDimitry Andric SDValue Ptr = Node->getOperand(1); // Get the pointer. 44480b57cec5SDimitry Andric 44490b57cec5SDimitry Andric unsigned TruncOp; 44500b57cec5SDimitry Andric if (OVT.isVector()) { 44510b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 44520b57cec5SDimitry Andric } else { 44530b57cec5SDimitry Andric assert(OVT.isInteger() 44540b57cec5SDimitry Andric && "VAARG promotion is supported only for vectors or integer types"); 44550b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 44560b57cec5SDimitry Andric } 44570b57cec5SDimitry Andric 44580b57cec5SDimitry Andric // Perform the larger operation, then convert back 44590b57cec5SDimitry Andric Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2), 44600b57cec5SDimitry Andric Node->getConstantOperandVal(3)); 44610b57cec5SDimitry Andric Chain = Tmp1.getValue(1); 44620b57cec5SDimitry Andric 44630b57cec5SDimitry Andric Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1); 44640b57cec5SDimitry Andric 44650b57cec5SDimitry Andric // Modified the chain result - switch anything that used the old chain to 44660b57cec5SDimitry Andric // use the new one. 44670b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2); 44680b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 44690b57cec5SDimitry Andric if (UpdatedNodes) { 44700b57cec5SDimitry Andric UpdatedNodes->insert(Tmp2.getNode()); 44710b57cec5SDimitry Andric UpdatedNodes->insert(Chain.getNode()); 44720b57cec5SDimitry Andric } 44730b57cec5SDimitry Andric ReplacedNode(Node); 44740b57cec5SDimitry Andric break; 44750b57cec5SDimitry Andric } 44760b57cec5SDimitry Andric case ISD::MUL: 44770b57cec5SDimitry Andric case ISD::SDIV: 44780b57cec5SDimitry Andric case ISD::SREM: 44790b57cec5SDimitry Andric case ISD::UDIV: 44800b57cec5SDimitry Andric case ISD::UREM: 44810b57cec5SDimitry Andric case ISD::AND: 44820b57cec5SDimitry Andric case ISD::OR: 44830b57cec5SDimitry Andric case ISD::XOR: { 44840b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 44850b57cec5SDimitry Andric if (OVT.isVector()) { 44860b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 44870b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 44880b57cec5SDimitry Andric } else { 44890b57cec5SDimitry Andric assert(OVT.isInteger() && "Cannot promote logic operation"); 44900b57cec5SDimitry Andric 44910b57cec5SDimitry Andric switch (Node->getOpcode()) { 44920b57cec5SDimitry Andric default: 44930b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 44940b57cec5SDimitry Andric break; 44950b57cec5SDimitry Andric case ISD::SDIV: 44960b57cec5SDimitry Andric case ISD::SREM: 44970b57cec5SDimitry Andric ExtOp = ISD::SIGN_EXTEND; 44980b57cec5SDimitry Andric break; 44990b57cec5SDimitry Andric case ISD::UDIV: 45000b57cec5SDimitry Andric case ISD::UREM: 45010b57cec5SDimitry Andric ExtOp = ISD::ZERO_EXTEND; 45020b57cec5SDimitry Andric break; 45030b57cec5SDimitry Andric } 45040b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 45050b57cec5SDimitry Andric } 45060b57cec5SDimitry Andric // Promote each of the values to the new type. 45070b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 45080b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45090b57cec5SDimitry Andric // Perform the larger operation, then convert back 45100b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 45110b57cec5SDimitry Andric Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 45120b57cec5SDimitry Andric break; 45130b57cec5SDimitry Andric } 45140b57cec5SDimitry Andric case ISD::UMUL_LOHI: 45150b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 45160b57cec5SDimitry Andric // Promote to a multiply in a wider integer type. 45170b57cec5SDimitry Andric unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND 45180b57cec5SDimitry Andric : ISD::SIGN_EXTEND; 45190b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 45200b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45210b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2); 45220b57cec5SDimitry Andric 45230b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 45240b57cec5SDimitry Andric unsigned OriginalSize = OVT.getScalarSizeInBits(); 45250b57cec5SDimitry Andric Tmp2 = DAG.getNode( 45260b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 45270b57cec5SDimitry Andric DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT))); 45280b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 45290b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2)); 45300b57cec5SDimitry Andric break; 45310b57cec5SDimitry Andric } 45320b57cec5SDimitry Andric case ISD::SELECT: { 45330b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 45340b57cec5SDimitry Andric if (Node->getValueType(0).isVector() || 45350b57cec5SDimitry Andric Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) { 45360b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 45370b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 45380b57cec5SDimitry Andric } else if (Node->getValueType(0).isInteger()) { 45390b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 45400b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 45410b57cec5SDimitry Andric } else { 45420b57cec5SDimitry Andric ExtOp = ISD::FP_EXTEND; 45430b57cec5SDimitry Andric TruncOp = ISD::FP_ROUND; 45440b57cec5SDimitry Andric } 45450b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 45460b57cec5SDimitry Andric // Promote each of the values to the new type. 45470b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 45480b57cec5SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 45490b57cec5SDimitry Andric // Perform the larger operation, then round down. 45500b57cec5SDimitry Andric Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3); 45510b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 45520b57cec5SDimitry Andric if (TruncOp != ISD::FP_ROUND) 45530b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 45540b57cec5SDimitry Andric else 45550b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 45560b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl)); 45570b57cec5SDimitry Andric Results.push_back(Tmp1); 45580b57cec5SDimitry Andric break; 45590b57cec5SDimitry Andric } 45600b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 45610b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 45620b57cec5SDimitry Andric 45630b57cec5SDimitry Andric // Cast the two input vectors. 45640b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); 45650b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); 45660b57cec5SDimitry Andric 45670b57cec5SDimitry Andric // Convert the shuffle mask to the right # elements. 45680b57cec5SDimitry Andric Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 45690b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); 45700b57cec5SDimitry Andric Results.push_back(Tmp1); 45710b57cec5SDimitry Andric break; 45720b57cec5SDimitry Andric } 4573*fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 4574*fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 4575*fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1)); 4576*fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2, 4577*fe6060f1SDimitry Andric Node->getOperand(2)); 4578*fe6060f1SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3)); 4579*fe6060f1SDimitry Andric break; 4580*fe6060f1SDimitry Andric } 4581*fe6060f1SDimitry Andric case ISD::SELECT_CC: { 4582*fe6060f1SDimitry Andric SDValue Cond = Node->getOperand(4); 4583*fe6060f1SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get(); 4584*fe6060f1SDimitry Andric // Type of the comparison operands. 4585*fe6060f1SDimitry Andric MVT CVT = Node->getSimpleValueType(0); 4586*fe6060f1SDimitry Andric assert(CVT == OVT && "not handled"); 4587*fe6060f1SDimitry Andric 4588*fe6060f1SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 4589*fe6060f1SDimitry Andric if (NVT.isInteger()) { 4590*fe6060f1SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 4591*fe6060f1SDimitry Andric } 4592*fe6060f1SDimitry Andric 4593*fe6060f1SDimitry Andric // Promote the comparison operands, if needed. 4594*fe6060f1SDimitry Andric if (TLI.isCondCodeLegal(CCCode, CVT)) { 4595*fe6060f1SDimitry Andric Tmp1 = Node->getOperand(0); 4596*fe6060f1SDimitry Andric Tmp2 = Node->getOperand(1); 4597*fe6060f1SDimitry Andric } else { 4598*fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 4599*fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 4600*fe6060f1SDimitry Andric } 4601*fe6060f1SDimitry Andric // Cast the true/false operands. 4602*fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 4603*fe6060f1SDimitry Andric Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 4604*fe6060f1SDimitry Andric 4605*fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond}, 4606*fe6060f1SDimitry Andric Node->getFlags()); 4607*fe6060f1SDimitry Andric 4608*fe6060f1SDimitry Andric // Cast the result back to the original type. 4609*fe6060f1SDimitry Andric if (ExtOp != ISD::FP_EXTEND) 4610*fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1); 4611*fe6060f1SDimitry Andric else 4612*fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1, 4613*fe6060f1SDimitry Andric DAG.getIntPtrConstant(0, dl)); 4614*fe6060f1SDimitry Andric 4615*fe6060f1SDimitry Andric Results.push_back(Tmp1); 4616*fe6060f1SDimitry Andric break; 4617*fe6060f1SDimitry Andric } 4618e8d8bef9SDimitry Andric case ISD::SETCC: 4619e8d8bef9SDimitry Andric case ISD::STRICT_FSETCC: 4620e8d8bef9SDimitry Andric case ISD::STRICT_FSETCCS: { 46210b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 46220b57cec5SDimitry Andric if (NVT.isInteger()) { 4623e8d8bef9SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get(); 46240b57cec5SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 46250b57cec5SDimitry Andric } 4626e8d8bef9SDimitry Andric if (Node->isStrictFPOpcode()) { 4627e8d8bef9SDimitry Andric SDValue InChain = Node->getOperand(0); 4628e8d8bef9SDimitry Andric std::tie(Tmp1, std::ignore) = 4629e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT); 4630e8d8bef9SDimitry Andric std::tie(Tmp2, std::ignore) = 4631e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT); 4632e8d8bef9SDimitry Andric SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)}; 4633e8d8bef9SDimitry Andric SDValue OutChain = DAG.getTokenFactor(dl, TmpChains); 4634e8d8bef9SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 4635e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs, 4636e8d8bef9SDimitry Andric {OutChain, Tmp1, Tmp2, Node->getOperand(3)}, 4637e8d8bef9SDimitry Andric Node->getFlags())); 4638e8d8bef9SDimitry Andric Results.push_back(Results.back().getValue(1)); 4639e8d8bef9SDimitry Andric break; 4640e8d8bef9SDimitry Andric } 46410b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 46420b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 46430b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1, 46440b57cec5SDimitry Andric Tmp2, Node->getOperand(2), Node->getFlags())); 46450b57cec5SDimitry Andric break; 46460b57cec5SDimitry Andric } 46470b57cec5SDimitry Andric case ISD::BR_CC: { 46480b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 46490b57cec5SDimitry Andric if (NVT.isInteger()) { 46500b57cec5SDimitry Andric ISD::CondCode CCCode = 46510b57cec5SDimitry Andric cast<CondCodeSDNode>(Node->getOperand(1))->get(); 46520b57cec5SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 46530b57cec5SDimitry Andric } 46540b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 46550b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 46560b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), 46570b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), 46580b57cec5SDimitry Andric Tmp1, Tmp2, Node->getOperand(4))); 46590b57cec5SDimitry Andric break; 46600b57cec5SDimitry Andric } 46610b57cec5SDimitry Andric case ISD::FADD: 46620b57cec5SDimitry Andric case ISD::FSUB: 46630b57cec5SDimitry Andric case ISD::FMUL: 46640b57cec5SDimitry Andric case ISD::FDIV: 46650b57cec5SDimitry Andric case ISD::FREM: 46660b57cec5SDimitry Andric case ISD::FMINNUM: 46670b57cec5SDimitry Andric case ISD::FMAXNUM: 46680b57cec5SDimitry Andric case ISD::FPOW: 46690b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 46700b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 46710b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, 46720b57cec5SDimitry Andric Node->getFlags()); 46730b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 46740b57cec5SDimitry Andric Tmp3, DAG.getIntPtrConstant(0, dl))); 46750b57cec5SDimitry Andric break; 4676480093f4SDimitry Andric case ISD::STRICT_FREM: 4677480093f4SDimitry Andric case ISD::STRICT_FPOW: 4678480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4679480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 4680480093f4SDimitry Andric Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4681480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(2)}); 4682480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1), 4683480093f4SDimitry Andric Tmp2.getValue(1)); 4684480093f4SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 4685480093f4SDimitry Andric {Tmp3, Tmp1, Tmp2}); 4686480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 4687480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)}); 4688480093f4SDimitry Andric Results.push_back(Tmp1); 4689480093f4SDimitry Andric Results.push_back(Tmp1.getValue(1)); 4690480093f4SDimitry Andric break; 46910b57cec5SDimitry Andric case ISD::FMA: 46920b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 46930b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 46940b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2)); 46950b57cec5SDimitry Andric Results.push_back( 46960b57cec5SDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, 46970b57cec5SDimitry Andric DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3), 46980b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl))); 46990b57cec5SDimitry Andric break; 47000b57cec5SDimitry Andric case ISD::FCOPYSIGN: 47010b57cec5SDimitry Andric case ISD::FPOWI: { 47020b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 47030b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 47040b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 47050b57cec5SDimitry Andric 47060b57cec5SDimitry Andric // fcopysign doesn't change anything but the sign bit, so 47070b57cec5SDimitry Andric // (fp_round (fcopysign (fpext a), b)) 47080b57cec5SDimitry Andric // is as precise as 47090b57cec5SDimitry Andric // (fp_round (fpext a)) 47100b57cec5SDimitry Andric // which is a no-op. Mark it as a TRUNCating FP_ROUND. 47110b57cec5SDimitry Andric const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN); 47120b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 47130b57cec5SDimitry Andric Tmp3, DAG.getIntPtrConstant(isTrunc, dl))); 47140b57cec5SDimitry Andric break; 47150b57cec5SDimitry Andric } 47160b57cec5SDimitry Andric case ISD::FFLOOR: 47170b57cec5SDimitry Andric case ISD::FCEIL: 47180b57cec5SDimitry Andric case ISD::FRINT: 47190b57cec5SDimitry Andric case ISD::FNEARBYINT: 47200b57cec5SDimitry Andric case ISD::FROUND: 47215ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 47220b57cec5SDimitry Andric case ISD::FTRUNC: 47230b57cec5SDimitry Andric case ISD::FNEG: 47240b57cec5SDimitry Andric case ISD::FSQRT: 47250b57cec5SDimitry Andric case ISD::FSIN: 47260b57cec5SDimitry Andric case ISD::FCOS: 47270b57cec5SDimitry Andric case ISD::FLOG: 47280b57cec5SDimitry Andric case ISD::FLOG2: 47290b57cec5SDimitry Andric case ISD::FLOG10: 47300b57cec5SDimitry Andric case ISD::FABS: 47310b57cec5SDimitry Andric case ISD::FEXP: 47320b57cec5SDimitry Andric case ISD::FEXP2: 47330b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 47340b57cec5SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 47350b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::FP_ROUND, dl, OVT, 47360b57cec5SDimitry Andric Tmp2, DAG.getIntPtrConstant(0, dl))); 47370b57cec5SDimitry Andric break; 4738480093f4SDimitry Andric case ISD::STRICT_FFLOOR: 4739480093f4SDimitry Andric case ISD::STRICT_FCEIL: 4740480093f4SDimitry Andric case ISD::STRICT_FSIN: 4741480093f4SDimitry Andric case ISD::STRICT_FCOS: 4742480093f4SDimitry Andric case ISD::STRICT_FLOG: 4743480093f4SDimitry Andric case ISD::STRICT_FLOG10: 4744480093f4SDimitry Andric case ISD::STRICT_FEXP: 4745480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 4746480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 4747480093f4SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 4748480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1}); 4749480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 4750480093f4SDimitry Andric {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)}); 4751480093f4SDimitry Andric Results.push_back(Tmp3); 4752480093f4SDimitry Andric Results.push_back(Tmp3.getValue(1)); 4753480093f4SDimitry Andric break; 47540b57cec5SDimitry Andric case ISD::BUILD_VECTOR: { 47550b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 47560b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 47570b57cec5SDimitry Andric 47580b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 47590b57cec5SDimitry Andric // 47600b57cec5SDimitry Andric // e.g. v2i64 = build_vector i64:x, i64:y => v4i32 47610b57cec5SDimitry Andric // => 47620b57cec5SDimitry Andric // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y)) 47630b57cec5SDimitry Andric 47640b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 47650b57cec5SDimitry Andric "Invalid promote type for build_vector"); 47660b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 47670b57cec5SDimitry Andric 47680b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 47690b57cec5SDimitry Andric 47700b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 47710b57cec5SDimitry Andric for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) { 47720b57cec5SDimitry Andric SDValue Op = Node->getOperand(I); 47730b57cec5SDimitry Andric NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op)); 47740b57cec5SDimitry Andric } 47750b57cec5SDimitry Andric 47760b57cec5SDimitry Andric SDLoc SL(Node); 47770b57cec5SDimitry Andric SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewOps); 47780b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 47790b57cec5SDimitry Andric Results.push_back(CvtVec); 47800b57cec5SDimitry Andric break; 47810b57cec5SDimitry Andric } 47820b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: { 47830b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 47840b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 47850b57cec5SDimitry Andric 47860b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size. 47870b57cec5SDimitry Andric // 47880b57cec5SDimitry Andric // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32 47890b57cec5SDimitry Andric // => 47900b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 47910b57cec5SDimitry Andric // 47920b57cec5SDimitry Andric // i64 = bitcast 47930b57cec5SDimitry Andric // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 47940b57cec5SDimitry Andric // (i32 (extract_vector_elt castx, (2 * y + 1))) 47950b57cec5SDimitry Andric // 47960b57cec5SDimitry Andric 47970b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 47980b57cec5SDimitry Andric "Invalid promote type for extract_vector_elt"); 47990b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 48000b57cec5SDimitry Andric 48010b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 48020b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 48030b57cec5SDimitry Andric 48040b57cec5SDimitry Andric SDValue Idx = Node->getOperand(1); 48050b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 48060b57cec5SDimitry Andric SDLoc SL(Node); 48070b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT); 48080b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 48090b57cec5SDimitry Andric 48100b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 48110b57cec5SDimitry Andric 48120b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 48130b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 48140b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 48150b57cec5SDimitry Andric SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 48160b57cec5SDimitry Andric 48170b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 48180b57cec5SDimitry Andric CastVec, TmpIdx); 48190b57cec5SDimitry Andric NewOps.push_back(Elt); 48200b57cec5SDimitry Andric } 48210b57cec5SDimitry Andric 48220b57cec5SDimitry Andric SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps); 48230b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec)); 48240b57cec5SDimitry Andric break; 48250b57cec5SDimitry Andric } 48260b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: { 48270b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 48280b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 48290b57cec5SDimitry Andric 48300b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 48310b57cec5SDimitry Andric // 48320b57cec5SDimitry Andric // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32 48330b57cec5SDimitry Andric // => 48340b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 48350b57cec5SDimitry Andric // v2i32:casty = bitcast y:i64 48360b57cec5SDimitry Andric // 48370b57cec5SDimitry Andric // v2i64 = bitcast 48380b57cec5SDimitry Andric // (v4i32 insert_vector_elt 48390b57cec5SDimitry Andric // (v4i32 insert_vector_elt v4i32:castx, 48400b57cec5SDimitry Andric // (extract_vector_elt casty, 0), 2 * z), 48410b57cec5SDimitry Andric // (extract_vector_elt casty, 1), (2 * z + 1)) 48420b57cec5SDimitry Andric 48430b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 48440b57cec5SDimitry Andric "Invalid promote type for insert_vector_elt"); 48450b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 48460b57cec5SDimitry Andric 48470b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 48480b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 48490b57cec5SDimitry Andric 48500b57cec5SDimitry Andric SDValue Val = Node->getOperand(1); 48510b57cec5SDimitry Andric SDValue Idx = Node->getOperand(2); 48520b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 48530b57cec5SDimitry Andric SDLoc SL(Node); 48540b57cec5SDimitry Andric 48550b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT); 48560b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 48570b57cec5SDimitry Andric 48580b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 48590b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 48600b57cec5SDimitry Andric 48610b57cec5SDimitry Andric SDValue NewVec = CastVec; 48620b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 48630b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 48640b57cec5SDimitry Andric SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 48650b57cec5SDimitry Andric 48660b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 48670b57cec5SDimitry Andric CastVal, IdxOffset); 48680b57cec5SDimitry Andric 48690b57cec5SDimitry Andric NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT, 48700b57cec5SDimitry Andric NewVec, Elt, InEltIdx); 48710b57cec5SDimitry Andric } 48720b57cec5SDimitry Andric 48730b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec)); 48740b57cec5SDimitry Andric break; 48750b57cec5SDimitry Andric } 48760b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: { 48770b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 48780b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 48790b57cec5SDimitry Andric 48800b57cec5SDimitry Andric // Handle bitcasts to different vector type with the same total bit size. 48810b57cec5SDimitry Andric // 48820b57cec5SDimitry Andric // e.g. v2i64 = scalar_to_vector x:i64 48830b57cec5SDimitry Andric // => 48840b57cec5SDimitry Andric // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef) 48850b57cec5SDimitry Andric // 48860b57cec5SDimitry Andric 48870b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 48880b57cec5SDimitry Andric SDValue Val = Node->getOperand(0); 48890b57cec5SDimitry Andric SDLoc SL(Node); 48900b57cec5SDimitry Andric 48910b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 48920b57cec5SDimitry Andric SDValue Undef = DAG.getUNDEF(MidVT); 48930b57cec5SDimitry Andric 48940b57cec5SDimitry Andric SmallVector<SDValue, 8> NewElts; 48950b57cec5SDimitry Andric NewElts.push_back(CastVal); 48960b57cec5SDimitry Andric for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I) 48970b57cec5SDimitry Andric NewElts.push_back(Undef); 48980b57cec5SDimitry Andric 48990b57cec5SDimitry Andric SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts); 49000b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 49010b57cec5SDimitry Andric Results.push_back(CvtVec); 49020b57cec5SDimitry Andric break; 49030b57cec5SDimitry Andric } 49040b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: { 49050b57cec5SDimitry Andric AtomicSDNode *AM = cast<AtomicSDNode>(Node); 49060b57cec5SDimitry Andric SDLoc SL(Node); 49070b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal()); 49080b57cec5SDimitry Andric assert(NVT.getSizeInBits() == OVT.getSizeInBits() && 49090b57cec5SDimitry Andric "unexpected promotion type"); 49100b57cec5SDimitry Andric assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() && 49110b57cec5SDimitry Andric "unexpected atomic_swap with illegal type"); 49120b57cec5SDimitry Andric 49130b57cec5SDimitry Andric SDValue NewAtomic 49140b57cec5SDimitry Andric = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT, 49150b57cec5SDimitry Andric DAG.getVTList(NVT, MVT::Other), 49160b57cec5SDimitry Andric { AM->getChain(), AM->getBasePtr(), CastVal }, 49170b57cec5SDimitry Andric AM->getMemOperand()); 49180b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic)); 49190b57cec5SDimitry Andric Results.push_back(NewAtomic.getValue(1)); 49200b57cec5SDimitry Andric break; 49210b57cec5SDimitry Andric } 49220b57cec5SDimitry Andric } 49230b57cec5SDimitry Andric 49240b57cec5SDimitry Andric // Replace the original node with the legalized result. 49250b57cec5SDimitry Andric if (!Results.empty()) { 49260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully promoted node\n"); 49270b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 49280b57cec5SDimitry Andric } else 49290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not promote node\n"); 49300b57cec5SDimitry Andric } 49310b57cec5SDimitry Andric 49320b57cec5SDimitry Andric /// This is the entry point for the file. 49330b57cec5SDimitry Andric void SelectionDAG::Legalize() { 49340b57cec5SDimitry Andric AssignTopologicalOrder(); 49350b57cec5SDimitry Andric 49360b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 49370b57cec5SDimitry Andric // Use a delete listener to remove nodes which were deleted during 49380b57cec5SDimitry Andric // legalization from LegalizeNodes. This is needed to handle the situation 49390b57cec5SDimitry Andric // where a new node is allocated by the object pool to the same address of a 49400b57cec5SDimitry Andric // previously deleted node. 49410b57cec5SDimitry Andric DAGNodeDeletedListener DeleteListener( 49420b57cec5SDimitry Andric *this, 49430b57cec5SDimitry Andric [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); }); 49440b57cec5SDimitry Andric 49450b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes); 49460b57cec5SDimitry Andric 49470b57cec5SDimitry Andric // Visit all the nodes. We start in topological order, so that we see 49480b57cec5SDimitry Andric // nodes with their original operands intact. Legalization can produce 49490b57cec5SDimitry Andric // new nodes which may themselves need to be legalized. Iterate until all 49500b57cec5SDimitry Andric // nodes have been legalized. 49510b57cec5SDimitry Andric while (true) { 49520b57cec5SDimitry Andric bool AnyLegalized = false; 49530b57cec5SDimitry Andric for (auto NI = allnodes_end(); NI != allnodes_begin();) { 49540b57cec5SDimitry Andric --NI; 49550b57cec5SDimitry Andric 49560b57cec5SDimitry Andric SDNode *N = &*NI; 49570b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 49580b57cec5SDimitry Andric ++NI; 49590b57cec5SDimitry Andric DeleteNode(N); 49600b57cec5SDimitry Andric continue; 49610b57cec5SDimitry Andric } 49620b57cec5SDimitry Andric 49630b57cec5SDimitry Andric if (LegalizedNodes.insert(N).second) { 49640b57cec5SDimitry Andric AnyLegalized = true; 49650b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 49660b57cec5SDimitry Andric 49670b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 49680b57cec5SDimitry Andric ++NI; 49690b57cec5SDimitry Andric DeleteNode(N); 49700b57cec5SDimitry Andric } 49710b57cec5SDimitry Andric } 49720b57cec5SDimitry Andric } 49730b57cec5SDimitry Andric if (!AnyLegalized) 49740b57cec5SDimitry Andric break; 49750b57cec5SDimitry Andric 49760b57cec5SDimitry Andric } 49770b57cec5SDimitry Andric 49780b57cec5SDimitry Andric // Remove dead nodes now. 49790b57cec5SDimitry Andric RemoveDeadNodes(); 49800b57cec5SDimitry Andric } 49810b57cec5SDimitry Andric 49820b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N, 49830b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> &UpdatedNodes) { 49840b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 49850b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes); 49860b57cec5SDimitry Andric 49870b57cec5SDimitry Andric // Directly insert the node in question, and legalize it. This will recurse 49880b57cec5SDimitry Andric // as needed through operands. 49890b57cec5SDimitry Andric LegalizedNodes.insert(N); 49900b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 49910b57cec5SDimitry Andric 49920b57cec5SDimitry Andric return LegalizedNodes.count(N); 49930b57cec5SDimitry Andric } 4994