10b57cec5SDimitry Andric //===- LegalizeDAG.cpp - Implement SelectionDAG::Legalize -----------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the SelectionDAG::Legalize method. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h" 140b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 150b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 1681ad6265SDimitry Andric #include "llvm/ADT/FloatingPointMode.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 190b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h" 200b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 215f757f3fSDimitry Andric #include "llvm/Analysis/ConstantFolding.h" 228bcb0991SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 2706c3fb27SDimitry Andric #include "llvm/CodeGen/MachineValueType.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/RuntimeLibcalls.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/ValueTypes.h" 350b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h" 360b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 370b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 380b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 390b57cec5SDimitry Andric #include "llvm/IR/Function.h" 400b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 410b57cec5SDimitry Andric #include "llvm/IR/Type.h" 420b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 430b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 440b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 450b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 460b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h" 470b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 480b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 490b57cec5SDimitry Andric #include "llvm/Target/TargetOptions.h" 500b57cec5SDimitry Andric #include <cassert> 510b57cec5SDimitry Andric #include <cstdint> 520b57cec5SDimitry Andric #include <tuple> 530b57cec5SDimitry Andric #include <utility> 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric using namespace llvm; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric #define DEBUG_TYPE "legalizedag" 580b57cec5SDimitry Andric 590b57cec5SDimitry Andric namespace { 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric /// Keeps track of state when getting the sign of a floating-point value as an 620b57cec5SDimitry Andric /// integer. 630b57cec5SDimitry Andric struct FloatSignAsInt { 640b57cec5SDimitry Andric EVT FloatVT; 650b57cec5SDimitry Andric SDValue Chain; 660b57cec5SDimitry Andric SDValue FloatPtr; 670b57cec5SDimitry Andric SDValue IntPtr; 680b57cec5SDimitry Andric MachinePointerInfo IntPointerInfo; 690b57cec5SDimitry Andric MachinePointerInfo FloatPointerInfo; 700b57cec5SDimitry Andric SDValue IntValue; 710b57cec5SDimitry Andric APInt SignMask; 720b57cec5SDimitry Andric uint8_t SignBit; 730b57cec5SDimitry Andric }; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 760b57cec5SDimitry Andric /// This takes an arbitrary SelectionDAG as input and 770b57cec5SDimitry Andric /// hacks on it until the target machine can handle it. This involves 780b57cec5SDimitry Andric /// eliminating value sizes the machine cannot handle (promoting small sizes to 790b57cec5SDimitry Andric /// large sizes or splitting up large values into small values) as well as 800b57cec5SDimitry Andric /// eliminating operations the machine cannot handle. 810b57cec5SDimitry Andric /// 820b57cec5SDimitry Andric /// This code also does a small amount of optimization and recognition of idioms 830b57cec5SDimitry Andric /// as part of its processing. For example, if a target does not support a 840b57cec5SDimitry Andric /// 'setcc' instruction efficiently, but does support 'brcc' instruction, this 850b57cec5SDimitry Andric /// will attempt merge setcc and brc instructions into brcc's. 860b57cec5SDimitry Andric class SelectionDAGLegalize { 870b57cec5SDimitry Andric const TargetMachine &TM; 880b57cec5SDimitry Andric const TargetLowering &TLI; 890b57cec5SDimitry Andric SelectionDAG &DAG; 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric /// The set of nodes which have already been legalized. We hold a 920b57cec5SDimitry Andric /// reference to it in order to update as necessary on node deletion. 930b57cec5SDimitry Andric SmallPtrSetImpl<SDNode *> &LegalizedNodes; 940b57cec5SDimitry Andric 950b57cec5SDimitry Andric /// A set of all the nodes updated during legalization. 960b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> *UpdatedNodes; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric EVT getSetCCResultType(EVT VT) const { 990b57cec5SDimitry Andric return TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Libcall insertion helpers. 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric public: 1050b57cec5SDimitry Andric SelectionDAGLegalize(SelectionDAG &DAG, 1060b57cec5SDimitry Andric SmallPtrSetImpl<SDNode *> &LegalizedNodes, 1070b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> *UpdatedNodes = nullptr) 1080b57cec5SDimitry Andric : TM(DAG.getTarget()), TLI(DAG.getTargetLoweringInfo()), DAG(DAG), 1090b57cec5SDimitry Andric LegalizedNodes(LegalizedNodes), UpdatedNodes(UpdatedNodes) {} 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric /// Legalizes the given operation. 1120b57cec5SDimitry Andric void LegalizeOp(SDNode *Node); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric private: 1150b57cec5SDimitry Andric SDValue OptimizeFloatStore(StoreSDNode *ST); 1160b57cec5SDimitry Andric 1170b57cec5SDimitry Andric void LegalizeLoadOps(SDNode *Node); 1180b57cec5SDimitry Andric void LegalizeStoreOps(SDNode *Node); 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric /// Some targets cannot handle a variable 1210b57cec5SDimitry Andric /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it 1220b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform 1230b57cec5SDimitry Andric /// the insert there, and then read the result back. 1240b57cec5SDimitry Andric SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx, 1250b57cec5SDimitry Andric const SDLoc &dl); 1260b57cec5SDimitry Andric SDValue ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, SDValue Idx, 1270b57cec5SDimitry Andric const SDLoc &dl); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric /// Return a vector shuffle operation which 1300b57cec5SDimitry Andric /// performs the same shuffe in terms of order or result bytes, but on a type 1310b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type. 1320b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 1330b57cec5SDimitry Andric SDValue ShuffleWithNarrowerEltType(EVT NVT, EVT VT, const SDLoc &dl, 1340b57cec5SDimitry Andric SDValue N1, SDValue N2, 1350b57cec5SDimitry Andric ArrayRef<int> Mask) const; 1360b57cec5SDimitry Andric 13706c3fb27SDimitry Andric std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 13806c3fb27SDimitry Andric TargetLowering::ArgListTy &&Args, bool isSigned); 13906c3fb27SDimitry Andric std::pair<SDValue, SDValue> ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned); 1400b57cec5SDimitry Andric 14106c3fb27SDimitry Andric void ExpandFrexpLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 142fe6060f1SDimitry Andric void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall LC, 143fe6060f1SDimitry Andric SmallVectorImpl<SDValue> &Results); 144480093f4SDimitry Andric void ExpandFPLibCall(SDNode *Node, RTLIB::Libcall Call_F32, 1450b57cec5SDimitry Andric RTLIB::Libcall Call_F64, RTLIB::Libcall Call_F80, 1460b57cec5SDimitry Andric RTLIB::Libcall Call_F128, 147480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 148480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 149bdd1243dSDimitry Andric SDValue ExpandIntLibCall(SDNode *Node, bool isSigned, 150bdd1243dSDimitry Andric RTLIB::Libcall Call_I8, 151bdd1243dSDimitry Andric RTLIB::Libcall Call_I16, 152bdd1243dSDimitry Andric RTLIB::Libcall Call_I32, 153bdd1243dSDimitry Andric RTLIB::Libcall Call_I64, 154bdd1243dSDimitry Andric RTLIB::Libcall Call_I128); 155480093f4SDimitry Andric void ExpandArgFPLibCall(SDNode *Node, 1560b57cec5SDimitry Andric RTLIB::Libcall Call_F32, RTLIB::Libcall Call_F64, 1570b57cec5SDimitry Andric RTLIB::Libcall Call_F80, RTLIB::Libcall Call_F128, 158480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 159480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 1600b57cec5SDimitry Andric void ExpandDivRemLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 1610b57cec5SDimitry Andric void ExpandSinCosLibCall(SDNode *Node, SmallVectorImpl<SDValue> &Results); 1620b57cec5SDimitry Andric 1630b57cec5SDimitry Andric SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 1640b57cec5SDimitry Andric const SDLoc &dl); 1650b57cec5SDimitry Andric SDValue EmitStackConvert(SDValue SrcOp, EVT SlotVT, EVT DestVT, 1660b57cec5SDimitry Andric const SDLoc &dl, SDValue ChainIn); 1670b57cec5SDimitry Andric SDValue ExpandBUILD_VECTOR(SDNode *Node); 1688bcb0991SDimitry Andric SDValue ExpandSPLAT_VECTOR(SDNode *Node); 1690b57cec5SDimitry Andric SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node); 1700b57cec5SDimitry Andric void ExpandDYNAMIC_STACKALLOC(SDNode *Node, 1710b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results); 1720b57cec5SDimitry Andric void getSignAsIntValue(FloatSignAsInt &State, const SDLoc &DL, 1730b57cec5SDimitry Andric SDValue Value) const; 1740b57cec5SDimitry Andric SDValue modifySignAsInt(const FloatSignAsInt &State, const SDLoc &DL, 1750b57cec5SDimitry Andric SDValue NewIntValue) const; 1760b57cec5SDimitry Andric SDValue ExpandFCOPYSIGN(SDNode *Node) const; 1770b57cec5SDimitry Andric SDValue ExpandFABS(SDNode *Node) const; 178e8d8bef9SDimitry Andric SDValue ExpandFNEG(SDNode *Node) const; 17906c3fb27SDimitry Andric SDValue expandLdexp(SDNode *Node) const; 18006c3fb27SDimitry Andric SDValue expandFrexp(SDNode *Node) const; 18106c3fb27SDimitry Andric 182480093f4SDimitry Andric SDValue ExpandLegalINT_TO_FP(SDNode *Node, SDValue &Chain); 183480093f4SDimitry Andric void PromoteLegalINT_TO_FP(SDNode *N, const SDLoc &dl, 184480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 185480093f4SDimitry Andric void PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl, 186480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results); 187e8d8bef9SDimitry Andric SDValue PromoteLegalFP_TO_INT_SAT(SDNode *Node, const SDLoc &dl); 1880b57cec5SDimitry Andric 189e8d8bef9SDimitry Andric SDValue ExpandPARITY(SDValue Op, const SDLoc &dl); 1900b57cec5SDimitry Andric 1910b57cec5SDimitry Andric SDValue ExpandExtractFromVectorThroughStack(SDValue Op); 1920b57cec5SDimitry Andric SDValue ExpandInsertToVectorThroughStack(SDValue Op); 1930b57cec5SDimitry Andric SDValue ExpandVectorBuildThroughStack(SDNode* Node); 1940b57cec5SDimitry Andric 1950b57cec5SDimitry Andric SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP); 1960b57cec5SDimitry Andric SDValue ExpandConstant(ConstantSDNode *CP); 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric // if ExpandNode returns false, LegalizeOp falls back to ConvertNodeToLibcall 1990b57cec5SDimitry Andric bool ExpandNode(SDNode *Node); 2000b57cec5SDimitry Andric void ConvertNodeToLibcall(SDNode *Node); 2010b57cec5SDimitry Andric void PromoteNode(SDNode *Node); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric public: 2040b57cec5SDimitry Andric // Node replacement helpers 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric void ReplacedNode(SDNode *N) { 2070b57cec5SDimitry Andric LegalizedNodes.erase(N); 2080b57cec5SDimitry Andric if (UpdatedNodes) 2090b57cec5SDimitry Andric UpdatedNodes->insert(N); 2100b57cec5SDimitry Andric } 2110b57cec5SDimitry Andric 2120b57cec5SDimitry Andric void ReplaceNode(SDNode *Old, SDNode *New) { 2130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2140b57cec5SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric assert(Old->getNumValues() == New->getNumValues() && 2170b57cec5SDimitry Andric "Replacing one node with another that produces a different number " 2180b57cec5SDimitry Andric "of values!"); 2190b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2200b57cec5SDimitry Andric if (UpdatedNodes) 2210b57cec5SDimitry Andric UpdatedNodes->insert(New); 2220b57cec5SDimitry Andric ReplacedNode(Old); 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric void ReplaceNode(SDValue Old, SDValue New) { 2260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2270b57cec5SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2300b57cec5SDimitry Andric if (UpdatedNodes) 2310b57cec5SDimitry Andric UpdatedNodes->insert(New.getNode()); 2320b57cec5SDimitry Andric ReplacedNode(Old.getNode()); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric void ReplaceNode(SDNode *Old, const SDValue *New) { 2360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG)); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric DAG.ReplaceAllUsesWith(Old, New); 2390b57cec5SDimitry Andric for (unsigned i = 0, e = Old->getNumValues(); i != e; ++i) { 2400b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (i == 0 ? " with: " : " and: "); 2410b57cec5SDimitry Andric New[i]->dump(&DAG)); 2420b57cec5SDimitry Andric if (UpdatedNodes) 2430b57cec5SDimitry Andric UpdatedNodes->insert(New[i].getNode()); 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric ReplacedNode(Old); 2460b57cec5SDimitry Andric } 2478bcb0991SDimitry Andric 2488bcb0991SDimitry Andric void ReplaceNodeWithValue(SDValue Old, SDValue New) { 2498bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << " ... replacing: "; Old->dump(&DAG); 2508bcb0991SDimitry Andric dbgs() << " with: "; New->dump(&DAG)); 2518bcb0991SDimitry Andric 2528bcb0991SDimitry Andric DAG.ReplaceAllUsesOfValueWith(Old, New); 2538bcb0991SDimitry Andric if (UpdatedNodes) 2548bcb0991SDimitry Andric UpdatedNodes->insert(New.getNode()); 2558bcb0991SDimitry Andric ReplacedNode(Old.getNode()); 2568bcb0991SDimitry Andric } 2570b57cec5SDimitry Andric }; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric } // end anonymous namespace 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric /// Return a vector shuffle operation which 2620b57cec5SDimitry Andric /// performs the same shuffle in terms of order or result bytes, but on a type 2630b57cec5SDimitry Andric /// whose vector element type is narrower than the original shuffle type. 2640b57cec5SDimitry Andric /// e.g. <v4i32> <0, 1, 0, 1> -> v8i16 <0, 1, 2, 3, 0, 1, 2, 3> 2650b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ShuffleWithNarrowerEltType( 2660b57cec5SDimitry Andric EVT NVT, EVT VT, const SDLoc &dl, SDValue N1, SDValue N2, 2670b57cec5SDimitry Andric ArrayRef<int> Mask) const { 2680b57cec5SDimitry Andric unsigned NumMaskElts = VT.getVectorNumElements(); 2690b57cec5SDimitry Andric unsigned NumDestElts = NVT.getVectorNumElements(); 2700b57cec5SDimitry Andric unsigned NumEltsGrowth = NumDestElts / NumMaskElts; 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!"); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric if (NumEltsGrowth == 1) 2750b57cec5SDimitry Andric return DAG.getVectorShuffle(NVT, dl, N1, N2, Mask); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric SmallVector<int, 8> NewMask; 2780b57cec5SDimitry Andric for (unsigned i = 0; i != NumMaskElts; ++i) { 2790b57cec5SDimitry Andric int Idx = Mask[i]; 2800b57cec5SDimitry Andric for (unsigned j = 0; j != NumEltsGrowth; ++j) { 2810b57cec5SDimitry Andric if (Idx < 0) 2820b57cec5SDimitry Andric NewMask.push_back(-1); 2830b57cec5SDimitry Andric else 2840b57cec5SDimitry Andric NewMask.push_back(Idx * NumEltsGrowth + j); 2850b57cec5SDimitry Andric } 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric assert(NewMask.size() == NumDestElts && "Non-integer NumEltsGrowth?"); 2880b57cec5SDimitry Andric assert(TLI.isShuffleMaskLegal(NewMask, NVT) && "Shuffle not legal?"); 2890b57cec5SDimitry Andric return DAG.getVectorShuffle(NVT, dl, N1, N2, NewMask); 2900b57cec5SDimitry Andric } 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric /// Expands the ConstantFP node to an integer constant or 2930b57cec5SDimitry Andric /// a load from the constant pool. 2940b57cec5SDimitry Andric SDValue 2950b57cec5SDimitry Andric SelectionDAGLegalize::ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP) { 2960b57cec5SDimitry Andric bool Extend = false; 2970b57cec5SDimitry Andric SDLoc dl(CFP); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric // If a FP immediate is precise when represented as a float and if the 3000b57cec5SDimitry Andric // target can do an extending load from float to double, we put it into 3010b57cec5SDimitry Andric // the constant pool as a float, even if it's is statically typed as a 3020b57cec5SDimitry Andric // double. This shrinks FP constants and canonicalizes them for targets where 3030b57cec5SDimitry Andric // an FP extending load is the same cost as a normal load (such as on the x87 3040b57cec5SDimitry Andric // fp stack or PPC FP unit). 3050b57cec5SDimitry Andric EVT VT = CFP->getValueType(0); 3060b57cec5SDimitry Andric ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue()); 3070b57cec5SDimitry Andric if (!UseCP) { 3080b57cec5SDimitry Andric assert((VT == MVT::f64 || VT == MVT::f32) && "Invalid type expansion"); 3090b57cec5SDimitry Andric return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(), dl, 3100b57cec5SDimitry Andric (VT == MVT::f64) ? MVT::i64 : MVT::i32); 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric APFloat APF = CFP->getValueAPF(); 3140b57cec5SDimitry Andric EVT OrigVT = VT; 3150b57cec5SDimitry Andric EVT SVT = VT; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // We don't want to shrink SNaNs. Converting the SNaN back to its real type 3180b57cec5SDimitry Andric // can cause it to be changed into a QNaN on some platforms (e.g. on SystemZ). 3190b57cec5SDimitry Andric if (!APF.isSignaling()) { 320bdd1243dSDimitry Andric while (SVT != MVT::f32 && SVT != MVT::f16 && SVT != MVT::bf16) { 3210b57cec5SDimitry Andric SVT = (MVT::SimpleValueType)(SVT.getSimpleVT().SimpleTy - 1); 3220b57cec5SDimitry Andric if (ConstantFPSDNode::isValueValidForType(SVT, APF) && 3230b57cec5SDimitry Andric // Only do this if the target has a native EXTLOAD instruction from 3240b57cec5SDimitry Andric // smaller type. 3250b57cec5SDimitry Andric TLI.isLoadExtLegal(ISD::EXTLOAD, OrigVT, SVT) && 3260b57cec5SDimitry Andric TLI.ShouldShrinkFPConstant(OrigVT)) { 3270b57cec5SDimitry Andric Type *SType = SVT.getTypeForEVT(*DAG.getContext()); 3285f757f3fSDimitry Andric LLVMC = cast<ConstantFP>(ConstantFoldCastOperand( 3295f757f3fSDimitry Andric Instruction::FPTrunc, LLVMC, SType, DAG.getDataLayout())); 3300b57cec5SDimitry Andric VT = SVT; 3310b57cec5SDimitry Andric Extend = true; 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 3360b57cec5SDimitry Andric SDValue CPIdx = 3370b57cec5SDimitry Andric DAG.getConstantPool(LLVMC, TLI.getPointerTy(DAG.getDataLayout())); 3385ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 3390b57cec5SDimitry Andric if (Extend) { 3400b57cec5SDimitry Andric SDValue Result = DAG.getExtLoad( 3410b57cec5SDimitry Andric ISD::EXTLOAD, dl, OrigVT, DAG.getEntryNode(), CPIdx, 3420b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), VT, 3430b57cec5SDimitry Andric Alignment); 3440b57cec5SDimitry Andric return Result; 3450b57cec5SDimitry Andric } 3460b57cec5SDimitry Andric SDValue Result = DAG.getLoad( 3470b57cec5SDimitry Andric OrigVT, dl, DAG.getEntryNode(), CPIdx, 3480b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 3490b57cec5SDimitry Andric return Result; 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric /// Expands the Constant node to a load from the constant pool. 3530b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandConstant(ConstantSDNode *CP) { 3540b57cec5SDimitry Andric SDLoc dl(CP); 3550b57cec5SDimitry Andric EVT VT = CP->getValueType(0); 3560b57cec5SDimitry Andric SDValue CPIdx = DAG.getConstantPool(CP->getConstantIntValue(), 3570b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 3585ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 3590b57cec5SDimitry Andric SDValue Result = DAG.getLoad( 3600b57cec5SDimitry Andric VT, dl, DAG.getEntryNode(), CPIdx, 3610b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Alignment); 3620b57cec5SDimitry Andric return Result; 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric 3650b57cec5SDimitry Andric /// Some target cannot handle a variable insertion index for the 3660b57cec5SDimitry Andric /// INSERT_VECTOR_ELT instruction. In this case, it 3670b57cec5SDimitry Andric /// is necessary to spill the vector being inserted into to memory, perform 3680b57cec5SDimitry Andric /// the insert there, and then read the result back. 3690b57cec5SDimitry Andric SDValue SelectionDAGLegalize::PerformInsertVectorEltInMemory(SDValue Vec, 3700b57cec5SDimitry Andric SDValue Val, 3710b57cec5SDimitry Andric SDValue Idx, 3720b57cec5SDimitry Andric const SDLoc &dl) { 3730b57cec5SDimitry Andric SDValue Tmp1 = Vec; 3740b57cec5SDimitry Andric SDValue Tmp2 = Val; 3750b57cec5SDimitry Andric SDValue Tmp3 = Idx; 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric // If the target doesn't support this, we have to spill the input vector 3780b57cec5SDimitry Andric // to a temporary stack slot, update the element, then reload it. This is 3790b57cec5SDimitry Andric // badness. We could also load the value into a vector register (either 3800b57cec5SDimitry Andric // with a "move to register" or "extload into register" instruction, then 3810b57cec5SDimitry Andric // permute it into place, if the idx is a constant and if the idx is 3820b57cec5SDimitry Andric // supported by the target. 3830b57cec5SDimitry Andric EVT VT = Tmp1.getValueType(); 3840b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 3850b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(VT); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // Store the vector. 3900b57cec5SDimitry Andric SDValue Ch = DAG.getStore( 3910b57cec5SDimitry Andric DAG.getEntryNode(), dl, Tmp1, StackPtr, 3920b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric SDValue StackPtr2 = TLI.getVectorElementPointer(DAG, StackPtr, VT, Tmp3); 3950b57cec5SDimitry Andric 3960b57cec5SDimitry Andric // Store the scalar value. 3975ffd83dbSDimitry Andric Ch = DAG.getTruncStore( 3985ffd83dbSDimitry Andric Ch, dl, Tmp2, StackPtr2, 3995ffd83dbSDimitry Andric MachinePointerInfo::getUnknownStack(DAG.getMachineFunction()), EltVT); 4000b57cec5SDimitry Andric // Load the updated vector. 4010b57cec5SDimitry Andric return DAG.getLoad(VT, dl, Ch, StackPtr, MachinePointerInfo::getFixedStack( 4020b57cec5SDimitry Andric DAG.getMachineFunction(), SPFI)); 4030b57cec5SDimitry Andric } 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandINSERT_VECTOR_ELT(SDValue Vec, SDValue Val, 4060b57cec5SDimitry Andric SDValue Idx, 4070b57cec5SDimitry Andric const SDLoc &dl) { 4080b57cec5SDimitry Andric if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Idx)) { 4090b57cec5SDimitry Andric // SCALAR_TO_VECTOR requires that the type of the value being inserted 4100b57cec5SDimitry Andric // match the element type of the vector being created, except for 4110b57cec5SDimitry Andric // integers in which case the inserted value can be over width. 4120b57cec5SDimitry Andric EVT EltVT = Vec.getValueType().getVectorElementType(); 4130b57cec5SDimitry Andric if (Val.getValueType() == EltVT || 4140b57cec5SDimitry Andric (EltVT.isInteger() && Val.getValueType().bitsGE(EltVT))) { 4150b57cec5SDimitry Andric SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, 4160b57cec5SDimitry Andric Vec.getValueType(), Val); 4170b57cec5SDimitry Andric 4180b57cec5SDimitry Andric unsigned NumElts = Vec.getValueType().getVectorNumElements(); 4190b57cec5SDimitry Andric // We generate a shuffle of InVec and ScVec, so the shuffle mask 4200b57cec5SDimitry Andric // should be 0,1,2,3,4,5... with the appropriate element replaced with 4210b57cec5SDimitry Andric // elt 0 of the RHS. 4220b57cec5SDimitry Andric SmallVector<int, 8> ShufOps; 4230b57cec5SDimitry Andric for (unsigned i = 0; i != NumElts; ++i) 4240b57cec5SDimitry Andric ShufOps.push_back(i != InsertPos->getZExtValue() ? i : NumElts); 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric return DAG.getVectorShuffle(Vec.getValueType(), dl, Vec, ScVec, ShufOps); 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric } 4290b57cec5SDimitry Andric return PerformInsertVectorEltInMemory(Vec, Val, Idx, dl); 4300b57cec5SDimitry Andric } 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric SDValue SelectionDAGLegalize::OptimizeFloatStore(StoreSDNode* ST) { 433480093f4SDimitry Andric if (!ISD::isNormalStore(ST)) 434480093f4SDimitry Andric return SDValue(); 435480093f4SDimitry Andric 4360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing float store operations\n"); 4370b57cec5SDimitry Andric // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr' 4380b57cec5SDimitry Andric // FIXME: move this to the DAG Combiner! Note that we can't regress due 4390b57cec5SDimitry Andric // to phase ordering between legalized code and the dag combiner. This 4400b57cec5SDimitry Andric // probably means that we need to integrate dag combiner and legalizer 4410b57cec5SDimitry Andric // together. 4420b57cec5SDimitry Andric // We generally can't do this one for long doubles. 4430b57cec5SDimitry Andric SDValue Chain = ST->getChain(); 4440b57cec5SDimitry Andric SDValue Ptr = ST->getBasePtr(); 445e8d8bef9SDimitry Andric SDValue Value = ST->getValue(); 4460b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 4470b57cec5SDimitry Andric AAMDNodes AAInfo = ST->getAAInfo(); 4480b57cec5SDimitry Andric SDLoc dl(ST); 449e8d8bef9SDimitry Andric 450e8d8bef9SDimitry Andric // Don't optimise TargetConstantFP 451e8d8bef9SDimitry Andric if (Value.getOpcode() == ISD::TargetConstantFP) 452e8d8bef9SDimitry Andric return SDValue(); 453e8d8bef9SDimitry Andric 454e8d8bef9SDimitry Andric if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(Value)) { 4550b57cec5SDimitry Andric if (CFP->getValueType(0) == MVT::f32 && 4560b57cec5SDimitry Andric TLI.isTypeLegal(MVT::i32)) { 4570b57cec5SDimitry Andric SDValue Con = DAG.getConstant(CFP->getValueAPF(). 4580b57cec5SDimitry Andric bitcastToAPInt().zextOrTrunc(32), 4590b57cec5SDimitry Andric SDLoc(CFP), MVT::i32); 4605ffd83dbSDimitry Andric return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 4615ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4645f757f3fSDimitry Andric if (CFP->getValueType(0) == MVT::f64 && 4655f757f3fSDimitry Andric !TLI.isFPImmLegal(CFP->getValueAPF(), MVT::f64)) { 4660b57cec5SDimitry Andric // If this target supports 64-bit registers, do a single 64-bit store. 4670b57cec5SDimitry Andric if (TLI.isTypeLegal(MVT::i64)) { 4680b57cec5SDimitry Andric SDValue Con = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt(). 4690b57cec5SDimitry Andric zextOrTrunc(64), SDLoc(CFP), MVT::i64); 4700b57cec5SDimitry Andric return DAG.getStore(Chain, dl, Con, Ptr, ST->getPointerInfo(), 4715ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric if (TLI.isTypeLegal(MVT::i32) && !ST->isVolatile()) { 4750b57cec5SDimitry Andric // Otherwise, if the target supports 32-bit registers, use 2 32-bit 4760b57cec5SDimitry Andric // stores. If the target supports neither 32- nor 64-bits, this 4770b57cec5SDimitry Andric // xform is certainly not worth it. 4780b57cec5SDimitry Andric const APInt &IntVal = CFP->getValueAPF().bitcastToAPInt(); 4790b57cec5SDimitry Andric SDValue Lo = DAG.getConstant(IntVal.trunc(32), dl, MVT::i32); 4800b57cec5SDimitry Andric SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), dl, MVT::i32); 4810b57cec5SDimitry Andric if (DAG.getDataLayout().isBigEndian()) 4820b57cec5SDimitry Andric std::swap(Lo, Hi); 4830b57cec5SDimitry Andric 4845ffd83dbSDimitry Andric Lo = DAG.getStore(Chain, dl, Lo, Ptr, ST->getPointerInfo(), 4855ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4865f757f3fSDimitry Andric Ptr = DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(4), dl); 4870b57cec5SDimitry Andric Hi = DAG.getStore(Chain, dl, Hi, Ptr, 4880b57cec5SDimitry Andric ST->getPointerInfo().getWithOffset(4), 4895ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric } 495e8d8bef9SDimitry Andric return SDValue(); 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeStoreOps(SDNode *Node) { 4990b57cec5SDimitry Andric StoreSDNode *ST = cast<StoreSDNode>(Node); 5000b57cec5SDimitry Andric SDValue Chain = ST->getChain(); 5010b57cec5SDimitry Andric SDValue Ptr = ST->getBasePtr(); 5020b57cec5SDimitry Andric SDLoc dl(Node); 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = ST->getMemOperand()->getFlags(); 5050b57cec5SDimitry Andric AAMDNodes AAInfo = ST->getAAInfo(); 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric if (!ST->isTruncatingStore()) { 5080b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing store operation\n"); 5090b57cec5SDimitry Andric if (SDNode *OptStore = OptimizeFloatStore(ST).getNode()) { 5100b57cec5SDimitry Andric ReplaceNode(ST, OptStore); 5110b57cec5SDimitry Andric return; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric 5140b57cec5SDimitry Andric SDValue Value = ST->getValue(); 5150b57cec5SDimitry Andric MVT VT = Value.getSimpleValueType(); 5160b57cec5SDimitry Andric switch (TLI.getOperationAction(ISD::STORE, VT)) { 5170b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 5180b57cec5SDimitry Andric case TargetLowering::Legal: { 5190b57cec5SDimitry Andric // If this is an unaligned store and the target doesn't support it, 5200b57cec5SDimitry Andric // expand it. 5210b57cec5SDimitry Andric EVT MemVT = ST->getMemoryVT(); 5220b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 5238bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 5240b57cec5SDimitry Andric *ST->getMemOperand())) { 5250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Expanding unsupported unaligned store\n"); 5260b57cec5SDimitry Andric SDValue Result = TLI.expandUnalignedStore(ST, DAG); 5270b57cec5SDimitry Andric ReplaceNode(SDValue(ST, 0), Result); 5280b57cec5SDimitry Andric } else 5290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legal store\n"); 5300b57cec5SDimitry Andric break; 5310b57cec5SDimitry Andric } 5320b57cec5SDimitry Andric case TargetLowering::Custom: { 5330b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying custom lowering\n"); 5340b57cec5SDimitry Andric SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 5350b57cec5SDimitry Andric if (Res && Res != SDValue(Node, 0)) 5360b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 5370b57cec5SDimitry Andric return; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric case TargetLowering::Promote: { 5400b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(ISD::STORE, VT); 5410b57cec5SDimitry Andric assert(NVT.getSizeInBits() == VT.getSizeInBits() && 5420b57cec5SDimitry Andric "Can only promote stores to same size type"); 5430b57cec5SDimitry Andric Value = DAG.getNode(ISD::BITCAST, dl, NVT, Value); 5445ffd83dbSDimitry Andric SDValue Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 5455ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 5460b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 5470b57cec5SDimitry Andric break; 5480b57cec5SDimitry Andric } 5490b57cec5SDimitry Andric } 5500b57cec5SDimitry Andric return; 5510b57cec5SDimitry Andric } 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing truncating store operations\n"); 5540b57cec5SDimitry Andric SDValue Value = ST->getValue(); 5550b57cec5SDimitry Andric EVT StVT = ST->getMemoryVT(); 556e8d8bef9SDimitry Andric TypeSize StWidth = StVT.getSizeInBits(); 557e8d8bef9SDimitry Andric TypeSize StSize = StVT.getStoreSizeInBits(); 5580b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 5590b57cec5SDimitry Andric 560e8d8bef9SDimitry Andric if (StWidth != StSize) { 5610b57cec5SDimitry Andric // Promote to a byte-sized store with upper bits zero if not 5620b57cec5SDimitry Andric // storing an integral number of bytes. For example, promote 5630b57cec5SDimitry Andric // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1) 564bdd1243dSDimitry Andric EVT NVT = EVT::getIntegerVT(*DAG.getContext(), StSize.getFixedValue()); 5650b57cec5SDimitry Andric Value = DAG.getZeroExtendInReg(Value, dl, StVT); 5660b57cec5SDimitry Andric SDValue Result = 5670b57cec5SDimitry Andric DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), NVT, 5685ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 5690b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 570bdd1243dSDimitry Andric } else if (!StVT.isVector() && !isPowerOf2_64(StWidth.getFixedValue())) { 5710b57cec5SDimitry Andric // If not storing a power-of-2 number of bits, expand as two stores. 5720b57cec5SDimitry Andric assert(!StVT.isVector() && "Unsupported truncstore!"); 573bdd1243dSDimitry Andric unsigned StWidthBits = StWidth.getFixedValue(); 574e8d8bef9SDimitry Andric unsigned LogStWidth = Log2_32(StWidthBits); 5750b57cec5SDimitry Andric assert(LogStWidth < 32); 5760b57cec5SDimitry Andric unsigned RoundWidth = 1 << LogStWidth; 577e8d8bef9SDimitry Andric assert(RoundWidth < StWidthBits); 578e8d8bef9SDimitry Andric unsigned ExtraWidth = StWidthBits - RoundWidth; 5790b57cec5SDimitry Andric assert(ExtraWidth < RoundWidth); 5800b57cec5SDimitry Andric assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 5810b57cec5SDimitry Andric "Store size not an integral number of bytes!"); 5820b57cec5SDimitry Andric EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 5830b57cec5SDimitry Andric EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 5840b57cec5SDimitry Andric SDValue Lo, Hi; 5850b57cec5SDimitry Andric unsigned IncrementSize; 5860b57cec5SDimitry Andric 5870b57cec5SDimitry Andric if (DL.isLittleEndian()) { 5880b57cec5SDimitry Andric // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16) 5890b57cec5SDimitry Andric // Store the bottom RoundWidth bits. 5900b57cec5SDimitry Andric Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 5915ffd83dbSDimitry Andric RoundVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric // Store the remaining ExtraWidth bits. 5940b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 5955f757f3fSDimitry Andric Ptr = 5965f757f3fSDimitry Andric DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl); 5970b57cec5SDimitry Andric Hi = DAG.getNode( 5980b57cec5SDimitry Andric ISD::SRL, dl, Value.getValueType(), Value, 5990b57cec5SDimitry Andric DAG.getConstant(RoundWidth, dl, 6000b57cec5SDimitry Andric TLI.getShiftAmountTy(Value.getValueType(), DL))); 6015ffd83dbSDimitry Andric Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, 6025ffd83dbSDimitry Andric ST->getPointerInfo().getWithOffset(IncrementSize), 6035ffd83dbSDimitry Andric ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 6040b57cec5SDimitry Andric } else { 6050b57cec5SDimitry Andric // Big endian - avoid unaligned stores. 6060b57cec5SDimitry Andric // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X 6070b57cec5SDimitry Andric // Store the top RoundWidth bits. 6080b57cec5SDimitry Andric Hi = DAG.getNode( 6090b57cec5SDimitry Andric ISD::SRL, dl, Value.getValueType(), Value, 6100b57cec5SDimitry Andric DAG.getConstant(ExtraWidth, dl, 6110b57cec5SDimitry Andric TLI.getShiftAmountTy(Value.getValueType(), DL))); 6125ffd83dbSDimitry Andric Hi = DAG.getTruncStore(Chain, dl, Hi, Ptr, ST->getPointerInfo(), RoundVT, 6135ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric // Store the remaining ExtraWidth bits. 6160b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 6170b57cec5SDimitry Andric Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, 6180b57cec5SDimitry Andric DAG.getConstant(IncrementSize, dl, 6190b57cec5SDimitry Andric Ptr.getValueType())); 6205ffd83dbSDimitry Andric Lo = DAG.getTruncStore(Chain, dl, Value, Ptr, 6215ffd83dbSDimitry Andric ST->getPointerInfo().getWithOffset(IncrementSize), 6225ffd83dbSDimitry Andric ExtraVT, ST->getOriginalAlign(), MMOFlags, AAInfo); 6230b57cec5SDimitry Andric } 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric // The order of the stores doesn't matter. 6260b57cec5SDimitry Andric SDValue Result = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo, Hi); 6270b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 6280b57cec5SDimitry Andric } else { 6290b57cec5SDimitry Andric switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) { 6300b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 6310b57cec5SDimitry Andric case TargetLowering::Legal: { 6320b57cec5SDimitry Andric EVT MemVT = ST->getMemoryVT(); 6330b57cec5SDimitry Andric // If this is an unaligned store and the target doesn't support it, 6340b57cec5SDimitry Andric // expand it. 6358bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 6360b57cec5SDimitry Andric *ST->getMemOperand())) { 6370b57cec5SDimitry Andric SDValue Result = TLI.expandUnalignedStore(ST, DAG); 6380b57cec5SDimitry Andric ReplaceNode(SDValue(ST, 0), Result); 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric break; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric case TargetLowering::Custom: { 6430b57cec5SDimitry Andric SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG); 6440b57cec5SDimitry Andric if (Res && Res != SDValue(Node, 0)) 6450b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 6460b57cec5SDimitry Andric return; 6470b57cec5SDimitry Andric } 6480b57cec5SDimitry Andric case TargetLowering::Expand: 6490b57cec5SDimitry Andric assert(!StVT.isVector() && 6500b57cec5SDimitry Andric "Vector Stores are handled in LegalizeVectorOps"); 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric SDValue Result; 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric // TRUNCSTORE:i16 i32 -> STORE i16 6550b57cec5SDimitry Andric if (TLI.isTypeLegal(StVT)) { 6560b57cec5SDimitry Andric Value = DAG.getNode(ISD::TRUNCATE, dl, StVT, Value); 6570b57cec5SDimitry Andric Result = DAG.getStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), 6585ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6590b57cec5SDimitry Andric } else { 6600b57cec5SDimitry Andric // The in-memory type isn't legal. Truncate to the type it would promote 6610b57cec5SDimitry Andric // to, and then do a truncstore. 6620b57cec5SDimitry Andric Value = DAG.getNode(ISD::TRUNCATE, dl, 6630b57cec5SDimitry Andric TLI.getTypeToTransformTo(*DAG.getContext(), StVT), 6640b57cec5SDimitry Andric Value); 6655ffd83dbSDimitry Andric Result = 6665ffd83dbSDimitry Andric DAG.getTruncStore(Chain, dl, Value, Ptr, ST->getPointerInfo(), StVT, 6675ffd83dbSDimitry Andric ST->getOriginalAlign(), MMOFlags, AAInfo); 6680b57cec5SDimitry Andric } 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Result); 6710b57cec5SDimitry Andric break; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric } 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeLoadOps(SDNode *Node) { 6770b57cec5SDimitry Andric LoadSDNode *LD = cast<LoadSDNode>(Node); 6780b57cec5SDimitry Andric SDValue Chain = LD->getChain(); // The chain. 6790b57cec5SDimitry Andric SDValue Ptr = LD->getBasePtr(); // The base pointer. 6800b57cec5SDimitry Andric SDValue Value; // The value returned by the load op. 6810b57cec5SDimitry Andric SDLoc dl(Node); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric ISD::LoadExtType ExtType = LD->getExtensionType(); 6840b57cec5SDimitry Andric if (ExtType == ISD::NON_EXTLOAD) { 6850b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing non-extending load operation\n"); 6860b57cec5SDimitry Andric MVT VT = Node->getSimpleValueType(0); 6870b57cec5SDimitry Andric SDValue RVal = SDValue(Node, 0); 6880b57cec5SDimitry Andric SDValue RChain = SDValue(Node, 1); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric switch (TLI.getOperationAction(Node->getOpcode(), VT)) { 6910b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 6920b57cec5SDimitry Andric case TargetLowering::Legal: { 6930b57cec5SDimitry Andric EVT MemVT = LD->getMemoryVT(); 6940b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 6950b57cec5SDimitry Andric // If this is an unaligned load and the target doesn't support it, 6960b57cec5SDimitry Andric // expand it. 6978bcb0991SDimitry Andric if (!TLI.allowsMemoryAccessForAlignment(*DAG.getContext(), DL, MemVT, 6980b57cec5SDimitry Andric *LD->getMemOperand())) { 6990b57cec5SDimitry Andric std::tie(RVal, RChain) = TLI.expandUnalignedLoad(LD, DAG); 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric break; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric case TargetLowering::Custom: 7040b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(RVal, DAG)) { 7050b57cec5SDimitry Andric RVal = Res; 7060b57cec5SDimitry Andric RChain = Res.getValue(1); 7070b57cec5SDimitry Andric } 7080b57cec5SDimitry Andric break; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric case TargetLowering::Promote: { 7110b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT); 7120b57cec5SDimitry Andric assert(NVT.getSizeInBits() == VT.getSizeInBits() && 7130b57cec5SDimitry Andric "Can only promote loads to same size type"); 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric SDValue Res = DAG.getLoad(NVT, dl, Chain, Ptr, LD->getMemOperand()); 7160b57cec5SDimitry Andric RVal = DAG.getNode(ISD::BITCAST, dl, VT, Res); 7170b57cec5SDimitry Andric RChain = Res.getValue(1); 7180b57cec5SDimitry Andric break; 7190b57cec5SDimitry Andric } 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric if (RChain.getNode() != Node) { 7220b57cec5SDimitry Andric assert(RVal.getNode() != Node && "Load must be completely replaced"); 7230b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), RVal); 7240b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), RChain); 7250b57cec5SDimitry Andric if (UpdatedNodes) { 7260b57cec5SDimitry Andric UpdatedNodes->insert(RVal.getNode()); 7270b57cec5SDimitry Andric UpdatedNodes->insert(RChain.getNode()); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric ReplacedNode(Node); 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric return; 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing extending load operation\n"); 7350b57cec5SDimitry Andric EVT SrcVT = LD->getMemoryVT(); 736e8d8bef9SDimitry Andric TypeSize SrcWidth = SrcVT.getSizeInBits(); 7370b57cec5SDimitry Andric MachineMemOperand::Flags MMOFlags = LD->getMemOperand()->getFlags(); 7380b57cec5SDimitry Andric AAMDNodes AAInfo = LD->getAAInfo(); 7390b57cec5SDimitry Andric 7400b57cec5SDimitry Andric if (SrcWidth != SrcVT.getStoreSizeInBits() && 7410b57cec5SDimitry Andric // Some targets pretend to have an i1 loading operation, and actually 7420b57cec5SDimitry Andric // load an i8. This trick is correct for ZEXTLOAD because the top 7 7430b57cec5SDimitry Andric // bits are guaranteed to be zero; it helps the optimizers understand 7440b57cec5SDimitry Andric // that these bits are zero. It is also useful for EXTLOAD, since it 7450b57cec5SDimitry Andric // tells the optimizers that those bits are undefined. It would be 7460b57cec5SDimitry Andric // nice to have an effective generic way of getting these benefits... 7470b57cec5SDimitry Andric // Until such a way is found, don't insist on promoting i1 here. 7480b57cec5SDimitry Andric (SrcVT != MVT::i1 || 7490b57cec5SDimitry Andric TLI.getLoadExtAction(ExtType, Node->getValueType(0), MVT::i1) == 7500b57cec5SDimitry Andric TargetLowering::Promote)) { 7510b57cec5SDimitry Andric // Promote to a byte-sized load if not loading an integral number of 7520b57cec5SDimitry Andric // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24. 7530b57cec5SDimitry Andric unsigned NewWidth = SrcVT.getStoreSizeInBits(); 7540b57cec5SDimitry Andric EVT NVT = EVT::getIntegerVT(*DAG.getContext(), NewWidth); 7550b57cec5SDimitry Andric SDValue Ch; 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric // The extra bits are guaranteed to be zero, since we stored them that 7580b57cec5SDimitry Andric // way. A zext load from NVT thus automatically gives zext from SrcVT. 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric ISD::LoadExtType NewExtType = 7610b57cec5SDimitry Andric ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD; 7620b57cec5SDimitry Andric 7635ffd83dbSDimitry Andric SDValue Result = DAG.getExtLoad(NewExtType, dl, Node->getValueType(0), 7645ffd83dbSDimitry Andric Chain, Ptr, LD->getPointerInfo(), NVT, 7655ffd83dbSDimitry Andric LD->getOriginalAlign(), MMOFlags, AAInfo); 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric Ch = Result.getValue(1); // The chain. 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric if (ExtType == ISD::SEXTLOAD) 7700b57cec5SDimitry Andric // Having the top bits zero doesn't help when sign extending. 7710b57cec5SDimitry Andric Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 7720b57cec5SDimitry Andric Result.getValueType(), 7730b57cec5SDimitry Andric Result, DAG.getValueType(SrcVT)); 7740b57cec5SDimitry Andric else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType()) 7750b57cec5SDimitry Andric // All the top bits are guaranteed to be zero - inform the optimizers. 7760b57cec5SDimitry Andric Result = DAG.getNode(ISD::AssertZext, dl, 7770b57cec5SDimitry Andric Result.getValueType(), Result, 7780b57cec5SDimitry Andric DAG.getValueType(SrcVT)); 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric Value = Result; 7810b57cec5SDimitry Andric Chain = Ch; 782bdd1243dSDimitry Andric } else if (!isPowerOf2_64(SrcWidth.getKnownMinValue())) { 7830b57cec5SDimitry Andric // If not loading a power-of-2 number of bits, expand as two loads. 7840b57cec5SDimitry Andric assert(!SrcVT.isVector() && "Unsupported extload!"); 785bdd1243dSDimitry Andric unsigned SrcWidthBits = SrcWidth.getFixedValue(); 786e8d8bef9SDimitry Andric unsigned LogSrcWidth = Log2_32(SrcWidthBits); 7870b57cec5SDimitry Andric assert(LogSrcWidth < 32); 7880b57cec5SDimitry Andric unsigned RoundWidth = 1 << LogSrcWidth; 789e8d8bef9SDimitry Andric assert(RoundWidth < SrcWidthBits); 790e8d8bef9SDimitry Andric unsigned ExtraWidth = SrcWidthBits - RoundWidth; 7910b57cec5SDimitry Andric assert(ExtraWidth < RoundWidth); 7920b57cec5SDimitry Andric assert(!(RoundWidth % 8) && !(ExtraWidth % 8) && 7930b57cec5SDimitry Andric "Load size not an integral number of bytes!"); 7940b57cec5SDimitry Andric EVT RoundVT = EVT::getIntegerVT(*DAG.getContext(), RoundWidth); 7950b57cec5SDimitry Andric EVT ExtraVT = EVT::getIntegerVT(*DAG.getContext(), ExtraWidth); 7960b57cec5SDimitry Andric SDValue Lo, Hi, Ch; 7970b57cec5SDimitry Andric unsigned IncrementSize; 7980b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric if (DL.isLittleEndian()) { 8010b57cec5SDimitry Andric // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16) 8020b57cec5SDimitry Andric // Load the bottom RoundWidth bits. 8030b57cec5SDimitry Andric Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 8045ffd83dbSDimitry Andric LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(), 8055ffd83dbSDimitry Andric MMOFlags, AAInfo); 8060b57cec5SDimitry Andric 8070b57cec5SDimitry Andric // Load the remaining ExtraWidth bits. 8080b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 8095f757f3fSDimitry Andric Ptr = 8105f757f3fSDimitry Andric DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl); 8110b57cec5SDimitry Andric Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 8120b57cec5SDimitry Andric LD->getPointerInfo().getWithOffset(IncrementSize), 8135ffd83dbSDimitry Andric ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo); 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric // Build a factor node to remember that this load is independent of 8160b57cec5SDimitry Andric // the other one. 8170b57cec5SDimitry Andric Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 8180b57cec5SDimitry Andric Hi.getValue(1)); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric // Move the top bits to the right place. 8210b57cec5SDimitry Andric Hi = DAG.getNode( 8220b57cec5SDimitry Andric ISD::SHL, dl, Hi.getValueType(), Hi, 8230b57cec5SDimitry Andric DAG.getConstant(RoundWidth, dl, 8240b57cec5SDimitry Andric TLI.getShiftAmountTy(Hi.getValueType(), DL))); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric // Join the hi and lo parts. 8270b57cec5SDimitry Andric Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 8280b57cec5SDimitry Andric } else { 8290b57cec5SDimitry Andric // Big endian - avoid unaligned loads. 8300b57cec5SDimitry Andric // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8 8310b57cec5SDimitry Andric // Load the top RoundWidth bits. 8320b57cec5SDimitry Andric Hi = DAG.getExtLoad(ExtType, dl, Node->getValueType(0), Chain, Ptr, 8335ffd83dbSDimitry Andric LD->getPointerInfo(), RoundVT, LD->getOriginalAlign(), 8345ffd83dbSDimitry Andric MMOFlags, AAInfo); 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric // Load the remaining ExtraWidth bits. 8370b57cec5SDimitry Andric IncrementSize = RoundWidth / 8; 8385f757f3fSDimitry Andric Ptr = 8395f757f3fSDimitry Andric DAG.getMemBasePlusOffset(Ptr, TypeSize::getFixed(IncrementSize), dl); 8400b57cec5SDimitry Andric Lo = DAG.getExtLoad(ISD::ZEXTLOAD, dl, Node->getValueType(0), Chain, Ptr, 8410b57cec5SDimitry Andric LD->getPointerInfo().getWithOffset(IncrementSize), 8425ffd83dbSDimitry Andric ExtraVT, LD->getOriginalAlign(), MMOFlags, AAInfo); 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric // Build a factor node to remember that this load is independent of 8450b57cec5SDimitry Andric // the other one. 8460b57cec5SDimitry Andric Ch = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Lo.getValue(1), 8470b57cec5SDimitry Andric Hi.getValue(1)); 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric // Move the top bits to the right place. 8500b57cec5SDimitry Andric Hi = DAG.getNode( 8510b57cec5SDimitry Andric ISD::SHL, dl, Hi.getValueType(), Hi, 8520b57cec5SDimitry Andric DAG.getConstant(ExtraWidth, dl, 8530b57cec5SDimitry Andric TLI.getShiftAmountTy(Hi.getValueType(), DL))); 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric // Join the hi and lo parts. 8560b57cec5SDimitry Andric Value = DAG.getNode(ISD::OR, dl, Node->getValueType(0), Lo, Hi); 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric Chain = Ch; 8600b57cec5SDimitry Andric } else { 8610b57cec5SDimitry Andric bool isCustom = false; 8620b57cec5SDimitry Andric switch (TLI.getLoadExtAction(ExtType, Node->getValueType(0), 8630b57cec5SDimitry Andric SrcVT.getSimpleVT())) { 8640b57cec5SDimitry Andric default: llvm_unreachable("This action is not supported yet!"); 8650b57cec5SDimitry Andric case TargetLowering::Custom: 8660b57cec5SDimitry Andric isCustom = true; 867bdd1243dSDimitry Andric [[fallthrough]]; 8680b57cec5SDimitry Andric case TargetLowering::Legal: 8690b57cec5SDimitry Andric Value = SDValue(Node, 0); 8700b57cec5SDimitry Andric Chain = SDValue(Node, 1); 8710b57cec5SDimitry Andric 8720b57cec5SDimitry Andric if (isCustom) { 8730b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 8740b57cec5SDimitry Andric Value = Res; 8750b57cec5SDimitry Andric Chain = Res.getValue(1); 8760b57cec5SDimitry Andric } 8770b57cec5SDimitry Andric } else { 8780b57cec5SDimitry Andric // If this is an unaligned load and the target doesn't support it, 8790b57cec5SDimitry Andric // expand it. 8800b57cec5SDimitry Andric EVT MemVT = LD->getMemoryVT(); 8810b57cec5SDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 8820b57cec5SDimitry Andric if (!TLI.allowsMemoryAccess(*DAG.getContext(), DL, MemVT, 8830b57cec5SDimitry Andric *LD->getMemOperand())) { 8840b57cec5SDimitry Andric std::tie(Value, Chain) = TLI.expandUnalignedLoad(LD, DAG); 8850b57cec5SDimitry Andric } 8860b57cec5SDimitry Andric } 8870b57cec5SDimitry Andric break; 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric case TargetLowering::Expand: { 8900b57cec5SDimitry Andric EVT DestVT = Node->getValueType(0); 8910b57cec5SDimitry Andric if (!TLI.isLoadExtLegal(ISD::EXTLOAD, DestVT, SrcVT)) { 8920b57cec5SDimitry Andric // If the source type is not legal, see if there is a legal extload to 8930b57cec5SDimitry Andric // an intermediate type that we can then extend further. 8940b57cec5SDimitry Andric EVT LoadVT = TLI.getRegisterType(SrcVT.getSimpleVT()); 89506c3fb27SDimitry Andric if ((LoadVT.isFloatingPoint() == SrcVT.isFloatingPoint()) && 89606c3fb27SDimitry Andric (TLI.isTypeLegal(SrcVT) || // Same as SrcVT == LoadVT? 89706c3fb27SDimitry Andric TLI.isLoadExtLegal(ExtType, LoadVT, SrcVT))) { 8980b57cec5SDimitry Andric // If we are loading a legal type, this is a non-extload followed by a 8990b57cec5SDimitry Andric // full extend. 9000b57cec5SDimitry Andric ISD::LoadExtType MidExtType = 9010b57cec5SDimitry Andric (LoadVT == SrcVT) ? ISD::NON_EXTLOAD : ExtType; 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric SDValue Load = DAG.getExtLoad(MidExtType, dl, LoadVT, Chain, Ptr, 9040b57cec5SDimitry Andric SrcVT, LD->getMemOperand()); 9050b57cec5SDimitry Andric unsigned ExtendOp = 9060b57cec5SDimitry Andric ISD::getExtForLoadExtType(SrcVT.isFloatingPoint(), ExtType); 9070b57cec5SDimitry Andric Value = DAG.getNode(ExtendOp, dl, Node->getValueType(0), Load); 9080b57cec5SDimitry Andric Chain = Load.getValue(1); 9090b57cec5SDimitry Andric break; 9100b57cec5SDimitry Andric } 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric // Handle the special case of fp16 extloads. EXTLOAD doesn't have the 9130b57cec5SDimitry Andric // normal undefined upper bits behavior to allow using an in-reg extend 9140b57cec5SDimitry Andric // with the illegal FP type, so load as an integer and do the 9150b57cec5SDimitry Andric // from-integer conversion. 916*cb14a3feSDimitry Andric EVT SVT = SrcVT.getScalarType(); 917*cb14a3feSDimitry Andric if (SVT == MVT::f16 || SVT == MVT::bf16) { 9180b57cec5SDimitry Andric EVT ISrcVT = SrcVT.changeTypeToInteger(); 9190b57cec5SDimitry Andric EVT IDestVT = DestVT.changeTypeToInteger(); 9208bcb0991SDimitry Andric EVT ILoadVT = TLI.getRegisterType(IDestVT.getSimpleVT()); 9210b57cec5SDimitry Andric 9228bcb0991SDimitry Andric SDValue Result = DAG.getExtLoad(ISD::ZEXTLOAD, dl, ILoadVT, Chain, 9238bcb0991SDimitry Andric Ptr, ISrcVT, LD->getMemOperand()); 924*cb14a3feSDimitry Andric Value = 925*cb14a3feSDimitry Andric DAG.getNode(SVT == MVT::f16 ? ISD::FP16_TO_FP : ISD::BF16_TO_FP, 926*cb14a3feSDimitry Andric dl, DestVT, Result); 9270b57cec5SDimitry Andric Chain = Result.getValue(1); 9280b57cec5SDimitry Andric break; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric 9320b57cec5SDimitry Andric assert(!SrcVT.isVector() && 9330b57cec5SDimitry Andric "Vector Loads are handled in LegalizeVectorOps"); 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric // FIXME: This does not work for vectors on most targets. Sign- 9360b57cec5SDimitry Andric // and zero-extend operations are currently folded into extending 9370b57cec5SDimitry Andric // loads, whether they are legal or not, and then we end up here 9380b57cec5SDimitry Andric // without any support for legalizing them. 9390b57cec5SDimitry Andric assert(ExtType != ISD::EXTLOAD && 9400b57cec5SDimitry Andric "EXTLOAD should always be supported!"); 9410b57cec5SDimitry Andric // Turn the unsupported load into an EXTLOAD followed by an 9420b57cec5SDimitry Andric // explicit zero/sign extend inreg. 9430b57cec5SDimitry Andric SDValue Result = DAG.getExtLoad(ISD::EXTLOAD, dl, 9440b57cec5SDimitry Andric Node->getValueType(0), 9450b57cec5SDimitry Andric Chain, Ptr, SrcVT, 9460b57cec5SDimitry Andric LD->getMemOperand()); 9470b57cec5SDimitry Andric SDValue ValRes; 9480b57cec5SDimitry Andric if (ExtType == ISD::SEXTLOAD) 9490b57cec5SDimitry Andric ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, 9500b57cec5SDimitry Andric Result.getValueType(), 9510b57cec5SDimitry Andric Result, DAG.getValueType(SrcVT)); 9520b57cec5SDimitry Andric else 9535ffd83dbSDimitry Andric ValRes = DAG.getZeroExtendInReg(Result, dl, SrcVT); 9540b57cec5SDimitry Andric Value = ValRes; 9550b57cec5SDimitry Andric Chain = Result.getValue(1); 9560b57cec5SDimitry Andric break; 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric // Since loads produce two values, make sure to remember that we legalized 9620b57cec5SDimitry Andric // both of them. 9630b57cec5SDimitry Andric if (Chain.getNode() != Node) { 9640b57cec5SDimitry Andric assert(Value.getNode() != Node && "Load must be completely replaced"); 9650b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Value); 9660b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 9670b57cec5SDimitry Andric if (UpdatedNodes) { 9680b57cec5SDimitry Andric UpdatedNodes->insert(Value.getNode()); 9690b57cec5SDimitry Andric UpdatedNodes->insert(Chain.getNode()); 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric ReplacedNode(Node); 9720b57cec5SDimitry Andric } 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric /// Return a legal replacement for the given operation, with all legal operands. 9760b57cec5SDimitry Andric void SelectionDAGLegalize::LegalizeOp(SDNode *Node) { 9770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nLegalizing: "; Node->dump(&DAG)); 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric // Allow illegal target nodes and illegal registers. 9800b57cec5SDimitry Andric if (Node->getOpcode() == ISD::TargetConstant || 9810b57cec5SDimitry Andric Node->getOpcode() == ISD::Register) 9820b57cec5SDimitry Andric return; 9830b57cec5SDimitry Andric 9840b57cec5SDimitry Andric #ifndef NDEBUG 9850b57cec5SDimitry Andric for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) 9868bcb0991SDimitry Andric assert(TLI.getTypeAction(*DAG.getContext(), Node->getValueType(i)) == 9878bcb0991SDimitry Andric TargetLowering::TypeLegal && 9880b57cec5SDimitry Andric "Unexpected illegal type!"); 9890b57cec5SDimitry Andric 9900b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) 9910b57cec5SDimitry Andric assert((TLI.getTypeAction(*DAG.getContext(), Op.getValueType()) == 9920b57cec5SDimitry Andric TargetLowering::TypeLegal || 9930b57cec5SDimitry Andric Op.getOpcode() == ISD::TargetConstant || 9940b57cec5SDimitry Andric Op.getOpcode() == ISD::Register) && 9950b57cec5SDimitry Andric "Unexpected illegal type!"); 9960b57cec5SDimitry Andric #endif 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric // Figure out the correct action; the way to query this varies by opcode 9990b57cec5SDimitry Andric TargetLowering::LegalizeAction Action = TargetLowering::Legal; 10000b57cec5SDimitry Andric bool SimpleFinishLegalizing = true; 10010b57cec5SDimitry Andric switch (Node->getOpcode()) { 10020b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 10030b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 10040b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 10050b57cec5SDimitry Andric case ISD::STACKSAVE: 10060b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 10070b57cec5SDimitry Andric break; 10080b57cec5SDimitry Andric case ISD::GET_DYNAMIC_AREA_OFFSET: 10090b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10100b57cec5SDimitry Andric Node->getValueType(0)); 10110b57cec5SDimitry Andric break; 10120b57cec5SDimitry Andric case ISD::VAARG: 10130b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10140b57cec5SDimitry Andric Node->getValueType(0)); 10150b57cec5SDimitry Andric if (Action != TargetLowering::Promote) 10160b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::Other); 10170b57cec5SDimitry Andric break; 101806c3fb27SDimitry Andric case ISD::SET_FPENV: 10195f757f3fSDimitry Andric case ISD::SET_FPMODE: 102006c3fb27SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 102106c3fb27SDimitry Andric Node->getOperand(1).getValueType()); 102206c3fb27SDimitry Andric break; 10230b57cec5SDimitry Andric case ISD::FP_TO_FP16: 102481ad6265SDimitry Andric case ISD::FP_TO_BF16: 10250b57cec5SDimitry Andric case ISD::SINT_TO_FP: 10260b57cec5SDimitry Andric case ISD::UINT_TO_FP: 10270b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 10280b57cec5SDimitry Andric case ISD::LROUND: 10290b57cec5SDimitry Andric case ISD::LLROUND: 10300b57cec5SDimitry Andric case ISD::LRINT: 10310b57cec5SDimitry Andric case ISD::LLRINT: 10320b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10330b57cec5SDimitry Andric Node->getOperand(0).getValueType()); 10340b57cec5SDimitry Andric break; 10355ffd83dbSDimitry Andric case ISD::STRICT_FP_TO_FP16: 1036480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 1037480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 1038480093f4SDimitry Andric case ISD::STRICT_LRINT: 1039480093f4SDimitry Andric case ISD::STRICT_LLRINT: 1040480093f4SDimitry Andric case ISD::STRICT_LROUND: 1041480093f4SDimitry Andric case ISD::STRICT_LLROUND: 1042480093f4SDimitry Andric // These pseudo-ops are the same as the other STRICT_ ops except 1043480093f4SDimitry Andric // they are registered with setOperationAction() using the input type 1044480093f4SDimitry Andric // instead of the output type. 1045480093f4SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 1046480093f4SDimitry Andric Node->getOperand(1).getValueType()); 1047480093f4SDimitry Andric break; 10480b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: { 10490b57cec5SDimitry Andric EVT InnerType = cast<VTSDNode>(Node->getOperand(1))->getVT(); 10500b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), InnerType); 10510b57cec5SDimitry Andric break; 10520b57cec5SDimitry Andric } 10530b57cec5SDimitry Andric case ISD::ATOMIC_STORE: 10540b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10555f757f3fSDimitry Andric Node->getOperand(1).getValueType()); 10560b57cec5SDimitry Andric break; 10570b57cec5SDimitry Andric case ISD::SELECT_CC: 1058480093f4SDimitry Andric case ISD::STRICT_FSETCC: 1059480093f4SDimitry Andric case ISD::STRICT_FSETCCS: 10600b57cec5SDimitry Andric case ISD::SETCC: 1061bdd1243dSDimitry Andric case ISD::SETCCCARRY: 106281ad6265SDimitry Andric case ISD::VP_SETCC: 10630b57cec5SDimitry Andric case ISD::BR_CC: { 106481ad6265SDimitry Andric unsigned Opc = Node->getOpcode(); 106581ad6265SDimitry Andric unsigned CCOperand = Opc == ISD::SELECT_CC ? 4 106681ad6265SDimitry Andric : Opc == ISD::STRICT_FSETCC ? 3 106781ad6265SDimitry Andric : Opc == ISD::STRICT_FSETCCS ? 3 1068bdd1243dSDimitry Andric : Opc == ISD::SETCCCARRY ? 3 106981ad6265SDimitry Andric : (Opc == ISD::SETCC || Opc == ISD::VP_SETCC) ? 2 107081ad6265SDimitry Andric : 1; 107181ad6265SDimitry Andric unsigned CompareOperand = Opc == ISD::BR_CC ? 2 107281ad6265SDimitry Andric : Opc == ISD::STRICT_FSETCC ? 1 107381ad6265SDimitry Andric : Opc == ISD::STRICT_FSETCCS ? 1 107481ad6265SDimitry Andric : 0; 10750b57cec5SDimitry Andric MVT OpVT = Node->getOperand(CompareOperand).getSimpleValueType(); 10760b57cec5SDimitry Andric ISD::CondCode CCCode = 10770b57cec5SDimitry Andric cast<CondCodeSDNode>(Node->getOperand(CCOperand))->get(); 10780b57cec5SDimitry Andric Action = TLI.getCondCodeAction(CCCode, OpVT); 10790b57cec5SDimitry Andric if (Action == TargetLowering::Legal) { 10800b57cec5SDimitry Andric if (Node->getOpcode() == ISD::SELECT_CC) 10810b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 10820b57cec5SDimitry Andric Node->getValueType(0)); 10830b57cec5SDimitry Andric else 10840b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), OpVT); 10850b57cec5SDimitry Andric } 10860b57cec5SDimitry Andric break; 10870b57cec5SDimitry Andric } 10880b57cec5SDimitry Andric case ISD::LOAD: 10890b57cec5SDimitry Andric case ISD::STORE: 10900b57cec5SDimitry Andric // FIXME: Model these properly. LOAD and STORE are complicated, and 10910b57cec5SDimitry Andric // STORE expects the unlegalized operand in some cases. 10920b57cec5SDimitry Andric SimpleFinishLegalizing = false; 10930b57cec5SDimitry Andric break; 10940b57cec5SDimitry Andric case ISD::CALLSEQ_START: 10950b57cec5SDimitry Andric case ISD::CALLSEQ_END: 10960b57cec5SDimitry Andric // FIXME: This shouldn't be necessary. These nodes have special properties 10970b57cec5SDimitry Andric // dealing with the recursive nature of legalization. Removing this 10980b57cec5SDimitry Andric // special case should be done as part of making LegalizeDAG non-recursive. 10990b57cec5SDimitry Andric SimpleFinishLegalizing = false; 11000b57cec5SDimitry Andric break; 11010b57cec5SDimitry Andric case ISD::EXTRACT_ELEMENT: 1102bdd1243dSDimitry Andric case ISD::GET_ROUNDING: 11030b57cec5SDimitry Andric case ISD::MERGE_VALUES: 11040b57cec5SDimitry Andric case ISD::EH_RETURN: 11050b57cec5SDimitry Andric case ISD::FRAME_TO_ARGS_OFFSET: 11060b57cec5SDimitry Andric case ISD::EH_DWARF_CFA: 11070b57cec5SDimitry Andric case ISD::EH_SJLJ_SETJMP: 11080b57cec5SDimitry Andric case ISD::EH_SJLJ_LONGJMP: 11090b57cec5SDimitry Andric case ISD::EH_SJLJ_SETUP_DISPATCH: 11100b57cec5SDimitry Andric // These operations lie about being legal: when they claim to be legal, 11110b57cec5SDimitry Andric // they should actually be expanded. 11120b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11130b57cec5SDimitry Andric if (Action == TargetLowering::Legal) 11140b57cec5SDimitry Andric Action = TargetLowering::Expand; 11150b57cec5SDimitry Andric break; 11160b57cec5SDimitry Andric case ISD::INIT_TRAMPOLINE: 11170b57cec5SDimitry Andric case ISD::ADJUST_TRAMPOLINE: 11180b57cec5SDimitry Andric case ISD::FRAMEADDR: 11190b57cec5SDimitry Andric case ISD::RETURNADDR: 11200b57cec5SDimitry Andric case ISD::ADDROFRETURNADDR: 11210b57cec5SDimitry Andric case ISD::SPONENTRY: 11220b57cec5SDimitry Andric // These operations lie about being legal: when they claim to be legal, 11230b57cec5SDimitry Andric // they should actually be custom-lowered. 11240b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11250b57cec5SDimitry Andric if (Action == TargetLowering::Legal) 11260b57cec5SDimitry Andric Action = TargetLowering::Custom; 11270b57cec5SDimitry Andric break; 11280b57cec5SDimitry Andric case ISD::READCYCLECOUNTER: 11290b57cec5SDimitry Andric // READCYCLECOUNTER returns an i64, even if type legalization might have 11300b57cec5SDimitry Andric // expanded that to several smaller types. 11310b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), MVT::i64); 11320b57cec5SDimitry Andric break; 11330b57cec5SDimitry Andric case ISD::READ_REGISTER: 11340b57cec5SDimitry Andric case ISD::WRITE_REGISTER: 11350b57cec5SDimitry Andric // Named register is legal in the DAG, but blocked by register name 11360b57cec5SDimitry Andric // selection if not implemented by target (to chose the correct register) 11370b57cec5SDimitry Andric // They'll be converted to Copy(To/From)Reg. 11380b57cec5SDimitry Andric Action = TargetLowering::Legal; 11390b57cec5SDimitry Andric break; 1140e8d8bef9SDimitry Andric case ISD::UBSANTRAP: 1141e8d8bef9SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 1142e8d8bef9SDimitry Andric if (Action == TargetLowering::Expand) { 1143e8d8bef9SDimitry Andric // replace ISD::UBSANTRAP with ISD::TRAP 1144e8d8bef9SDimitry Andric SDValue NewVal; 1145e8d8bef9SDimitry Andric NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 1146e8d8bef9SDimitry Andric Node->getOperand(0)); 1147e8d8bef9SDimitry Andric ReplaceNode(Node, NewVal.getNode()); 1148e8d8bef9SDimitry Andric LegalizeOp(NewVal.getNode()); 1149e8d8bef9SDimitry Andric return; 1150e8d8bef9SDimitry Andric } 1151e8d8bef9SDimitry Andric break; 11520b57cec5SDimitry Andric case ISD::DEBUGTRAP: 11530b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11540b57cec5SDimitry Andric if (Action == TargetLowering::Expand) { 11550b57cec5SDimitry Andric // replace ISD::DEBUGTRAP with ISD::TRAP 11560b57cec5SDimitry Andric SDValue NewVal; 11570b57cec5SDimitry Andric NewVal = DAG.getNode(ISD::TRAP, SDLoc(Node), Node->getVTList(), 11580b57cec5SDimitry Andric Node->getOperand(0)); 11590b57cec5SDimitry Andric ReplaceNode(Node, NewVal.getNode()); 11600b57cec5SDimitry Andric LegalizeOp(NewVal.getNode()); 11610b57cec5SDimitry Andric return; 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric break; 11640b57cec5SDimitry Andric case ISD::SADDSAT: 11650b57cec5SDimitry Andric case ISD::UADDSAT: 11660b57cec5SDimitry Andric case ISD::SSUBSAT: 1167e8d8bef9SDimitry Andric case ISD::USUBSAT: 1168e8d8bef9SDimitry Andric case ISD::SSHLSAT: 1169e8d8bef9SDimitry Andric case ISD::USHLSAT: 1170e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 1171e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 11720b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 11730b57cec5SDimitry Andric break; 11740b57cec5SDimitry Andric case ISD::SMULFIX: 11750b57cec5SDimitry Andric case ISD::SMULFIXSAT: 11768bcb0991SDimitry Andric case ISD::UMULFIX: 1177480093f4SDimitry Andric case ISD::UMULFIXSAT: 1178480093f4SDimitry Andric case ISD::SDIVFIX: 11795ffd83dbSDimitry Andric case ISD::SDIVFIXSAT: 11805ffd83dbSDimitry Andric case ISD::UDIVFIX: 11815ffd83dbSDimitry Andric case ISD::UDIVFIXSAT: { 11820b57cec5SDimitry Andric unsigned Scale = Node->getConstantOperandVal(2); 11830b57cec5SDimitry Andric Action = TLI.getFixedPointOperationAction(Node->getOpcode(), 11840b57cec5SDimitry Andric Node->getValueType(0), Scale); 11850b57cec5SDimitry Andric break; 11860b57cec5SDimitry Andric } 11870b57cec5SDimitry Andric case ISD::MSCATTER: 11880b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 11890b57cec5SDimitry Andric cast<MaskedScatterSDNode>(Node)->getValue().getValueType()); 11900b57cec5SDimitry Andric break; 11910b57cec5SDimitry Andric case ISD::MSTORE: 11920b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), 11930b57cec5SDimitry Andric cast<MaskedStoreSDNode>(Node)->getValue().getValueType()); 11940b57cec5SDimitry Andric break; 1195349cc55cSDimitry Andric case ISD::VP_SCATTER: 1196349cc55cSDimitry Andric Action = TLI.getOperationAction( 1197349cc55cSDimitry Andric Node->getOpcode(), 1198349cc55cSDimitry Andric cast<VPScatterSDNode>(Node)->getValue().getValueType()); 1199349cc55cSDimitry Andric break; 1200349cc55cSDimitry Andric case ISD::VP_STORE: 1201349cc55cSDimitry Andric Action = TLI.getOperationAction( 1202349cc55cSDimitry Andric Node->getOpcode(), 1203349cc55cSDimitry Andric cast<VPStoreSDNode>(Node)->getValue().getValueType()); 1204349cc55cSDimitry Andric break; 120581ad6265SDimitry Andric case ISD::EXPERIMENTAL_VP_STRIDED_STORE: 120681ad6265SDimitry Andric Action = TLI.getOperationAction( 120781ad6265SDimitry Andric Node->getOpcode(), 120881ad6265SDimitry Andric cast<VPStridedStoreSDNode>(Node)->getValue().getValueType()); 120981ad6265SDimitry Andric break; 12100b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 12110b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 12120b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 12130b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 12140b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 12150b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 12160b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 12170b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 12180b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 12190b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 12200b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 12210b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 12220b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 122306c3fb27SDimitry Andric case ISD::VECREDUCE_FMAXIMUM: 122406c3fb27SDimitry Andric case ISD::VECREDUCE_FMINIMUM: 122581ad6265SDimitry Andric case ISD::IS_FPCLASS: 12260b57cec5SDimitry Andric Action = TLI.getOperationAction( 12270b57cec5SDimitry Andric Node->getOpcode(), Node->getOperand(0).getValueType()); 12280b57cec5SDimitry Andric break; 1229e8d8bef9SDimitry Andric case ISD::VECREDUCE_SEQ_FADD: 1230349cc55cSDimitry Andric case ISD::VECREDUCE_SEQ_FMUL: 1231349cc55cSDimitry Andric case ISD::VP_REDUCE_FADD: 1232349cc55cSDimitry Andric case ISD::VP_REDUCE_FMUL: 1233349cc55cSDimitry Andric case ISD::VP_REDUCE_ADD: 1234349cc55cSDimitry Andric case ISD::VP_REDUCE_MUL: 1235349cc55cSDimitry Andric case ISD::VP_REDUCE_AND: 1236349cc55cSDimitry Andric case ISD::VP_REDUCE_OR: 1237349cc55cSDimitry Andric case ISD::VP_REDUCE_XOR: 1238349cc55cSDimitry Andric case ISD::VP_REDUCE_SMAX: 1239349cc55cSDimitry Andric case ISD::VP_REDUCE_SMIN: 1240349cc55cSDimitry Andric case ISD::VP_REDUCE_UMAX: 1241349cc55cSDimitry Andric case ISD::VP_REDUCE_UMIN: 1242349cc55cSDimitry Andric case ISD::VP_REDUCE_FMAX: 1243349cc55cSDimitry Andric case ISD::VP_REDUCE_FMIN: 1244349cc55cSDimitry Andric case ISD::VP_REDUCE_SEQ_FADD: 1245349cc55cSDimitry Andric case ISD::VP_REDUCE_SEQ_FMUL: 1246e8d8bef9SDimitry Andric Action = TLI.getOperationAction( 1247e8d8bef9SDimitry Andric Node->getOpcode(), Node->getOperand(1).getValueType()); 1248e8d8bef9SDimitry Andric break; 12490b57cec5SDimitry Andric default: 12500b57cec5SDimitry Andric if (Node->getOpcode() >= ISD::BUILTIN_OP_END) { 125181ad6265SDimitry Andric Action = TLI.getCustomOperationAction(*Node); 12520b57cec5SDimitry Andric } else { 12530b57cec5SDimitry Andric Action = TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)); 12540b57cec5SDimitry Andric } 12550b57cec5SDimitry Andric break; 12560b57cec5SDimitry Andric } 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric if (SimpleFinishLegalizing) { 12590b57cec5SDimitry Andric SDNode *NewNode = Node; 12600b57cec5SDimitry Andric switch (Node->getOpcode()) { 12610b57cec5SDimitry Andric default: break; 12620b57cec5SDimitry Andric case ISD::SHL: 12630b57cec5SDimitry Andric case ISD::SRL: 12640b57cec5SDimitry Andric case ISD::SRA: 12650b57cec5SDimitry Andric case ISD::ROTL: 12660b57cec5SDimitry Andric case ISD::ROTR: { 12670b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12680b57cec5SDimitry Andric // to the appropriate width. 12690b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12700b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12710b57cec5SDimitry Andric if (!Op1.getValueType().isVector()) { 12720b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op1); 12730b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12740b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12750b57cec5SDimitry Andric // to update the parent node. 12760b57cec5SDimitry Andric // Do not try to legalize SAO here! It will be automatically legalized 12770b57cec5SDimitry Andric // in the next round. 12780b57cec5SDimitry Andric if (SAO != Op1) 12790b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, SAO); 12800b57cec5SDimitry Andric } 12810b57cec5SDimitry Andric } 12820b57cec5SDimitry Andric break; 12830b57cec5SDimitry Andric case ISD::FSHL: 12840b57cec5SDimitry Andric case ISD::FSHR: 12850b57cec5SDimitry Andric case ISD::SRL_PARTS: 12860b57cec5SDimitry Andric case ISD::SRA_PARTS: 12870b57cec5SDimitry Andric case ISD::SHL_PARTS: { 12880b57cec5SDimitry Andric // Legalizing shifts/rotates requires adjusting the shift amount 12890b57cec5SDimitry Andric // to the appropriate width. 12900b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 12910b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 12920b57cec5SDimitry Andric SDValue Op2 = Node->getOperand(2); 12930b57cec5SDimitry Andric if (!Op2.getValueType().isVector()) { 12940b57cec5SDimitry Andric SDValue SAO = DAG.getShiftAmountOperand(Op0.getValueType(), Op2); 12950b57cec5SDimitry Andric // The getShiftAmountOperand() may create a new operand node or 12960b57cec5SDimitry Andric // return the existing one. If new operand is created we need 12970b57cec5SDimitry Andric // to update the parent node. 12980b57cec5SDimitry Andric if (SAO != Op2) 12990b57cec5SDimitry Andric NewNode = DAG.UpdateNodeOperands(Node, Op0, Op1, SAO); 13000b57cec5SDimitry Andric } 13010b57cec5SDimitry Andric break; 13020b57cec5SDimitry Andric } 13030b57cec5SDimitry Andric } 13040b57cec5SDimitry Andric 13050b57cec5SDimitry Andric if (NewNode != Node) { 13060b57cec5SDimitry Andric ReplaceNode(Node, NewNode); 13070b57cec5SDimitry Andric Node = NewNode; 13080b57cec5SDimitry Andric } 13090b57cec5SDimitry Andric switch (Action) { 13100b57cec5SDimitry Andric case TargetLowering::Legal: 13110b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legal node: nothing to do\n"); 13120b57cec5SDimitry Andric return; 13130b57cec5SDimitry Andric case TargetLowering::Custom: 13140b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying custom legalization\n"); 13150b57cec5SDimitry Andric // FIXME: The handling for custom lowering with multiple results is 13160b57cec5SDimitry Andric // a complete mess. 13170b57cec5SDimitry Andric if (SDValue Res = TLI.LowerOperation(SDValue(Node, 0), DAG)) { 13180b57cec5SDimitry Andric if (!(Res.getNode() != Node || Res.getResNo() != 0)) 13190b57cec5SDimitry Andric return; 13200b57cec5SDimitry Andric 13210b57cec5SDimitry Andric if (Node->getNumValues() == 1) { 1322fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1323fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1324fe6060f1SDimitry Andric assert((Res.getValueType() == Node->getValueType(0) || 1325fe6060f1SDimitry Andric Node->getValueType(0) == MVT::Glue) && 1326fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 13270b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 13280b57cec5SDimitry Andric // We can just directly replace this node with the lowered value. 13290b57cec5SDimitry Andric ReplaceNode(SDValue(Node, 0), Res); 13300b57cec5SDimitry Andric return; 13310b57cec5SDimitry Andric } 13320b57cec5SDimitry Andric 13330b57cec5SDimitry Andric SmallVector<SDValue, 8> ResultVals; 1334fe6060f1SDimitry Andric for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) { 1335fe6060f1SDimitry Andric // Verify the new types match the original. Glue is waived because 1336fe6060f1SDimitry Andric // ISD::ADDC can be legalized by replacing Glue with an integer type. 1337fe6060f1SDimitry Andric assert((Res->getValueType(i) == Node->getValueType(i) || 1338fe6060f1SDimitry Andric Node->getValueType(i) == MVT::Glue) && 1339fe6060f1SDimitry Andric "Type mismatch for custom legalized operation"); 13400b57cec5SDimitry Andric ResultVals.push_back(Res.getValue(i)); 1341fe6060f1SDimitry Andric } 13420b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully custom legalized node\n"); 13430b57cec5SDimitry Andric ReplaceNode(Node, ResultVals.data()); 13440b57cec5SDimitry Andric return; 13450b57cec5SDimitry Andric } 13460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not custom legalize node\n"); 1347bdd1243dSDimitry Andric [[fallthrough]]; 13480b57cec5SDimitry Andric case TargetLowering::Expand: 13490b57cec5SDimitry Andric if (ExpandNode(Node)) 13500b57cec5SDimitry Andric return; 1351bdd1243dSDimitry Andric [[fallthrough]]; 13520b57cec5SDimitry Andric case TargetLowering::LibCall: 13530b57cec5SDimitry Andric ConvertNodeToLibcall(Node); 13540b57cec5SDimitry Andric return; 13550b57cec5SDimitry Andric case TargetLowering::Promote: 13560b57cec5SDimitry Andric PromoteNode(Node); 13570b57cec5SDimitry Andric return; 13580b57cec5SDimitry Andric } 13590b57cec5SDimitry Andric } 13600b57cec5SDimitry Andric 13610b57cec5SDimitry Andric switch (Node->getOpcode()) { 13620b57cec5SDimitry Andric default: 13630b57cec5SDimitry Andric #ifndef NDEBUG 13640b57cec5SDimitry Andric dbgs() << "NODE: "; 13650b57cec5SDimitry Andric Node->dump( &DAG); 13660b57cec5SDimitry Andric dbgs() << "\n"; 13670b57cec5SDimitry Andric #endif 13680b57cec5SDimitry Andric llvm_unreachable("Do not know how to legalize this operator!"); 13690b57cec5SDimitry Andric 13700b57cec5SDimitry Andric case ISD::CALLSEQ_START: 13710b57cec5SDimitry Andric case ISD::CALLSEQ_END: 13720b57cec5SDimitry Andric break; 13730b57cec5SDimitry Andric case ISD::LOAD: 13740b57cec5SDimitry Andric return LegalizeLoadOps(Node); 13750b57cec5SDimitry Andric case ISD::STORE: 13760b57cec5SDimitry Andric return LegalizeStoreOps(Node); 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric } 13790b57cec5SDimitry Andric 13800b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) { 13810b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 13820b57cec5SDimitry Andric SDValue Idx = Op.getOperand(1); 13830b57cec5SDimitry Andric SDLoc dl(Op); 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // Before we generate a new store to a temporary stack slot, see if there is 13860b57cec5SDimitry Andric // already one that we can use. There often is because when we scalarize 13870b57cec5SDimitry Andric // vector operations (using SelectionDAG::UnrollVectorOp for example) a whole 13880b57cec5SDimitry Andric // series of EXTRACT_VECTOR_ELT nodes are generated, one for each element in 13890b57cec5SDimitry Andric // the vector. If all are expanded here, we don't want one store per vector 13900b57cec5SDimitry Andric // element. 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric // Caches for hasPredecessorHelper 13930b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 32> Visited; 13940b57cec5SDimitry Andric SmallVector<const SDNode *, 16> Worklist; 13950b57cec5SDimitry Andric Visited.insert(Op.getNode()); 13960b57cec5SDimitry Andric Worklist.push_back(Idx.getNode()); 13970b57cec5SDimitry Andric SDValue StackPtr, Ch; 1398349cc55cSDimitry Andric for (SDNode *User : Vec.getNode()->uses()) { 13990b57cec5SDimitry Andric if (StoreSDNode *ST = dyn_cast<StoreSDNode>(User)) { 14000b57cec5SDimitry Andric if (ST->isIndexed() || ST->isTruncatingStore() || 14010b57cec5SDimitry Andric ST->getValue() != Vec) 14020b57cec5SDimitry Andric continue; 14030b57cec5SDimitry Andric 14040b57cec5SDimitry Andric // Make sure that nothing else could have stored into the destination of 14050b57cec5SDimitry Andric // this store. 14060b57cec5SDimitry Andric if (!ST->getChain().reachesChainWithoutSideEffects(DAG.getEntryNode())) 14070b57cec5SDimitry Andric continue; 14080b57cec5SDimitry Andric 14090b57cec5SDimitry Andric // If the index is dependent on the store we will introduce a cycle when 14100b57cec5SDimitry Andric // creating the load (the load uses the index, and by replacing the chain 14110b57cec5SDimitry Andric // we will make the index dependent on the load). Also, the store might be 14120b57cec5SDimitry Andric // dependent on the extractelement and introduce a cycle when creating 14130b57cec5SDimitry Andric // the load. 14140b57cec5SDimitry Andric if (SDNode::hasPredecessorHelper(ST, Visited, Worklist) || 14150b57cec5SDimitry Andric ST->hasPredecessor(Op.getNode())) 14160b57cec5SDimitry Andric continue; 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric StackPtr = ST->getBasePtr(); 14190b57cec5SDimitry Andric Ch = SDValue(ST, 0); 14200b57cec5SDimitry Andric break; 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric } 14230b57cec5SDimitry Andric 14240b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 14250b57cec5SDimitry Andric 14260b57cec5SDimitry Andric if (!Ch.getNode()) { 14270b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 14280b57cec5SDimitry Andric StackPtr = DAG.CreateStackTemporary(VecVT); 14290b57cec5SDimitry Andric Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, 14300b57cec5SDimitry Andric MachinePointerInfo()); 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric SDValue NewLoad; 1434fcaf7f86SDimitry Andric Align ElementAlignment = 1435fcaf7f86SDimitry Andric std::min(cast<StoreSDNode>(Ch)->getAlign(), 1436fcaf7f86SDimitry Andric DAG.getDataLayout().getPrefTypeAlign( 1437fcaf7f86SDimitry Andric Op.getValueType().getTypeForEVT(*DAG.getContext()))); 14380b57cec5SDimitry Andric 1439fe6060f1SDimitry Andric if (Op.getValueType().isVector()) { 1440fe6060f1SDimitry Andric StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, 1441fe6060f1SDimitry Andric Op.getValueType(), Idx); 1442fcaf7f86SDimitry Andric NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, 1443fcaf7f86SDimitry Andric MachinePointerInfo(), ElementAlignment); 1444fe6060f1SDimitry Andric } else { 1445fe6060f1SDimitry Andric StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx); 14460b57cec5SDimitry Andric NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr, 1447fcaf7f86SDimitry Andric MachinePointerInfo(), VecVT.getVectorElementType(), 1448fcaf7f86SDimitry Andric ElementAlignment); 1449fe6060f1SDimitry Andric } 14500b57cec5SDimitry Andric 14510b57cec5SDimitry Andric // Replace the chain going out of the store, by the one out of the load. 14520b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(Ch, SDValue(NewLoad.getNode(), 1)); 14530b57cec5SDimitry Andric 14540b57cec5SDimitry Andric // We introduced a cycle though, so update the loads operands, making sure 14550b57cec5SDimitry Andric // to use the original store's chain as an incoming chain. 14560b57cec5SDimitry Andric SmallVector<SDValue, 6> NewLoadOperands(NewLoad->op_begin(), 14570b57cec5SDimitry Andric NewLoad->op_end()); 14580b57cec5SDimitry Andric NewLoadOperands[0] = Ch; 14590b57cec5SDimitry Andric NewLoad = 14600b57cec5SDimitry Andric SDValue(DAG.UpdateNodeOperands(NewLoad.getNode(), NewLoadOperands), 0); 14610b57cec5SDimitry Andric return NewLoad; 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandInsertToVectorThroughStack(SDValue Op) { 14650b57cec5SDimitry Andric assert(Op.getValueType().isVector() && "Non-vector insert subvector!"); 14660b57cec5SDimitry Andric 14670b57cec5SDimitry Andric SDValue Vec = Op.getOperand(0); 14680b57cec5SDimitry Andric SDValue Part = Op.getOperand(1); 14690b57cec5SDimitry Andric SDValue Idx = Op.getOperand(2); 14700b57cec5SDimitry Andric SDLoc dl(Op); 14710b57cec5SDimitry Andric 14720b57cec5SDimitry Andric // Store the value to a temporary stack slot, then LOAD the returned part. 14730b57cec5SDimitry Andric EVT VecVT = Vec.getValueType(); 1474fe6060f1SDimitry Andric EVT SubVecVT = Part.getValueType(); 14750b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(VecVT); 14760b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 14770b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 14780b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric // First store the whole vector. 14810b57cec5SDimitry Andric SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, Vec, StackPtr, PtrInfo); 14820b57cec5SDimitry Andric 14830b57cec5SDimitry Andric // Then store the inserted part. 1484fe6060f1SDimitry Andric SDValue SubStackPtr = 1485fe6060f1SDimitry Andric TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT, SubVecVT, Idx); 14860b57cec5SDimitry Andric 14870b57cec5SDimitry Andric // Store the subvector. 14885ffd83dbSDimitry Andric Ch = DAG.getStore( 14895ffd83dbSDimitry Andric Ch, dl, Part, SubStackPtr, 14905ffd83dbSDimitry Andric MachinePointerInfo::getUnknownStack(DAG.getMachineFunction())); 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric // Finally, load the updated vector. 14930b57cec5SDimitry Andric return DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, PtrInfo); 14940b57cec5SDimitry Andric } 14950b57cec5SDimitry Andric 14960b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandVectorBuildThroughStack(SDNode* Node) { 14975ffd83dbSDimitry Andric assert((Node->getOpcode() == ISD::BUILD_VECTOR || 14985ffd83dbSDimitry Andric Node->getOpcode() == ISD::CONCAT_VECTORS) && 14995ffd83dbSDimitry Andric "Unexpected opcode!"); 15005ffd83dbSDimitry Andric 15010b57cec5SDimitry Andric // We can't handle this case efficiently. Allocate a sufficiently 15025ffd83dbSDimitry Andric // aligned object on the stack, store each operand into it, then load 15030b57cec5SDimitry Andric // the result as a vector. 15040b57cec5SDimitry Andric // Create the stack frame object. 15050b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 15065ffd83dbSDimitry Andric EVT MemVT = isa<BuildVectorSDNode>(Node) ? VT.getVectorElementType() 15075ffd83dbSDimitry Andric : Node->getOperand(0).getValueType(); 15080b57cec5SDimitry Andric SDLoc dl(Node); 15090b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(VT); 15100b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(FIPtr.getNode())->getIndex(); 15110b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 15120b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI); 15130b57cec5SDimitry Andric 15140b57cec5SDimitry Andric // Emit a store of each element to the stack slot. 15150b57cec5SDimitry Andric SmallVector<SDValue, 8> Stores; 15165ffd83dbSDimitry Andric unsigned TypeByteSize = MemVT.getSizeInBits() / 8; 15170b57cec5SDimitry Andric assert(TypeByteSize > 0 && "Vector element type too small for stack store!"); 1518e8d8bef9SDimitry Andric 1519e8d8bef9SDimitry Andric // If the destination vector element type of a BUILD_VECTOR is narrower than 1520e8d8bef9SDimitry Andric // the source element type, only store the bits necessary. 1521e8d8bef9SDimitry Andric bool Truncate = isa<BuildVectorSDNode>(Node) && 1522e8d8bef9SDimitry Andric MemVT.bitsLT(Node->getOperand(0).getValueType()); 1523e8d8bef9SDimitry Andric 15240b57cec5SDimitry Andric // Store (in the right endianness) the elements to memory. 15250b57cec5SDimitry Andric for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) { 15260b57cec5SDimitry Andric // Ignore undef elements. 15270b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) continue; 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric unsigned Offset = TypeByteSize*i; 15300b57cec5SDimitry Andric 15315f757f3fSDimitry Andric SDValue Idx = 15325f757f3fSDimitry Andric DAG.getMemBasePlusOffset(FIPtr, TypeSize::getFixed(Offset), dl); 15330b57cec5SDimitry Andric 1534e8d8bef9SDimitry Andric if (Truncate) 15350b57cec5SDimitry Andric Stores.push_back(DAG.getTruncStore(DAG.getEntryNode(), dl, 15360b57cec5SDimitry Andric Node->getOperand(i), Idx, 15375ffd83dbSDimitry Andric PtrInfo.getWithOffset(Offset), MemVT)); 15385ffd83dbSDimitry Andric else 15390b57cec5SDimitry Andric Stores.push_back(DAG.getStore(DAG.getEntryNode(), dl, Node->getOperand(i), 15400b57cec5SDimitry Andric Idx, PtrInfo.getWithOffset(Offset))); 15410b57cec5SDimitry Andric } 15420b57cec5SDimitry Andric 15430b57cec5SDimitry Andric SDValue StoreChain; 15440b57cec5SDimitry Andric if (!Stores.empty()) // Not all undef elements? 15450b57cec5SDimitry Andric StoreChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Stores); 15460b57cec5SDimitry Andric else 15470b57cec5SDimitry Andric StoreChain = DAG.getEntryNode(); 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andric // Result is a load from the stack slot. 15500b57cec5SDimitry Andric return DAG.getLoad(VT, dl, StoreChain, FIPtr, PtrInfo); 15510b57cec5SDimitry Andric } 15520b57cec5SDimitry Andric 15530b57cec5SDimitry Andric /// Bitcast a floating-point value to an integer value. Only bitcast the part 15540b57cec5SDimitry Andric /// containing the sign bit if the target has no integer value capable of 15550b57cec5SDimitry Andric /// holding all bits of the floating-point value. 15560b57cec5SDimitry Andric void SelectionDAGLegalize::getSignAsIntValue(FloatSignAsInt &State, 15570b57cec5SDimitry Andric const SDLoc &DL, 15580b57cec5SDimitry Andric SDValue Value) const { 15590b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 1560e8d8bef9SDimitry Andric unsigned NumBits = FloatVT.getScalarSizeInBits(); 15610b57cec5SDimitry Andric State.FloatVT = FloatVT; 15620b57cec5SDimitry Andric EVT IVT = EVT::getIntegerVT(*DAG.getContext(), NumBits); 15630b57cec5SDimitry Andric // Convert to an integer of the same size. 15640b57cec5SDimitry Andric if (TLI.isTypeLegal(IVT)) { 15650b57cec5SDimitry Andric State.IntValue = DAG.getNode(ISD::BITCAST, DL, IVT, Value); 15660b57cec5SDimitry Andric State.SignMask = APInt::getSignMask(NumBits); 15670b57cec5SDimitry Andric State.SignBit = NumBits - 1; 15680b57cec5SDimitry Andric return; 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric auto &DataLayout = DAG.getDataLayout(); 15720b57cec5SDimitry Andric // Store the float to memory, then load the sign part out as an integer. 157306c3fb27SDimitry Andric MVT LoadTy = TLI.getRegisterType(MVT::i8); 15740b57cec5SDimitry Andric // First create a temporary that is aligned for both the load and store. 15750b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(FloatVT, LoadTy); 15760b57cec5SDimitry Andric int FI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 15770b57cec5SDimitry Andric // Then store the float to it. 15780b57cec5SDimitry Andric State.FloatPtr = StackPtr; 15790b57cec5SDimitry Andric MachineFunction &MF = DAG.getMachineFunction(); 15800b57cec5SDimitry Andric State.FloatPointerInfo = MachinePointerInfo::getFixedStack(MF, FI); 15810b57cec5SDimitry Andric State.Chain = DAG.getStore(DAG.getEntryNode(), DL, Value, State.FloatPtr, 15820b57cec5SDimitry Andric State.FloatPointerInfo); 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric SDValue IntPtr; 15850b57cec5SDimitry Andric if (DataLayout.isBigEndian()) { 15860b57cec5SDimitry Andric assert(FloatVT.isByteSized() && "Unsupported floating point type!"); 15870b57cec5SDimitry Andric // Load out a legal integer with the same sign bit as the float. 15880b57cec5SDimitry Andric IntPtr = StackPtr; 15890b57cec5SDimitry Andric State.IntPointerInfo = State.FloatPointerInfo; 15900b57cec5SDimitry Andric } else { 15910b57cec5SDimitry Andric // Advance the pointer so that the loaded byte will contain the sign bit. 1592e8d8bef9SDimitry Andric unsigned ByteOffset = (NumBits / 8) - 1; 1593e8d8bef9SDimitry Andric IntPtr = 15945f757f3fSDimitry Andric DAG.getMemBasePlusOffset(StackPtr, TypeSize::getFixed(ByteOffset), DL); 15950b57cec5SDimitry Andric State.IntPointerInfo = MachinePointerInfo::getFixedStack(MF, FI, 15960b57cec5SDimitry Andric ByteOffset); 15970b57cec5SDimitry Andric } 15980b57cec5SDimitry Andric 15990b57cec5SDimitry Andric State.IntPtr = IntPtr; 16000b57cec5SDimitry Andric State.IntValue = DAG.getExtLoad(ISD::EXTLOAD, DL, LoadTy, State.Chain, IntPtr, 16010b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 1602e8d8bef9SDimitry Andric State.SignMask = APInt::getOneBitSet(LoadTy.getScalarSizeInBits(), 7); 16030b57cec5SDimitry Andric State.SignBit = 7; 16040b57cec5SDimitry Andric } 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric /// Replace the integer value produced by getSignAsIntValue() with a new value 16070b57cec5SDimitry Andric /// and cast the result back to a floating-point type. 16080b57cec5SDimitry Andric SDValue SelectionDAGLegalize::modifySignAsInt(const FloatSignAsInt &State, 16090b57cec5SDimitry Andric const SDLoc &DL, 16100b57cec5SDimitry Andric SDValue NewIntValue) const { 16110b57cec5SDimitry Andric if (!State.Chain) 16120b57cec5SDimitry Andric return DAG.getNode(ISD::BITCAST, DL, State.FloatVT, NewIntValue); 16130b57cec5SDimitry Andric 16140b57cec5SDimitry Andric // Override the part containing the sign bit in the value stored on the stack. 16150b57cec5SDimitry Andric SDValue Chain = DAG.getTruncStore(State.Chain, DL, NewIntValue, State.IntPtr, 16160b57cec5SDimitry Andric State.IntPointerInfo, MVT::i8); 16170b57cec5SDimitry Andric return DAG.getLoad(State.FloatVT, DL, Chain, State.FloatPtr, 16180b57cec5SDimitry Andric State.FloatPointerInfo); 16190b57cec5SDimitry Andric } 16200b57cec5SDimitry Andric 16210b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFCOPYSIGN(SDNode *Node) const { 16220b57cec5SDimitry Andric SDLoc DL(Node); 16230b57cec5SDimitry Andric SDValue Mag = Node->getOperand(0); 16240b57cec5SDimitry Andric SDValue Sign = Node->getOperand(1); 16250b57cec5SDimitry Andric 16260b57cec5SDimitry Andric // Get sign bit into an integer value. 16270b57cec5SDimitry Andric FloatSignAsInt SignAsInt; 16280b57cec5SDimitry Andric getSignAsIntValue(SignAsInt, DL, Sign); 16290b57cec5SDimitry Andric 16300b57cec5SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 16310b57cec5SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 16320b57cec5SDimitry Andric SDValue SignBit = DAG.getNode(ISD::AND, DL, IntVT, SignAsInt.IntValue, 16330b57cec5SDimitry Andric SignMask); 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric // If FABS is legal transform FCOPYSIGN(x, y) => sign(x) ? -FABS(x) : FABS(X) 16360b57cec5SDimitry Andric EVT FloatVT = Mag.getValueType(); 16370b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FABS, FloatVT) && 16380b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, FloatVT)) { 16390b57cec5SDimitry Andric SDValue AbsValue = DAG.getNode(ISD::FABS, DL, FloatVT, Mag); 16400b57cec5SDimitry Andric SDValue NegValue = DAG.getNode(ISD::FNEG, DL, FloatVT, AbsValue); 16410b57cec5SDimitry Andric SDValue Cond = DAG.getSetCC(DL, getSetCCResultType(IntVT), SignBit, 16420b57cec5SDimitry Andric DAG.getConstant(0, DL, IntVT), ISD::SETNE); 16430b57cec5SDimitry Andric return DAG.getSelect(DL, FloatVT, Cond, NegValue, AbsValue); 16440b57cec5SDimitry Andric } 16450b57cec5SDimitry Andric 16460b57cec5SDimitry Andric // Transform Mag value to integer, and clear the sign bit. 16470b57cec5SDimitry Andric FloatSignAsInt MagAsInt; 16480b57cec5SDimitry Andric getSignAsIntValue(MagAsInt, DL, Mag); 16490b57cec5SDimitry Andric EVT MagVT = MagAsInt.IntValue.getValueType(); 16500b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~MagAsInt.SignMask, DL, MagVT); 16510b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, MagVT, MagAsInt.IntValue, 16520b57cec5SDimitry Andric ClearSignMask); 16530b57cec5SDimitry Andric 16540b57cec5SDimitry Andric // Get the signbit at the right position for MagAsInt. 16550b57cec5SDimitry Andric int ShiftAmount = SignAsInt.SignBit - MagAsInt.SignBit; 16560b57cec5SDimitry Andric EVT ShiftVT = IntVT; 1657e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() < 1658e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 16590b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::ZERO_EXTEND, DL, MagVT, SignBit); 16600b57cec5SDimitry Andric ShiftVT = MagVT; 16610b57cec5SDimitry Andric } 16620b57cec5SDimitry Andric if (ShiftAmount > 0) { 16630b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(ShiftAmount, DL, ShiftVT); 16640b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SRL, DL, ShiftVT, SignBit, ShiftCnst); 16650b57cec5SDimitry Andric } else if (ShiftAmount < 0) { 16660b57cec5SDimitry Andric SDValue ShiftCnst = DAG.getConstant(-ShiftAmount, DL, ShiftVT); 16670b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::SHL, DL, ShiftVT, SignBit, ShiftCnst); 16680b57cec5SDimitry Andric } 1669e8d8bef9SDimitry Andric if (SignBit.getScalarValueSizeInBits() > 1670e8d8bef9SDimitry Andric ClearedSign.getScalarValueSizeInBits()) { 16710b57cec5SDimitry Andric SignBit = DAG.getNode(ISD::TRUNCATE, DL, MagVT, SignBit); 16720b57cec5SDimitry Andric } 16730b57cec5SDimitry Andric 16740b57cec5SDimitry Andric // Store the part with the modified sign and convert back to float. 16750b57cec5SDimitry Andric SDValue CopiedSign = DAG.getNode(ISD::OR, DL, MagVT, ClearedSign, SignBit); 16760b57cec5SDimitry Andric return modifySignAsInt(MagAsInt, DL, CopiedSign); 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric 1679e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandFNEG(SDNode *Node) const { 1680e8d8bef9SDimitry Andric // Get the sign bit as an integer. 1681e8d8bef9SDimitry Andric SDLoc DL(Node); 1682e8d8bef9SDimitry Andric FloatSignAsInt SignAsInt; 1683e8d8bef9SDimitry Andric getSignAsIntValue(SignAsInt, DL, Node->getOperand(0)); 1684e8d8bef9SDimitry Andric EVT IntVT = SignAsInt.IntValue.getValueType(); 1685e8d8bef9SDimitry Andric 1686e8d8bef9SDimitry Andric // Flip the sign. 1687e8d8bef9SDimitry Andric SDValue SignMask = DAG.getConstant(SignAsInt.SignMask, DL, IntVT); 1688e8d8bef9SDimitry Andric SDValue SignFlip = 1689e8d8bef9SDimitry Andric DAG.getNode(ISD::XOR, DL, IntVT, SignAsInt.IntValue, SignMask); 1690e8d8bef9SDimitry Andric 1691e8d8bef9SDimitry Andric // Convert back to float. 1692e8d8bef9SDimitry Andric return modifySignAsInt(SignAsInt, DL, SignFlip); 1693e8d8bef9SDimitry Andric } 1694e8d8bef9SDimitry Andric 16950b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandFABS(SDNode *Node) const { 16960b57cec5SDimitry Andric SDLoc DL(Node); 16970b57cec5SDimitry Andric SDValue Value = Node->getOperand(0); 16980b57cec5SDimitry Andric 16990b57cec5SDimitry Andric // Transform FABS(x) => FCOPYSIGN(x, 0.0) if FCOPYSIGN is legal. 17000b57cec5SDimitry Andric EVT FloatVT = Value.getValueType(); 17010b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FCOPYSIGN, FloatVT)) { 17020b57cec5SDimitry Andric SDValue Zero = DAG.getConstantFP(0.0, DL, FloatVT); 17030b57cec5SDimitry Andric return DAG.getNode(ISD::FCOPYSIGN, DL, FloatVT, Value, Zero); 17040b57cec5SDimitry Andric } 17050b57cec5SDimitry Andric 17060b57cec5SDimitry Andric // Transform value to integer, clear the sign bit and transform back. 17070b57cec5SDimitry Andric FloatSignAsInt ValueAsInt; 17080b57cec5SDimitry Andric getSignAsIntValue(ValueAsInt, DL, Value); 17090b57cec5SDimitry Andric EVT IntVT = ValueAsInt.IntValue.getValueType(); 17100b57cec5SDimitry Andric SDValue ClearSignMask = DAG.getConstant(~ValueAsInt.SignMask, DL, IntVT); 17110b57cec5SDimitry Andric SDValue ClearedSign = DAG.getNode(ISD::AND, DL, IntVT, ValueAsInt.IntValue, 17120b57cec5SDimitry Andric ClearSignMask); 17130b57cec5SDimitry Andric return modifySignAsInt(ValueAsInt, DL, ClearedSign); 17140b57cec5SDimitry Andric } 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric void SelectionDAGLegalize::ExpandDYNAMIC_STACKALLOC(SDNode* Node, 17170b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 1718e8d8bef9SDimitry Andric Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); 17190b57cec5SDimitry Andric assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" 17200b57cec5SDimitry Andric " not tell us which reg is the stack pointer!"); 17210b57cec5SDimitry Andric SDLoc dl(Node); 17220b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 17230b57cec5SDimitry Andric SDValue Tmp1 = SDValue(Node, 0); 17240b57cec5SDimitry Andric SDValue Tmp2 = SDValue(Node, 1); 17250b57cec5SDimitry Andric SDValue Tmp3 = Node->getOperand(2); 17260b57cec5SDimitry Andric SDValue Chain = Tmp1.getOperand(0); 17270b57cec5SDimitry Andric 17280b57cec5SDimitry Andric // Chain the dynamic stack allocation so that it doesn't modify the stack 17290b57cec5SDimitry Andric // pointer when other instructions are using the stack. 17300b57cec5SDimitry Andric Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl); 17310b57cec5SDimitry Andric 17320b57cec5SDimitry Andric SDValue Size = Tmp2.getOperand(1); 17330b57cec5SDimitry Andric SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); 17340b57cec5SDimitry Andric Chain = SP.getValue(1); 17355ffd83dbSDimitry Andric Align Alignment = cast<ConstantSDNode>(Tmp3)->getAlignValue(); 17365ffd83dbSDimitry Andric const TargetFrameLowering *TFL = DAG.getSubtarget().getFrameLowering(); 17375ffd83dbSDimitry Andric unsigned Opc = 17385ffd83dbSDimitry Andric TFL->getStackGrowthDirection() == TargetFrameLowering::StackGrowsUp ? 17395ffd83dbSDimitry Andric ISD::ADD : ISD::SUB; 17405ffd83dbSDimitry Andric 17415ffd83dbSDimitry Andric Align StackAlign = TFL->getStackAlign(); 17425ffd83dbSDimitry Andric Tmp1 = DAG.getNode(Opc, dl, VT, SP, Size); // Value 17435ffd83dbSDimitry Andric if (Alignment > StackAlign) 17440b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::AND, dl, VT, Tmp1, 17455ffd83dbSDimitry Andric DAG.getConstant(-Alignment.value(), dl, VT)); 17460b57cec5SDimitry Andric Chain = DAG.getCopyToReg(Chain, dl, SPReg, Tmp1); // Output chain 17470b57cec5SDimitry Andric 1748bdd1243dSDimitry Andric Tmp2 = DAG.getCALLSEQ_END(Chain, 0, 0, SDValue(), dl); 17490b57cec5SDimitry Andric 17500b57cec5SDimitry Andric Results.push_back(Tmp1); 17510b57cec5SDimitry Andric Results.push_back(Tmp2); 17520b57cec5SDimitry Andric } 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric /// Emit a store/load combination to the stack. This stores 17550b57cec5SDimitry Andric /// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does 17560b57cec5SDimitry Andric /// a load from the stack slot to DestVT, extending it if needed. 17570b57cec5SDimitry Andric /// The resultant code need not be legal. 17580b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 17590b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl) { 17600b57cec5SDimitry Andric return EmitStackConvert(SrcOp, SlotVT, DestVT, dl, DAG.getEntryNode()); 17610b57cec5SDimitry Andric } 17620b57cec5SDimitry Andric 17630b57cec5SDimitry Andric SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp, EVT SlotVT, 17640b57cec5SDimitry Andric EVT DestVT, const SDLoc &dl, 17650b57cec5SDimitry Andric SDValue Chain) { 176681ad6265SDimitry Andric EVT SrcVT = SrcOp.getValueType(); 1767e8d8bef9SDimitry Andric Type *DestType = DestVT.getTypeForEVT(*DAG.getContext()); 1768e8d8bef9SDimitry Andric Align DestAlign = DAG.getDataLayout().getPrefTypeAlign(DestType); 1769e8d8bef9SDimitry Andric 1770e8d8bef9SDimitry Andric // Don't convert with stack if the load/store is expensive. 177181ad6265SDimitry Andric if ((SrcVT.bitsGT(SlotVT) && 1772e8d8bef9SDimitry Andric !TLI.isTruncStoreLegalOrCustom(SrcOp.getValueType(), SlotVT)) || 177381ad6265SDimitry Andric (SlotVT.bitsLT(DestVT) && 1774e8d8bef9SDimitry Andric !TLI.isLoadExtLegalOrCustom(ISD::EXTLOAD, DestVT, SlotVT))) 1775e8d8bef9SDimitry Andric return SDValue(); 1776e8d8bef9SDimitry Andric 17770b57cec5SDimitry Andric // Create the stack frame object. 1778e8d8bef9SDimitry Andric Align SrcAlign = DAG.getDataLayout().getPrefTypeAlign( 17790b57cec5SDimitry Andric SrcOp.getValueType().getTypeForEVT(*DAG.getContext())); 1780e8d8bef9SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(SlotVT.getStoreSize(), SrcAlign); 17810b57cec5SDimitry Andric 17820b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr); 17830b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 17840b57cec5SDimitry Andric MachinePointerInfo PtrInfo = 17850b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); 17860b57cec5SDimitry Andric 17870b57cec5SDimitry Andric // Emit a store to the stack slot. Use a truncstore if the input value is 17880b57cec5SDimitry Andric // later than DestVT. 17890b57cec5SDimitry Andric SDValue Store; 17900b57cec5SDimitry Andric 179181ad6265SDimitry Andric if (SrcVT.bitsGT(SlotVT)) 17920b57cec5SDimitry Andric Store = DAG.getTruncStore(Chain, dl, SrcOp, FIPtr, PtrInfo, 17930b57cec5SDimitry Andric SlotVT, SrcAlign); 17940b57cec5SDimitry Andric else { 179581ad6265SDimitry Andric assert(SrcVT.bitsEq(SlotVT) && "Invalid store"); 179681ad6265SDimitry Andric Store = DAG.getStore(Chain, dl, SrcOp, FIPtr, PtrInfo, SrcAlign); 17970b57cec5SDimitry Andric } 17980b57cec5SDimitry Andric 17990b57cec5SDimitry Andric // Result is a load from the stack slot. 180081ad6265SDimitry Andric if (SlotVT.bitsEq(DestVT)) 18010b57cec5SDimitry Andric return DAG.getLoad(DestVT, dl, Store, FIPtr, PtrInfo, DestAlign); 18020b57cec5SDimitry Andric 180381ad6265SDimitry Andric assert(SlotVT.bitsLT(DestVT) && "Unknown extension!"); 18040b57cec5SDimitry Andric return DAG.getExtLoad(ISD::EXTLOAD, dl, DestVT, Store, FIPtr, PtrInfo, SlotVT, 18050b57cec5SDimitry Andric DestAlign); 18060b57cec5SDimitry Andric } 18070b57cec5SDimitry Andric 18080b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) { 18090b57cec5SDimitry Andric SDLoc dl(Node); 18100b57cec5SDimitry Andric // Create a vector sized/aligned stack slot, store the value to element #0, 18110b57cec5SDimitry Andric // then load the whole vector back out. 18120b57cec5SDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0)); 18130b57cec5SDimitry Andric 18140b57cec5SDimitry Andric FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr); 18150b57cec5SDimitry Andric int SPFI = StackPtrFI->getIndex(); 18160b57cec5SDimitry Andric 18170b57cec5SDimitry Andric SDValue Ch = DAG.getTruncStore( 18180b57cec5SDimitry Andric DAG.getEntryNode(), dl, Node->getOperand(0), StackPtr, 18190b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI), 18200b57cec5SDimitry Andric Node->getValueType(0).getVectorElementType()); 18210b57cec5SDimitry Andric return DAG.getLoad( 18220b57cec5SDimitry Andric Node->getValueType(0), dl, Ch, StackPtr, 18230b57cec5SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric 18260b57cec5SDimitry Andric static bool 18270b57cec5SDimitry Andric ExpandBVWithShuffles(SDNode *Node, SelectionDAG &DAG, 18280b57cec5SDimitry Andric const TargetLowering &TLI, SDValue &Res) { 18290b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 18300b57cec5SDimitry Andric SDLoc dl(Node); 18310b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 18320b57cec5SDimitry Andric 18330b57cec5SDimitry Andric // Try to group the scalars into pairs, shuffle the pairs together, then 18340b57cec5SDimitry Andric // shuffle the pairs of pairs together, etc. until the vector has 18350b57cec5SDimitry Andric // been built. This will work only if all of the necessary shuffle masks 18360b57cec5SDimitry Andric // are legal. 18370b57cec5SDimitry Andric 18380b57cec5SDimitry Andric // We do this in two phases; first to check the legality of the shuffles, 18390b57cec5SDimitry Andric // and next, assuming that all shuffles are legal, to create the new nodes. 18400b57cec5SDimitry Andric for (int Phase = 0; Phase < 2; ++Phase) { 18410b57cec5SDimitry Andric SmallVector<std::pair<SDValue, SmallVector<int, 16>>, 16> IntermedVals, 18420b57cec5SDimitry Andric NewIntermedVals; 18430b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 18440b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 18450b57cec5SDimitry Andric if (V.isUndef()) 18460b57cec5SDimitry Andric continue; 18470b57cec5SDimitry Andric 18480b57cec5SDimitry Andric SDValue Vec; 18490b57cec5SDimitry Andric if (Phase) 18500b57cec5SDimitry Andric Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, V); 18510b57cec5SDimitry Andric IntermedVals.push_back(std::make_pair(Vec, SmallVector<int, 16>(1, i))); 18520b57cec5SDimitry Andric } 18530b57cec5SDimitry Andric 18540b57cec5SDimitry Andric while (IntermedVals.size() > 2) { 18550b57cec5SDimitry Andric NewIntermedVals.clear(); 18560b57cec5SDimitry Andric for (unsigned i = 0, e = (IntermedVals.size() & ~1u); i < e; i += 2) { 18570b57cec5SDimitry Andric // This vector and the next vector are shuffled together (simply to 18580b57cec5SDimitry Andric // append the one to the other). 18590b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 18600b57cec5SDimitry Andric 18610b57cec5SDimitry Andric SmallVector<int, 16> FinalIndices; 18620b57cec5SDimitry Andric FinalIndices.reserve(IntermedVals[i].second.size() + 18630b57cec5SDimitry Andric IntermedVals[i+1].second.size()); 18640b57cec5SDimitry Andric 18650b57cec5SDimitry Andric int k = 0; 18660b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i].second.size(); j != f; 18670b57cec5SDimitry Andric ++j, ++k) { 18680b57cec5SDimitry Andric ShuffleVec[k] = j; 18690b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i].second[j]); 18700b57cec5SDimitry Andric } 18710b57cec5SDimitry Andric for (unsigned j = 0, f = IntermedVals[i+1].second.size(); j != f; 18720b57cec5SDimitry Andric ++j, ++k) { 18730b57cec5SDimitry Andric ShuffleVec[k] = NumElems + j; 18740b57cec5SDimitry Andric FinalIndices.push_back(IntermedVals[i+1].second[j]); 18750b57cec5SDimitry Andric } 18760b57cec5SDimitry Andric 18770b57cec5SDimitry Andric SDValue Shuffle; 18780b57cec5SDimitry Andric if (Phase) 18790b57cec5SDimitry Andric Shuffle = DAG.getVectorShuffle(VT, dl, IntermedVals[i].first, 18800b57cec5SDimitry Andric IntermedVals[i+1].first, 18810b57cec5SDimitry Andric ShuffleVec); 18820b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 18830b57cec5SDimitry Andric return false; 18840b57cec5SDimitry Andric NewIntermedVals.push_back( 18850b57cec5SDimitry Andric std::make_pair(Shuffle, std::move(FinalIndices))); 18860b57cec5SDimitry Andric } 18870b57cec5SDimitry Andric 18880b57cec5SDimitry Andric // If we had an odd number of defined values, then append the last 18890b57cec5SDimitry Andric // element to the array of new vectors. 18900b57cec5SDimitry Andric if ((IntermedVals.size() & 1) != 0) 18910b57cec5SDimitry Andric NewIntermedVals.push_back(IntermedVals.back()); 18920b57cec5SDimitry Andric 18930b57cec5SDimitry Andric IntermedVals.swap(NewIntermedVals); 18940b57cec5SDimitry Andric } 18950b57cec5SDimitry Andric 18960b57cec5SDimitry Andric assert(IntermedVals.size() <= 2 && IntermedVals.size() > 0 && 18970b57cec5SDimitry Andric "Invalid number of intermediate vectors"); 18980b57cec5SDimitry Andric SDValue Vec1 = IntermedVals[0].first; 18990b57cec5SDimitry Andric SDValue Vec2; 19000b57cec5SDimitry Andric if (IntermedVals.size() > 1) 19010b57cec5SDimitry Andric Vec2 = IntermedVals[1].first; 19020b57cec5SDimitry Andric else if (Phase) 19030b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric SmallVector<int, 16> ShuffleVec(NumElems, -1); 19060b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[0].second.size(); i != e; ++i) 19070b57cec5SDimitry Andric ShuffleVec[IntermedVals[0].second[i]] = i; 19080b57cec5SDimitry Andric for (unsigned i = 0, e = IntermedVals[1].second.size(); i != e; ++i) 19090b57cec5SDimitry Andric ShuffleVec[IntermedVals[1].second[i]] = NumElems + i; 19100b57cec5SDimitry Andric 19110b57cec5SDimitry Andric if (Phase) 19120b57cec5SDimitry Andric Res = DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 19130b57cec5SDimitry Andric else if (!TLI.isShuffleMaskLegal(ShuffleVec, VT)) 19140b57cec5SDimitry Andric return false; 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric return true; 19180b57cec5SDimitry Andric } 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric /// Expand a BUILD_VECTOR node on targets that don't 19210b57cec5SDimitry Andric /// support the operation, but do support the resultant vector type. 19220b57cec5SDimitry Andric SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) { 19230b57cec5SDimitry Andric unsigned NumElems = Node->getNumOperands(); 19240b57cec5SDimitry Andric SDValue Value1, Value2; 19250b57cec5SDimitry Andric SDLoc dl(Node); 19260b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 19270b57cec5SDimitry Andric EVT OpVT = Node->getOperand(0).getValueType(); 19280b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 19290b57cec5SDimitry Andric 19300b57cec5SDimitry Andric // If the only non-undef value is the low element, turn this into a 19310b57cec5SDimitry Andric // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X. 19320b57cec5SDimitry Andric bool isOnlyLowElement = true; 19330b57cec5SDimitry Andric bool MoreThanTwoValues = false; 19340b57cec5SDimitry Andric bool isConstant = true; 19350b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19360b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 19370b57cec5SDimitry Andric if (V.isUndef()) 19380b57cec5SDimitry Andric continue; 19390b57cec5SDimitry Andric if (i > 0) 19400b57cec5SDimitry Andric isOnlyLowElement = false; 19410b57cec5SDimitry Andric if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V)) 19420b57cec5SDimitry Andric isConstant = false; 19430b57cec5SDimitry Andric 19440b57cec5SDimitry Andric if (!Value1.getNode()) { 19450b57cec5SDimitry Andric Value1 = V; 19460b57cec5SDimitry Andric } else if (!Value2.getNode()) { 19470b57cec5SDimitry Andric if (V != Value1) 19480b57cec5SDimitry Andric Value2 = V; 19490b57cec5SDimitry Andric } else if (V != Value1 && V != Value2) { 19500b57cec5SDimitry Andric MoreThanTwoValues = true; 19510b57cec5SDimitry Andric } 19520b57cec5SDimitry Andric } 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric if (!Value1.getNode()) 19550b57cec5SDimitry Andric return DAG.getUNDEF(VT); 19560b57cec5SDimitry Andric 19570b57cec5SDimitry Andric if (isOnlyLowElement) 19580b57cec5SDimitry Andric return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Node->getOperand(0)); 19590b57cec5SDimitry Andric 19600b57cec5SDimitry Andric // If all elements are constants, create a load from the constant pool. 19610b57cec5SDimitry Andric if (isConstant) { 19620b57cec5SDimitry Andric SmallVector<Constant*, 16> CV; 19630b57cec5SDimitry Andric for (unsigned i = 0, e = NumElems; i != e; ++i) { 19640b57cec5SDimitry Andric if (ConstantFPSDNode *V = 19650b57cec5SDimitry Andric dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) { 19660b57cec5SDimitry Andric CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue())); 19670b57cec5SDimitry Andric } else if (ConstantSDNode *V = 19680b57cec5SDimitry Andric dyn_cast<ConstantSDNode>(Node->getOperand(i))) { 19690b57cec5SDimitry Andric if (OpVT==EltVT) 19700b57cec5SDimitry Andric CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue())); 19710b57cec5SDimitry Andric else { 19720b57cec5SDimitry Andric // If OpVT and EltVT don't match, EltVT is not legal and the 19730b57cec5SDimitry Andric // element values have been promoted/truncated earlier. Undo this; 19740b57cec5SDimitry Andric // we don't want a v16i8 to become a v16i32 for example. 19750b57cec5SDimitry Andric const ConstantInt *CI = V->getConstantIntValue(); 19760b57cec5SDimitry Andric CV.push_back(ConstantInt::get(EltVT.getTypeForEVT(*DAG.getContext()), 19770b57cec5SDimitry Andric CI->getZExtValue())); 19780b57cec5SDimitry Andric } 19790b57cec5SDimitry Andric } else { 19800b57cec5SDimitry Andric assert(Node->getOperand(i).isUndef()); 19810b57cec5SDimitry Andric Type *OpNTy = EltVT.getTypeForEVT(*DAG.getContext()); 19820b57cec5SDimitry Andric CV.push_back(UndefValue::get(OpNTy)); 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric } 19850b57cec5SDimitry Andric Constant *CP = ConstantVector::get(CV); 19860b57cec5SDimitry Andric SDValue CPIdx = 19870b57cec5SDimitry Andric DAG.getConstantPool(CP, TLI.getPointerTy(DAG.getDataLayout())); 19885ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 19890b57cec5SDimitry Andric return DAG.getLoad( 19900b57cec5SDimitry Andric VT, dl, DAG.getEntryNode(), CPIdx, 19910b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 19920b57cec5SDimitry Andric Alignment); 19930b57cec5SDimitry Andric } 19940b57cec5SDimitry Andric 19950b57cec5SDimitry Andric SmallSet<SDValue, 16> DefinedValues; 19960b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 19970b57cec5SDimitry Andric if (Node->getOperand(i).isUndef()) 19980b57cec5SDimitry Andric continue; 19990b57cec5SDimitry Andric DefinedValues.insert(Node->getOperand(i)); 20000b57cec5SDimitry Andric } 20010b57cec5SDimitry Andric 20020b57cec5SDimitry Andric if (TLI.shouldExpandBuildVectorWithShuffles(VT, DefinedValues.size())) { 20030b57cec5SDimitry Andric if (!MoreThanTwoValues) { 20040b57cec5SDimitry Andric SmallVector<int, 8> ShuffleVec(NumElems, -1); 20050b57cec5SDimitry Andric for (unsigned i = 0; i < NumElems; ++i) { 20060b57cec5SDimitry Andric SDValue V = Node->getOperand(i); 20070b57cec5SDimitry Andric if (V.isUndef()) 20080b57cec5SDimitry Andric continue; 20090b57cec5SDimitry Andric ShuffleVec[i] = V == Value1 ? 0 : NumElems; 20100b57cec5SDimitry Andric } 20110b57cec5SDimitry Andric if (TLI.isShuffleMaskLegal(ShuffleVec, Node->getValueType(0))) { 20120b57cec5SDimitry Andric // Get the splatted value into the low element of a vector register. 20130b57cec5SDimitry Andric SDValue Vec1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value1); 20140b57cec5SDimitry Andric SDValue Vec2; 20150b57cec5SDimitry Andric if (Value2.getNode()) 20160b57cec5SDimitry Andric Vec2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Value2); 20170b57cec5SDimitry Andric else 20180b57cec5SDimitry Andric Vec2 = DAG.getUNDEF(VT); 20190b57cec5SDimitry Andric 20200b57cec5SDimitry Andric // Return shuffle(LowValVec, undef, <0,0,0,0>) 20210b57cec5SDimitry Andric return DAG.getVectorShuffle(VT, dl, Vec1, Vec2, ShuffleVec); 20220b57cec5SDimitry Andric } 20230b57cec5SDimitry Andric } else { 20240b57cec5SDimitry Andric SDValue Res; 20250b57cec5SDimitry Andric if (ExpandBVWithShuffles(Node, DAG, TLI, Res)) 20260b57cec5SDimitry Andric return Res; 20270b57cec5SDimitry Andric } 20280b57cec5SDimitry Andric } 20290b57cec5SDimitry Andric 20300b57cec5SDimitry Andric // Otherwise, we can't handle this case efficiently. 20310b57cec5SDimitry Andric return ExpandVectorBuildThroughStack(Node); 20320b57cec5SDimitry Andric } 20330b57cec5SDimitry Andric 20348bcb0991SDimitry Andric SDValue SelectionDAGLegalize::ExpandSPLAT_VECTOR(SDNode *Node) { 20358bcb0991SDimitry Andric SDLoc DL(Node); 20368bcb0991SDimitry Andric EVT VT = Node->getValueType(0); 20378bcb0991SDimitry Andric SDValue SplatVal = Node->getOperand(0); 20388bcb0991SDimitry Andric 20398bcb0991SDimitry Andric return DAG.getSplatBuildVector(VT, DL, SplatVal); 20408bcb0991SDimitry Andric } 20418bcb0991SDimitry Andric 204206c3fb27SDimitry Andric // Expand a node into a call to a libcall, returning the value as the first 204306c3fb27SDimitry Andric // result and the chain as the second. If the result value does not fit into a 204406c3fb27SDimitry Andric // register, return the lo part and set the hi part to the by-reg argument in 204506c3fb27SDimitry Andric // the first. If it does fit into a single register, return the result and 204606c3fb27SDimitry Andric // leave the Hi part unset. 204706c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 204806c3fb27SDimitry Andric TargetLowering::ArgListTy &&Args, 20490b57cec5SDimitry Andric bool isSigned) { 20500b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 20510b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 20520b57cec5SDimitry Andric 20530b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 20540b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 20550b57cec5SDimitry Andric 20560b57cec5SDimitry Andric // By default, the input chain to this libcall is the entry node of the 20570b57cec5SDimitry Andric // function. If the libcall is going to be emitted as a tail call then 20580b57cec5SDimitry Andric // TLI.isUsedByReturnOnly will change it to the right chain if the return 20590b57cec5SDimitry Andric // node which is being folded has a non-entry input chain. 20600b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 20610b57cec5SDimitry Andric 20620b57cec5SDimitry Andric // isTailCall may be true since the callee does not reference caller stack 20630b57cec5SDimitry Andric // frame. Check if it's in the right position and that the return types match. 20640b57cec5SDimitry Andric SDValue TCChain = InChain; 20650b57cec5SDimitry Andric const Function &F = DAG.getMachineFunction().getFunction(); 20660b57cec5SDimitry Andric bool isTailCall = 20670b57cec5SDimitry Andric TLI.isInTailCallPosition(DAG, Node, TCChain) && 20680b57cec5SDimitry Andric (RetTy == F.getReturnType() || F.getReturnType()->isVoidTy()); 20690b57cec5SDimitry Andric if (isTailCall) 20700b57cec5SDimitry Andric InChain = TCChain; 20710b57cec5SDimitry Andric 20720b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 20730b57cec5SDimitry Andric bool signExtend = TLI.shouldSignExtendTypeInLibCall(RetVT, isSigned); 20740b57cec5SDimitry Andric CLI.setDebugLoc(SDLoc(Node)) 20750b57cec5SDimitry Andric .setChain(InChain) 20760b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 20770b57cec5SDimitry Andric std::move(Args)) 20780b57cec5SDimitry Andric .setTailCall(isTailCall) 20790b57cec5SDimitry Andric .setSExtResult(signExtend) 20800b57cec5SDimitry Andric .setZExtResult(!signExtend) 20810b57cec5SDimitry Andric .setIsPostTypeLegalization(true); 20820b57cec5SDimitry Andric 20830b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 20840b57cec5SDimitry Andric 20850b57cec5SDimitry Andric if (!CallInfo.second.getNode()) { 20868bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created tailcall: "; DAG.getRoot().dump(&DAG)); 20870b57cec5SDimitry Andric // It's a tailcall, return the chain (which is the DAG root). 208806c3fb27SDimitry Andric return {DAG.getRoot(), DAG.getRoot()}; 20890b57cec5SDimitry Andric } 20900b57cec5SDimitry Andric 20918bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Created libcall: "; CallInfo.first.dump(&DAG)); 209206c3fb27SDimitry Andric return CallInfo; 209306c3fb27SDimitry Andric } 209406c3fb27SDimitry Andric 209506c3fb27SDimitry Andric std::pair<SDValue, SDValue> SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, 209606c3fb27SDimitry Andric bool isSigned) { 209706c3fb27SDimitry Andric TargetLowering::ArgListTy Args; 209806c3fb27SDimitry Andric TargetLowering::ArgListEntry Entry; 209906c3fb27SDimitry Andric for (const SDValue &Op : Node->op_values()) { 210006c3fb27SDimitry Andric EVT ArgVT = Op.getValueType(); 210106c3fb27SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 210206c3fb27SDimitry Andric Entry.Node = Op; 210306c3fb27SDimitry Andric Entry.Ty = ArgTy; 210406c3fb27SDimitry Andric Entry.IsSExt = TLI.shouldSignExtendTypeInLibCall(ArgVT, isSigned); 210506c3fb27SDimitry Andric Entry.IsZExt = !Entry.IsSExt; 210606c3fb27SDimitry Andric Args.push_back(Entry); 210706c3fb27SDimitry Andric } 210806c3fb27SDimitry Andric 210906c3fb27SDimitry Andric return ExpandLibCall(LC, Node, std::move(Args), isSigned); 211006c3fb27SDimitry Andric } 211106c3fb27SDimitry Andric 211206c3fb27SDimitry Andric void SelectionDAGLegalize::ExpandFrexpLibCall( 211306c3fb27SDimitry Andric SDNode *Node, SmallVectorImpl<SDValue> &Results) { 211406c3fb27SDimitry Andric SDLoc dl(Node); 211506c3fb27SDimitry Andric EVT VT = Node->getValueType(0); 211606c3fb27SDimitry Andric EVT ExpVT = Node->getValueType(1); 211706c3fb27SDimitry Andric 211806c3fb27SDimitry Andric SDValue FPOp = Node->getOperand(0); 211906c3fb27SDimitry Andric 212006c3fb27SDimitry Andric EVT ArgVT = FPOp.getValueType(); 212106c3fb27SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 212206c3fb27SDimitry Andric 212306c3fb27SDimitry Andric TargetLowering::ArgListEntry FPArgEntry; 212406c3fb27SDimitry Andric FPArgEntry.Node = FPOp; 212506c3fb27SDimitry Andric FPArgEntry.Ty = ArgTy; 212606c3fb27SDimitry Andric 212706c3fb27SDimitry Andric SDValue StackSlot = DAG.CreateStackTemporary(ExpVT); 212806c3fb27SDimitry Andric TargetLowering::ArgListEntry PtrArgEntry; 212906c3fb27SDimitry Andric PtrArgEntry.Node = StackSlot; 213006c3fb27SDimitry Andric PtrArgEntry.Ty = PointerType::get(*DAG.getContext(), 213106c3fb27SDimitry Andric DAG.getDataLayout().getAllocaAddrSpace()); 213206c3fb27SDimitry Andric 213306c3fb27SDimitry Andric TargetLowering::ArgListTy Args = {FPArgEntry, PtrArgEntry}; 213406c3fb27SDimitry Andric 213506c3fb27SDimitry Andric RTLIB::Libcall LC = RTLIB::getFREXP(VT); 213606c3fb27SDimitry Andric auto [Call, Chain] = ExpandLibCall(LC, Node, std::move(Args), false); 213706c3fb27SDimitry Andric 213806c3fb27SDimitry Andric // FIXME: Get type of int for libcall declaration and cast 213906c3fb27SDimitry Andric 214006c3fb27SDimitry Andric int FrameIdx = cast<FrameIndexSDNode>(StackSlot)->getIndex(); 214106c3fb27SDimitry Andric auto PtrInfo = 214206c3fb27SDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FrameIdx); 214306c3fb27SDimitry Andric 214406c3fb27SDimitry Andric SDValue LoadExp = DAG.getLoad(ExpVT, dl, Chain, StackSlot, PtrInfo); 214506c3fb27SDimitry Andric SDValue OutputChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 214606c3fb27SDimitry Andric LoadExp.getValue(1), DAG.getRoot()); 214706c3fb27SDimitry Andric DAG.setRoot(OutputChain); 214806c3fb27SDimitry Andric 214906c3fb27SDimitry Andric Results.push_back(Call); 215006c3fb27SDimitry Andric Results.push_back(LoadExp); 21510b57cec5SDimitry Andric } 21520b57cec5SDimitry Andric 2153480093f4SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2154fe6060f1SDimitry Andric RTLIB::Libcall LC, 2155480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2156fe6060f1SDimitry Andric if (LC == RTLIB::UNKNOWN_LIBCALL) 2157fe6060f1SDimitry Andric llvm_unreachable("Can't create an unknown libcall!"); 2158480093f4SDimitry Andric 2159480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2160480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 2161e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops(drop_begin(Node->ops())); 2162480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 2163480093f4SDimitry Andric // FIXME: This doesn't support tail calls. 2164480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 2165480093f4SDimitry Andric Ops, CallOptions, 2166480093f4SDimitry Andric SDLoc(Node), 2167480093f4SDimitry Andric Node->getOperand(0)); 2168480093f4SDimitry Andric Results.push_back(Tmp.first); 2169480093f4SDimitry Andric Results.push_back(Tmp.second); 2170480093f4SDimitry Andric } else { 217106c3fb27SDimitry Andric SDValue Tmp = ExpandLibCall(LC, Node, false).first; 2172480093f4SDimitry Andric Results.push_back(Tmp); 2173480093f4SDimitry Andric } 21740b57cec5SDimitry Andric } 21750b57cec5SDimitry Andric 2176fe6060f1SDimitry Andric /// Expand the node to a libcall based on the result type. 2177fe6060f1SDimitry Andric void SelectionDAGLegalize::ExpandFPLibCall(SDNode* Node, 2178fe6060f1SDimitry Andric RTLIB::Libcall Call_F32, 2179fe6060f1SDimitry Andric RTLIB::Libcall Call_F64, 2180fe6060f1SDimitry Andric RTLIB::Libcall Call_F80, 2181fe6060f1SDimitry Andric RTLIB::Libcall Call_F128, 2182fe6060f1SDimitry Andric RTLIB::Libcall Call_PPCF128, 2183fe6060f1SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2184fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(Node->getSimpleValueType(0), 2185fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2186fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2187fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 2188fe6060f1SDimitry Andric } 2189fe6060f1SDimitry Andric 2190bdd1243dSDimitry Andric SDValue SelectionDAGLegalize::ExpandIntLibCall(SDNode* Node, bool isSigned, 2191bdd1243dSDimitry Andric RTLIB::Libcall Call_I8, 2192bdd1243dSDimitry Andric RTLIB::Libcall Call_I16, 2193bdd1243dSDimitry Andric RTLIB::Libcall Call_I32, 2194bdd1243dSDimitry Andric RTLIB::Libcall Call_I64, 2195bdd1243dSDimitry Andric RTLIB::Libcall Call_I128) { 21960b57cec5SDimitry Andric RTLIB::Libcall LC; 21970b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 2198bdd1243dSDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 21990b57cec5SDimitry Andric case MVT::i8: LC = Call_I8; break; 22000b57cec5SDimitry Andric case MVT::i16: LC = Call_I16; break; 22010b57cec5SDimitry Andric case MVT::i32: LC = Call_I32; break; 22020b57cec5SDimitry Andric case MVT::i64: LC = Call_I64; break; 22030b57cec5SDimitry Andric case MVT::i128: LC = Call_I128; break; 22040b57cec5SDimitry Andric } 220506c3fb27SDimitry Andric return ExpandLibCall(LC, Node, isSigned).first; 22060b57cec5SDimitry Andric } 22070b57cec5SDimitry Andric 22080b57cec5SDimitry Andric /// Expand the node to a libcall based on first argument type (for instance 22090b57cec5SDimitry Andric /// lround and its variant). 2210480093f4SDimitry Andric void SelectionDAGLegalize::ExpandArgFPLibCall(SDNode* Node, 22110b57cec5SDimitry Andric RTLIB::Libcall Call_F32, 22120b57cec5SDimitry Andric RTLIB::Libcall Call_F64, 22130b57cec5SDimitry Andric RTLIB::Libcall Call_F80, 22140b57cec5SDimitry Andric RTLIB::Libcall Call_F128, 2215480093f4SDimitry Andric RTLIB::Libcall Call_PPCF128, 2216480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2217480093f4SDimitry Andric EVT InVT = Node->getOperand(Node->isStrictFPOpcode() ? 1 : 0).getValueType(); 2218fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPLibCall(InVT.getSimpleVT(), 2219fe6060f1SDimitry Andric Call_F32, Call_F64, Call_F80, 2220fe6060f1SDimitry Andric Call_F128, Call_PPCF128); 2221fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 22220b57cec5SDimitry Andric } 22230b57cec5SDimitry Andric 22240b57cec5SDimitry Andric /// Issue libcalls to __{u}divmod to compute div / rem pairs. 22250b57cec5SDimitry Andric void 22260b57cec5SDimitry Andric SelectionDAGLegalize::ExpandDivRemLibCall(SDNode *Node, 22270b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 22280b57cec5SDimitry Andric unsigned Opcode = Node->getOpcode(); 22290b57cec5SDimitry Andric bool isSigned = Opcode == ISD::SDIVREM; 22300b57cec5SDimitry Andric 22310b57cec5SDimitry Andric RTLIB::Libcall LC; 22320b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 2233bdd1243dSDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 22340b57cec5SDimitry Andric case MVT::i8: LC= isSigned ? RTLIB::SDIVREM_I8 : RTLIB::UDIVREM_I8; break; 22350b57cec5SDimitry Andric case MVT::i16: LC= isSigned ? RTLIB::SDIVREM_I16 : RTLIB::UDIVREM_I16; break; 22360b57cec5SDimitry Andric case MVT::i32: LC= isSigned ? RTLIB::SDIVREM_I32 : RTLIB::UDIVREM_I32; break; 22370b57cec5SDimitry Andric case MVT::i64: LC= isSigned ? RTLIB::SDIVREM_I64 : RTLIB::UDIVREM_I64; break; 22380b57cec5SDimitry Andric case MVT::i128: LC= isSigned ? RTLIB::SDIVREM_I128:RTLIB::UDIVREM_I128; break; 22390b57cec5SDimitry Andric } 22400b57cec5SDimitry Andric 22410b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 22420b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 22430b57cec5SDimitry Andric // dependence. 22440b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 22450b57cec5SDimitry Andric 22460b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 22470b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 22480b57cec5SDimitry Andric 22490b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 22500b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 22510b57cec5SDimitry Andric for (const SDValue &Op : Node->op_values()) { 22520b57cec5SDimitry Andric EVT ArgVT = Op.getValueType(); 22530b57cec5SDimitry Andric Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); 22540b57cec5SDimitry Andric Entry.Node = Op; 22550b57cec5SDimitry Andric Entry.Ty = ArgTy; 22560b57cec5SDimitry Andric Entry.IsSExt = isSigned; 22570b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 22580b57cec5SDimitry Andric Args.push_back(Entry); 22590b57cec5SDimitry Andric } 22600b57cec5SDimitry Andric 22610b57cec5SDimitry Andric // Also pass the return address of the remainder. 22620b57cec5SDimitry Andric SDValue FIPtr = DAG.CreateStackTemporary(RetVT); 22630b57cec5SDimitry Andric Entry.Node = FIPtr; 22645f757f3fSDimitry Andric Entry.Ty = PointerType::getUnqual(RetTy->getContext()); 22650b57cec5SDimitry Andric Entry.IsSExt = isSigned; 22660b57cec5SDimitry Andric Entry.IsZExt = !isSigned; 22670b57cec5SDimitry Andric Args.push_back(Entry); 22680b57cec5SDimitry Andric 22690b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 22700b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 22710b57cec5SDimitry Andric 22720b57cec5SDimitry Andric SDLoc dl(Node); 22730b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 22740b57cec5SDimitry Andric CLI.setDebugLoc(dl) 22750b57cec5SDimitry Andric .setChain(InChain) 22760b57cec5SDimitry Andric .setLibCallee(TLI.getLibcallCallingConv(LC), RetTy, Callee, 22770b57cec5SDimitry Andric std::move(Args)) 22780b57cec5SDimitry Andric .setSExtResult(isSigned) 22790b57cec5SDimitry Andric .setZExtResult(!isSigned); 22800b57cec5SDimitry Andric 22810b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 22820b57cec5SDimitry Andric 22830b57cec5SDimitry Andric // Remainder is loaded back from the stack frame. 22840b57cec5SDimitry Andric SDValue Rem = 22850b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, FIPtr, MachinePointerInfo()); 22860b57cec5SDimitry Andric Results.push_back(CallInfo.first); 22870b57cec5SDimitry Andric Results.push_back(Rem); 22880b57cec5SDimitry Andric } 22890b57cec5SDimitry Andric 22900b57cec5SDimitry Andric /// Return true if sincos libcall is available. 22910b57cec5SDimitry Andric static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) { 22920b57cec5SDimitry Andric RTLIB::Libcall LC; 22930b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 22940b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 22950b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 22960b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 22970b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 22980b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 22990b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 23000b57cec5SDimitry Andric } 23010b57cec5SDimitry Andric return TLI.getLibcallName(LC) != nullptr; 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric 23040b57cec5SDimitry Andric /// Only issue sincos libcall if both sin and cos are needed. 23050b57cec5SDimitry Andric static bool useSinCos(SDNode *Node) { 23060b57cec5SDimitry Andric unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN 23070b57cec5SDimitry Andric ? ISD::FCOS : ISD::FSIN; 23080b57cec5SDimitry Andric 23090b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 2310349cc55cSDimitry Andric for (const SDNode *User : Op0.getNode()->uses()) { 23110b57cec5SDimitry Andric if (User == Node) 23120b57cec5SDimitry Andric continue; 23130b57cec5SDimitry Andric // The other user might have been turned into sincos already. 23140b57cec5SDimitry Andric if (User->getOpcode() == OtherOpcode || User->getOpcode() == ISD::FSINCOS) 23150b57cec5SDimitry Andric return true; 23160b57cec5SDimitry Andric } 23170b57cec5SDimitry Andric return false; 23180b57cec5SDimitry Andric } 23190b57cec5SDimitry Andric 23200b57cec5SDimitry Andric /// Issue libcalls to sincos to compute sin / cos pairs. 23210b57cec5SDimitry Andric void 23220b57cec5SDimitry Andric SelectionDAGLegalize::ExpandSinCosLibCall(SDNode *Node, 23230b57cec5SDimitry Andric SmallVectorImpl<SDValue> &Results) { 23240b57cec5SDimitry Andric RTLIB::Libcall LC; 23250b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 23260b57cec5SDimitry Andric default: llvm_unreachable("Unexpected request for libcall!"); 23270b57cec5SDimitry Andric case MVT::f32: LC = RTLIB::SINCOS_F32; break; 23280b57cec5SDimitry Andric case MVT::f64: LC = RTLIB::SINCOS_F64; break; 23290b57cec5SDimitry Andric case MVT::f80: LC = RTLIB::SINCOS_F80; break; 23300b57cec5SDimitry Andric case MVT::f128: LC = RTLIB::SINCOS_F128; break; 23310b57cec5SDimitry Andric case MVT::ppcf128: LC = RTLIB::SINCOS_PPCF128; break; 23320b57cec5SDimitry Andric } 23330b57cec5SDimitry Andric 23340b57cec5SDimitry Andric // The input chain to this libcall is the entry node of the function. 23350b57cec5SDimitry Andric // Legalizing the call will automatically add the previous call to the 23360b57cec5SDimitry Andric // dependence. 23370b57cec5SDimitry Andric SDValue InChain = DAG.getEntryNode(); 23380b57cec5SDimitry Andric 23390b57cec5SDimitry Andric EVT RetVT = Node->getValueType(0); 23400b57cec5SDimitry Andric Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext()); 23410b57cec5SDimitry Andric 23420b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 23430b57cec5SDimitry Andric TargetLowering::ArgListEntry Entry; 23440b57cec5SDimitry Andric 23450b57cec5SDimitry Andric // Pass the argument. 23460b57cec5SDimitry Andric Entry.Node = Node->getOperand(0); 23470b57cec5SDimitry Andric Entry.Ty = RetTy; 23480b57cec5SDimitry Andric Entry.IsSExt = false; 23490b57cec5SDimitry Andric Entry.IsZExt = false; 23500b57cec5SDimitry Andric Args.push_back(Entry); 23510b57cec5SDimitry Andric 23520b57cec5SDimitry Andric // Pass the return address of sin. 23530b57cec5SDimitry Andric SDValue SinPtr = DAG.CreateStackTemporary(RetVT); 23540b57cec5SDimitry Andric Entry.Node = SinPtr; 23555f757f3fSDimitry Andric Entry.Ty = PointerType::getUnqual(RetTy->getContext()); 23560b57cec5SDimitry Andric Entry.IsSExt = false; 23570b57cec5SDimitry Andric Entry.IsZExt = false; 23580b57cec5SDimitry Andric Args.push_back(Entry); 23590b57cec5SDimitry Andric 23600b57cec5SDimitry Andric // Also pass the return address of the cos. 23610b57cec5SDimitry Andric SDValue CosPtr = DAG.CreateStackTemporary(RetVT); 23620b57cec5SDimitry Andric Entry.Node = CosPtr; 23635f757f3fSDimitry Andric Entry.Ty = PointerType::getUnqual(RetTy->getContext()); 23640b57cec5SDimitry Andric Entry.IsSExt = false; 23650b57cec5SDimitry Andric Entry.IsZExt = false; 23660b57cec5SDimitry Andric Args.push_back(Entry); 23670b57cec5SDimitry Andric 23680b57cec5SDimitry Andric SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC), 23690b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 23700b57cec5SDimitry Andric 23710b57cec5SDimitry Andric SDLoc dl(Node); 23720b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 23730b57cec5SDimitry Andric CLI.setDebugLoc(dl).setChain(InChain).setLibCallee( 23740b57cec5SDimitry Andric TLI.getLibcallCallingConv(LC), Type::getVoidTy(*DAG.getContext()), Callee, 23750b57cec5SDimitry Andric std::move(Args)); 23760b57cec5SDimitry Andric 23770b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallInfo = TLI.LowerCallTo(CLI); 23780b57cec5SDimitry Andric 23790b57cec5SDimitry Andric Results.push_back( 23800b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, SinPtr, MachinePointerInfo())); 23810b57cec5SDimitry Andric Results.push_back( 23820b57cec5SDimitry Andric DAG.getLoad(RetVT, dl, CallInfo.second, CosPtr, MachinePointerInfo())); 23830b57cec5SDimitry Andric } 23840b57cec5SDimitry Andric 238506c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandLdexp(SDNode *Node) const { 238606c3fb27SDimitry Andric SDLoc dl(Node); 238706c3fb27SDimitry Andric EVT VT = Node->getValueType(0); 238806c3fb27SDimitry Andric SDValue X = Node->getOperand(0); 238906c3fb27SDimitry Andric SDValue N = Node->getOperand(1); 239006c3fb27SDimitry Andric EVT ExpVT = N.getValueType(); 239106c3fb27SDimitry Andric EVT AsIntVT = VT.changeTypeToInteger(); 239206c3fb27SDimitry Andric if (AsIntVT == EVT()) // TODO: How to handle f80? 239306c3fb27SDimitry Andric return SDValue(); 239406c3fb27SDimitry Andric 239506c3fb27SDimitry Andric if (Node->getOpcode() == ISD::STRICT_FLDEXP) // TODO 239606c3fb27SDimitry Andric return SDValue(); 239706c3fb27SDimitry Andric 239806c3fb27SDimitry Andric SDNodeFlags NSW; 239906c3fb27SDimitry Andric NSW.setNoSignedWrap(true); 240006c3fb27SDimitry Andric SDNodeFlags NUW_NSW; 240106c3fb27SDimitry Andric NUW_NSW.setNoUnsignedWrap(true); 240206c3fb27SDimitry Andric NUW_NSW.setNoSignedWrap(true); 240306c3fb27SDimitry Andric 240406c3fb27SDimitry Andric EVT SetCCVT = 240506c3fb27SDimitry Andric TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), ExpVT); 240606c3fb27SDimitry Andric const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT); 240706c3fb27SDimitry Andric 240806c3fb27SDimitry Andric const APFloat::ExponentType MaxExpVal = APFloat::semanticsMaxExponent(FltSem); 240906c3fb27SDimitry Andric const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem); 241006c3fb27SDimitry Andric const int Precision = APFloat::semanticsPrecision(FltSem); 241106c3fb27SDimitry Andric 241206c3fb27SDimitry Andric const SDValue MaxExp = DAG.getConstant(MaxExpVal, dl, ExpVT); 241306c3fb27SDimitry Andric const SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT); 241406c3fb27SDimitry Andric 241506c3fb27SDimitry Andric const SDValue DoubleMaxExp = DAG.getConstant(2 * MaxExpVal, dl, ExpVT); 241606c3fb27SDimitry Andric 241706c3fb27SDimitry Andric const APFloat One(FltSem, "1.0"); 241806c3fb27SDimitry Andric APFloat ScaleUpK = scalbn(One, MaxExpVal, APFloat::rmNearestTiesToEven); 241906c3fb27SDimitry Andric 242006c3fb27SDimitry Andric // Offset by precision to avoid denormal range. 242106c3fb27SDimitry Andric APFloat ScaleDownK = 242206c3fb27SDimitry Andric scalbn(One, MinExpVal + Precision, APFloat::rmNearestTiesToEven); 242306c3fb27SDimitry Andric 242406c3fb27SDimitry Andric // TODO: Should really introduce control flow and use a block for the > 242506c3fb27SDimitry Andric // MaxExp, < MinExp cases 242606c3fb27SDimitry Andric 242706c3fb27SDimitry Andric // First, handle exponents Exp > MaxExp and scale down. 242806c3fb27SDimitry Andric SDValue NGtMaxExp = DAG.getSetCC(dl, SetCCVT, N, MaxExp, ISD::SETGT); 242906c3fb27SDimitry Andric 243006c3fb27SDimitry Andric SDValue DecN0 = DAG.getNode(ISD::SUB, dl, ExpVT, N, MaxExp, NSW); 243106c3fb27SDimitry Andric SDValue ClampMaxVal = DAG.getConstant(3 * MaxExpVal, dl, ExpVT); 243206c3fb27SDimitry Andric SDValue ClampN_Big = DAG.getNode(ISD::SMIN, dl, ExpVT, N, ClampMaxVal); 243306c3fb27SDimitry Andric SDValue DecN1 = 243406c3fb27SDimitry Andric DAG.getNode(ISD::SUB, dl, ExpVT, ClampN_Big, DoubleMaxExp, NSW); 243506c3fb27SDimitry Andric 243606c3fb27SDimitry Andric SDValue ScaleUpTwice = 243706c3fb27SDimitry Andric DAG.getSetCC(dl, SetCCVT, N, DoubleMaxExp, ISD::SETUGT); 243806c3fb27SDimitry Andric 243906c3fb27SDimitry Andric const SDValue ScaleUpVal = DAG.getConstantFP(ScaleUpK, dl, VT); 244006c3fb27SDimitry Andric SDValue ScaleUp0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleUpVal); 244106c3fb27SDimitry Andric SDValue ScaleUp1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleUp0, ScaleUpVal); 244206c3fb27SDimitry Andric 244306c3fb27SDimitry Andric SDValue SelectN_Big = 244406c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleUpTwice, DecN1, DecN0); 244506c3fb27SDimitry Andric SDValue SelectX_Big = 244606c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, VT, ScaleUpTwice, ScaleUp1, ScaleUp0); 244706c3fb27SDimitry Andric 244806c3fb27SDimitry Andric // Now handle exponents Exp < MinExp 244906c3fb27SDimitry Andric SDValue NLtMinExp = DAG.getSetCC(dl, SetCCVT, N, MinExp, ISD::SETLT); 245006c3fb27SDimitry Andric 245106c3fb27SDimitry Andric SDValue Increment0 = DAG.getConstant(-(MinExpVal + Precision), dl, ExpVT); 245206c3fb27SDimitry Andric SDValue Increment1 = DAG.getConstant(-2 * (MinExpVal + Precision), dl, ExpVT); 245306c3fb27SDimitry Andric 245406c3fb27SDimitry Andric SDValue IncN0 = DAG.getNode(ISD::ADD, dl, ExpVT, N, Increment0, NUW_NSW); 245506c3fb27SDimitry Andric 245606c3fb27SDimitry Andric SDValue ClampMinVal = 245706c3fb27SDimitry Andric DAG.getConstant(3 * MinExpVal + 2 * Precision, dl, ExpVT); 245806c3fb27SDimitry Andric SDValue ClampN_Small = DAG.getNode(ISD::SMAX, dl, ExpVT, N, ClampMinVal); 245906c3fb27SDimitry Andric SDValue IncN1 = 246006c3fb27SDimitry Andric DAG.getNode(ISD::ADD, dl, ExpVT, ClampN_Small, Increment1, NSW); 246106c3fb27SDimitry Andric 246206c3fb27SDimitry Andric const SDValue ScaleDownVal = DAG.getConstantFP(ScaleDownK, dl, VT); 246306c3fb27SDimitry Andric SDValue ScaleDown0 = DAG.getNode(ISD::FMUL, dl, VT, X, ScaleDownVal); 246406c3fb27SDimitry Andric SDValue ScaleDown1 = DAG.getNode(ISD::FMUL, dl, VT, ScaleDown0, ScaleDownVal); 246506c3fb27SDimitry Andric 246606c3fb27SDimitry Andric SDValue ScaleDownTwice = DAG.getSetCC( 246706c3fb27SDimitry Andric dl, SetCCVT, N, DAG.getConstant(2 * MinExpVal + Precision, dl, ExpVT), 246806c3fb27SDimitry Andric ISD::SETULT); 246906c3fb27SDimitry Andric 247006c3fb27SDimitry Andric SDValue SelectN_Small = 247106c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, ExpVT, ScaleDownTwice, IncN1, IncN0); 247206c3fb27SDimitry Andric SDValue SelectX_Small = 247306c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, VT, ScaleDownTwice, ScaleDown1, ScaleDown0); 247406c3fb27SDimitry Andric 247506c3fb27SDimitry Andric // Now combine the two out of range exponent handling cases with the base 247606c3fb27SDimitry Andric // case. 247706c3fb27SDimitry Andric SDValue NewX = DAG.getNode( 247806c3fb27SDimitry Andric ISD::SELECT, dl, VT, NGtMaxExp, SelectX_Big, 247906c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, VT, NLtMinExp, SelectX_Small, X)); 248006c3fb27SDimitry Andric 248106c3fb27SDimitry Andric SDValue NewN = DAG.getNode( 248206c3fb27SDimitry Andric ISD::SELECT, dl, ExpVT, NGtMaxExp, SelectN_Big, 248306c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, ExpVT, NLtMinExp, SelectN_Small, N)); 248406c3fb27SDimitry Andric 248506c3fb27SDimitry Andric SDValue BiasedN = DAG.getNode(ISD::ADD, dl, ExpVT, NewN, MaxExp, NSW); 248606c3fb27SDimitry Andric 248706c3fb27SDimitry Andric SDValue ExponentShiftAmt = 248806c3fb27SDimitry Andric DAG.getShiftAmountConstant(Precision - 1, ExpVT, dl); 248906c3fb27SDimitry Andric SDValue CastExpToValTy = DAG.getZExtOrTrunc(BiasedN, dl, AsIntVT); 249006c3fb27SDimitry Andric 249106c3fb27SDimitry Andric SDValue AsInt = DAG.getNode(ISD::SHL, dl, AsIntVT, CastExpToValTy, 249206c3fb27SDimitry Andric ExponentShiftAmt, NUW_NSW); 249306c3fb27SDimitry Andric SDValue AsFP = DAG.getNode(ISD::BITCAST, dl, VT, AsInt); 249406c3fb27SDimitry Andric return DAG.getNode(ISD::FMUL, dl, VT, NewX, AsFP); 249506c3fb27SDimitry Andric } 249606c3fb27SDimitry Andric 249706c3fb27SDimitry Andric SDValue SelectionDAGLegalize::expandFrexp(SDNode *Node) const { 249806c3fb27SDimitry Andric SDLoc dl(Node); 249906c3fb27SDimitry Andric SDValue Val = Node->getOperand(0); 250006c3fb27SDimitry Andric EVT VT = Val.getValueType(); 250106c3fb27SDimitry Andric EVT ExpVT = Node->getValueType(1); 250206c3fb27SDimitry Andric EVT AsIntVT = VT.changeTypeToInteger(); 250306c3fb27SDimitry Andric if (AsIntVT == EVT()) // TODO: How to handle f80? 250406c3fb27SDimitry Andric return SDValue(); 250506c3fb27SDimitry Andric 250606c3fb27SDimitry Andric const fltSemantics &FltSem = SelectionDAG::EVTToAPFloatSemantics(VT); 250706c3fb27SDimitry Andric const APFloat::ExponentType MinExpVal = APFloat::semanticsMinExponent(FltSem); 250806c3fb27SDimitry Andric const unsigned Precision = APFloat::semanticsPrecision(FltSem); 250906c3fb27SDimitry Andric const unsigned BitSize = VT.getScalarSizeInBits(); 251006c3fb27SDimitry Andric 251106c3fb27SDimitry Andric // TODO: Could introduce control flow and skip over the denormal handling. 251206c3fb27SDimitry Andric 251306c3fb27SDimitry Andric // scale_up = fmul value, scalbn(1.0, precision + 1) 251406c3fb27SDimitry Andric // extracted_exp = (bitcast value to uint) >> precision - 1 251506c3fb27SDimitry Andric // biased_exp = extracted_exp + min_exp 251606c3fb27SDimitry Andric // extracted_fract = (bitcast value to uint) & (fract_mask | sign_mask) 251706c3fb27SDimitry Andric // 251806c3fb27SDimitry Andric // is_denormal = val < smallest_normalized 251906c3fb27SDimitry Andric // computed_fract = is_denormal ? scale_up : extracted_fract 252006c3fb27SDimitry Andric // computed_exp = is_denormal ? biased_exp + (-precision - 1) : biased_exp 252106c3fb27SDimitry Andric // 252206c3fb27SDimitry Andric // result_0 = (!isfinite(val) || iszero(val)) ? val : computed_fract 252306c3fb27SDimitry Andric // result_1 = (!isfinite(val) || iszero(val)) ? 0 : computed_exp 252406c3fb27SDimitry Andric 252506c3fb27SDimitry Andric SDValue NegSmallestNormalizedInt = DAG.getConstant( 252606c3fb27SDimitry Andric APFloat::getSmallestNormalized(FltSem, true).bitcastToAPInt(), dl, 252706c3fb27SDimitry Andric AsIntVT); 252806c3fb27SDimitry Andric 252906c3fb27SDimitry Andric SDValue SmallestNormalizedInt = DAG.getConstant( 253006c3fb27SDimitry Andric APFloat::getSmallestNormalized(FltSem, false).bitcastToAPInt(), dl, 253106c3fb27SDimitry Andric AsIntVT); 253206c3fb27SDimitry Andric 253306c3fb27SDimitry Andric // Masks out the exponent bits. 253406c3fb27SDimitry Andric SDValue ExpMask = 253506c3fb27SDimitry Andric DAG.getConstant(APFloat::getInf(FltSem).bitcastToAPInt(), dl, AsIntVT); 253606c3fb27SDimitry Andric 253706c3fb27SDimitry Andric // Mask out the exponent part of the value. 253806c3fb27SDimitry Andric // 253906c3fb27SDimitry Andric // e.g, for f32 FractSignMaskVal = 0x807fffff 254006c3fb27SDimitry Andric APInt FractSignMaskVal = APInt::getBitsSet(BitSize, 0, Precision - 1); 254106c3fb27SDimitry Andric FractSignMaskVal.setBit(BitSize - 1); // Set the sign bit 254206c3fb27SDimitry Andric 254306c3fb27SDimitry Andric APInt SignMaskVal = APInt::getSignedMaxValue(BitSize); 254406c3fb27SDimitry Andric SDValue SignMask = DAG.getConstant(SignMaskVal, dl, AsIntVT); 254506c3fb27SDimitry Andric 254606c3fb27SDimitry Andric SDValue FractSignMask = DAG.getConstant(FractSignMaskVal, dl, AsIntVT); 254706c3fb27SDimitry Andric 254806c3fb27SDimitry Andric const APFloat One(FltSem, "1.0"); 254906c3fb27SDimitry Andric // Scale a possible denormal input. 255006c3fb27SDimitry Andric // e.g., for f64, 0x1p+54 255106c3fb27SDimitry Andric APFloat ScaleUpKVal = 255206c3fb27SDimitry Andric scalbn(One, Precision + 1, APFloat::rmNearestTiesToEven); 255306c3fb27SDimitry Andric 255406c3fb27SDimitry Andric SDValue ScaleUpK = DAG.getConstantFP(ScaleUpKVal, dl, VT); 255506c3fb27SDimitry Andric SDValue ScaleUp = DAG.getNode(ISD::FMUL, dl, VT, Val, ScaleUpK); 255606c3fb27SDimitry Andric 255706c3fb27SDimitry Andric EVT SetCCVT = 255806c3fb27SDimitry Andric TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); 255906c3fb27SDimitry Andric 256006c3fb27SDimitry Andric SDValue AsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, Val); 256106c3fb27SDimitry Andric 256206c3fb27SDimitry Andric SDValue Abs = DAG.getNode(ISD::AND, dl, AsIntVT, AsInt, SignMask); 256306c3fb27SDimitry Andric 256406c3fb27SDimitry Andric SDValue AddNegSmallestNormal = 256506c3fb27SDimitry Andric DAG.getNode(ISD::ADD, dl, AsIntVT, Abs, NegSmallestNormalizedInt); 256606c3fb27SDimitry Andric SDValue DenormOrZero = DAG.getSetCC(dl, SetCCVT, AddNegSmallestNormal, 256706c3fb27SDimitry Andric NegSmallestNormalizedInt, ISD::SETULE); 256806c3fb27SDimitry Andric 256906c3fb27SDimitry Andric SDValue IsDenormal = 257006c3fb27SDimitry Andric DAG.getSetCC(dl, SetCCVT, Abs, SmallestNormalizedInt, ISD::SETULT); 257106c3fb27SDimitry Andric 257206c3fb27SDimitry Andric SDValue MinExp = DAG.getConstant(MinExpVal, dl, ExpVT); 257306c3fb27SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, ExpVT); 257406c3fb27SDimitry Andric 257506c3fb27SDimitry Andric SDValue ScaledAsInt = DAG.getNode(ISD::BITCAST, dl, AsIntVT, ScaleUp); 257606c3fb27SDimitry Andric SDValue ScaledSelect = 257706c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ScaledAsInt, AsInt); 257806c3fb27SDimitry Andric 257906c3fb27SDimitry Andric SDValue ExpMaskScaled = 258006c3fb27SDimitry Andric DAG.getNode(ISD::AND, dl, AsIntVT, ScaledAsInt, ExpMask); 258106c3fb27SDimitry Andric 258206c3fb27SDimitry Andric SDValue ScaledValue = 258306c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, AsIntVT, IsDenormal, ExpMaskScaled, Abs); 258406c3fb27SDimitry Andric 258506c3fb27SDimitry Andric // Extract the exponent bits. 258606c3fb27SDimitry Andric SDValue ExponentShiftAmt = 258706c3fb27SDimitry Andric DAG.getShiftAmountConstant(Precision - 1, AsIntVT, dl); 258806c3fb27SDimitry Andric SDValue ShiftedExp = 258906c3fb27SDimitry Andric DAG.getNode(ISD::SRL, dl, AsIntVT, ScaledValue, ExponentShiftAmt); 259006c3fb27SDimitry Andric SDValue Exp = DAG.getSExtOrTrunc(ShiftedExp, dl, ExpVT); 259106c3fb27SDimitry Andric 259206c3fb27SDimitry Andric SDValue NormalBiasedExp = DAG.getNode(ISD::ADD, dl, ExpVT, Exp, MinExp); 259306c3fb27SDimitry Andric SDValue DenormalOffset = DAG.getConstant(-Precision - 1, dl, ExpVT); 259406c3fb27SDimitry Andric SDValue DenormalExpBias = 259506c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, ExpVT, IsDenormal, DenormalOffset, Zero); 259606c3fb27SDimitry Andric 259706c3fb27SDimitry Andric SDValue MaskedFractAsInt = 259806c3fb27SDimitry Andric DAG.getNode(ISD::AND, dl, AsIntVT, ScaledSelect, FractSignMask); 259906c3fb27SDimitry Andric const APFloat Half(FltSem, "0.5"); 260006c3fb27SDimitry Andric SDValue FPHalf = DAG.getConstant(Half.bitcastToAPInt(), dl, AsIntVT); 260106c3fb27SDimitry Andric SDValue Or = DAG.getNode(ISD::OR, dl, AsIntVT, MaskedFractAsInt, FPHalf); 260206c3fb27SDimitry Andric SDValue MaskedFract = DAG.getNode(ISD::BITCAST, dl, VT, Or); 260306c3fb27SDimitry Andric 260406c3fb27SDimitry Andric SDValue ComputedExp = 260506c3fb27SDimitry Andric DAG.getNode(ISD::ADD, dl, ExpVT, NormalBiasedExp, DenormalExpBias); 260606c3fb27SDimitry Andric 260706c3fb27SDimitry Andric SDValue Result0 = 260806c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, VT, DenormOrZero, Val, MaskedFract); 260906c3fb27SDimitry Andric 261006c3fb27SDimitry Andric SDValue Result1 = 261106c3fb27SDimitry Andric DAG.getNode(ISD::SELECT, dl, ExpVT, DenormOrZero, Zero, ComputedExp); 261206c3fb27SDimitry Andric 261306c3fb27SDimitry Andric return DAG.getMergeValues({Result0, Result1}, dl); 261406c3fb27SDimitry Andric } 261506c3fb27SDimitry Andric 26160b57cec5SDimitry Andric /// This function is responsible for legalizing a 26170b57cec5SDimitry Andric /// INT_TO_FP operation of the specified operand when the target requests that 26180b57cec5SDimitry Andric /// we expand it. At this point, we know that the result and operand types are 26190b57cec5SDimitry Andric /// legal for the target. 2620480093f4SDimitry Andric SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(SDNode *Node, 2621480093f4SDimitry Andric SDValue &Chain) { 2622480093f4SDimitry Andric bool isSigned = (Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 2623480093f4SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP); 2624480093f4SDimitry Andric EVT DestVT = Node->getValueType(0); 2625480093f4SDimitry Andric SDLoc dl(Node); 2626480093f4SDimitry Andric unsigned OpNo = Node->isStrictFPOpcode() ? 1 : 0; 2627480093f4SDimitry Andric SDValue Op0 = Node->getOperand(OpNo); 26280b57cec5SDimitry Andric EVT SrcVT = Op0.getValueType(); 26290b57cec5SDimitry Andric 26300b57cec5SDimitry Andric // TODO: Should any fast-math-flags be set for the created nodes? 26310b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing INT_TO_FP\n"); 2632e8d8bef9SDimitry Andric if (SrcVT == MVT::i32 && TLI.isTypeLegal(MVT::f64) && 2633e8d8bef9SDimitry Andric (DestVT.bitsLE(MVT::f64) || 2634e8d8bef9SDimitry Andric TLI.isOperationLegal(Node->isStrictFPOpcode() ? ISD::STRICT_FP_EXTEND 2635e8d8bef9SDimitry Andric : ISD::FP_EXTEND, 2636e8d8bef9SDimitry Andric DestVT))) { 26370b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "32-bit [signed|unsigned] integer to float/double " 26380b57cec5SDimitry Andric "expansion\n"); 26390b57cec5SDimitry Andric 26400b57cec5SDimitry Andric // Get the stack frame index of a 8 byte buffer. 26410b57cec5SDimitry Andric SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64); 26420b57cec5SDimitry Andric 26435ffd83dbSDimitry Andric SDValue Lo = Op0; 26440b57cec5SDimitry Andric // if signed map to unsigned space 26450b57cec5SDimitry Andric if (isSigned) { 26465ffd83dbSDimitry Andric // Invert sign bit (signed to unsigned mapping). 26475ffd83dbSDimitry Andric Lo = DAG.getNode(ISD::XOR, dl, MVT::i32, Lo, 26485ffd83dbSDimitry Andric DAG.getConstant(0x80000000u, dl, MVT::i32)); 26490b57cec5SDimitry Andric } 26505ffd83dbSDimitry Andric // Initial hi portion of constructed double. 26515ffd83dbSDimitry Andric SDValue Hi = DAG.getConstant(0x43300000u, dl, MVT::i32); 26525ffd83dbSDimitry Andric 26535ffd83dbSDimitry Andric // If this a big endian target, swap the lo and high data. 26545ffd83dbSDimitry Andric if (DAG.getDataLayout().isBigEndian()) 26555ffd83dbSDimitry Andric std::swap(Lo, Hi); 26565ffd83dbSDimitry Andric 26575ffd83dbSDimitry Andric SDValue MemChain = DAG.getEntryNode(); 26585ffd83dbSDimitry Andric 26595ffd83dbSDimitry Andric // Store the lo of the constructed double. 26605ffd83dbSDimitry Andric SDValue Store1 = DAG.getStore(MemChain, dl, Lo, StackSlot, 26610b57cec5SDimitry Andric MachinePointerInfo()); 26625ffd83dbSDimitry Andric // Store the hi of the constructed double. 26635f757f3fSDimitry Andric SDValue HiPtr = 26645f757f3fSDimitry Andric DAG.getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), dl); 26650b57cec5SDimitry Andric SDValue Store2 = 26665ffd83dbSDimitry Andric DAG.getStore(MemChain, dl, Hi, HiPtr, MachinePointerInfo()); 26675ffd83dbSDimitry Andric MemChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Store1, Store2); 26685ffd83dbSDimitry Andric 26690b57cec5SDimitry Andric // load the constructed double 26700b57cec5SDimitry Andric SDValue Load = 26715ffd83dbSDimitry Andric DAG.getLoad(MVT::f64, dl, MemChain, StackSlot, MachinePointerInfo()); 26720b57cec5SDimitry Andric // FP constant to bias correct the final result 267306c3fb27SDimitry Andric SDValue Bias = DAG.getConstantFP( 267406c3fb27SDimitry Andric isSigned ? llvm::bit_cast<double>(0x4330000080000000ULL) 267506c3fb27SDimitry Andric : llvm::bit_cast<double>(0x4330000000000000ULL), 26760b57cec5SDimitry Andric dl, MVT::f64); 2677480093f4SDimitry Andric // Subtract the bias and get the final result. 2678480093f4SDimitry Andric SDValue Sub; 2679480093f4SDimitry Andric SDValue Result; 2680480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2681480093f4SDimitry Andric Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other}, 2682480093f4SDimitry Andric {Node->getOperand(0), Load, Bias}); 2683480093f4SDimitry Andric Chain = Sub.getValue(1); 2684480093f4SDimitry Andric if (DestVT != Sub.getValueType()) { 2685480093f4SDimitry Andric std::pair<SDValue, SDValue> ResultPair; 2686480093f4SDimitry Andric ResultPair = 2687480093f4SDimitry Andric DAG.getStrictFPExtendOrRound(Sub, Chain, dl, DestVT); 2688480093f4SDimitry Andric Result = ResultPair.first; 2689480093f4SDimitry Andric Chain = ResultPair.second; 2690480093f4SDimitry Andric } 2691480093f4SDimitry Andric else 2692480093f4SDimitry Andric Result = Sub; 2693480093f4SDimitry Andric } else { 2694480093f4SDimitry Andric Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Load, Bias); 2695480093f4SDimitry Andric Result = DAG.getFPExtendOrRound(Sub, dl, DestVT); 2696480093f4SDimitry Andric } 26970b57cec5SDimitry Andric return Result; 26980b57cec5SDimitry Andric } 2699e8d8bef9SDimitry Andric 2700e8d8bef9SDimitry Andric if (isSigned) 2701e8d8bef9SDimitry Andric return SDValue(); 27025ffd83dbSDimitry Andric 27035ffd83dbSDimitry Andric // TODO: Generalize this for use with other types. 2704e8d8bef9SDimitry Andric if (((SrcVT == MVT::i32 || SrcVT == MVT::i64) && DestVT == MVT::f32) || 2705e8d8bef9SDimitry Andric (SrcVT == MVT::i64 && DestVT == MVT::f64)) { 2706e8d8bef9SDimitry Andric LLVM_DEBUG(dbgs() << "Converting unsigned i32/i64 to f32/f64\n"); 27075ffd83dbSDimitry Andric // For unsigned conversions, convert them to signed conversions using the 27085ffd83dbSDimitry Andric // algorithm from the x86_64 __floatundisf in compiler_rt. That method 27095ffd83dbSDimitry Andric // should be valid for i32->f32 as well. 27105ffd83dbSDimitry Andric 2711e8d8bef9SDimitry Andric // More generally this transform should be valid if there are 3 more bits 2712e8d8bef9SDimitry Andric // in the integer type than the significand. Rounding uses the first bit 2713e8d8bef9SDimitry Andric // after the width of the significand and the OR of all bits after that. So 2714e8d8bef9SDimitry Andric // we need to be able to OR the shifted out bit into one of the bits that 2715e8d8bef9SDimitry Andric // participate in the OR. 2716e8d8bef9SDimitry Andric 27175ffd83dbSDimitry Andric // TODO: This really should be implemented using a branch rather than a 27185ffd83dbSDimitry Andric // select. We happen to get lucky and machinesink does the right 27195ffd83dbSDimitry Andric // thing most of the time. This would be a good candidate for a 27205ffd83dbSDimitry Andric // pseudo-op, or, even better, for whole-function isel. 27215ffd83dbSDimitry Andric EVT SetCCVT = getSetCCResultType(SrcVT); 27225ffd83dbSDimitry Andric 27235ffd83dbSDimitry Andric SDValue SignBitTest = DAG.getSetCC( 27245ffd83dbSDimitry Andric dl, SetCCVT, Op0, DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 27255ffd83dbSDimitry Andric 27265ffd83dbSDimitry Andric EVT ShiftVT = TLI.getShiftAmountTy(SrcVT, DAG.getDataLayout()); 27275ffd83dbSDimitry Andric SDValue ShiftConst = DAG.getConstant(1, dl, ShiftVT); 27285ffd83dbSDimitry Andric SDValue Shr = DAG.getNode(ISD::SRL, dl, SrcVT, Op0, ShiftConst); 27295ffd83dbSDimitry Andric SDValue AndConst = DAG.getConstant(1, dl, SrcVT); 27305ffd83dbSDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, SrcVT, Op0, AndConst); 27315ffd83dbSDimitry Andric SDValue Or = DAG.getNode(ISD::OR, dl, SrcVT, And, Shr); 27325ffd83dbSDimitry Andric 27335ffd83dbSDimitry Andric SDValue Slow, Fast; 27345ffd83dbSDimitry Andric if (Node->isStrictFPOpcode()) { 27355ffd83dbSDimitry Andric // In strict mode, we must avoid spurious exceptions, and therefore 27365ffd83dbSDimitry Andric // must make sure to only emit a single STRICT_SINT_TO_FP. 27375ffd83dbSDimitry Andric SDValue InCvt = DAG.getSelect(dl, SrcVT, SignBitTest, Or, Op0); 27385ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 27395ffd83dbSDimitry Andric { Node->getOperand(0), InCvt }); 27405ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 27415ffd83dbSDimitry Andric { Fast.getValue(1), Fast, Fast }); 27425ffd83dbSDimitry Andric Chain = Slow.getValue(1); 27435ffd83dbSDimitry Andric // The STRICT_SINT_TO_FP inherits the exception mode from the 27445ffd83dbSDimitry Andric // incoming STRICT_UINT_TO_FP node; the STRICT_FADD node can 27455ffd83dbSDimitry Andric // never raise any exception. 27465ffd83dbSDimitry Andric SDNodeFlags Flags; 27475ffd83dbSDimitry Andric Flags.setNoFPExcept(Node->getFlags().hasNoFPExcept()); 27485ffd83dbSDimitry Andric Fast->setFlags(Flags); 27495ffd83dbSDimitry Andric Flags.setNoFPExcept(true); 27505ffd83dbSDimitry Andric Slow->setFlags(Flags); 27515ffd83dbSDimitry Andric } else { 27525ffd83dbSDimitry Andric SDValue SignCvt = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Or); 27535ffd83dbSDimitry Andric Slow = DAG.getNode(ISD::FADD, dl, DestVT, SignCvt, SignCvt); 27545ffd83dbSDimitry Andric Fast = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 27555ffd83dbSDimitry Andric } 27565ffd83dbSDimitry Andric 27575ffd83dbSDimitry Andric return DAG.getSelect(dl, DestVT, SignBitTest, Slow, Fast); 27585ffd83dbSDimitry Andric } 27595ffd83dbSDimitry Andric 2760e8d8bef9SDimitry Andric // Don't expand it if there isn't cheap fadd. 2761e8d8bef9SDimitry Andric if (!TLI.isOperationLegalOrCustom( 2762e8d8bef9SDimitry Andric Node->isStrictFPOpcode() ? ISD::STRICT_FADD : ISD::FADD, DestVT)) 2763e8d8bef9SDimitry Andric return SDValue(); 2764e8d8bef9SDimitry Andric 27655ffd83dbSDimitry Andric // The following optimization is valid only if every value in SrcVT (when 27665ffd83dbSDimitry Andric // treated as signed) is representable in DestVT. Check that the mantissa 27675ffd83dbSDimitry Andric // size of DestVT is >= than the number of bits in SrcVT -1. 27685ffd83dbSDimitry Andric assert(APFloat::semanticsPrecision(DAG.EVTToAPFloatSemantics(DestVT)) >= 27695ffd83dbSDimitry Andric SrcVT.getSizeInBits() - 1 && 27705ffd83dbSDimitry Andric "Cannot perform lossless SINT_TO_FP!"); 27710b57cec5SDimitry Andric 2772480093f4SDimitry Andric SDValue Tmp1; 2773480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2774480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, { DestVT, MVT::Other }, 2775480093f4SDimitry Andric { Node->getOperand(0), Op0 }); 2776480093f4SDimitry Andric } else 2777480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::SINT_TO_FP, dl, DestVT, Op0); 27780b57cec5SDimitry Andric 27790b57cec5SDimitry Andric SDValue SignSet = DAG.getSetCC(dl, getSetCCResultType(SrcVT), Op0, 27800b57cec5SDimitry Andric DAG.getConstant(0, dl, SrcVT), ISD::SETLT); 27810b57cec5SDimitry Andric SDValue Zero = DAG.getIntPtrConstant(0, dl), 27820b57cec5SDimitry Andric Four = DAG.getIntPtrConstant(4, dl); 27830b57cec5SDimitry Andric SDValue CstOffset = DAG.getSelect(dl, Zero.getValueType(), 27840b57cec5SDimitry Andric SignSet, Four, Zero); 27850b57cec5SDimitry Andric 27860b57cec5SDimitry Andric // If the sign bit of the integer is set, the large number will be treated 27870b57cec5SDimitry Andric // as a negative number. To counteract this, the dynamic code adds an 27880b57cec5SDimitry Andric // offset depending on the data type. 27890b57cec5SDimitry Andric uint64_t FF; 27900b57cec5SDimitry Andric switch (SrcVT.getSimpleVT().SimpleTy) { 2791e8d8bef9SDimitry Andric default: 2792e8d8bef9SDimitry Andric return SDValue(); 27930b57cec5SDimitry Andric case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float) 27940b57cec5SDimitry Andric case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float) 27950b57cec5SDimitry Andric case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float) 27960b57cec5SDimitry Andric case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float) 27970b57cec5SDimitry Andric } 27980b57cec5SDimitry Andric if (DAG.getDataLayout().isLittleEndian()) 27990b57cec5SDimitry Andric FF <<= 32; 28000b57cec5SDimitry Andric Constant *FudgeFactor = ConstantInt::get( 28010b57cec5SDimitry Andric Type::getInt64Ty(*DAG.getContext()), FF); 28020b57cec5SDimitry Andric 28030b57cec5SDimitry Andric SDValue CPIdx = 28040b57cec5SDimitry Andric DAG.getConstantPool(FudgeFactor, TLI.getPointerTy(DAG.getDataLayout())); 28055ffd83dbSDimitry Andric Align Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlign(); 28060b57cec5SDimitry Andric CPIdx = DAG.getNode(ISD::ADD, dl, CPIdx.getValueType(), CPIdx, CstOffset); 28075ffd83dbSDimitry Andric Alignment = commonAlignment(Alignment, 4); 28080b57cec5SDimitry Andric SDValue FudgeInReg; 28090b57cec5SDimitry Andric if (DestVT == MVT::f32) 28100b57cec5SDimitry Andric FudgeInReg = DAG.getLoad( 28110b57cec5SDimitry Andric MVT::f32, dl, DAG.getEntryNode(), CPIdx, 28120b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), 28130b57cec5SDimitry Andric Alignment); 28140b57cec5SDimitry Andric else { 28150b57cec5SDimitry Andric SDValue Load = DAG.getExtLoad( 28160b57cec5SDimitry Andric ISD::EXTLOAD, dl, DestVT, DAG.getEntryNode(), CPIdx, 28170b57cec5SDimitry Andric MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, 28180b57cec5SDimitry Andric Alignment); 28190b57cec5SDimitry Andric HandleSDNode Handle(Load); 28200b57cec5SDimitry Andric LegalizeOp(Load.getNode()); 28210b57cec5SDimitry Andric FudgeInReg = Handle.getValue(); 28220b57cec5SDimitry Andric } 28230b57cec5SDimitry Andric 2824480093f4SDimitry Andric if (Node->isStrictFPOpcode()) { 2825480093f4SDimitry Andric SDValue Result = DAG.getNode(ISD::STRICT_FADD, dl, { DestVT, MVT::Other }, 2826480093f4SDimitry Andric { Tmp1.getValue(1), Tmp1, FudgeInReg }); 2827480093f4SDimitry Andric Chain = Result.getValue(1); 2828480093f4SDimitry Andric return Result; 2829480093f4SDimitry Andric } 2830480093f4SDimitry Andric 28310b57cec5SDimitry Andric return DAG.getNode(ISD::FADD, dl, DestVT, Tmp1, FudgeInReg); 28320b57cec5SDimitry Andric } 28330b57cec5SDimitry Andric 28340b57cec5SDimitry Andric /// This function is responsible for legalizing a 28350b57cec5SDimitry Andric /// *INT_TO_FP operation of the specified operand when the target requests that 28360b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 28370b57cec5SDimitry Andric /// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP 28380b57cec5SDimitry Andric /// operation that takes a larger input. 2839480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalINT_TO_FP( 2840480093f4SDimitry Andric SDNode *N, const SDLoc &dl, SmallVectorImpl<SDValue> &Results) { 2841480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2842480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP || 2843480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_SINT_TO_FP; 2844480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2845480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 2846480093f4SDimitry Andric unsigned UIntOp = IsStrict ? ISD::STRICT_UINT_TO_FP : ISD::UINT_TO_FP; 2847480093f4SDimitry Andric unsigned SIntOp = IsStrict ? ISD::STRICT_SINT_TO_FP : ISD::SINT_TO_FP; 2848480093f4SDimitry Andric 28490b57cec5SDimitry Andric // First step, figure out the appropriate *INT_TO_FP operation to use. 28500b57cec5SDimitry Andric EVT NewInTy = LegalOp.getValueType(); 28510b57cec5SDimitry Andric 28520b57cec5SDimitry Andric unsigned OpToUse = 0; 28530b57cec5SDimitry Andric 28540b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 28550b57cec5SDimitry Andric while (true) { 28560b57cec5SDimitry Andric NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT().SimpleTy+1); 28570b57cec5SDimitry Andric assert(NewInTy.isInteger() && "Ran out of possibilities!"); 28580b57cec5SDimitry Andric 28590b57cec5SDimitry Andric // If the target supports SINT_TO_FP of this type, use it. 2860480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(SIntOp, NewInTy)) { 2861480093f4SDimitry Andric OpToUse = SIntOp; 28620b57cec5SDimitry Andric break; 28630b57cec5SDimitry Andric } 2864480093f4SDimitry Andric if (IsSigned) 2865480093f4SDimitry Andric continue; 28660b57cec5SDimitry Andric 28670b57cec5SDimitry Andric // If the target supports UINT_TO_FP of this type, use it. 2868480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(UIntOp, NewInTy)) { 2869480093f4SDimitry Andric OpToUse = UIntOp; 28700b57cec5SDimitry Andric break; 28710b57cec5SDimitry Andric } 28720b57cec5SDimitry Andric 28730b57cec5SDimitry Andric // Otherwise, try a larger type. 28740b57cec5SDimitry Andric } 28750b57cec5SDimitry Andric 28760b57cec5SDimitry Andric // Okay, we found the operation and type to use. Zero extend our input to the 28770b57cec5SDimitry Andric // desired type then run the operation on it. 2878480093f4SDimitry Andric if (IsStrict) { 2879480093f4SDimitry Andric SDValue Res = 2880480093f4SDimitry Andric DAG.getNode(OpToUse, dl, {DestVT, MVT::Other}, 2881480093f4SDimitry Andric {N->getOperand(0), 2882480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2883480093f4SDimitry Andric dl, NewInTy, LegalOp)}); 2884480093f4SDimitry Andric Results.push_back(Res); 2885480093f4SDimitry Andric Results.push_back(Res.getValue(1)); 2886480093f4SDimitry Andric return; 2887480093f4SDimitry Andric } 2888480093f4SDimitry Andric 2889480093f4SDimitry Andric Results.push_back( 2890480093f4SDimitry Andric DAG.getNode(OpToUse, dl, DestVT, 2891480093f4SDimitry Andric DAG.getNode(IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, 2892480093f4SDimitry Andric dl, NewInTy, LegalOp))); 28930b57cec5SDimitry Andric } 28940b57cec5SDimitry Andric 28950b57cec5SDimitry Andric /// This function is responsible for legalizing a 28960b57cec5SDimitry Andric /// FP_TO_*INT operation of the specified operand when the target requests that 28970b57cec5SDimitry Andric /// we promote it. At this point, we know that the result and operand types are 28980b57cec5SDimitry Andric /// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT 28990b57cec5SDimitry Andric /// operation that returns a larger result. 2900480093f4SDimitry Andric void SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDNode *N, const SDLoc &dl, 2901480093f4SDimitry Andric SmallVectorImpl<SDValue> &Results) { 2902480093f4SDimitry Andric bool IsStrict = N->isStrictFPOpcode(); 2903480093f4SDimitry Andric bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT || 2904480093f4SDimitry Andric N->getOpcode() == ISD::STRICT_FP_TO_SINT; 2905480093f4SDimitry Andric EVT DestVT = N->getValueType(0); 2906480093f4SDimitry Andric SDValue LegalOp = N->getOperand(IsStrict ? 1 : 0); 29070b57cec5SDimitry Andric // First step, figure out the appropriate FP_TO*INT operation to use. 29080b57cec5SDimitry Andric EVT NewOutTy = DestVT; 29090b57cec5SDimitry Andric 29100b57cec5SDimitry Andric unsigned OpToUse = 0; 29110b57cec5SDimitry Andric 29120b57cec5SDimitry Andric // Scan for the appropriate larger type to use. 29130b57cec5SDimitry Andric while (true) { 29140b57cec5SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy+1); 29150b57cec5SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 29160b57cec5SDimitry Andric 29170b57cec5SDimitry Andric // A larger signed type can hold all unsigned values of the requested type, 29180b57cec5SDimitry Andric // so using FP_TO_SINT is valid 2919480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_SINT : ISD::FP_TO_SINT; 2920480093f4SDimitry Andric if (TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 29210b57cec5SDimitry Andric break; 29220b57cec5SDimitry Andric 29230b57cec5SDimitry Andric // However, if the value may be < 0.0, we *must* use some FP_TO_SINT. 2924480093f4SDimitry Andric OpToUse = IsStrict ? ISD::STRICT_FP_TO_UINT : ISD::FP_TO_UINT; 2925480093f4SDimitry Andric if (!IsSigned && TLI.isOperationLegalOrCustom(OpToUse, NewOutTy)) 29260b57cec5SDimitry Andric break; 29270b57cec5SDimitry Andric 29280b57cec5SDimitry Andric // Otherwise, try a larger type. 29290b57cec5SDimitry Andric } 29300b57cec5SDimitry Andric 29310b57cec5SDimitry Andric // Okay, we found the operation and type to use. 2932480093f4SDimitry Andric SDValue Operation; 2933480093f4SDimitry Andric if (IsStrict) { 2934480093f4SDimitry Andric SDVTList VTs = DAG.getVTList(NewOutTy, MVT::Other); 2935480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, VTs, N->getOperand(0), LegalOp); 2936480093f4SDimitry Andric } else 2937480093f4SDimitry Andric Operation = DAG.getNode(OpToUse, dl, NewOutTy, LegalOp); 29380b57cec5SDimitry Andric 29390b57cec5SDimitry Andric // Truncate the result of the extended FP_TO_*INT operation to the desired 29400b57cec5SDimitry Andric // size. 2941480093f4SDimitry Andric SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, DestVT, Operation); 2942480093f4SDimitry Andric Results.push_back(Trunc); 2943480093f4SDimitry Andric if (IsStrict) 2944480093f4SDimitry Andric Results.push_back(Operation.getValue(1)); 29450b57cec5SDimitry Andric } 29460b57cec5SDimitry Andric 2947e8d8bef9SDimitry Andric /// Promote FP_TO_*INT_SAT operation to a larger result type. At this point 2948e8d8bef9SDimitry Andric /// the result and operand types are legal and there must be a legal 2949e8d8bef9SDimitry Andric /// FP_TO_*INT_SAT operation for a larger result type. 2950e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT_SAT(SDNode *Node, 2951e8d8bef9SDimitry Andric const SDLoc &dl) { 2952e8d8bef9SDimitry Andric unsigned Opcode = Node->getOpcode(); 2953e8d8bef9SDimitry Andric 2954e8d8bef9SDimitry Andric // Scan for the appropriate larger type to use. 2955e8d8bef9SDimitry Andric EVT NewOutTy = Node->getValueType(0); 2956e8d8bef9SDimitry Andric while (true) { 2957e8d8bef9SDimitry Andric NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT().SimpleTy + 1); 2958e8d8bef9SDimitry Andric assert(NewOutTy.isInteger() && "Ran out of possibilities!"); 2959e8d8bef9SDimitry Andric 2960e8d8bef9SDimitry Andric if (TLI.isOperationLegalOrCustom(Opcode, NewOutTy)) 2961e8d8bef9SDimitry Andric break; 2962e8d8bef9SDimitry Andric } 2963e8d8bef9SDimitry Andric 2964e8d8bef9SDimitry Andric // Saturation width is determined by second operand, so we don't have to 2965e8d8bef9SDimitry Andric // perform any fixup and can directly truncate the result. 2966e8d8bef9SDimitry Andric SDValue Result = DAG.getNode(Opcode, dl, NewOutTy, Node->getOperand(0), 2967e8d8bef9SDimitry Andric Node->getOperand(1)); 2968e8d8bef9SDimitry Andric return DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Result); 2969e8d8bef9SDimitry Andric } 2970e8d8bef9SDimitry Andric 2971e8d8bef9SDimitry Andric /// Open code the operations for PARITY of the specified operation. 2972e8d8bef9SDimitry Andric SDValue SelectionDAGLegalize::ExpandPARITY(SDValue Op, const SDLoc &dl) { 2973e8d8bef9SDimitry Andric EVT VT = Op.getValueType(); 2974e8d8bef9SDimitry Andric EVT ShVT = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 2975e8d8bef9SDimitry Andric unsigned Sz = VT.getScalarSizeInBits(); 2976e8d8bef9SDimitry Andric 2977e8d8bef9SDimitry Andric // If CTPOP is legal, use it. Otherwise use shifts and xor. 2978e8d8bef9SDimitry Andric SDValue Result; 2979349cc55cSDimitry Andric if (TLI.isOperationLegalOrPromote(ISD::CTPOP, VT)) { 2980e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::CTPOP, dl, VT, Op); 2981e8d8bef9SDimitry Andric } else { 2982e8d8bef9SDimitry Andric Result = Op; 2983e8d8bef9SDimitry Andric for (unsigned i = Log2_32_Ceil(Sz); i != 0;) { 2984e8d8bef9SDimitry Andric SDValue Shift = DAG.getNode(ISD::SRL, dl, VT, Result, 2985e8d8bef9SDimitry Andric DAG.getConstant(1ULL << (--i), dl, ShVT)); 2986e8d8bef9SDimitry Andric Result = DAG.getNode(ISD::XOR, dl, VT, Result, Shift); 2987e8d8bef9SDimitry Andric } 2988e8d8bef9SDimitry Andric } 2989e8d8bef9SDimitry Andric 2990e8d8bef9SDimitry Andric return DAG.getNode(ISD::AND, dl, VT, Result, DAG.getConstant(1, dl, VT)); 2991e8d8bef9SDimitry Andric } 2992e8d8bef9SDimitry Andric 29930b57cec5SDimitry Andric bool SelectionDAGLegalize::ExpandNode(SDNode *Node) { 29940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to expand node\n"); 29950b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 29960b57cec5SDimitry Andric SDLoc dl(Node); 29970b57cec5SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 29980b57cec5SDimitry Andric bool NeedInvert; 29990b57cec5SDimitry Andric switch (Node->getOpcode()) { 30000b57cec5SDimitry Andric case ISD::ABS: 3001349cc55cSDimitry Andric if ((Tmp1 = TLI.expandABS(Node, DAG))) 30020b57cec5SDimitry Andric Results.push_back(Tmp1); 30030b57cec5SDimitry Andric break; 300406c3fb27SDimitry Andric case ISD::ABDS: 300506c3fb27SDimitry Andric case ISD::ABDU: 300606c3fb27SDimitry Andric if ((Tmp1 = TLI.expandABD(Node, DAG))) 300706c3fb27SDimitry Andric Results.push_back(Tmp1); 300806c3fb27SDimitry Andric break; 30090b57cec5SDimitry Andric case ISD::CTPOP: 3010349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTPOP(Node, DAG))) 30110b57cec5SDimitry Andric Results.push_back(Tmp1); 30120b57cec5SDimitry Andric break; 30130b57cec5SDimitry Andric case ISD::CTLZ: 30140b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 3015349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTLZ(Node, DAG))) 30160b57cec5SDimitry Andric Results.push_back(Tmp1); 30170b57cec5SDimitry Andric break; 30180b57cec5SDimitry Andric case ISD::CTTZ: 30190b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 3020349cc55cSDimitry Andric if ((Tmp1 = TLI.expandCTTZ(Node, DAG))) 30210b57cec5SDimitry Andric Results.push_back(Tmp1); 30220b57cec5SDimitry Andric break; 30230b57cec5SDimitry Andric case ISD::BITREVERSE: 3024fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBITREVERSE(Node, DAG))) 3025fe6060f1SDimitry Andric Results.push_back(Tmp1); 30260b57cec5SDimitry Andric break; 30270b57cec5SDimitry Andric case ISD::BSWAP: 3028fe6060f1SDimitry Andric if ((Tmp1 = TLI.expandBSWAP(Node, DAG))) 3029fe6060f1SDimitry Andric Results.push_back(Tmp1); 30300b57cec5SDimitry Andric break; 3031e8d8bef9SDimitry Andric case ISD::PARITY: 3032e8d8bef9SDimitry Andric Results.push_back(ExpandPARITY(Node->getOperand(0), dl)); 3033e8d8bef9SDimitry Andric break; 30340b57cec5SDimitry Andric case ISD::FRAMEADDR: 30350b57cec5SDimitry Andric case ISD::RETURNADDR: 30360b57cec5SDimitry Andric case ISD::FRAME_TO_ARGS_OFFSET: 30370b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 30380b57cec5SDimitry Andric break; 30390b57cec5SDimitry Andric case ISD::EH_DWARF_CFA: { 30400b57cec5SDimitry Andric SDValue CfaArg = DAG.getSExtOrTrunc(Node->getOperand(0), dl, 30410b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())); 30420b57cec5SDimitry Andric SDValue Offset = DAG.getNode(ISD::ADD, dl, 30430b57cec5SDimitry Andric CfaArg.getValueType(), 30440b57cec5SDimitry Andric DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl, 30450b57cec5SDimitry Andric CfaArg.getValueType()), 30460b57cec5SDimitry Andric CfaArg); 30470b57cec5SDimitry Andric SDValue FA = DAG.getNode( 30480b57cec5SDimitry Andric ISD::FRAMEADDR, dl, TLI.getPointerTy(DAG.getDataLayout()), 30490b57cec5SDimitry Andric DAG.getConstant(0, dl, TLI.getPointerTy(DAG.getDataLayout()))); 30500b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, FA.getValueType(), 30510b57cec5SDimitry Andric FA, Offset)); 30520b57cec5SDimitry Andric break; 30530b57cec5SDimitry Andric } 3054bdd1243dSDimitry Andric case ISD::GET_ROUNDING: 30550b57cec5SDimitry Andric Results.push_back(DAG.getConstant(1, dl, Node->getValueType(0))); 30565ffd83dbSDimitry Andric Results.push_back(Node->getOperand(0)); 30570b57cec5SDimitry Andric break; 30580b57cec5SDimitry Andric case ISD::EH_RETURN: 30590b57cec5SDimitry Andric case ISD::EH_LABEL: 30600b57cec5SDimitry Andric case ISD::PREFETCH: 30610b57cec5SDimitry Andric case ISD::VAEND: 30620b57cec5SDimitry Andric case ISD::EH_SJLJ_LONGJMP: 30630b57cec5SDimitry Andric // If the target didn't expand these, there's nothing to do, so just 30640b57cec5SDimitry Andric // preserve the chain and be done. 30650b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 30660b57cec5SDimitry Andric break; 30670b57cec5SDimitry Andric case ISD::READCYCLECOUNTER: 30680b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 30690b57cec5SDimitry Andric // chain. 30700b57cec5SDimitry Andric Results.append(Node->getNumValues() - 1, 30710b57cec5SDimitry Andric DAG.getConstant(0, dl, Node->getValueType(0))); 30720b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 30730b57cec5SDimitry Andric break; 30740b57cec5SDimitry Andric case ISD::EH_SJLJ_SETJMP: 30750b57cec5SDimitry Andric // If the target didn't expand this, just return 'zero' and preserve the 30760b57cec5SDimitry Andric // chain. 30770b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, MVT::i32)); 30780b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 30790b57cec5SDimitry Andric break; 30800b57cec5SDimitry Andric case ISD::ATOMIC_LOAD: { 30810b57cec5SDimitry Andric // There is no libcall for atomic load; fake it with ATOMIC_CMP_SWAP. 30820b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, Node->getValueType(0)); 30830b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 30840b57cec5SDimitry Andric SDValue Swap = DAG.getAtomicCmpSwap( 30850b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 30860b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Zero, Zero, 30870b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 30880b57cec5SDimitry Andric Results.push_back(Swap.getValue(0)); 30890b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 30900b57cec5SDimitry Andric break; 30910b57cec5SDimitry Andric } 30920b57cec5SDimitry Andric case ISD::ATOMIC_STORE: { 30930b57cec5SDimitry Andric // There is no libcall for atomic store; fake it with ATOMIC_SWAP. 30945f757f3fSDimitry Andric SDValue Swap = DAG.getAtomic( 30955f757f3fSDimitry Andric ISD::ATOMIC_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), 30965f757f3fSDimitry Andric Node->getOperand(0), Node->getOperand(2), Node->getOperand(1), 30970b57cec5SDimitry Andric cast<AtomicSDNode>(Node)->getMemOperand()); 30980b57cec5SDimitry Andric Results.push_back(Swap.getValue(1)); 30990b57cec5SDimitry Andric break; 31000b57cec5SDimitry Andric } 31010b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { 31020b57cec5SDimitry Andric // Expanding an ATOMIC_CMP_SWAP_WITH_SUCCESS produces an ATOMIC_CMP_SWAP and 31030b57cec5SDimitry Andric // splits out the success value as a comparison. Expanding the resulting 31040b57cec5SDimitry Andric // ATOMIC_CMP_SWAP will produce a libcall. 31050b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 31060b57cec5SDimitry Andric SDValue Res = DAG.getAtomicCmpSwap( 31070b57cec5SDimitry Andric ISD::ATOMIC_CMP_SWAP, dl, cast<AtomicSDNode>(Node)->getMemoryVT(), VTs, 31080b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), Node->getOperand(2), 31090b57cec5SDimitry Andric Node->getOperand(3), cast<MemSDNode>(Node)->getMemOperand()); 31100b57cec5SDimitry Andric 31110b57cec5SDimitry Andric SDValue ExtRes = Res; 31120b57cec5SDimitry Andric SDValue LHS = Res; 31130b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 31140b57cec5SDimitry Andric 31150b57cec5SDimitry Andric EVT AtomicType = cast<AtomicSDNode>(Node)->getMemoryVT(); 31160b57cec5SDimitry Andric EVT OuterType = Node->getValueType(0); 31170b57cec5SDimitry Andric switch (TLI.getExtendForAtomicOps()) { 31180b57cec5SDimitry Andric case ISD::SIGN_EXTEND: 31190b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertSext, dl, OuterType, Res, 31200b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 31210b57cec5SDimitry Andric RHS = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, OuterType, 31220b57cec5SDimitry Andric Node->getOperand(2), DAG.getValueType(AtomicType)); 31230b57cec5SDimitry Andric ExtRes = LHS; 31240b57cec5SDimitry Andric break; 31250b57cec5SDimitry Andric case ISD::ZERO_EXTEND: 31260b57cec5SDimitry Andric LHS = DAG.getNode(ISD::AssertZext, dl, OuterType, Res, 31270b57cec5SDimitry Andric DAG.getValueType(AtomicType)); 31280b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 31290b57cec5SDimitry Andric ExtRes = LHS; 31300b57cec5SDimitry Andric break; 31310b57cec5SDimitry Andric case ISD::ANY_EXTEND: 31320b57cec5SDimitry Andric LHS = DAG.getZeroExtendInReg(Res, dl, AtomicType); 31330b57cec5SDimitry Andric RHS = DAG.getZeroExtendInReg(Node->getOperand(2), dl, AtomicType); 31340b57cec5SDimitry Andric break; 31350b57cec5SDimitry Andric default: 31360b57cec5SDimitry Andric llvm_unreachable("Invalid atomic op extension"); 31370b57cec5SDimitry Andric } 31380b57cec5SDimitry Andric 31390b57cec5SDimitry Andric SDValue Success = 31400b57cec5SDimitry Andric DAG.getSetCC(dl, Node->getValueType(1), LHS, RHS, ISD::SETEQ); 31410b57cec5SDimitry Andric 31420b57cec5SDimitry Andric Results.push_back(ExtRes.getValue(0)); 31430b57cec5SDimitry Andric Results.push_back(Success); 31440b57cec5SDimitry Andric Results.push_back(Res.getValue(1)); 31450b57cec5SDimitry Andric break; 31460b57cec5SDimitry Andric } 31475f757f3fSDimitry Andric case ISD::ATOMIC_LOAD_SUB: { 31485f757f3fSDimitry Andric SDLoc DL(Node); 31495f757f3fSDimitry Andric EVT VT = Node->getValueType(0); 31505f757f3fSDimitry Andric SDValue RHS = Node->getOperand(2); 31515f757f3fSDimitry Andric AtomicSDNode *AN = cast<AtomicSDNode>(Node); 31525f757f3fSDimitry Andric if (RHS->getOpcode() == ISD::SIGN_EXTEND_INREG && 31535f757f3fSDimitry Andric cast<VTSDNode>(RHS->getOperand(1))->getVT() == AN->getMemoryVT()) 31545f757f3fSDimitry Andric RHS = RHS->getOperand(0); 31555f757f3fSDimitry Andric SDValue NewRHS = 31565f757f3fSDimitry Andric DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS); 31575f757f3fSDimitry Andric SDValue Res = DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, AN->getMemoryVT(), 31585f757f3fSDimitry Andric Node->getOperand(0), Node->getOperand(1), 31595f757f3fSDimitry Andric NewRHS, AN->getMemOperand()); 31605f757f3fSDimitry Andric Results.push_back(Res); 31615f757f3fSDimitry Andric Results.push_back(Res.getValue(1)); 31625f757f3fSDimitry Andric break; 31635f757f3fSDimitry Andric } 31640b57cec5SDimitry Andric case ISD::DYNAMIC_STACKALLOC: 31650b57cec5SDimitry Andric ExpandDYNAMIC_STACKALLOC(Node, Results); 31660b57cec5SDimitry Andric break; 31670b57cec5SDimitry Andric case ISD::MERGE_VALUES: 31680b57cec5SDimitry Andric for (unsigned i = 0; i < Node->getNumValues(); i++) 31690b57cec5SDimitry Andric Results.push_back(Node->getOperand(i)); 31700b57cec5SDimitry Andric break; 31710b57cec5SDimitry Andric case ISD::UNDEF: { 31720b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 31730b57cec5SDimitry Andric if (VT.isInteger()) 31740b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, VT)); 31750b57cec5SDimitry Andric else { 31760b57cec5SDimitry Andric assert(VT.isFloatingPoint() && "Unknown value type!"); 31770b57cec5SDimitry Andric Results.push_back(DAG.getConstantFP(0, dl, VT)); 31780b57cec5SDimitry Andric } 31790b57cec5SDimitry Andric break; 31800b57cec5SDimitry Andric } 31810b57cec5SDimitry Andric case ISD::STRICT_FP_ROUND: 3182480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 3183480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 3184480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 3185480093f4SDimitry Andric break; 3186480093f4SDimitry Andric // We might as well mutate to FP_ROUND when FP_ROUND operation is legal 3187480093f4SDimitry Andric // since this operation is more efficient than stack operation. 31888bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 31898bcb0991SDimitry Andric Node->getValueType(0)) 31908bcb0991SDimitry Andric == TargetLowering::Legal) 31918bcb0991SDimitry Andric break; 3192480093f4SDimitry Andric // We fall back to use stack operation when the FP_ROUND operation 3193480093f4SDimitry Andric // isn't available. 3194e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(1), Node->getValueType(0), 3195e8d8bef9SDimitry Andric Node->getValueType(0), dl, 3196e8d8bef9SDimitry Andric Node->getOperand(0)))) { 31970b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 31980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_ROUND node\n"); 31990b57cec5SDimitry Andric return true; 3200e8d8bef9SDimitry Andric } 3201e8d8bef9SDimitry Andric break; 32020b57cec5SDimitry Andric case ISD::FP_ROUND: 32030b57cec5SDimitry Andric case ISD::BITCAST: 3204e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), Node->getValueType(0), 3205e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 32060b57cec5SDimitry Andric Results.push_back(Tmp1); 32070b57cec5SDimitry Andric break; 32080b57cec5SDimitry Andric case ISD::STRICT_FP_EXTEND: 3209480093f4SDimitry Andric // When strict mode is enforced we can't do expansion because it 3210480093f4SDimitry Andric // does not honor the "strict" properties. Only libcall is allowed. 3211480093f4SDimitry Andric if (TLI.isStrictFPEnabled()) 3212480093f4SDimitry Andric break; 3213480093f4SDimitry Andric // We might as well mutate to FP_EXTEND when FP_EXTEND operation is legal 3214480093f4SDimitry Andric // since this operation is more efficient than stack operation. 32158bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 32168bcb0991SDimitry Andric Node->getValueType(0)) 32178bcb0991SDimitry Andric == TargetLowering::Legal) 32188bcb0991SDimitry Andric break; 3219480093f4SDimitry Andric // We fall back to use stack operation when the FP_EXTEND operation 3220480093f4SDimitry Andric // isn't available. 3221e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert( 3222e8d8bef9SDimitry Andric Node->getOperand(1), Node->getOperand(1).getValueType(), 3223e8d8bef9SDimitry Andric Node->getValueType(0), dl, Node->getOperand(0)))) { 32240b57cec5SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 32250b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_EXTEND node\n"); 32260b57cec5SDimitry Andric return true; 3227e8d8bef9SDimitry Andric } 3228e8d8bef9SDimitry Andric break; 32290b57cec5SDimitry Andric case ISD::FP_EXTEND: 3230e8d8bef9SDimitry Andric if ((Tmp1 = EmitStackConvert(Node->getOperand(0), 32310b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 3232e8d8bef9SDimitry Andric Node->getValueType(0), dl))) 32330b57cec5SDimitry Andric Results.push_back(Tmp1); 32340b57cec5SDimitry Andric break; 323581ad6265SDimitry Andric case ISD::BF16_TO_FP: { 323681ad6265SDimitry Andric // Always expand bf16 to f32 casts, they lower to ext + shift. 3237bdd1243dSDimitry Andric // 3238bdd1243dSDimitry Andric // Note that the operand of this code can be bf16 or an integer type in case 3239bdd1243dSDimitry Andric // bf16 is not supported on the target and was softened. 3240bdd1243dSDimitry Andric SDValue Op = Node->getOperand(0); 3241bdd1243dSDimitry Andric if (Op.getValueType() == MVT::bf16) { 3242bdd1243dSDimitry Andric Op = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, 3243bdd1243dSDimitry Andric DAG.getNode(ISD::BITCAST, dl, MVT::i16, Op)); 3244bdd1243dSDimitry Andric } else { 3245bdd1243dSDimitry Andric Op = DAG.getAnyExtOrTrunc(Op, dl, MVT::i32); 3246bdd1243dSDimitry Andric } 324781ad6265SDimitry Andric Op = DAG.getNode( 324881ad6265SDimitry Andric ISD::SHL, dl, MVT::i32, Op, 324981ad6265SDimitry Andric DAG.getConstant(16, dl, 325081ad6265SDimitry Andric TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout()))); 325181ad6265SDimitry Andric Op = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op); 3252bdd1243dSDimitry Andric // Add fp_extend in case the output is bigger than f32. 3253bdd1243dSDimitry Andric if (Node->getValueType(0) != MVT::f32) 3254bdd1243dSDimitry Andric Op = DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Op); 3255bdd1243dSDimitry Andric Results.push_back(Op); 3256bdd1243dSDimitry Andric break; 3257bdd1243dSDimitry Andric } 3258bdd1243dSDimitry Andric case ISD::FP_TO_BF16: { 3259bdd1243dSDimitry Andric SDValue Op = Node->getOperand(0); 3260bdd1243dSDimitry Andric if (Op.getValueType() != MVT::f32) 3261bdd1243dSDimitry Andric Op = DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 3262bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)); 3263bdd1243dSDimitry Andric Op = DAG.getNode( 3264bdd1243dSDimitry Andric ISD::SRL, dl, MVT::i32, DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op), 3265bdd1243dSDimitry Andric DAG.getConstant(16, dl, 3266bdd1243dSDimitry Andric TLI.getShiftAmountTy(MVT::i32, DAG.getDataLayout()))); 3267bdd1243dSDimitry Andric // The result of this node can be bf16 or an integer type in case bf16 is 3268bdd1243dSDimitry Andric // not supported on the target and was softened to i16 for storage. 3269bdd1243dSDimitry Andric if (Node->getValueType(0) == MVT::bf16) { 3270bdd1243dSDimitry Andric Op = DAG.getNode(ISD::BITCAST, dl, MVT::bf16, 3271bdd1243dSDimitry Andric DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Op)); 3272bdd1243dSDimitry Andric } else { 3273bdd1243dSDimitry Andric Op = DAG.getAnyExtOrTrunc(Op, dl, Node->getValueType(0)); 3274bdd1243dSDimitry Andric } 327581ad6265SDimitry Andric Results.push_back(Op); 327681ad6265SDimitry Andric break; 327781ad6265SDimitry Andric } 32780b57cec5SDimitry Andric case ISD::SIGN_EXTEND_INREG: { 32790b57cec5SDimitry Andric EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); 32800b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 32810b57cec5SDimitry Andric 32820b57cec5SDimitry Andric // An in-register sign-extend of a boolean is a negation: 32830b57cec5SDimitry Andric // 'true' (1) sign-extended is -1. 32840b57cec5SDimitry Andric // 'false' (0) sign-extended is 0. 32850b57cec5SDimitry Andric // However, we must mask the high bits of the source operand because the 32860b57cec5SDimitry Andric // SIGN_EXTEND_INREG does not guarantee that the high bits are already zero. 32870b57cec5SDimitry Andric 32880b57cec5SDimitry Andric // TODO: Do this for vectors too? 328981ad6265SDimitry Andric if (ExtraVT.isScalarInteger() && ExtraVT.getSizeInBits() == 1) { 32900b57cec5SDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 32910b57cec5SDimitry Andric SDValue And = DAG.getNode(ISD::AND, dl, VT, Node->getOperand(0), One); 32920b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 32930b57cec5SDimitry Andric SDValue Neg = DAG.getNode(ISD::SUB, dl, VT, Zero, And); 32940b57cec5SDimitry Andric Results.push_back(Neg); 32950b57cec5SDimitry Andric break; 32960b57cec5SDimitry Andric } 32970b57cec5SDimitry Andric 32980b57cec5SDimitry Andric // NOTE: we could fall back on load/store here too for targets without 32990b57cec5SDimitry Andric // SRA. However, it is doubtful that any exist. 33000b57cec5SDimitry Andric EVT ShiftAmountTy = TLI.getShiftAmountTy(VT, DAG.getDataLayout()); 33010b57cec5SDimitry Andric unsigned BitsDiff = VT.getScalarSizeInBits() - 33020b57cec5SDimitry Andric ExtraVT.getScalarSizeInBits(); 33030b57cec5SDimitry Andric SDValue ShiftCst = DAG.getConstant(BitsDiff, dl, ShiftAmountTy); 33040b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SHL, dl, Node->getValueType(0), 33050b57cec5SDimitry Andric Node->getOperand(0), ShiftCst); 33060b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRA, dl, Node->getValueType(0), Tmp1, ShiftCst); 33070b57cec5SDimitry Andric Results.push_back(Tmp1); 33080b57cec5SDimitry Andric break; 33090b57cec5SDimitry Andric } 33100b57cec5SDimitry Andric case ISD::UINT_TO_FP: 3311480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 3312480093f4SDimitry Andric if (TLI.expandUINT_TO_FP(Node, Tmp1, Tmp2, DAG)) { 33130b57cec5SDimitry Andric Results.push_back(Tmp1); 3314480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 3315480093f4SDimitry Andric Results.push_back(Tmp2); 33160b57cec5SDimitry Andric break; 33170b57cec5SDimitry Andric } 3318bdd1243dSDimitry Andric [[fallthrough]]; 33190b57cec5SDimitry Andric case ISD::SINT_TO_FP: 3320480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 3321e8d8bef9SDimitry Andric if ((Tmp1 = ExpandLegalINT_TO_FP(Node, Tmp2))) { 33220b57cec5SDimitry Andric Results.push_back(Tmp1); 3323480093f4SDimitry Andric if (Node->isStrictFPOpcode()) 3324480093f4SDimitry Andric Results.push_back(Tmp2); 3325e8d8bef9SDimitry Andric } 33260b57cec5SDimitry Andric break; 33270b57cec5SDimitry Andric case ISD::FP_TO_SINT: 33280b57cec5SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) 33290b57cec5SDimitry Andric Results.push_back(Tmp1); 33300b57cec5SDimitry Andric break; 33318bcb0991SDimitry Andric case ISD::STRICT_FP_TO_SINT: 33328bcb0991SDimitry Andric if (TLI.expandFP_TO_SINT(Node, Tmp1, DAG)) { 33338bcb0991SDimitry Andric ReplaceNode(Node, Tmp1.getNode()); 33348bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_SINT node\n"); 33358bcb0991SDimitry Andric return true; 33368bcb0991SDimitry Andric } 33378bcb0991SDimitry Andric break; 33380b57cec5SDimitry Andric case ISD::FP_TO_UINT: 33398bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) 33400b57cec5SDimitry Andric Results.push_back(Tmp1); 33410b57cec5SDimitry Andric break; 33428bcb0991SDimitry Andric case ISD::STRICT_FP_TO_UINT: 33438bcb0991SDimitry Andric if (TLI.expandFP_TO_UINT(Node, Tmp1, Tmp2, DAG)) { 33448bcb0991SDimitry Andric // Relink the chain. 33458bcb0991SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node,1), Tmp2); 33468bcb0991SDimitry Andric // Replace the new UINT result. 33478bcb0991SDimitry Andric ReplaceNodeWithValue(SDValue(Node, 0), Tmp1); 33488bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded STRICT_FP_TO_UINT node\n"); 33498bcb0991SDimitry Andric return true; 33508bcb0991SDimitry Andric } 33510b57cec5SDimitry Andric break; 3352e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 3353e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 3354e8d8bef9SDimitry Andric Results.push_back(TLI.expandFP_TO_INT_SAT(Node, DAG)); 3355e8d8bef9SDimitry Andric break; 33560b57cec5SDimitry Andric case ISD::VAARG: 33570b57cec5SDimitry Andric Results.push_back(DAG.expandVAArg(Node)); 33580b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 33590b57cec5SDimitry Andric break; 33600b57cec5SDimitry Andric case ISD::VACOPY: 33610b57cec5SDimitry Andric Results.push_back(DAG.expandVACopy(Node)); 33620b57cec5SDimitry Andric break; 33630b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: 33645f757f3fSDimitry Andric if (Node->getOperand(0).getValueType().getVectorElementCount().isScalar()) 33650b57cec5SDimitry Andric // This must be an access of the only element. Return it. 33660b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), 33670b57cec5SDimitry Andric Node->getOperand(0)); 33680b57cec5SDimitry Andric else 33690b57cec5SDimitry Andric Tmp1 = ExpandExtractFromVectorThroughStack(SDValue(Node, 0)); 33700b57cec5SDimitry Andric Results.push_back(Tmp1); 33710b57cec5SDimitry Andric break; 33720b57cec5SDimitry Andric case ISD::EXTRACT_SUBVECTOR: 33730b57cec5SDimitry Andric Results.push_back(ExpandExtractFromVectorThroughStack(SDValue(Node, 0))); 33740b57cec5SDimitry Andric break; 33750b57cec5SDimitry Andric case ISD::INSERT_SUBVECTOR: 33760b57cec5SDimitry Andric Results.push_back(ExpandInsertToVectorThroughStack(SDValue(Node, 0))); 33770b57cec5SDimitry Andric break; 33780b57cec5SDimitry Andric case ISD::CONCAT_VECTORS: 33790b57cec5SDimitry Andric Results.push_back(ExpandVectorBuildThroughStack(Node)); 33800b57cec5SDimitry Andric break; 33810b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: 33820b57cec5SDimitry Andric Results.push_back(ExpandSCALAR_TO_VECTOR(Node)); 33830b57cec5SDimitry Andric break; 33840b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: 33850b57cec5SDimitry Andric Results.push_back(ExpandINSERT_VECTOR_ELT(Node->getOperand(0), 33860b57cec5SDimitry Andric Node->getOperand(1), 33870b57cec5SDimitry Andric Node->getOperand(2), dl)); 33880b57cec5SDimitry Andric break; 33890b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 33900b57cec5SDimitry Andric SmallVector<int, 32> NewMask; 33910b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 33920b57cec5SDimitry Andric 33930b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 33940b57cec5SDimitry Andric EVT EltVT = VT.getVectorElementType(); 33950b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 33960b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 33970b57cec5SDimitry Andric if (!TLI.isTypeLegal(EltVT)) { 33980b57cec5SDimitry Andric EVT NewEltVT = TLI.getTypeToTransformTo(*DAG.getContext(), EltVT); 33990b57cec5SDimitry Andric 34000b57cec5SDimitry Andric // BUILD_VECTOR operands are allowed to be wider than the element type. 34010b57cec5SDimitry Andric // But if NewEltVT is smaller that EltVT the BUILD_VECTOR does not accept 34020b57cec5SDimitry Andric // it. 34030b57cec5SDimitry Andric if (NewEltVT.bitsLT(EltVT)) { 34040b57cec5SDimitry Andric // Convert shuffle node. 34050b57cec5SDimitry Andric // If original node was v4i64 and the new EltVT is i32, 34060b57cec5SDimitry Andric // cast operands to v8i32 and re-build the mask. 34070b57cec5SDimitry Andric 34080b57cec5SDimitry Andric // Calculate new VT, the size of the new VT should be equal to original. 34090b57cec5SDimitry Andric EVT NewVT = 34100b57cec5SDimitry Andric EVT::getVectorVT(*DAG.getContext(), NewEltVT, 34110b57cec5SDimitry Andric VT.getSizeInBits() / NewEltVT.getSizeInBits()); 34120b57cec5SDimitry Andric assert(NewVT.bitsEq(VT)); 34130b57cec5SDimitry Andric 34140b57cec5SDimitry Andric // cast operands to new VT 34150b57cec5SDimitry Andric Op0 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op0); 34160b57cec5SDimitry Andric Op1 = DAG.getNode(ISD::BITCAST, dl, NewVT, Op1); 34170b57cec5SDimitry Andric 34180b57cec5SDimitry Andric // Convert the shuffle mask 34190b57cec5SDimitry Andric unsigned int factor = 34200b57cec5SDimitry Andric NewVT.getVectorNumElements()/VT.getVectorNumElements(); 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric // EltVT gets smaller 34230b57cec5SDimitry Andric assert(factor > 0); 34240b57cec5SDimitry Andric 34250b57cec5SDimitry Andric for (unsigned i = 0; i < VT.getVectorNumElements(); ++i) { 34260b57cec5SDimitry Andric if (Mask[i] < 0) { 34270b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 34280b57cec5SDimitry Andric NewMask.push_back(Mask[i]); 34290b57cec5SDimitry Andric } 34300b57cec5SDimitry Andric else { 34310b57cec5SDimitry Andric for (unsigned fi = 0; fi < factor; ++fi) 34320b57cec5SDimitry Andric NewMask.push_back(Mask[i]*factor+fi); 34330b57cec5SDimitry Andric } 34340b57cec5SDimitry Andric } 34350b57cec5SDimitry Andric Mask = NewMask; 34360b57cec5SDimitry Andric VT = NewVT; 34370b57cec5SDimitry Andric } 34380b57cec5SDimitry Andric EltVT = NewEltVT; 34390b57cec5SDimitry Andric } 34400b57cec5SDimitry Andric unsigned NumElems = VT.getVectorNumElements(); 34410b57cec5SDimitry Andric SmallVector<SDValue, 16> Ops; 34420b57cec5SDimitry Andric for (unsigned i = 0; i != NumElems; ++i) { 34430b57cec5SDimitry Andric if (Mask[i] < 0) { 34440b57cec5SDimitry Andric Ops.push_back(DAG.getUNDEF(EltVT)); 34450b57cec5SDimitry Andric continue; 34460b57cec5SDimitry Andric } 34470b57cec5SDimitry Andric unsigned Idx = Mask[i]; 34480b57cec5SDimitry Andric if (Idx < NumElems) 34495ffd83dbSDimitry Andric Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op0, 34505ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx, dl))); 34510b57cec5SDimitry Andric else 34525ffd83dbSDimitry Andric Ops.push_back( 34535ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, Op1, 34545ffd83dbSDimitry Andric DAG.getVectorIdxConstant(Idx - NumElems, dl))); 34550b57cec5SDimitry Andric } 34560b57cec5SDimitry Andric 34570b57cec5SDimitry Andric Tmp1 = DAG.getBuildVector(VT, dl, Ops); 34580b57cec5SDimitry Andric // We may have changed the BUILD_VECTOR type. Cast it back to the Node type. 34590b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, Node->getValueType(0), Tmp1); 34600b57cec5SDimitry Andric Results.push_back(Tmp1); 34610b57cec5SDimitry Andric break; 34620b57cec5SDimitry Andric } 3463fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 3464fe6060f1SDimitry Andric Results.push_back(TLI.expandVectorSplice(Node, DAG)); 3465fe6060f1SDimitry Andric break; 3466fe6060f1SDimitry Andric } 34670b57cec5SDimitry Andric case ISD::EXTRACT_ELEMENT: { 34680b57cec5SDimitry Andric EVT OpTy = Node->getOperand(0).getValueType(); 3469bdd1243dSDimitry Andric if (Node->getConstantOperandVal(1)) { 34700b57cec5SDimitry Andric // 1 -> Hi 34710b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SRL, dl, OpTy, Node->getOperand(0), 34720b57cec5SDimitry Andric DAG.getConstant(OpTy.getSizeInBits() / 2, dl, 34730b57cec5SDimitry Andric TLI.getShiftAmountTy( 34740b57cec5SDimitry Andric Node->getOperand(0).getValueType(), 34750b57cec5SDimitry Andric DAG.getDataLayout()))); 34760b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), Tmp1); 34770b57cec5SDimitry Andric } else { 34780b57cec5SDimitry Andric // 0 -> Lo 34790b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, Node->getValueType(0), 34800b57cec5SDimitry Andric Node->getOperand(0)); 34810b57cec5SDimitry Andric } 34820b57cec5SDimitry Andric Results.push_back(Tmp1); 34830b57cec5SDimitry Andric break; 34840b57cec5SDimitry Andric } 34850b57cec5SDimitry Andric case ISD::STACKSAVE: 34860b57cec5SDimitry Andric // Expand to CopyFromReg if the target set 34870b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3488e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 34890b57cec5SDimitry Andric Results.push_back(DAG.getCopyFromReg(Node->getOperand(0), dl, SP, 34900b57cec5SDimitry Andric Node->getValueType(0))); 34910b57cec5SDimitry Andric Results.push_back(Results[0].getValue(1)); 34920b57cec5SDimitry Andric } else { 34930b57cec5SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 34940b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 34950b57cec5SDimitry Andric } 34960b57cec5SDimitry Andric break; 34970b57cec5SDimitry Andric case ISD::STACKRESTORE: 34980b57cec5SDimitry Andric // Expand to CopyToReg if the target set 34990b57cec5SDimitry Andric // StackPointerRegisterToSaveRestore. 3500e8d8bef9SDimitry Andric if (Register SP = TLI.getStackPointerRegisterToSaveRestore()) { 35010b57cec5SDimitry Andric Results.push_back(DAG.getCopyToReg(Node->getOperand(0), dl, SP, 35020b57cec5SDimitry Andric Node->getOperand(1))); 35030b57cec5SDimitry Andric } else { 35040b57cec5SDimitry Andric Results.push_back(Node->getOperand(0)); 35050b57cec5SDimitry Andric } 35060b57cec5SDimitry Andric break; 35070b57cec5SDimitry Andric case ISD::GET_DYNAMIC_AREA_OFFSET: 35080b57cec5SDimitry Andric Results.push_back(DAG.getConstant(0, dl, Node->getValueType(0))); 35090b57cec5SDimitry Andric Results.push_back(Results[0].getValue(0)); 35100b57cec5SDimitry Andric break; 35110b57cec5SDimitry Andric case ISD::FCOPYSIGN: 35120b57cec5SDimitry Andric Results.push_back(ExpandFCOPYSIGN(Node)); 35130b57cec5SDimitry Andric break; 35140b57cec5SDimitry Andric case ISD::FNEG: 3515e8d8bef9SDimitry Andric Results.push_back(ExpandFNEG(Node)); 35160b57cec5SDimitry Andric break; 35170b57cec5SDimitry Andric case ISD::FABS: 35180b57cec5SDimitry Andric Results.push_back(ExpandFABS(Node)); 35190b57cec5SDimitry Andric break; 352081ad6265SDimitry Andric case ISD::IS_FPCLASS: { 352181ad6265SDimitry Andric auto CNode = cast<ConstantSDNode>(Node->getOperand(1)); 352281ad6265SDimitry Andric auto Test = static_cast<FPClassTest>(CNode->getZExtValue()); 352381ad6265SDimitry Andric if (SDValue Expanded = 352481ad6265SDimitry Andric TLI.expandIS_FPCLASS(Node->getValueType(0), Node->getOperand(0), 352581ad6265SDimitry Andric Test, Node->getFlags(), SDLoc(Node), DAG)) 352681ad6265SDimitry Andric Results.push_back(Expanded); 352781ad6265SDimitry Andric break; 352881ad6265SDimitry Andric } 35290b57cec5SDimitry Andric case ISD::SMIN: 35300b57cec5SDimitry Andric case ISD::SMAX: 35310b57cec5SDimitry Andric case ISD::UMIN: 35320b57cec5SDimitry Andric case ISD::UMAX: { 35330b57cec5SDimitry Andric // Expand Y = MAX(A, B) -> Y = (A > B) ? A : B 35340b57cec5SDimitry Andric ISD::CondCode Pred; 35350b57cec5SDimitry Andric switch (Node->getOpcode()) { 35360b57cec5SDimitry Andric default: llvm_unreachable("How did we get here?"); 35370b57cec5SDimitry Andric case ISD::SMAX: Pred = ISD::SETGT; break; 35380b57cec5SDimitry Andric case ISD::SMIN: Pred = ISD::SETLT; break; 35390b57cec5SDimitry Andric case ISD::UMAX: Pred = ISD::SETUGT; break; 35400b57cec5SDimitry Andric case ISD::UMIN: Pred = ISD::SETULT; break; 35410b57cec5SDimitry Andric } 35420b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 35430b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 35440b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp1, Tmp2, Pred); 35450b57cec5SDimitry Andric Results.push_back(Tmp1); 35460b57cec5SDimitry Andric break; 35470b57cec5SDimitry Andric } 35480b57cec5SDimitry Andric case ISD::FMINNUM: 35490b57cec5SDimitry Andric case ISD::FMAXNUM: { 35500b57cec5SDimitry Andric if (SDValue Expanded = TLI.expandFMINNUM_FMAXNUM(Node, DAG)) 35510b57cec5SDimitry Andric Results.push_back(Expanded); 35520b57cec5SDimitry Andric break; 35530b57cec5SDimitry Andric } 35540b57cec5SDimitry Andric case ISD::FSIN: 35550b57cec5SDimitry Andric case ISD::FCOS: { 35560b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 35570b57cec5SDimitry Andric // Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin / 35580b57cec5SDimitry Andric // fcos which share the same operand and both are used. 35590b57cec5SDimitry Andric if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) || 35600b57cec5SDimitry Andric isSinCosLibcallAvailable(Node, TLI)) 35610b57cec5SDimitry Andric && useSinCos(Node)) { 35620b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 35630b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0)); 35640b57cec5SDimitry Andric if (Node->getOpcode() == ISD::FCOS) 35650b57cec5SDimitry Andric Tmp1 = Tmp1.getValue(1); 35660b57cec5SDimitry Andric Results.push_back(Tmp1); 35670b57cec5SDimitry Andric } 35680b57cec5SDimitry Andric break; 35690b57cec5SDimitry Andric } 357006c3fb27SDimitry Andric case ISD::FLDEXP: 357106c3fb27SDimitry Andric case ISD::STRICT_FLDEXP: { 357206c3fb27SDimitry Andric EVT VT = Node->getValueType(0); 357306c3fb27SDimitry Andric RTLIB::Libcall LC = RTLIB::getLDEXP(VT); 357406c3fb27SDimitry Andric // Use the LibCall instead, it is very likely faster 357506c3fb27SDimitry Andric // FIXME: Use separate LibCall action. 357606c3fb27SDimitry Andric if (TLI.getLibcallName(LC)) 357706c3fb27SDimitry Andric break; 357806c3fb27SDimitry Andric 357906c3fb27SDimitry Andric if (SDValue Expanded = expandLdexp(Node)) { 358006c3fb27SDimitry Andric Results.push_back(Expanded); 358106c3fb27SDimitry Andric if (Node->getOpcode() == ISD::STRICT_FLDEXP) 358206c3fb27SDimitry Andric Results.push_back(Expanded.getValue(1)); 358306c3fb27SDimitry Andric } 358406c3fb27SDimitry Andric 358506c3fb27SDimitry Andric break; 358606c3fb27SDimitry Andric } 358706c3fb27SDimitry Andric case ISD::FFREXP: { 358806c3fb27SDimitry Andric RTLIB::Libcall LC = RTLIB::getFREXP(Node->getValueType(0)); 358906c3fb27SDimitry Andric // Use the LibCall instead, it is very likely faster 359006c3fb27SDimitry Andric // FIXME: Use separate LibCall action. 359106c3fb27SDimitry Andric if (TLI.getLibcallName(LC)) 359206c3fb27SDimitry Andric break; 359306c3fb27SDimitry Andric 359406c3fb27SDimitry Andric if (SDValue Expanded = expandFrexp(Node)) { 359506c3fb27SDimitry Andric Results.push_back(Expanded); 359606c3fb27SDimitry Andric Results.push_back(Expanded.getValue(1)); 359706c3fb27SDimitry Andric } 359806c3fb27SDimitry Andric break; 359906c3fb27SDimitry Andric } 36000b57cec5SDimitry Andric case ISD::FMAD: 36010b57cec5SDimitry Andric llvm_unreachable("Illegal fmad should never be formed"); 36020b57cec5SDimitry Andric 36030b57cec5SDimitry Andric case ISD::FP16_TO_FP: 36040b57cec5SDimitry Andric if (Node->getValueType(0) != MVT::f32) { 36050b57cec5SDimitry Andric // We can extend to types bigger than f32 in two steps without changing 36060b57cec5SDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 36070b57cec5SDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 36080b57cec5SDimitry Andric SDValue Res = 36090b57cec5SDimitry Andric DAG.getNode(ISD::FP16_TO_FP, dl, MVT::f32, Node->getOperand(0)); 36100b57cec5SDimitry Andric Results.push_back( 36110b57cec5SDimitry Andric DAG.getNode(ISD::FP_EXTEND, dl, Node->getValueType(0), Res)); 36120b57cec5SDimitry Andric } 36130b57cec5SDimitry Andric break; 36145ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: 36155ffd83dbSDimitry Andric if (Node->getValueType(0) != MVT::f32) { 36165ffd83dbSDimitry Andric // We can extend to types bigger than f32 in two steps without changing 36175ffd83dbSDimitry Andric // the result. Since "f16 -> f32" is much more commonly available, give 36185ffd83dbSDimitry Andric // CodeGen the option of emitting that before resorting to a libcall. 36195ffd83dbSDimitry Andric SDValue Res = 36205ffd83dbSDimitry Andric DAG.getNode(ISD::STRICT_FP16_TO_FP, dl, {MVT::f32, MVT::Other}, 36215ffd83dbSDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 36225ffd83dbSDimitry Andric Res = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, 36235ffd83dbSDimitry Andric {Node->getValueType(0), MVT::Other}, 36245ffd83dbSDimitry Andric {Res.getValue(1), Res}); 36255ffd83dbSDimitry Andric Results.push_back(Res); 36265ffd83dbSDimitry Andric Results.push_back(Res.getValue(1)); 36275ffd83dbSDimitry Andric } 36285ffd83dbSDimitry Andric break; 36290b57cec5SDimitry Andric case ISD::FP_TO_FP16: 36300b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Legalizing FP_TO_FP16\n"); 36310b57cec5SDimitry Andric if (!TLI.useSoftFloat() && TM.Options.UnsafeFPMath) { 36320b57cec5SDimitry Andric SDValue Op = Node->getOperand(0); 36330b57cec5SDimitry Andric MVT SVT = Op.getSimpleValueType(); 36340b57cec5SDimitry Andric if ((SVT == MVT::f64 || SVT == MVT::f80) && 36350b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FP_TO_FP16, MVT::f32)) { 36360b57cec5SDimitry Andric // Under fastmath, we can expand this node into a fround followed by 36370b57cec5SDimitry Andric // a float-half conversion. 3638bdd1243dSDimitry Andric SDValue FloatVal = 3639bdd1243dSDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, MVT::f32, Op, 3640bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)); 36410b57cec5SDimitry Andric Results.push_back( 36420b57cec5SDimitry Andric DAG.getNode(ISD::FP_TO_FP16, dl, Node->getValueType(0), FloatVal)); 36430b57cec5SDimitry Andric } 36440b57cec5SDimitry Andric } 36450b57cec5SDimitry Andric break; 36460b57cec5SDimitry Andric case ISD::ConstantFP: { 36470b57cec5SDimitry Andric ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node); 36480b57cec5SDimitry Andric // Check to see if this FP immediate is already legal. 36490b57cec5SDimitry Andric // If this is a legal constant, turn it into a TargetConstantFP node. 36500b57cec5SDimitry Andric if (!TLI.isFPImmLegal(CFP->getValueAPF(), Node->getValueType(0), 3651e8d8bef9SDimitry Andric DAG.shouldOptForSize())) 36520b57cec5SDimitry Andric Results.push_back(ExpandConstantFP(CFP, true)); 36530b57cec5SDimitry Andric break; 36540b57cec5SDimitry Andric } 36550b57cec5SDimitry Andric case ISD::Constant: { 36560b57cec5SDimitry Andric ConstantSDNode *CP = cast<ConstantSDNode>(Node); 36570b57cec5SDimitry Andric Results.push_back(ExpandConstant(CP)); 36580b57cec5SDimitry Andric break; 36590b57cec5SDimitry Andric } 36600b57cec5SDimitry Andric case ISD::FSUB: { 36610b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 36620b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::FADD, VT) && 36630b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::FNEG, VT)) { 36640b57cec5SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 36650b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(1)); 36660b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FADD, dl, VT, Node->getOperand(0), Tmp1, Flags); 36670b57cec5SDimitry Andric Results.push_back(Tmp1); 36680b57cec5SDimitry Andric } 36690b57cec5SDimitry Andric break; 36700b57cec5SDimitry Andric } 36710b57cec5SDimitry Andric case ISD::SUB: { 36720b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 36730b57cec5SDimitry Andric assert(TLI.isOperationLegalOrCustom(ISD::ADD, VT) && 36740b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::XOR, VT) && 36750b57cec5SDimitry Andric "Don't know how to expand this subtraction!"); 3676349cc55cSDimitry Andric Tmp1 = DAG.getNOT(dl, Node->getOperand(1), VT); 36770b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ADD, dl, VT, Tmp1, DAG.getConstant(1, dl, VT)); 36780b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::ADD, dl, VT, Node->getOperand(0), Tmp1)); 36790b57cec5SDimitry Andric break; 36800b57cec5SDimitry Andric } 36810b57cec5SDimitry Andric case ISD::UREM: 36825ffd83dbSDimitry Andric case ISD::SREM: 36835ffd83dbSDimitry Andric if (TLI.expandREM(Node, Tmp1, DAG)) 36840b57cec5SDimitry Andric Results.push_back(Tmp1); 36850b57cec5SDimitry Andric break; 36860b57cec5SDimitry Andric case ISD::UDIV: 36870b57cec5SDimitry Andric case ISD::SDIV: { 36880b57cec5SDimitry Andric bool isSigned = Node->getOpcode() == ISD::SDIV; 36890b57cec5SDimitry Andric unsigned DivRemOpc = isSigned ? ISD::SDIVREM : ISD::UDIVREM; 36900b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 36910b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(DivRemOpc, VT)) { 36920b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 36930b57cec5SDimitry Andric Tmp1 = DAG.getNode(DivRemOpc, dl, VTs, Node->getOperand(0), 36940b57cec5SDimitry Andric Node->getOperand(1)); 36950b57cec5SDimitry Andric Results.push_back(Tmp1); 36960b57cec5SDimitry Andric } 36970b57cec5SDimitry Andric break; 36980b57cec5SDimitry Andric } 36990b57cec5SDimitry Andric case ISD::MULHU: 37000b57cec5SDimitry Andric case ISD::MULHS: { 37010b57cec5SDimitry Andric unsigned ExpandOpcode = 37020b57cec5SDimitry Andric Node->getOpcode() == ISD::MULHU ? ISD::UMUL_LOHI : ISD::SMUL_LOHI; 37030b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 37040b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 37050b57cec5SDimitry Andric 37060b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExpandOpcode, dl, VTs, Node->getOperand(0), 37070b57cec5SDimitry Andric Node->getOperand(1)); 37080b57cec5SDimitry Andric Results.push_back(Tmp1.getValue(1)); 37090b57cec5SDimitry Andric break; 37100b57cec5SDimitry Andric } 37110b57cec5SDimitry Andric case ISD::UMUL_LOHI: 37120b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 37130b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 37140b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 37150b57cec5SDimitry Andric MVT VT = LHS.getSimpleValueType(); 37160b57cec5SDimitry Andric unsigned MULHOpcode = 37170b57cec5SDimitry Andric Node->getOpcode() == ISD::UMUL_LOHI ? ISD::MULHU : ISD::MULHS; 37180b57cec5SDimitry Andric 37190b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(MULHOpcode, VT)) { 37200b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::MUL, dl, VT, LHS, RHS)); 37210b57cec5SDimitry Andric Results.push_back(DAG.getNode(MULHOpcode, dl, VT, LHS, RHS)); 37220b57cec5SDimitry Andric break; 37230b57cec5SDimitry Andric } 37240b57cec5SDimitry Andric 37250b57cec5SDimitry Andric SmallVector<SDValue, 4> Halves; 37260b57cec5SDimitry Andric EVT HalfType = EVT(VT).getHalfSizedIntegerVT(*DAG.getContext()); 37270b57cec5SDimitry Andric assert(TLI.isTypeLegal(HalfType)); 3728e8d8bef9SDimitry Andric if (TLI.expandMUL_LOHI(Node->getOpcode(), VT, dl, LHS, RHS, Halves, 37290b57cec5SDimitry Andric HalfType, DAG, 37300b57cec5SDimitry Andric TargetLowering::MulExpansionKind::Always)) { 37310b57cec5SDimitry Andric for (unsigned i = 0; i < 2; ++i) { 37320b57cec5SDimitry Andric SDValue Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Halves[2 * i]); 37330b57cec5SDimitry Andric SDValue Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Halves[2 * i + 1]); 37340b57cec5SDimitry Andric SDValue Shift = DAG.getConstant( 37350b57cec5SDimitry Andric HalfType.getScalarSizeInBits(), dl, 37360b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 37370b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 37380b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 37390b57cec5SDimitry Andric } 37400b57cec5SDimitry Andric break; 37410b57cec5SDimitry Andric } 37420b57cec5SDimitry Andric break; 37430b57cec5SDimitry Andric } 37440b57cec5SDimitry Andric case ISD::MUL: { 37450b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 37460b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(VT, VT); 37470b57cec5SDimitry Andric // See if multiply or divide can be lowered using two-result operations. 37480b57cec5SDimitry Andric // We just need the low half of the multiply; try both the signed 37490b57cec5SDimitry Andric // and unsigned forms. If the target supports both SMUL_LOHI and 37500b57cec5SDimitry Andric // UMUL_LOHI, form a preference by checking which forms of plain 37510b57cec5SDimitry Andric // MULH it supports. 37520b57cec5SDimitry Andric bool HasSMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::SMUL_LOHI, VT); 37530b57cec5SDimitry Andric bool HasUMUL_LOHI = TLI.isOperationLegalOrCustom(ISD::UMUL_LOHI, VT); 37540b57cec5SDimitry Andric bool HasMULHS = TLI.isOperationLegalOrCustom(ISD::MULHS, VT); 37550b57cec5SDimitry Andric bool HasMULHU = TLI.isOperationLegalOrCustom(ISD::MULHU, VT); 37560b57cec5SDimitry Andric unsigned OpToUse = 0; 37570b57cec5SDimitry Andric if (HasSMUL_LOHI && !HasMULHS) { 37580b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 37590b57cec5SDimitry Andric } else if (HasUMUL_LOHI && !HasMULHU) { 37600b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 37610b57cec5SDimitry Andric } else if (HasSMUL_LOHI) { 37620b57cec5SDimitry Andric OpToUse = ISD::SMUL_LOHI; 37630b57cec5SDimitry Andric } else if (HasUMUL_LOHI) { 37640b57cec5SDimitry Andric OpToUse = ISD::UMUL_LOHI; 37650b57cec5SDimitry Andric } 37660b57cec5SDimitry Andric if (OpToUse) { 37670b57cec5SDimitry Andric Results.push_back(DAG.getNode(OpToUse, dl, VTs, Node->getOperand(0), 37680b57cec5SDimitry Andric Node->getOperand(1))); 37690b57cec5SDimitry Andric break; 37700b57cec5SDimitry Andric } 37710b57cec5SDimitry Andric 37720b57cec5SDimitry Andric SDValue Lo, Hi; 37730b57cec5SDimitry Andric EVT HalfType = VT.getHalfSizedIntegerVT(*DAG.getContext()); 37740b57cec5SDimitry Andric if (TLI.isOperationLegalOrCustom(ISD::ZERO_EXTEND, VT) && 37750b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::ANY_EXTEND, VT) && 37760b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::SHL, VT) && 37770b57cec5SDimitry Andric TLI.isOperationLegalOrCustom(ISD::OR, VT) && 37780b57cec5SDimitry Andric TLI.expandMUL(Node, Lo, Hi, HalfType, DAG, 37790b57cec5SDimitry Andric TargetLowering::MulExpansionKind::OnlyLegalOrCustom)) { 37800b57cec5SDimitry Andric Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, VT, Lo); 37810b57cec5SDimitry Andric Hi = DAG.getNode(ISD::ANY_EXTEND, dl, VT, Hi); 37820b57cec5SDimitry Andric SDValue Shift = 37830b57cec5SDimitry Andric DAG.getConstant(HalfType.getSizeInBits(), dl, 37840b57cec5SDimitry Andric TLI.getShiftAmountTy(HalfType, DAG.getDataLayout())); 37850b57cec5SDimitry Andric Hi = DAG.getNode(ISD::SHL, dl, VT, Hi, Shift); 37860b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, VT, Lo, Hi)); 37870b57cec5SDimitry Andric } 37880b57cec5SDimitry Andric break; 37890b57cec5SDimitry Andric } 37900b57cec5SDimitry Andric case ISD::FSHL: 37910b57cec5SDimitry Andric case ISD::FSHR: 37920eae32dcSDimitry Andric if (SDValue Expanded = TLI.expandFunnelShift(Node, DAG)) 37930eae32dcSDimitry Andric Results.push_back(Expanded); 37940b57cec5SDimitry Andric break; 37950b57cec5SDimitry Andric case ISD::ROTL: 37960b57cec5SDimitry Andric case ISD::ROTR: 37970eae32dcSDimitry Andric if (SDValue Expanded = TLI.expandROT(Node, true /*AllowVectorOps*/, DAG)) 37980eae32dcSDimitry Andric Results.push_back(Expanded); 37990b57cec5SDimitry Andric break; 38000b57cec5SDimitry Andric case ISD::SADDSAT: 38010b57cec5SDimitry Andric case ISD::UADDSAT: 38020b57cec5SDimitry Andric case ISD::SSUBSAT: 38030b57cec5SDimitry Andric case ISD::USUBSAT: 38040b57cec5SDimitry Andric Results.push_back(TLI.expandAddSubSat(Node, DAG)); 38050b57cec5SDimitry Andric break; 3806e8d8bef9SDimitry Andric case ISD::SSHLSAT: 3807e8d8bef9SDimitry Andric case ISD::USHLSAT: 3808e8d8bef9SDimitry Andric Results.push_back(TLI.expandShlSat(Node, DAG)); 3809e8d8bef9SDimitry Andric break; 38100b57cec5SDimitry Andric case ISD::SMULFIX: 38110b57cec5SDimitry Andric case ISD::SMULFIXSAT: 38120b57cec5SDimitry Andric case ISD::UMULFIX: 38138bcb0991SDimitry Andric case ISD::UMULFIXSAT: 38140b57cec5SDimitry Andric Results.push_back(TLI.expandFixedPointMul(Node, DAG)); 38150b57cec5SDimitry Andric break; 3816480093f4SDimitry Andric case ISD::SDIVFIX: 38175ffd83dbSDimitry Andric case ISD::SDIVFIXSAT: 3818480093f4SDimitry Andric case ISD::UDIVFIX: 38195ffd83dbSDimitry Andric case ISD::UDIVFIXSAT: 3820480093f4SDimitry Andric if (SDValue V = TLI.expandFixedPointDiv(Node->getOpcode(), SDLoc(Node), 3821480093f4SDimitry Andric Node->getOperand(0), 3822480093f4SDimitry Andric Node->getOperand(1), 3823480093f4SDimitry Andric Node->getConstantOperandVal(2), 3824480093f4SDimitry Andric DAG)) { 3825480093f4SDimitry Andric Results.push_back(V); 3826480093f4SDimitry Andric break; 3827480093f4SDimitry Andric } 3828480093f4SDimitry Andric // FIXME: We might want to retry here with a wider type if we fail, if that 3829480093f4SDimitry Andric // type is legal. 3830480093f4SDimitry Andric // FIXME: Technically, so long as we only have sdivfixes where BW+Scale is 3831480093f4SDimitry Andric // <= 128 (which is the case for all of the default Embedded-C types), 3832480093f4SDimitry Andric // we will only get here with types and scales that we could always expand 3833480093f4SDimitry Andric // if we were allowed to generate libcalls to division functions of illegal 3834480093f4SDimitry Andric // type. But we cannot do that. 3835480093f4SDimitry Andric llvm_unreachable("Cannot expand DIVFIX!"); 383606c3fb27SDimitry Andric case ISD::UADDO_CARRY: 383706c3fb27SDimitry Andric case ISD::USUBO_CARRY: { 38380b57cec5SDimitry Andric SDValue LHS = Node->getOperand(0); 38390b57cec5SDimitry Andric SDValue RHS = Node->getOperand(1); 38400b57cec5SDimitry Andric SDValue Carry = Node->getOperand(2); 38410b57cec5SDimitry Andric 384206c3fb27SDimitry Andric bool IsAdd = Node->getOpcode() == ISD::UADDO_CARRY; 38430b57cec5SDimitry Andric 38440b57cec5SDimitry Andric // Initial add of the 2 operands. 38450b57cec5SDimitry Andric unsigned Op = IsAdd ? ISD::ADD : ISD::SUB; 38460b57cec5SDimitry Andric EVT VT = LHS.getValueType(); 38470b57cec5SDimitry Andric SDValue Sum = DAG.getNode(Op, dl, VT, LHS, RHS); 38480b57cec5SDimitry Andric 38490b57cec5SDimitry Andric // Initial check for overflow. 38500b57cec5SDimitry Andric EVT CarryType = Node->getValueType(1); 38510b57cec5SDimitry Andric EVT SetCCType = getSetCCResultType(Node->getValueType(0)); 38520b57cec5SDimitry Andric ISD::CondCode CC = IsAdd ? ISD::SETULT : ISD::SETUGT; 38530b57cec5SDimitry Andric SDValue Overflow = DAG.getSetCC(dl, SetCCType, Sum, LHS, CC); 38540b57cec5SDimitry Andric 38550b57cec5SDimitry Andric // Add of the sum and the carry. 38565ffd83dbSDimitry Andric SDValue One = DAG.getConstant(1, dl, VT); 38570b57cec5SDimitry Andric SDValue CarryExt = 38585ffd83dbSDimitry Andric DAG.getNode(ISD::AND, dl, VT, DAG.getZExtOrTrunc(Carry, dl, VT), One); 38590b57cec5SDimitry Andric SDValue Sum2 = DAG.getNode(Op, dl, VT, Sum, CarryExt); 38600b57cec5SDimitry Andric 38610b57cec5SDimitry Andric // Second check for overflow. If we are adding, we can only overflow if the 38620b57cec5SDimitry Andric // initial sum is all 1s ang the carry is set, resulting in a new sum of 0. 38630b57cec5SDimitry Andric // If we are subtracting, we can only overflow if the initial sum is 0 and 38640b57cec5SDimitry Andric // the carry is set, resulting in a new sum of all 1s. 38650b57cec5SDimitry Andric SDValue Zero = DAG.getConstant(0, dl, VT); 38660b57cec5SDimitry Andric SDValue Overflow2 = 38670b57cec5SDimitry Andric IsAdd ? DAG.getSetCC(dl, SetCCType, Sum2, Zero, ISD::SETEQ) 38680b57cec5SDimitry Andric : DAG.getSetCC(dl, SetCCType, Sum, Zero, ISD::SETEQ); 38690b57cec5SDimitry Andric Overflow2 = DAG.getNode(ISD::AND, dl, SetCCType, Overflow2, 38700b57cec5SDimitry Andric DAG.getZExtOrTrunc(Carry, dl, SetCCType)); 38710b57cec5SDimitry Andric 38720b57cec5SDimitry Andric SDValue ResultCarry = 38730b57cec5SDimitry Andric DAG.getNode(ISD::OR, dl, SetCCType, Overflow, Overflow2); 38740b57cec5SDimitry Andric 38750b57cec5SDimitry Andric Results.push_back(Sum2); 38760b57cec5SDimitry Andric Results.push_back(DAG.getBoolExtOrTrunc(ResultCarry, dl, CarryType, VT)); 38770b57cec5SDimitry Andric break; 38780b57cec5SDimitry Andric } 38790b57cec5SDimitry Andric case ISD::SADDO: 38800b57cec5SDimitry Andric case ISD::SSUBO: { 38810b57cec5SDimitry Andric SDValue Result, Overflow; 38820b57cec5SDimitry Andric TLI.expandSADDSUBO(Node, Result, Overflow, DAG); 38830b57cec5SDimitry Andric Results.push_back(Result); 38840b57cec5SDimitry Andric Results.push_back(Overflow); 38850b57cec5SDimitry Andric break; 38860b57cec5SDimitry Andric } 38870b57cec5SDimitry Andric case ISD::UADDO: 38880b57cec5SDimitry Andric case ISD::USUBO: { 38890b57cec5SDimitry Andric SDValue Result, Overflow; 38900b57cec5SDimitry Andric TLI.expandUADDSUBO(Node, Result, Overflow, DAG); 38910b57cec5SDimitry Andric Results.push_back(Result); 38920b57cec5SDimitry Andric Results.push_back(Overflow); 38930b57cec5SDimitry Andric break; 38940b57cec5SDimitry Andric } 38950b57cec5SDimitry Andric case ISD::UMULO: 38960b57cec5SDimitry Andric case ISD::SMULO: { 38970b57cec5SDimitry Andric SDValue Result, Overflow; 38980b57cec5SDimitry Andric if (TLI.expandMULO(Node, Result, Overflow, DAG)) { 38990b57cec5SDimitry Andric Results.push_back(Result); 39000b57cec5SDimitry Andric Results.push_back(Overflow); 39010b57cec5SDimitry Andric } 39020b57cec5SDimitry Andric break; 39030b57cec5SDimitry Andric } 39040b57cec5SDimitry Andric case ISD::BUILD_PAIR: { 39050b57cec5SDimitry Andric EVT PairTy = Node->getValueType(0); 39060b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, PairTy, Node->getOperand(0)); 39070b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, PairTy, Node->getOperand(1)); 39080b57cec5SDimitry Andric Tmp2 = DAG.getNode( 39090b57cec5SDimitry Andric ISD::SHL, dl, PairTy, Tmp2, 39100b57cec5SDimitry Andric DAG.getConstant(PairTy.getSizeInBits() / 2, dl, 39110b57cec5SDimitry Andric TLI.getShiftAmountTy(PairTy, DAG.getDataLayout()))); 39120b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::OR, dl, PairTy, Tmp1, Tmp2)); 39130b57cec5SDimitry Andric break; 39140b57cec5SDimitry Andric } 39150b57cec5SDimitry Andric case ISD::SELECT: 39160b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 39170b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 39180b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); 39190b57cec5SDimitry Andric if (Tmp1.getOpcode() == ISD::SETCC) { 39200b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1.getOperand(0), Tmp1.getOperand(1), 39210b57cec5SDimitry Andric Tmp2, Tmp3, 39220b57cec5SDimitry Andric cast<CondCodeSDNode>(Tmp1.getOperand(2))->get()); 39230b57cec5SDimitry Andric } else { 39240b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, 39250b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp1.getValueType()), 39260b57cec5SDimitry Andric Tmp2, Tmp3, ISD::SETNE); 39270b57cec5SDimitry Andric } 39280b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 39290b57cec5SDimitry Andric Results.push_back(Tmp1); 39300b57cec5SDimitry Andric break; 39310b57cec5SDimitry Andric case ISD::BR_JT: { 39320b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); 39330b57cec5SDimitry Andric SDValue Table = Node->getOperand(1); 39340b57cec5SDimitry Andric SDValue Index = Node->getOperand(2); 39355f757f3fSDimitry Andric int JTI = cast<JumpTableSDNode>(Table.getNode())->getIndex(); 39360b57cec5SDimitry Andric 39370b57cec5SDimitry Andric const DataLayout &TD = DAG.getDataLayout(); 39380b57cec5SDimitry Andric EVT PTy = TLI.getPointerTy(TD); 39390b57cec5SDimitry Andric 39400b57cec5SDimitry Andric unsigned EntrySize = 39410b57cec5SDimitry Andric DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(TD); 39420b57cec5SDimitry Andric 39430b57cec5SDimitry Andric // For power-of-two jumptable entry sizes convert multiplication to a shift. 39440b57cec5SDimitry Andric // This transformation needs to be done here since otherwise the MIPS 39450b57cec5SDimitry Andric // backend will end up emitting a three instruction multiply sequence 39460b57cec5SDimitry Andric // instead of a single shift and MSP430 will call a runtime function. 39470b57cec5SDimitry Andric if (llvm::isPowerOf2_32(EntrySize)) 39480b57cec5SDimitry Andric Index = DAG.getNode( 39490b57cec5SDimitry Andric ISD::SHL, dl, Index.getValueType(), Index, 39500b57cec5SDimitry Andric DAG.getConstant(llvm::Log2_32(EntrySize), dl, Index.getValueType())); 39510b57cec5SDimitry Andric else 39520b57cec5SDimitry Andric Index = DAG.getNode(ISD::MUL, dl, Index.getValueType(), Index, 39530b57cec5SDimitry Andric DAG.getConstant(EntrySize, dl, Index.getValueType())); 39540b57cec5SDimitry Andric SDValue Addr = DAG.getNode(ISD::ADD, dl, Index.getValueType(), 39550b57cec5SDimitry Andric Index, Table); 39560b57cec5SDimitry Andric 39570b57cec5SDimitry Andric EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8); 39580b57cec5SDimitry Andric SDValue LD = DAG.getExtLoad( 39590b57cec5SDimitry Andric ISD::SEXTLOAD, dl, PTy, Chain, Addr, 39600b57cec5SDimitry Andric MachinePointerInfo::getJumpTable(DAG.getMachineFunction()), MemVT); 39610b57cec5SDimitry Andric Addr = LD; 39620b57cec5SDimitry Andric if (TLI.isJumpTableRelative()) { 39630b57cec5SDimitry Andric // For PIC, the sequence is: 39640b57cec5SDimitry Andric // BRIND(load(Jumptable + index) + RelocBase) 39650b57cec5SDimitry Andric // RelocBase can be JumpTable, GOT or some sort of global base. 39660b57cec5SDimitry Andric Addr = DAG.getNode(ISD::ADD, dl, PTy, Addr, 39670b57cec5SDimitry Andric TLI.getPICJumpTableRelocBase(Table, DAG)); 39680b57cec5SDimitry Andric } 39690b57cec5SDimitry Andric 39705f757f3fSDimitry Andric Tmp1 = TLI.expandIndirectJTBranch(dl, LD.getValue(1), Addr, JTI, DAG); 39710b57cec5SDimitry Andric Results.push_back(Tmp1); 39720b57cec5SDimitry Andric break; 39730b57cec5SDimitry Andric } 39740b57cec5SDimitry Andric case ISD::BRCOND: 39750b57cec5SDimitry Andric // Expand brcond's setcc into its constituent parts and create a BR_CC 39760b57cec5SDimitry Andric // Node. 39770b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 39780b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 39794824e7fdSDimitry Andric if (Tmp2.getOpcode() == ISD::SETCC && 39804824e7fdSDimitry Andric TLI.isOperationLegalOrCustom(ISD::BR_CC, 39814824e7fdSDimitry Andric Tmp2.getOperand(0).getValueType())) { 39824824e7fdSDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, Tmp2.getOperand(2), 39830b57cec5SDimitry Andric Tmp2.getOperand(0), Tmp2.getOperand(1), 39840b57cec5SDimitry Andric Node->getOperand(2)); 39850b57cec5SDimitry Andric } else { 39860b57cec5SDimitry Andric // We test only the i1 bit. Skip the AND if UNDEF or another AND. 39870b57cec5SDimitry Andric if (Tmp2.isUndef() || 398806c3fb27SDimitry Andric (Tmp2.getOpcode() == ISD::AND && isOneConstant(Tmp2.getOperand(1)))) 39890b57cec5SDimitry Andric Tmp3 = Tmp2; 39900b57cec5SDimitry Andric else 39910b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::AND, dl, Tmp2.getValueType(), Tmp2, 39920b57cec5SDimitry Andric DAG.getConstant(1, dl, Tmp2.getValueType())); 39930b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, MVT::Other, Tmp1, 39940b57cec5SDimitry Andric DAG.getCondCode(ISD::SETNE), Tmp3, 39950b57cec5SDimitry Andric DAG.getConstant(0, dl, Tmp3.getValueType()), 39960b57cec5SDimitry Andric Node->getOperand(2)); 39970b57cec5SDimitry Andric } 39980b57cec5SDimitry Andric Results.push_back(Tmp1); 39990b57cec5SDimitry Andric break; 4000480093f4SDimitry Andric case ISD::SETCC: 400181ad6265SDimitry Andric case ISD::VP_SETCC: 4002480093f4SDimitry Andric case ISD::STRICT_FSETCC: 4003480093f4SDimitry Andric case ISD::STRICT_FSETCCS: { 400481ad6265SDimitry Andric bool IsVP = Node->getOpcode() == ISD::VP_SETCC; 400581ad6265SDimitry Andric bool IsStrict = Node->getOpcode() == ISD::STRICT_FSETCC || 400681ad6265SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS; 4007480093f4SDimitry Andric bool IsSignaling = Node->getOpcode() == ISD::STRICT_FSETCCS; 4008480093f4SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4009480093f4SDimitry Andric unsigned Offset = IsStrict ? 1 : 0; 4010480093f4SDimitry Andric Tmp1 = Node->getOperand(0 + Offset); 4011480093f4SDimitry Andric Tmp2 = Node->getOperand(1 + Offset); 4012480093f4SDimitry Andric Tmp3 = Node->getOperand(2 + Offset); 401381ad6265SDimitry Andric SDValue Mask, EVL; 401481ad6265SDimitry Andric if (IsVP) { 401581ad6265SDimitry Andric Mask = Node->getOperand(3 + Offset); 401681ad6265SDimitry Andric EVL = Node->getOperand(4 + Offset); 401781ad6265SDimitry Andric } 401881ad6265SDimitry Andric bool Legalized = TLI.LegalizeSetCCCondCode( 401981ad6265SDimitry Andric DAG, Node->getValueType(0), Tmp1, Tmp2, Tmp3, Mask, EVL, NeedInvert, dl, 402081ad6265SDimitry Andric Chain, IsSignaling); 40210b57cec5SDimitry Andric 40220b57cec5SDimitry Andric if (Legalized) { 40230b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 40240b57cec5SDimitry Andric // condition code, create a new SETCC node. 402504eeddc0SDimitry Andric if (Tmp3.getNode()) { 402604eeddc0SDimitry Andric if (IsStrict) { 402704eeddc0SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getVTList(), 402804eeddc0SDimitry Andric {Chain, Tmp1, Tmp2, Tmp3}, Node->getFlags()); 402904eeddc0SDimitry Andric Chain = Tmp1.getValue(1); 403081ad6265SDimitry Andric } else if (IsVP) { 403181ad6265SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), 403281ad6265SDimitry Andric {Tmp1, Tmp2, Tmp3, Mask, EVL}, Node->getFlags()); 403304eeddc0SDimitry Andric } else { 403404eeddc0SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, Node->getValueType(0), Tmp1, 403504eeddc0SDimitry Andric Tmp2, Tmp3, Node->getFlags()); 403604eeddc0SDimitry Andric } 403704eeddc0SDimitry Andric } 40380b57cec5SDimitry Andric 40390b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then wrap 40400b57cec5SDimitry Andric // the existing SETCC in a NOT to restore the intended condition. 404181ad6265SDimitry Andric if (NeedInvert) { 404281ad6265SDimitry Andric if (!IsVP) 40430b57cec5SDimitry Andric Tmp1 = DAG.getLogicalNOT(dl, Tmp1, Tmp1->getValueType(0)); 404481ad6265SDimitry Andric else 404581ad6265SDimitry Andric Tmp1 = 404681ad6265SDimitry Andric DAG.getVPLogicalNOT(dl, Tmp1, Mask, EVL, Tmp1->getValueType(0)); 404781ad6265SDimitry Andric } 40480b57cec5SDimitry Andric 40490b57cec5SDimitry Andric Results.push_back(Tmp1); 4050480093f4SDimitry Andric if (IsStrict) 4051480093f4SDimitry Andric Results.push_back(Chain); 4052480093f4SDimitry Andric 40530b57cec5SDimitry Andric break; 40540b57cec5SDimitry Andric } 40550b57cec5SDimitry Andric 4056480093f4SDimitry Andric // FIXME: It seems Legalized is false iff CCCode is Legal. I don't 4057480093f4SDimitry Andric // understand if this code is useful for strict nodes. 4058480093f4SDimitry Andric assert(!IsStrict && "Don't know how to expand for strict nodes."); 4059480093f4SDimitry Andric 40600b57cec5SDimitry Andric // Otherwise, SETCC for the given comparison type must be completely 40610b57cec5SDimitry Andric // illegal; expand it into a SELECT_CC. 406281ad6265SDimitry Andric // FIXME: This drops the mask/evl for VP_SETCC. 40630b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 406481ad6265SDimitry Andric EVT Tmp1VT = Tmp1.getValueType(); 40650b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, VT, Tmp1, Tmp2, 406681ad6265SDimitry Andric DAG.getBoolConstant(true, dl, VT, Tmp1VT), 406781ad6265SDimitry Andric DAG.getBoolConstant(false, dl, VT, Tmp1VT), Tmp3); 40680b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 40690b57cec5SDimitry Andric Results.push_back(Tmp1); 40700b57cec5SDimitry Andric break; 40710b57cec5SDimitry Andric } 40720b57cec5SDimitry Andric case ISD::SELECT_CC: { 4073480093f4SDimitry Andric // TODO: need to add STRICT_SELECT_CC and STRICT_SELECT_CCS 40740b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // LHS 40750b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); // RHS 40760b57cec5SDimitry Andric Tmp3 = Node->getOperand(2); // True 40770b57cec5SDimitry Andric Tmp4 = Node->getOperand(3); // False 40780b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 4079480093f4SDimitry Andric SDValue Chain; 40800b57cec5SDimitry Andric SDValue CC = Node->getOperand(4); 40810b57cec5SDimitry Andric ISD::CondCode CCOp = cast<CondCodeSDNode>(CC)->get(); 40820b57cec5SDimitry Andric 40830b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(CCOp, Tmp1.getSimpleValueType())) { 40840b57cec5SDimitry Andric // If the condition code is legal, then we need to expand this 40850b57cec5SDimitry Andric // node using SETCC and SELECT. 40860b57cec5SDimitry Andric EVT CmpVT = Tmp1.getValueType(); 40870b57cec5SDimitry Andric assert(!TLI.isOperationExpand(ISD::SELECT, VT) && 40880b57cec5SDimitry Andric "Cannot expand ISD::SELECT_CC when ISD::SELECT also needs to be " 40890b57cec5SDimitry Andric "expanded."); 40900b57cec5SDimitry Andric EVT CCVT = getSetCCResultType(CmpVT); 40910b57cec5SDimitry Andric SDValue Cond = DAG.getNode(ISD::SETCC, dl, CCVT, Tmp1, Tmp2, CC, Node->getFlags()); 40920b57cec5SDimitry Andric Results.push_back(DAG.getSelect(dl, VT, Cond, Tmp3, Tmp4)); 40930b57cec5SDimitry Andric break; 40940b57cec5SDimitry Andric } 40950b57cec5SDimitry Andric 40960b57cec5SDimitry Andric // SELECT_CC is legal, so the condition code must not be. 40970b57cec5SDimitry Andric bool Legalized = false; 40980b57cec5SDimitry Andric // Try to legalize by inverting the condition. This is for targets that 40990b57cec5SDimitry Andric // might support an ordered version of a condition, but not the unordered 41000b57cec5SDimitry Andric // version (or vice versa). 4101480093f4SDimitry Andric ISD::CondCode InvCC = ISD::getSetCCInverse(CCOp, Tmp1.getValueType()); 41020b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(InvCC, Tmp1.getSimpleValueType())) { 41030b57cec5SDimitry Andric // Use the new condition code and swap true and false 41040b57cec5SDimitry Andric Legalized = true; 41050b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp1, Tmp2, Tmp4, Tmp3, InvCC); 41060b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 41070b57cec5SDimitry Andric } else { 41080b57cec5SDimitry Andric // If The inverse is not legal, then try to swap the arguments using 41090b57cec5SDimitry Andric // the inverse condition code. 41100b57cec5SDimitry Andric ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InvCC); 41110b57cec5SDimitry Andric if (TLI.isCondCodeLegalOrCustom(SwapInvCC, Tmp1.getSimpleValueType())) { 41120b57cec5SDimitry Andric // The swapped inverse condition is legal, so swap true and false, 41130b57cec5SDimitry Andric // lhs and rhs. 41140b57cec5SDimitry Andric Legalized = true; 41150b57cec5SDimitry Andric Tmp1 = DAG.getSelectCC(dl, Tmp2, Tmp1, Tmp4, Tmp3, SwapInvCC); 41160b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 41170b57cec5SDimitry Andric } 41180b57cec5SDimitry Andric } 41190b57cec5SDimitry Andric 41200b57cec5SDimitry Andric if (!Legalized) { 4121fe6060f1SDimitry Andric Legalized = TLI.LegalizeSetCCCondCode( 4122fe6060f1SDimitry Andric DAG, getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC, 412381ad6265SDimitry Andric /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain); 41240b57cec5SDimitry Andric 41250b57cec5SDimitry Andric assert(Legalized && "Can't legalize SELECT_CC with legal condition!"); 41260b57cec5SDimitry Andric 41270b57cec5SDimitry Andric // If we expanded the SETCC by inverting the condition code, then swap 41280b57cec5SDimitry Andric // the True/False operands to match. 41290b57cec5SDimitry Andric if (NeedInvert) 41300b57cec5SDimitry Andric std::swap(Tmp3, Tmp4); 41310b57cec5SDimitry Andric 41320b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, or by inverting the 41330b57cec5SDimitry Andric // condition code, create a new SELECT_CC node. 41340b57cec5SDimitry Andric if (CC.getNode()) { 41350b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), 41360b57cec5SDimitry Andric Tmp1, Tmp2, Tmp3, Tmp4, CC); 41370b57cec5SDimitry Andric } else { 41380b57cec5SDimitry Andric Tmp2 = DAG.getConstant(0, dl, Tmp1.getValueType()); 41390b57cec5SDimitry Andric CC = DAG.getCondCode(ISD::SETNE); 41400b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, Node->getValueType(0), Tmp1, 41410b57cec5SDimitry Andric Tmp2, Tmp3, Tmp4, CC); 41420b57cec5SDimitry Andric } 41430b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 41440b57cec5SDimitry Andric } 41450b57cec5SDimitry Andric Results.push_back(Tmp1); 41460b57cec5SDimitry Andric break; 41470b57cec5SDimitry Andric } 41480b57cec5SDimitry Andric case ISD::BR_CC: { 4149480093f4SDimitry Andric // TODO: need to add STRICT_BR_CC and STRICT_BR_CCS 4150480093f4SDimitry Andric SDValue Chain; 41510b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); // Chain 41520b57cec5SDimitry Andric Tmp2 = Node->getOperand(2); // LHS 41530b57cec5SDimitry Andric Tmp3 = Node->getOperand(3); // RHS 41540b57cec5SDimitry Andric Tmp4 = Node->getOperand(1); // CC 41550b57cec5SDimitry Andric 415681ad6265SDimitry Andric bool Legalized = TLI.LegalizeSetCCCondCode( 415781ad6265SDimitry Andric DAG, getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3, Tmp4, 415881ad6265SDimitry Andric /*Mask*/ SDValue(), /*EVL*/ SDValue(), NeedInvert, dl, Chain); 41590b57cec5SDimitry Andric (void)Legalized; 41600b57cec5SDimitry Andric assert(Legalized && "Can't legalize BR_CC with legal condition!"); 41610b57cec5SDimitry Andric 41620b57cec5SDimitry Andric // If we expanded the SETCC by swapping LHS and RHS, create a new BR_CC 41630b57cec5SDimitry Andric // node. 41640b57cec5SDimitry Andric if (Tmp4.getNode()) { 4165e8d8bef9SDimitry Andric assert(!NeedInvert && "Don't know how to invert BR_CC!"); 4166e8d8bef9SDimitry Andric 41670b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, 41680b57cec5SDimitry Andric Tmp4, Tmp2, Tmp3, Node->getOperand(4)); 41690b57cec5SDimitry Andric } else { 41700b57cec5SDimitry Andric Tmp3 = DAG.getConstant(0, dl, Tmp2.getValueType()); 4171e8d8bef9SDimitry Andric Tmp4 = DAG.getCondCode(NeedInvert ? ISD::SETEQ : ISD::SETNE); 41720b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), Tmp1, Tmp4, 41730b57cec5SDimitry Andric Tmp2, Tmp3, Node->getOperand(4)); 41740b57cec5SDimitry Andric } 41750b57cec5SDimitry Andric Results.push_back(Tmp1); 41760b57cec5SDimitry Andric break; 41770b57cec5SDimitry Andric } 41780b57cec5SDimitry Andric case ISD::BUILD_VECTOR: 41790b57cec5SDimitry Andric Results.push_back(ExpandBUILD_VECTOR(Node)); 41800b57cec5SDimitry Andric break; 41818bcb0991SDimitry Andric case ISD::SPLAT_VECTOR: 41828bcb0991SDimitry Andric Results.push_back(ExpandSPLAT_VECTOR(Node)); 41838bcb0991SDimitry Andric break; 41840b57cec5SDimitry Andric case ISD::SRA: 41850b57cec5SDimitry Andric case ISD::SRL: 41860b57cec5SDimitry Andric case ISD::SHL: { 41870b57cec5SDimitry Andric // Scalarize vector SRA/SRL/SHL. 41880b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 41890b57cec5SDimitry Andric assert(VT.isVector() && "Unable to legalize non-vector shift"); 41900b57cec5SDimitry Andric assert(TLI.isTypeLegal(VT.getScalarType())&& "Element type must be legal"); 41910b57cec5SDimitry Andric unsigned NumElem = VT.getVectorNumElements(); 41920b57cec5SDimitry Andric 41930b57cec5SDimitry Andric SmallVector<SDValue, 8> Scalars; 41940b57cec5SDimitry Andric for (unsigned Idx = 0; Idx < NumElem; Idx++) { 41955ffd83dbSDimitry Andric SDValue Ex = 41965ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 41975ffd83dbSDimitry Andric Node->getOperand(0), DAG.getVectorIdxConstant(Idx, dl)); 41985ffd83dbSDimitry Andric SDValue Sh = 41995ffd83dbSDimitry Andric DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT.getScalarType(), 42005ffd83dbSDimitry Andric Node->getOperand(1), DAG.getVectorIdxConstant(Idx, dl)); 42010b57cec5SDimitry Andric Scalars.push_back(DAG.getNode(Node->getOpcode(), dl, 42020b57cec5SDimitry Andric VT.getScalarType(), Ex, Sh)); 42030b57cec5SDimitry Andric } 42040b57cec5SDimitry Andric 42050b57cec5SDimitry Andric SDValue Result = DAG.getBuildVector(Node->getValueType(0), dl, Scalars); 4206480093f4SDimitry Andric Results.push_back(Result); 42070b57cec5SDimitry Andric break; 42080b57cec5SDimitry Andric } 42090b57cec5SDimitry Andric case ISD::VECREDUCE_FADD: 42100b57cec5SDimitry Andric case ISD::VECREDUCE_FMUL: 42110b57cec5SDimitry Andric case ISD::VECREDUCE_ADD: 42120b57cec5SDimitry Andric case ISD::VECREDUCE_MUL: 42130b57cec5SDimitry Andric case ISD::VECREDUCE_AND: 42140b57cec5SDimitry Andric case ISD::VECREDUCE_OR: 42150b57cec5SDimitry Andric case ISD::VECREDUCE_XOR: 42160b57cec5SDimitry Andric case ISD::VECREDUCE_SMAX: 42170b57cec5SDimitry Andric case ISD::VECREDUCE_SMIN: 42180b57cec5SDimitry Andric case ISD::VECREDUCE_UMAX: 42190b57cec5SDimitry Andric case ISD::VECREDUCE_UMIN: 42200b57cec5SDimitry Andric case ISD::VECREDUCE_FMAX: 42210b57cec5SDimitry Andric case ISD::VECREDUCE_FMIN: 422206c3fb27SDimitry Andric case ISD::VECREDUCE_FMAXIMUM: 422306c3fb27SDimitry Andric case ISD::VECREDUCE_FMINIMUM: 42240b57cec5SDimitry Andric Results.push_back(TLI.expandVecReduce(Node, DAG)); 42250b57cec5SDimitry Andric break; 42260b57cec5SDimitry Andric case ISD::GLOBAL_OFFSET_TABLE: 42270b57cec5SDimitry Andric case ISD::GlobalAddress: 42280b57cec5SDimitry Andric case ISD::GlobalTLSAddress: 42290b57cec5SDimitry Andric case ISD::ExternalSymbol: 42300b57cec5SDimitry Andric case ISD::ConstantPool: 42310b57cec5SDimitry Andric case ISD::JumpTable: 42320b57cec5SDimitry Andric case ISD::INTRINSIC_W_CHAIN: 42330b57cec5SDimitry Andric case ISD::INTRINSIC_WO_CHAIN: 42340b57cec5SDimitry Andric case ISD::INTRINSIC_VOID: 42350b57cec5SDimitry Andric // FIXME: Custom lowering for these operations shouldn't return null! 4236480093f4SDimitry Andric // Return true so that we don't call ConvertNodeToLibcall which also won't 4237480093f4SDimitry Andric // do anything. 4238480093f4SDimitry Andric return true; 42390b57cec5SDimitry Andric } 42400b57cec5SDimitry Andric 4241480093f4SDimitry Andric if (!TLI.isStrictFPEnabled() && Results.empty() && Node->isStrictFPOpcode()) { 42428bcb0991SDimitry Andric // FIXME: We were asked to expand a strict floating-point operation, 42438bcb0991SDimitry Andric // but there is currently no expansion implemented that would preserve 42448bcb0991SDimitry Andric // the "strict" properties. For now, we just fall back to the non-strict 42458bcb0991SDimitry Andric // version if that is legal on the target. The actual mutation of the 42468bcb0991SDimitry Andric // operation will happen in SelectionDAGISel::DoInstructionSelection. 42478bcb0991SDimitry Andric switch (Node->getOpcode()) { 42488bcb0991SDimitry Andric default: 42498bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 42508bcb0991SDimitry Andric Node->getValueType(0)) 42518bcb0991SDimitry Andric == TargetLowering::Legal) 42528bcb0991SDimitry Andric return true; 42538bcb0991SDimitry Andric break; 4254e8d8bef9SDimitry Andric case ISD::STRICT_FSUB: { 4255e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 4256e8d8bef9SDimitry Andric ISD::STRICT_FSUB, Node->getValueType(0)) == TargetLowering::Legal) 4257e8d8bef9SDimitry Andric return true; 4258e8d8bef9SDimitry Andric if (TLI.getStrictFPOperationAction( 4259e8d8bef9SDimitry Andric ISD::STRICT_FADD, Node->getValueType(0)) != TargetLowering::Legal) 4260e8d8bef9SDimitry Andric break; 4261e8d8bef9SDimitry Andric 4262e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 4263e8d8bef9SDimitry Andric const SDNodeFlags Flags = Node->getFlags(); 4264e8d8bef9SDimitry Andric SDValue Neg = DAG.getNode(ISD::FNEG, dl, VT, Node->getOperand(2), Flags); 4265e8d8bef9SDimitry Andric SDValue Fadd = DAG.getNode(ISD::STRICT_FADD, dl, Node->getVTList(), 4266e8d8bef9SDimitry Andric {Node->getOperand(0), Node->getOperand(1), Neg}, 4267e8d8bef9SDimitry Andric Flags); 4268e8d8bef9SDimitry Andric 4269e8d8bef9SDimitry Andric Results.push_back(Fadd); 4270e8d8bef9SDimitry Andric Results.push_back(Fadd.getValue(1)); 4271e8d8bef9SDimitry Andric break; 4272e8d8bef9SDimitry Andric } 4273e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4274e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 42758bcb0991SDimitry Andric case ISD::STRICT_LRINT: 42768bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 42778bcb0991SDimitry Andric case ISD::STRICT_LROUND: 42788bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 42798bcb0991SDimitry Andric // These are registered by the operand type instead of the value 42808bcb0991SDimitry Andric // type. Reflect that here. 42818bcb0991SDimitry Andric if (TLI.getStrictFPOperationAction(Node->getOpcode(), 42828bcb0991SDimitry Andric Node->getOperand(1).getValueType()) 42838bcb0991SDimitry Andric == TargetLowering::Legal) 42848bcb0991SDimitry Andric return true; 42858bcb0991SDimitry Andric break; 42868bcb0991SDimitry Andric } 42878bcb0991SDimitry Andric } 42888bcb0991SDimitry Andric 42890b57cec5SDimitry Andric // Replace the original node with the legalized result. 42900b57cec5SDimitry Andric if (Results.empty()) { 42910b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Cannot expand node\n"); 42920b57cec5SDimitry Andric return false; 42930b57cec5SDimitry Andric } 42940b57cec5SDimitry Andric 42950b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully expanded node\n"); 42960b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 42970b57cec5SDimitry Andric return true; 42980b57cec5SDimitry Andric } 42990b57cec5SDimitry Andric 43000b57cec5SDimitry Andric void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) { 43010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to convert node to libcall\n"); 43020b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 43030b57cec5SDimitry Andric SDLoc dl(Node); 43040b57cec5SDimitry Andric // FIXME: Check flags on the node to see if we can use a finite call. 43050b57cec5SDimitry Andric unsigned Opc = Node->getOpcode(); 43060b57cec5SDimitry Andric switch (Opc) { 43070b57cec5SDimitry Andric case ISD::ATOMIC_FENCE: { 43080b57cec5SDimitry Andric // If the target didn't lower this, lower it to '__sync_synchronize()' call 43090b57cec5SDimitry Andric // FIXME: handle "fence singlethread" more efficiently. 43100b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 43110b57cec5SDimitry Andric 43120b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 43130b57cec5SDimitry Andric CLI.setDebugLoc(dl) 43140b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 43150b57cec5SDimitry Andric .setLibCallee( 43160b57cec5SDimitry Andric CallingConv::C, Type::getVoidTy(*DAG.getContext()), 43170b57cec5SDimitry Andric DAG.getExternalSymbol("__sync_synchronize", 43180b57cec5SDimitry Andric TLI.getPointerTy(DAG.getDataLayout())), 43190b57cec5SDimitry Andric std::move(Args)); 43200b57cec5SDimitry Andric 43210b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 43220b57cec5SDimitry Andric 43230b57cec5SDimitry Andric Results.push_back(CallResult.second); 43240b57cec5SDimitry Andric break; 43250b57cec5SDimitry Andric } 43260b57cec5SDimitry Andric // By default, atomic intrinsics are marked Legal and lowered. Targets 43270b57cec5SDimitry Andric // which don't support them directly, however, may want libcalls, in which 43280b57cec5SDimitry Andric // case they mark them Expand, and we get here. 43290b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: 43300b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_ADD: 43310b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_SUB: 43320b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_AND: 43330b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_CLR: 43340b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_OR: 43350b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_XOR: 43360b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_NAND: 43370b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MIN: 43380b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_MAX: 43390b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMIN: 43400b57cec5SDimitry Andric case ISD::ATOMIC_LOAD_UMAX: 43410b57cec5SDimitry Andric case ISD::ATOMIC_CMP_SWAP: { 43420b57cec5SDimitry Andric MVT VT = cast<AtomicSDNode>(Node)->getMemoryVT().getSimpleVT(); 4343fe6060f1SDimitry Andric AtomicOrdering Order = cast<AtomicSDNode>(Node)->getMergedOrdering(); 4344e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getOUTLINE_ATOMIC(Opc, Order, VT); 4345480093f4SDimitry Andric EVT RetVT = Node->getValueType(0); 4346480093f4SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4347e8d8bef9SDimitry Andric SmallVector<SDValue, 4> Ops; 4348e8d8bef9SDimitry Andric if (TLI.getLibcallName(LC)) { 4349e8d8bef9SDimitry Andric // If outline atomic available, prepare its arguments and expand. 4350e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 2, Node->op_end()); 4351e8d8bef9SDimitry Andric Ops.push_back(Node->getOperand(1)); 4352e8d8bef9SDimitry Andric 4353e8d8bef9SDimitry Andric } else { 4354e8d8bef9SDimitry Andric LC = RTLIB::getSYNC(Opc, VT); 4355e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && 4356e8d8bef9SDimitry Andric "Unexpected atomic op or value type!"); 4357e8d8bef9SDimitry Andric // Arguments for expansion to sync libcall 4358e8d8bef9SDimitry Andric Ops.append(Node->op_begin() + 1, Node->op_end()); 4359e8d8bef9SDimitry Andric } 4360480093f4SDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall(DAG, LC, RetVT, 4361480093f4SDimitry Andric Ops, CallOptions, 4362480093f4SDimitry Andric SDLoc(Node), 4363480093f4SDimitry Andric Node->getOperand(0)); 43640b57cec5SDimitry Andric Results.push_back(Tmp.first); 43650b57cec5SDimitry Andric Results.push_back(Tmp.second); 43660b57cec5SDimitry Andric break; 43670b57cec5SDimitry Andric } 43680b57cec5SDimitry Andric case ISD::TRAP: { 43690b57cec5SDimitry Andric // If this operation is not supported, lower it to 'abort()' call 43700b57cec5SDimitry Andric TargetLowering::ArgListTy Args; 43710b57cec5SDimitry Andric TargetLowering::CallLoweringInfo CLI(DAG); 43720b57cec5SDimitry Andric CLI.setDebugLoc(dl) 43730b57cec5SDimitry Andric .setChain(Node->getOperand(0)) 43740b57cec5SDimitry Andric .setLibCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()), 43750b57cec5SDimitry Andric DAG.getExternalSymbol( 43760b57cec5SDimitry Andric "abort", TLI.getPointerTy(DAG.getDataLayout())), 43770b57cec5SDimitry Andric std::move(Args)); 43780b57cec5SDimitry Andric std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); 43790b57cec5SDimitry Andric 43800b57cec5SDimitry Andric Results.push_back(CallResult.second); 43810b57cec5SDimitry Andric break; 43820b57cec5SDimitry Andric } 43830b57cec5SDimitry Andric case ISD::FMINNUM: 43840b57cec5SDimitry Andric case ISD::STRICT_FMINNUM: 4385480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMIN_F32, RTLIB::FMIN_F64, 43860b57cec5SDimitry Andric RTLIB::FMIN_F80, RTLIB::FMIN_F128, 4387480093f4SDimitry Andric RTLIB::FMIN_PPCF128, Results); 43880b57cec5SDimitry Andric break; 438906c3fb27SDimitry Andric // FIXME: We do not have libcalls for FMAXIMUM and FMINIMUM. So, we cannot use 439006c3fb27SDimitry Andric // libcall legalization for these nodes, but there is no default expasion for 439106c3fb27SDimitry Andric // these nodes either (see PR63267 for example). 43920b57cec5SDimitry Andric case ISD::FMAXNUM: 43930b57cec5SDimitry Andric case ISD::STRICT_FMAXNUM: 4394480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMAX_F32, RTLIB::FMAX_F64, 43950b57cec5SDimitry Andric RTLIB::FMAX_F80, RTLIB::FMAX_F128, 4396480093f4SDimitry Andric RTLIB::FMAX_PPCF128, Results); 43970b57cec5SDimitry Andric break; 43980b57cec5SDimitry Andric case ISD::FSQRT: 43990b57cec5SDimitry Andric case ISD::STRICT_FSQRT: 4400480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SQRT_F32, RTLIB::SQRT_F64, 44010b57cec5SDimitry Andric RTLIB::SQRT_F80, RTLIB::SQRT_F128, 4402480093f4SDimitry Andric RTLIB::SQRT_PPCF128, Results); 44030b57cec5SDimitry Andric break; 44040b57cec5SDimitry Andric case ISD::FCBRT: 4405480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CBRT_F32, RTLIB::CBRT_F64, 44060b57cec5SDimitry Andric RTLIB::CBRT_F80, RTLIB::CBRT_F128, 4407480093f4SDimitry Andric RTLIB::CBRT_PPCF128, Results); 44080b57cec5SDimitry Andric break; 44090b57cec5SDimitry Andric case ISD::FSIN: 44100b57cec5SDimitry Andric case ISD::STRICT_FSIN: 4411480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SIN_F32, RTLIB::SIN_F64, 44120b57cec5SDimitry Andric RTLIB::SIN_F80, RTLIB::SIN_F128, 4413480093f4SDimitry Andric RTLIB::SIN_PPCF128, Results); 44140b57cec5SDimitry Andric break; 44150b57cec5SDimitry Andric case ISD::FCOS: 44160b57cec5SDimitry Andric case ISD::STRICT_FCOS: 4417480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::COS_F32, RTLIB::COS_F64, 44180b57cec5SDimitry Andric RTLIB::COS_F80, RTLIB::COS_F128, 4419480093f4SDimitry Andric RTLIB::COS_PPCF128, Results); 44200b57cec5SDimitry Andric break; 44210b57cec5SDimitry Andric case ISD::FSINCOS: 44220b57cec5SDimitry Andric // Expand into sincos libcall. 44230b57cec5SDimitry Andric ExpandSinCosLibCall(Node, Results); 44240b57cec5SDimitry Andric break; 44250b57cec5SDimitry Andric case ISD::FLOG: 44260b57cec5SDimitry Andric case ISD::STRICT_FLOG: 44278c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG_F32, RTLIB::LOG_F64, RTLIB::LOG_F80, 44288c27c554SDimitry Andric RTLIB::LOG_F128, RTLIB::LOG_PPCF128, Results); 44290b57cec5SDimitry Andric break; 44300b57cec5SDimitry Andric case ISD::FLOG2: 44310b57cec5SDimitry Andric case ISD::STRICT_FLOG2: 44328c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG2_F32, RTLIB::LOG2_F64, RTLIB::LOG2_F80, 44338c27c554SDimitry Andric RTLIB::LOG2_F128, RTLIB::LOG2_PPCF128, Results); 44340b57cec5SDimitry Andric break; 44350b57cec5SDimitry Andric case ISD::FLOG10: 44360b57cec5SDimitry Andric case ISD::STRICT_FLOG10: 44378c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::LOG10_F32, RTLIB::LOG10_F64, RTLIB::LOG10_F80, 44388c27c554SDimitry Andric RTLIB::LOG10_F128, RTLIB::LOG10_PPCF128, Results); 44390b57cec5SDimitry Andric break; 44400b57cec5SDimitry Andric case ISD::FEXP: 44410b57cec5SDimitry Andric case ISD::STRICT_FEXP: 44428c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP_F32, RTLIB::EXP_F64, RTLIB::EXP_F80, 44438c27c554SDimitry Andric RTLIB::EXP_F128, RTLIB::EXP_PPCF128, Results); 44440b57cec5SDimitry Andric break; 44450b57cec5SDimitry Andric case ISD::FEXP2: 44460b57cec5SDimitry Andric case ISD::STRICT_FEXP2: 44478c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP2_F32, RTLIB::EXP2_F64, RTLIB::EXP2_F80, 44488c27c554SDimitry Andric RTLIB::EXP2_F128, RTLIB::EXP2_PPCF128, Results); 44490b57cec5SDimitry Andric break; 44505f757f3fSDimitry Andric case ISD::FEXP10: 44515f757f3fSDimitry Andric ExpandFPLibCall(Node, RTLIB::EXP10_F32, RTLIB::EXP10_F64, RTLIB::EXP10_F80, 44525f757f3fSDimitry Andric RTLIB::EXP10_F128, RTLIB::EXP10_PPCF128, Results); 44535f757f3fSDimitry Andric break; 44540b57cec5SDimitry Andric case ISD::FTRUNC: 44550b57cec5SDimitry Andric case ISD::STRICT_FTRUNC: 4456480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64, 44570b57cec5SDimitry Andric RTLIB::TRUNC_F80, RTLIB::TRUNC_F128, 4458480093f4SDimitry Andric RTLIB::TRUNC_PPCF128, Results); 44590b57cec5SDimitry Andric break; 44600b57cec5SDimitry Andric case ISD::FFLOOR: 44610b57cec5SDimitry Andric case ISD::STRICT_FFLOOR: 4462480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64, 44630b57cec5SDimitry Andric RTLIB::FLOOR_F80, RTLIB::FLOOR_F128, 4464480093f4SDimitry Andric RTLIB::FLOOR_PPCF128, Results); 44650b57cec5SDimitry Andric break; 44660b57cec5SDimitry Andric case ISD::FCEIL: 44670b57cec5SDimitry Andric case ISD::STRICT_FCEIL: 4468480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::CEIL_F32, RTLIB::CEIL_F64, 44690b57cec5SDimitry Andric RTLIB::CEIL_F80, RTLIB::CEIL_F128, 4470480093f4SDimitry Andric RTLIB::CEIL_PPCF128, Results); 44710b57cec5SDimitry Andric break; 44720b57cec5SDimitry Andric case ISD::FRINT: 44730b57cec5SDimitry Andric case ISD::STRICT_FRINT: 4474480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::RINT_F32, RTLIB::RINT_F64, 44750b57cec5SDimitry Andric RTLIB::RINT_F80, RTLIB::RINT_F128, 4476480093f4SDimitry Andric RTLIB::RINT_PPCF128, Results); 44770b57cec5SDimitry Andric break; 44780b57cec5SDimitry Andric case ISD::FNEARBYINT: 44790b57cec5SDimitry Andric case ISD::STRICT_FNEARBYINT: 4480480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::NEARBYINT_F32, 44810b57cec5SDimitry Andric RTLIB::NEARBYINT_F64, 44820b57cec5SDimitry Andric RTLIB::NEARBYINT_F80, 44830b57cec5SDimitry Andric RTLIB::NEARBYINT_F128, 4484480093f4SDimitry Andric RTLIB::NEARBYINT_PPCF128, Results); 44850b57cec5SDimitry Andric break; 44860b57cec5SDimitry Andric case ISD::FROUND: 44870b57cec5SDimitry Andric case ISD::STRICT_FROUND: 4488480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUND_F32, 44890b57cec5SDimitry Andric RTLIB::ROUND_F64, 44900b57cec5SDimitry Andric RTLIB::ROUND_F80, 44910b57cec5SDimitry Andric RTLIB::ROUND_F128, 4492480093f4SDimitry Andric RTLIB::ROUND_PPCF128, Results); 44930b57cec5SDimitry Andric break; 44945ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 44955ffd83dbSDimitry Andric case ISD::STRICT_FROUNDEVEN: 44965ffd83dbSDimitry Andric ExpandFPLibCall(Node, RTLIB::ROUNDEVEN_F32, 44975ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F64, 44985ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F80, 44995ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_F128, 45005ffd83dbSDimitry Andric RTLIB::ROUNDEVEN_PPCF128, Results); 45015ffd83dbSDimitry Andric break; 450206c3fb27SDimitry Andric case ISD::FLDEXP: 450306c3fb27SDimitry Andric case ISD::STRICT_FLDEXP: 450406c3fb27SDimitry Andric ExpandFPLibCall(Node, RTLIB::LDEXP_F32, RTLIB::LDEXP_F64, RTLIB::LDEXP_F80, 450506c3fb27SDimitry Andric RTLIB::LDEXP_F128, RTLIB::LDEXP_PPCF128, Results); 450606c3fb27SDimitry Andric break; 450706c3fb27SDimitry Andric case ISD::FFREXP: { 450806c3fb27SDimitry Andric ExpandFrexpLibCall(Node, Results); 450906c3fb27SDimitry Andric break; 451006c3fb27SDimitry Andric } 45110b57cec5SDimitry Andric case ISD::FPOWI: 4512480093f4SDimitry Andric case ISD::STRICT_FPOWI: { 4513fe6060f1SDimitry Andric RTLIB::Libcall LC = RTLIB::getPOWI(Node->getSimpleValueType(0)); 4514fe6060f1SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fpowi."); 4515480093f4SDimitry Andric if (!TLI.getLibcallName(LC)) { 4516480093f4SDimitry Andric // Some targets don't have a powi libcall; use pow instead. 451781ad6265SDimitry Andric if (Node->isStrictFPOpcode()) { 451881ad6265SDimitry Andric SDValue Exponent = 451981ad6265SDimitry Andric DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(Node), 452081ad6265SDimitry Andric {Node->getValueType(0), Node->getValueType(1)}, 452181ad6265SDimitry Andric {Node->getOperand(0), Node->getOperand(2)}); 452281ad6265SDimitry Andric SDValue FPOW = 452381ad6265SDimitry Andric DAG.getNode(ISD::STRICT_FPOW, SDLoc(Node), 452481ad6265SDimitry Andric {Node->getValueType(0), Node->getValueType(1)}, 452581ad6265SDimitry Andric {Exponent.getValue(1), Node->getOperand(1), Exponent}); 452681ad6265SDimitry Andric Results.push_back(FPOW); 452781ad6265SDimitry Andric Results.push_back(FPOW.getValue(1)); 452881ad6265SDimitry Andric } else { 452981ad6265SDimitry Andric SDValue Exponent = 453081ad6265SDimitry Andric DAG.getNode(ISD::SINT_TO_FP, SDLoc(Node), Node->getValueType(0), 4531480093f4SDimitry Andric Node->getOperand(1)); 4532480093f4SDimitry Andric Results.push_back(DAG.getNode(ISD::FPOW, SDLoc(Node), 453381ad6265SDimitry Andric Node->getValueType(0), 453481ad6265SDimitry Andric Node->getOperand(0), Exponent)); 453581ad6265SDimitry Andric } 45360b57cec5SDimitry Andric break; 4537480093f4SDimitry Andric } 4538fe6060f1SDimitry Andric unsigned Offset = Node->isStrictFPOpcode() ? 1 : 0; 4539fe6060f1SDimitry Andric bool ExponentHasSizeOfInt = 4540fe6060f1SDimitry Andric DAG.getLibInfo().getIntSize() == 4541fe6060f1SDimitry Andric Node->getOperand(1 + Offset).getValueType().getSizeInBits(); 4542fe6060f1SDimitry Andric if (!ExponentHasSizeOfInt) { 4543fe6060f1SDimitry Andric // If the exponent does not match with sizeof(int) a libcall to 4544fe6060f1SDimitry Andric // RTLIB::POWI would use the wrong type for the argument. 4545fe6060f1SDimitry Andric DAG.getContext()->emitError("POWI exponent does not match sizeof(int)"); 4546fe6060f1SDimitry Andric Results.push_back(DAG.getUNDEF(Node->getValueType(0))); 4547fe6060f1SDimitry Andric break; 4548fe6060f1SDimitry Andric } 4549fe6060f1SDimitry Andric ExpandFPLibCall(Node, LC, Results); 4550480093f4SDimitry Andric break; 4551480093f4SDimitry Andric } 45520b57cec5SDimitry Andric case ISD::FPOW: 45530b57cec5SDimitry Andric case ISD::STRICT_FPOW: 45548c27c554SDimitry Andric ExpandFPLibCall(Node, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80, 45558c27c554SDimitry Andric RTLIB::POW_F128, RTLIB::POW_PPCF128, Results); 45560b57cec5SDimitry Andric break; 45578bcb0991SDimitry Andric case ISD::LROUND: 45588bcb0991SDimitry Andric case ISD::STRICT_LROUND: 4559480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LROUND_F32, 45608bcb0991SDimitry Andric RTLIB::LROUND_F64, RTLIB::LROUND_F80, 45618bcb0991SDimitry Andric RTLIB::LROUND_F128, 4562480093f4SDimitry Andric RTLIB::LROUND_PPCF128, Results); 45638bcb0991SDimitry Andric break; 45648bcb0991SDimitry Andric case ISD::LLROUND: 45658bcb0991SDimitry Andric case ISD::STRICT_LLROUND: 4566480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLROUND_F32, 45678bcb0991SDimitry Andric RTLIB::LLROUND_F64, RTLIB::LLROUND_F80, 45688bcb0991SDimitry Andric RTLIB::LLROUND_F128, 4569480093f4SDimitry Andric RTLIB::LLROUND_PPCF128, Results); 45708bcb0991SDimitry Andric break; 45718bcb0991SDimitry Andric case ISD::LRINT: 45728bcb0991SDimitry Andric case ISD::STRICT_LRINT: 4573480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LRINT_F32, 45748bcb0991SDimitry Andric RTLIB::LRINT_F64, RTLIB::LRINT_F80, 45758bcb0991SDimitry Andric RTLIB::LRINT_F128, 4576480093f4SDimitry Andric RTLIB::LRINT_PPCF128, Results); 45778bcb0991SDimitry Andric break; 45788bcb0991SDimitry Andric case ISD::LLRINT: 45798bcb0991SDimitry Andric case ISD::STRICT_LLRINT: 4580480093f4SDimitry Andric ExpandArgFPLibCall(Node, RTLIB::LLRINT_F32, 45818bcb0991SDimitry Andric RTLIB::LLRINT_F64, RTLIB::LLRINT_F80, 45828bcb0991SDimitry Andric RTLIB::LLRINT_F128, 4583480093f4SDimitry Andric RTLIB::LLRINT_PPCF128, Results); 45848bcb0991SDimitry Andric break; 45850b57cec5SDimitry Andric case ISD::FDIV: 4586480093f4SDimitry Andric case ISD::STRICT_FDIV: 4587480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::DIV_F32, RTLIB::DIV_F64, 45880b57cec5SDimitry Andric RTLIB::DIV_F80, RTLIB::DIV_F128, 4589480093f4SDimitry Andric RTLIB::DIV_PPCF128, Results); 45900b57cec5SDimitry Andric break; 45910b57cec5SDimitry Andric case ISD::FREM: 45920b57cec5SDimitry Andric case ISD::STRICT_FREM: 4593480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::REM_F32, RTLIB::REM_F64, 45940b57cec5SDimitry Andric RTLIB::REM_F80, RTLIB::REM_F128, 4595480093f4SDimitry Andric RTLIB::REM_PPCF128, Results); 45960b57cec5SDimitry Andric break; 45970b57cec5SDimitry Andric case ISD::FMA: 45980b57cec5SDimitry Andric case ISD::STRICT_FMA: 4599480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::FMA_F32, RTLIB::FMA_F64, 46000b57cec5SDimitry Andric RTLIB::FMA_F80, RTLIB::FMA_F128, 4601480093f4SDimitry Andric RTLIB::FMA_PPCF128, Results); 46020b57cec5SDimitry Andric break; 46030b57cec5SDimitry Andric case ISD::FADD: 4604480093f4SDimitry Andric case ISD::STRICT_FADD: 4605480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::ADD_F32, RTLIB::ADD_F64, 46060b57cec5SDimitry Andric RTLIB::ADD_F80, RTLIB::ADD_F128, 4607480093f4SDimitry Andric RTLIB::ADD_PPCF128, Results); 46080b57cec5SDimitry Andric break; 46090b57cec5SDimitry Andric case ISD::FMUL: 4610480093f4SDimitry Andric case ISD::STRICT_FMUL: 4611480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::MUL_F32, RTLIB::MUL_F64, 46120b57cec5SDimitry Andric RTLIB::MUL_F80, RTLIB::MUL_F128, 4613480093f4SDimitry Andric RTLIB::MUL_PPCF128, Results); 46140b57cec5SDimitry Andric break; 46150b57cec5SDimitry Andric case ISD::FP16_TO_FP: 46160b57cec5SDimitry Andric if (Node->getValueType(0) == MVT::f32) { 461706c3fb27SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::FPEXT_F16_F32, Node, false).first); 46180b57cec5SDimitry Andric } 46190b57cec5SDimitry Andric break; 46205ffd83dbSDimitry Andric case ISD::STRICT_FP16_TO_FP: { 46215ffd83dbSDimitry Andric if (Node->getValueType(0) == MVT::f32) { 46225ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 46235ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = TLI.makeLibCall( 46245ffd83dbSDimitry Andric DAG, RTLIB::FPEXT_F16_F32, MVT::f32, Node->getOperand(1), CallOptions, 46255ffd83dbSDimitry Andric SDLoc(Node), Node->getOperand(0)); 46265ffd83dbSDimitry Andric Results.push_back(Tmp.first); 46275ffd83dbSDimitry Andric Results.push_back(Tmp.second); 46285ffd83dbSDimitry Andric } 46295ffd83dbSDimitry Andric break; 46305ffd83dbSDimitry Andric } 46310b57cec5SDimitry Andric case ISD::FP_TO_FP16: { 46320b57cec5SDimitry Andric RTLIB::Libcall LC = 46330b57cec5SDimitry Andric RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::f16); 46340b57cec5SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_fp16"); 463506c3fb27SDimitry Andric Results.push_back(ExpandLibCall(LC, Node, false).first); 46360b57cec5SDimitry Andric break; 46370b57cec5SDimitry Andric } 463881ad6265SDimitry Andric case ISD::FP_TO_BF16: { 463981ad6265SDimitry Andric RTLIB::Libcall LC = 464081ad6265SDimitry Andric RTLIB::getFPROUND(Node->getOperand(0).getValueType(), MVT::bf16); 464181ad6265SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to expand fp_to_bf16"); 464206c3fb27SDimitry Andric Results.push_back(ExpandLibCall(LC, Node, false).first); 464381ad6265SDimitry Andric break; 464481ad6265SDimitry Andric } 4645e8d8bef9SDimitry Andric case ISD::STRICT_SINT_TO_FP: 4646e8d8bef9SDimitry Andric case ISD::STRICT_UINT_TO_FP: 4647e8d8bef9SDimitry Andric case ISD::SINT_TO_FP: 4648e8d8bef9SDimitry Andric case ISD::UINT_TO_FP: { 4649e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatRes_XINT_TO_FP 4650e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4651e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::SINT_TO_FP || 4652e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP; 4653e8d8bef9SDimitry Andric EVT SVT = Node->getOperand(IsStrict ? 1 : 0).getValueType(); 4654e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4655e8d8bef9SDimitry Andric EVT NVT = EVT(); 4656e8d8bef9SDimitry Andric SDLoc dl(Node); 4657e8d8bef9SDimitry Andric 4658e8d8bef9SDimitry Andric // Even if the input is legal, no libcall may exactly match, eg. we don't 4659e8d8bef9SDimitry Andric // have i1 -> fp conversions. So, it needs to be promoted to a larger type, 4660e8d8bef9SDimitry Andric // eg: i13 -> fp. Then, look for an appropriate libcall. 4661e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4662e8d8bef9SDimitry Andric for (unsigned t = MVT::FIRST_INTEGER_VALUETYPE; 4663e8d8bef9SDimitry Andric t <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4664e8d8bef9SDimitry Andric ++t) { 4665e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)t; 4666e8d8bef9SDimitry Andric // The source needs to big enough to hold the operand. 4667e8d8bef9SDimitry Andric if (NVT.bitsGE(SVT)) 4668e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getSINTTOFP(NVT, RVT) 4669e8d8bef9SDimitry Andric : RTLIB::getUINTTOFP(NVT, RVT); 4670e8d8bef9SDimitry Andric } 4671e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4672e8d8bef9SDimitry Andric 4673e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4674e8d8bef9SDimitry Andric // Sign/zero extend the argument if the libcall takes a larger type. 4675e8d8bef9SDimitry Andric SDValue Op = DAG.getNode(Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND, dl, 4676e8d8bef9SDimitry Andric NVT, Node->getOperand(IsStrict ? 1 : 0)); 4677e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4678e8d8bef9SDimitry Andric CallOptions.setSExt(Signed); 4679e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4680e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, RVT, Op, CallOptions, dl, Chain); 4681e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4682e8d8bef9SDimitry Andric if (IsStrict) 4683e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4684e8d8bef9SDimitry Andric break; 4685e8d8bef9SDimitry Andric } 4686e8d8bef9SDimitry Andric case ISD::FP_TO_SINT: 4687e8d8bef9SDimitry Andric case ISD::FP_TO_UINT: 4688e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4689e8d8bef9SDimitry Andric case ISD::STRICT_FP_TO_UINT: { 4690e8d8bef9SDimitry Andric // TODO - Common the code with DAGTypeLegalizer::SoftenFloatOp_FP_TO_XINT. 4691e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4692e8d8bef9SDimitry Andric bool Signed = Node->getOpcode() == ISD::FP_TO_SINT || 4693e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_SINT; 4694e8d8bef9SDimitry Andric 4695e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4696e8d8bef9SDimitry Andric EVT SVT = Op.getValueType(); 4697e8d8bef9SDimitry Andric EVT RVT = Node->getValueType(0); 4698e8d8bef9SDimitry Andric EVT NVT = EVT(); 4699e8d8bef9SDimitry Andric SDLoc dl(Node); 4700e8d8bef9SDimitry Andric 4701e8d8bef9SDimitry Andric // Even if the result is legal, no libcall may exactly match, eg. we don't 4702e8d8bef9SDimitry Andric // have fp -> i1 conversions. So, it needs to be promoted to a larger type, 4703e8d8bef9SDimitry Andric // eg: fp -> i32. Then, look for an appropriate libcall. 4704e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL; 4705e8d8bef9SDimitry Andric for (unsigned IntVT = MVT::FIRST_INTEGER_VALUETYPE; 4706e8d8bef9SDimitry Andric IntVT <= MVT::LAST_INTEGER_VALUETYPE && LC == RTLIB::UNKNOWN_LIBCALL; 4707e8d8bef9SDimitry Andric ++IntVT) { 4708e8d8bef9SDimitry Andric NVT = (MVT::SimpleValueType)IntVT; 4709e8d8bef9SDimitry Andric // The type needs to big enough to hold the result. 4710e8d8bef9SDimitry Andric if (NVT.bitsGE(RVT)) 4711e8d8bef9SDimitry Andric LC = Signed ? RTLIB::getFPTOSINT(SVT, NVT) 4712e8d8bef9SDimitry Andric : RTLIB::getFPTOUINT(SVT, NVT); 4713e8d8bef9SDimitry Andric } 4714e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4715e8d8bef9SDimitry Andric 4716e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4717e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4718e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4719e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, NVT, Op, CallOptions, dl, Chain); 4720e8d8bef9SDimitry Andric 4721e8d8bef9SDimitry Andric // Truncate the result if the libcall returns a larger type. 4722e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, RVT, Tmp.first)); 4723e8d8bef9SDimitry Andric if (IsStrict) 4724e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4725e8d8bef9SDimitry Andric break; 4726e8d8bef9SDimitry Andric } 4727e8d8bef9SDimitry Andric 4728e8d8bef9SDimitry Andric case ISD::FP_ROUND: 4729e8d8bef9SDimitry Andric case ISD::STRICT_FP_ROUND: { 4730e8d8bef9SDimitry Andric // X = FP_ROUND(Y, TRUNC) 4731e8d8bef9SDimitry Andric // TRUNC is a flag, which is always an integer that is zero or one. 4732e8d8bef9SDimitry Andric // If TRUNC is 0, this is a normal rounding, if it is 1, this FP_ROUND 4733e8d8bef9SDimitry Andric // is known to not change the value of Y. 4734e8d8bef9SDimitry Andric // We can only expand it into libcall if the TRUNC is 0. 4735e8d8bef9SDimitry Andric bool IsStrict = Node->isStrictFPOpcode(); 4736e8d8bef9SDimitry Andric SDValue Op = Node->getOperand(IsStrict ? 1 : 0); 4737e8d8bef9SDimitry Andric SDValue Chain = IsStrict ? Node->getOperand(0) : SDValue(); 4738e8d8bef9SDimitry Andric EVT VT = Node->getValueType(0); 4739349cc55cSDimitry Andric assert(cast<ConstantSDNode>(Node->getOperand(IsStrict ? 2 : 1))->isZero() && 4740e8d8bef9SDimitry Andric "Unable to expand as libcall if it is not normal rounding"); 4741e8d8bef9SDimitry Andric 4742e8d8bef9SDimitry Andric RTLIB::Libcall LC = RTLIB::getFPROUND(Op.getValueType(), VT); 4743e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4744e8d8bef9SDimitry Andric 4745e8d8bef9SDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 4746e8d8bef9SDimitry Andric std::pair<SDValue, SDValue> Tmp = 4747e8d8bef9SDimitry Andric TLI.makeLibCall(DAG, LC, VT, Op, CallOptions, SDLoc(Node), Chain); 4748e8d8bef9SDimitry Andric Results.push_back(Tmp.first); 4749e8d8bef9SDimitry Andric if (IsStrict) 4750e8d8bef9SDimitry Andric Results.push_back(Tmp.second); 4751e8d8bef9SDimitry Andric break; 4752e8d8bef9SDimitry Andric } 4753e8d8bef9SDimitry Andric case ISD::FP_EXTEND: { 4754e8d8bef9SDimitry Andric Results.push_back( 4755e8d8bef9SDimitry Andric ExpandLibCall(RTLIB::getFPEXT(Node->getOperand(0).getValueType(), 4756e8d8bef9SDimitry Andric Node->getValueType(0)), 475706c3fb27SDimitry Andric Node, false).first); 4758e8d8bef9SDimitry Andric break; 4759e8d8bef9SDimitry Andric } 4760e8d8bef9SDimitry Andric case ISD::STRICT_FP_EXTEND: 47615ffd83dbSDimitry Andric case ISD::STRICT_FP_TO_FP16: { 47625ffd83dbSDimitry Andric RTLIB::Libcall LC = 4763e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FP_TO_FP16 4764e8d8bef9SDimitry Andric ? RTLIB::getFPROUND(Node->getOperand(1).getValueType(), MVT::f16) 4765e8d8bef9SDimitry Andric : RTLIB::getFPEXT(Node->getOperand(1).getValueType(), 4766e8d8bef9SDimitry Andric Node->getValueType(0)); 4767e8d8bef9SDimitry Andric assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unable to legalize as libcall"); 4768e8d8bef9SDimitry Andric 47695ffd83dbSDimitry Andric TargetLowering::MakeLibCallOptions CallOptions; 47705ffd83dbSDimitry Andric std::pair<SDValue, SDValue> Tmp = 47715ffd83dbSDimitry Andric TLI.makeLibCall(DAG, LC, Node->getValueType(0), Node->getOperand(1), 47725ffd83dbSDimitry Andric CallOptions, SDLoc(Node), Node->getOperand(0)); 47735ffd83dbSDimitry Andric Results.push_back(Tmp.first); 47745ffd83dbSDimitry Andric Results.push_back(Tmp.second); 47755ffd83dbSDimitry Andric break; 47765ffd83dbSDimitry Andric } 47770b57cec5SDimitry Andric case ISD::FSUB: 4778480093f4SDimitry Andric case ISD::STRICT_FSUB: 4779480093f4SDimitry Andric ExpandFPLibCall(Node, RTLIB::SUB_F32, RTLIB::SUB_F64, 47800b57cec5SDimitry Andric RTLIB::SUB_F80, RTLIB::SUB_F128, 4781480093f4SDimitry Andric RTLIB::SUB_PPCF128, Results); 47820b57cec5SDimitry Andric break; 47830b57cec5SDimitry Andric case ISD::SREM: 4784bdd1243dSDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 4785bdd1243dSDimitry Andric RTLIB::SREM_I8, 4786bdd1243dSDimitry Andric RTLIB::SREM_I16, RTLIB::SREM_I32, 4787bdd1243dSDimitry Andric RTLIB::SREM_I64, RTLIB::SREM_I128)); 47880b57cec5SDimitry Andric break; 47890b57cec5SDimitry Andric case ISD::UREM: 4790bdd1243dSDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 4791bdd1243dSDimitry Andric RTLIB::UREM_I8, 4792bdd1243dSDimitry Andric RTLIB::UREM_I16, RTLIB::UREM_I32, 4793bdd1243dSDimitry Andric RTLIB::UREM_I64, RTLIB::UREM_I128)); 47940b57cec5SDimitry Andric break; 47950b57cec5SDimitry Andric case ISD::SDIV: 4796bdd1243dSDimitry Andric Results.push_back(ExpandIntLibCall(Node, true, 4797bdd1243dSDimitry Andric RTLIB::SDIV_I8, 4798bdd1243dSDimitry Andric RTLIB::SDIV_I16, RTLIB::SDIV_I32, 4799bdd1243dSDimitry Andric RTLIB::SDIV_I64, RTLIB::SDIV_I128)); 48000b57cec5SDimitry Andric break; 48010b57cec5SDimitry Andric case ISD::UDIV: 4802bdd1243dSDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 4803bdd1243dSDimitry Andric RTLIB::UDIV_I8, 4804bdd1243dSDimitry Andric RTLIB::UDIV_I16, RTLIB::UDIV_I32, 4805bdd1243dSDimitry Andric RTLIB::UDIV_I64, RTLIB::UDIV_I128)); 48060b57cec5SDimitry Andric break; 48070b57cec5SDimitry Andric case ISD::SDIVREM: 48080b57cec5SDimitry Andric case ISD::UDIVREM: 48090b57cec5SDimitry Andric // Expand into divrem libcall 48100b57cec5SDimitry Andric ExpandDivRemLibCall(Node, Results); 48110b57cec5SDimitry Andric break; 48120b57cec5SDimitry Andric case ISD::MUL: 4813bdd1243dSDimitry Andric Results.push_back(ExpandIntLibCall(Node, false, 4814bdd1243dSDimitry Andric RTLIB::MUL_I8, 4815bdd1243dSDimitry Andric RTLIB::MUL_I16, RTLIB::MUL_I32, 4816bdd1243dSDimitry Andric RTLIB::MUL_I64, RTLIB::MUL_I128)); 48170b57cec5SDimitry Andric break; 48180b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 48190b57cec5SDimitry Andric switch (Node->getSimpleValueType(0).SimpleTy) { 48200b57cec5SDimitry Andric default: 48210b57cec5SDimitry Andric llvm_unreachable("LibCall explicitly requested, but not available"); 48220b57cec5SDimitry Andric case MVT::i32: 482306c3fb27SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I32, Node, false).first); 48240b57cec5SDimitry Andric break; 48250b57cec5SDimitry Andric case MVT::i64: 482606c3fb27SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I64, Node, false).first); 48270b57cec5SDimitry Andric break; 48280b57cec5SDimitry Andric case MVT::i128: 482906c3fb27SDimitry Andric Results.push_back(ExpandLibCall(RTLIB::CTLZ_I128, Node, false).first); 48300b57cec5SDimitry Andric break; 48310b57cec5SDimitry Andric } 48320b57cec5SDimitry Andric break; 483306c3fb27SDimitry Andric case ISD::RESET_FPENV: { 483406c3fb27SDimitry Andric // It is legalized to call 'fesetenv(FE_DFL_ENV)'. On most targets 483506c3fb27SDimitry Andric // FE_DFL_ENV is defined as '((const fenv_t *) -1)' in glibc. 483606c3fb27SDimitry Andric SDValue Ptr = DAG.getIntPtrConstant(-1LL, dl); 483706c3fb27SDimitry Andric SDValue Chain = Node->getOperand(0); 483806c3fb27SDimitry Andric Results.push_back( 483906c3fb27SDimitry Andric DAG.makeStateFunctionCall(RTLIB::FESETENV, Ptr, Chain, dl)); 484006c3fb27SDimitry Andric break; 484106c3fb27SDimitry Andric } 484206c3fb27SDimitry Andric case ISD::GET_FPENV_MEM: { 484306c3fb27SDimitry Andric SDValue Chain = Node->getOperand(0); 484406c3fb27SDimitry Andric SDValue EnvPtr = Node->getOperand(1); 484506c3fb27SDimitry Andric Results.push_back( 484606c3fb27SDimitry Andric DAG.makeStateFunctionCall(RTLIB::FEGETENV, EnvPtr, Chain, dl)); 484706c3fb27SDimitry Andric break; 484806c3fb27SDimitry Andric } 484906c3fb27SDimitry Andric case ISD::SET_FPENV_MEM: { 485006c3fb27SDimitry Andric SDValue Chain = Node->getOperand(0); 485106c3fb27SDimitry Andric SDValue EnvPtr = Node->getOperand(1); 485206c3fb27SDimitry Andric Results.push_back( 485306c3fb27SDimitry Andric DAG.makeStateFunctionCall(RTLIB::FESETENV, EnvPtr, Chain, dl)); 485406c3fb27SDimitry Andric break; 485506c3fb27SDimitry Andric } 48565f757f3fSDimitry Andric case ISD::GET_FPMODE: { 48575f757f3fSDimitry Andric // Call fegetmode, which saves control modes into a stack slot. Then load 48585f757f3fSDimitry Andric // the value to return from the stack. 48595f757f3fSDimitry Andric EVT ModeVT = Node->getValueType(0); 48605f757f3fSDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(ModeVT); 48615f757f3fSDimitry Andric int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 48625f757f3fSDimitry Andric SDValue Chain = DAG.makeStateFunctionCall(RTLIB::FEGETMODE, StackPtr, 48635f757f3fSDimitry Andric Node->getOperand(0), dl); 48645f757f3fSDimitry Andric SDValue LdInst = DAG.getLoad( 48655f757f3fSDimitry Andric ModeVT, dl, Chain, StackPtr, 48665f757f3fSDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 48675f757f3fSDimitry Andric Results.push_back(LdInst); 48685f757f3fSDimitry Andric Results.push_back(LdInst.getValue(1)); 48695f757f3fSDimitry Andric break; 48705f757f3fSDimitry Andric } 48715f757f3fSDimitry Andric case ISD::SET_FPMODE: { 48725f757f3fSDimitry Andric // Move control modes to stack slot and then call fesetmode with the pointer 48735f757f3fSDimitry Andric // to the slot as argument. 48745f757f3fSDimitry Andric SDValue Mode = Node->getOperand(1); 48755f757f3fSDimitry Andric EVT ModeVT = Mode.getValueType(); 48765f757f3fSDimitry Andric SDValue StackPtr = DAG.CreateStackTemporary(ModeVT); 48775f757f3fSDimitry Andric int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); 48785f757f3fSDimitry Andric SDValue StInst = DAG.getStore( 48795f757f3fSDimitry Andric Node->getOperand(0), dl, Mode, StackPtr, 48805f757f3fSDimitry Andric MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI)); 48815f757f3fSDimitry Andric Results.push_back( 48825f757f3fSDimitry Andric DAG.makeStateFunctionCall(RTLIB::FESETMODE, StackPtr, StInst, dl)); 48835f757f3fSDimitry Andric break; 48845f757f3fSDimitry Andric } 48855f757f3fSDimitry Andric case ISD::RESET_FPMODE: { 48865f757f3fSDimitry Andric // It is legalized to a call 'fesetmode(FE_DFL_MODE)'. On most targets 48875f757f3fSDimitry Andric // FE_DFL_MODE is defined as '((const femode_t *) -1)' in glibc. If not, the 48885f757f3fSDimitry Andric // target must provide custom lowering. 48895f757f3fSDimitry Andric const DataLayout &DL = DAG.getDataLayout(); 48905f757f3fSDimitry Andric EVT PtrTy = TLI.getPointerTy(DL); 48915f757f3fSDimitry Andric SDValue Mode = DAG.getConstant(-1LL, dl, PtrTy); 48925f757f3fSDimitry Andric Results.push_back(DAG.makeStateFunctionCall(RTLIB::FESETMODE, Mode, 48935f757f3fSDimitry Andric Node->getOperand(0), dl)); 48945f757f3fSDimitry Andric break; 48955f757f3fSDimitry Andric } 48960b57cec5SDimitry Andric } 48970b57cec5SDimitry Andric 48980b57cec5SDimitry Andric // Replace the original node with the legalized result. 48990b57cec5SDimitry Andric if (!Results.empty()) { 49000b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully converted node to libcall\n"); 49010b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 49020b57cec5SDimitry Andric } else 49030b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not convert node to libcall\n"); 49040b57cec5SDimitry Andric } 49050b57cec5SDimitry Andric 49060b57cec5SDimitry Andric // Determine the vector type to use in place of an original scalar element when 49070b57cec5SDimitry Andric // promoting equally sized vectors. 49080b57cec5SDimitry Andric static MVT getPromotedVectorElementType(const TargetLowering &TLI, 49090b57cec5SDimitry Andric MVT EltVT, MVT NewEltVT) { 49100b57cec5SDimitry Andric unsigned OldEltsPerNewElt = EltVT.getSizeInBits() / NewEltVT.getSizeInBits(); 4911*cb14a3feSDimitry Andric MVT MidVT = OldEltsPerNewElt == 1 4912*cb14a3feSDimitry Andric ? NewEltVT 4913*cb14a3feSDimitry Andric : MVT::getVectorVT(NewEltVT, OldEltsPerNewElt); 49140b57cec5SDimitry Andric assert(TLI.isTypeLegal(MidVT) && "unexpected"); 49150b57cec5SDimitry Andric return MidVT; 49160b57cec5SDimitry Andric } 49170b57cec5SDimitry Andric 49180b57cec5SDimitry Andric void SelectionDAGLegalize::PromoteNode(SDNode *Node) { 49190b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Trying to promote node\n"); 49200b57cec5SDimitry Andric SmallVector<SDValue, 8> Results; 49210b57cec5SDimitry Andric MVT OVT = Node->getSimpleValueType(0); 49220b57cec5SDimitry Andric if (Node->getOpcode() == ISD::UINT_TO_FP || 49230b57cec5SDimitry Andric Node->getOpcode() == ISD::SINT_TO_FP || 49240b57cec5SDimitry Andric Node->getOpcode() == ISD::SETCC || 49250b57cec5SDimitry Andric Node->getOpcode() == ISD::EXTRACT_VECTOR_ELT || 49260b57cec5SDimitry Andric Node->getOpcode() == ISD::INSERT_VECTOR_ELT) { 49270b57cec5SDimitry Andric OVT = Node->getOperand(0).getSimpleValueType(); 49280b57cec5SDimitry Andric } 4929480093f4SDimitry Andric if (Node->getOpcode() == ISD::STRICT_UINT_TO_FP || 4930e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_SINT_TO_FP || 4931e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCC || 4932e8d8bef9SDimitry Andric Node->getOpcode() == ISD::STRICT_FSETCCS) 4933480093f4SDimitry Andric OVT = Node->getOperand(1).getSimpleValueType(); 4934fe6060f1SDimitry Andric if (Node->getOpcode() == ISD::BR_CC || 4935fe6060f1SDimitry Andric Node->getOpcode() == ISD::SELECT_CC) 49360b57cec5SDimitry Andric OVT = Node->getOperand(2).getSimpleValueType(); 49370b57cec5SDimitry Andric MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT); 49380b57cec5SDimitry Andric SDLoc dl(Node); 4939fe6060f1SDimitry Andric SDValue Tmp1, Tmp2, Tmp3, Tmp4; 49400b57cec5SDimitry Andric switch (Node->getOpcode()) { 49410b57cec5SDimitry Andric case ISD::CTTZ: 49420b57cec5SDimitry Andric case ISD::CTTZ_ZERO_UNDEF: 49430b57cec5SDimitry Andric case ISD::CTLZ: 49440b57cec5SDimitry Andric case ISD::CTLZ_ZERO_UNDEF: 49450b57cec5SDimitry Andric case ISD::CTPOP: 49465ffd83dbSDimitry Andric // Zero extend the argument unless its cttz, then use any_extend. 49475ffd83dbSDimitry Andric if (Node->getOpcode() == ISD::CTTZ || 49485ffd83dbSDimitry Andric Node->getOpcode() == ISD::CTTZ_ZERO_UNDEF) 49495ffd83dbSDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 49505ffd83dbSDimitry Andric else 49510b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 49525ffd83dbSDimitry Andric 49530b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTTZ) { 49540b57cec5SDimitry Andric // The count is the same in the promoted type except if the original 49550b57cec5SDimitry Andric // value was zero. This can be handled by setting the bit just off 49560b57cec5SDimitry Andric // the top of the original type. 49570b57cec5SDimitry Andric auto TopBit = APInt::getOneBitSet(NVT.getSizeInBits(), 49580b57cec5SDimitry Andric OVT.getSizeInBits()); 49590b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::OR, dl, NVT, Tmp1, 49600b57cec5SDimitry Andric DAG.getConstant(TopBit, dl, NVT)); 49610b57cec5SDimitry Andric } 49620b57cec5SDimitry Andric // Perform the larger operation. For CTPOP and CTTZ_ZERO_UNDEF, this is 49630b57cec5SDimitry Andric // already the correct result. 49640b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 49650b57cec5SDimitry Andric if (Node->getOpcode() == ISD::CTLZ || 49660b57cec5SDimitry Andric Node->getOpcode() == ISD::CTLZ_ZERO_UNDEF) { 49670b57cec5SDimitry Andric // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT)) 49680b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::SUB, dl, NVT, Tmp1, 49690b57cec5SDimitry Andric DAG.getConstant(NVT.getSizeInBits() - 49700b57cec5SDimitry Andric OVT.getSizeInBits(), dl, NVT)); 49710b57cec5SDimitry Andric } 49720b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 49730b57cec5SDimitry Andric break; 49740b57cec5SDimitry Andric case ISD::BITREVERSE: 49750b57cec5SDimitry Andric case ISD::BSWAP: { 49760b57cec5SDimitry Andric unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits(); 49770b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, Node->getOperand(0)); 49780b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 49790b57cec5SDimitry Andric Tmp1 = DAG.getNode( 49800b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 49810b57cec5SDimitry Andric DAG.getConstant(DiffBits, dl, 49820b57cec5SDimitry Andric TLI.getShiftAmountTy(NVT, DAG.getDataLayout()))); 49830b57cec5SDimitry Andric 49840b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 49850b57cec5SDimitry Andric break; 49860b57cec5SDimitry Andric } 49870b57cec5SDimitry Andric case ISD::FP_TO_UINT: 4988480093f4SDimitry Andric case ISD::STRICT_FP_TO_UINT: 49890b57cec5SDimitry Andric case ISD::FP_TO_SINT: 4990480093f4SDimitry Andric case ISD::STRICT_FP_TO_SINT: 4991480093f4SDimitry Andric PromoteLegalFP_TO_INT(Node, dl, Results); 49920b57cec5SDimitry Andric break; 4993e8d8bef9SDimitry Andric case ISD::FP_TO_UINT_SAT: 4994e8d8bef9SDimitry Andric case ISD::FP_TO_SINT_SAT: 4995e8d8bef9SDimitry Andric Results.push_back(PromoteLegalFP_TO_INT_SAT(Node, dl)); 4996e8d8bef9SDimitry Andric break; 49970b57cec5SDimitry Andric case ISD::UINT_TO_FP: 4998480093f4SDimitry Andric case ISD::STRICT_UINT_TO_FP: 49990b57cec5SDimitry Andric case ISD::SINT_TO_FP: 5000480093f4SDimitry Andric case ISD::STRICT_SINT_TO_FP: 5001480093f4SDimitry Andric PromoteLegalINT_TO_FP(Node, dl, Results); 50020b57cec5SDimitry Andric break; 50030b57cec5SDimitry Andric case ISD::VAARG: { 50040b57cec5SDimitry Andric SDValue Chain = Node->getOperand(0); // Get the chain. 50050b57cec5SDimitry Andric SDValue Ptr = Node->getOperand(1); // Get the pointer. 50060b57cec5SDimitry Andric 50070b57cec5SDimitry Andric unsigned TruncOp; 50080b57cec5SDimitry Andric if (OVT.isVector()) { 50090b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 50100b57cec5SDimitry Andric } else { 50110b57cec5SDimitry Andric assert(OVT.isInteger() 50120b57cec5SDimitry Andric && "VAARG promotion is supported only for vectors or integer types"); 50130b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 50140b57cec5SDimitry Andric } 50150b57cec5SDimitry Andric 50160b57cec5SDimitry Andric // Perform the larger operation, then convert back 50170b57cec5SDimitry Andric Tmp1 = DAG.getVAArg(NVT, dl, Chain, Ptr, Node->getOperand(2), 50180b57cec5SDimitry Andric Node->getConstantOperandVal(3)); 50190b57cec5SDimitry Andric Chain = Tmp1.getValue(1); 50200b57cec5SDimitry Andric 50210b57cec5SDimitry Andric Tmp2 = DAG.getNode(TruncOp, dl, OVT, Tmp1); 50220b57cec5SDimitry Andric 50230b57cec5SDimitry Andric // Modified the chain result - switch anything that used the old chain to 50240b57cec5SDimitry Andric // use the new one. 50250b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), Tmp2); 50260b57cec5SDimitry Andric DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), Chain); 50270b57cec5SDimitry Andric if (UpdatedNodes) { 50280b57cec5SDimitry Andric UpdatedNodes->insert(Tmp2.getNode()); 50290b57cec5SDimitry Andric UpdatedNodes->insert(Chain.getNode()); 50300b57cec5SDimitry Andric } 50310b57cec5SDimitry Andric ReplacedNode(Node); 50320b57cec5SDimitry Andric break; 50330b57cec5SDimitry Andric } 50340b57cec5SDimitry Andric case ISD::MUL: 50350b57cec5SDimitry Andric case ISD::SDIV: 50360b57cec5SDimitry Andric case ISD::SREM: 50370b57cec5SDimitry Andric case ISD::UDIV: 50380b57cec5SDimitry Andric case ISD::UREM: 50395f757f3fSDimitry Andric case ISD::SMIN: 50405f757f3fSDimitry Andric case ISD::SMAX: 50415f757f3fSDimitry Andric case ISD::UMIN: 50425f757f3fSDimitry Andric case ISD::UMAX: 50430b57cec5SDimitry Andric case ISD::AND: 50440b57cec5SDimitry Andric case ISD::OR: 50450b57cec5SDimitry Andric case ISD::XOR: { 50460b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 50470b57cec5SDimitry Andric if (OVT.isVector()) { 50480b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 50490b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 50500b57cec5SDimitry Andric } else { 50510b57cec5SDimitry Andric assert(OVT.isInteger() && "Cannot promote logic operation"); 50520b57cec5SDimitry Andric 50530b57cec5SDimitry Andric switch (Node->getOpcode()) { 50540b57cec5SDimitry Andric default: 50550b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 50560b57cec5SDimitry Andric break; 50570b57cec5SDimitry Andric case ISD::SDIV: 50580b57cec5SDimitry Andric case ISD::SREM: 50595f757f3fSDimitry Andric case ISD::SMIN: 50605f757f3fSDimitry Andric case ISD::SMAX: 50610b57cec5SDimitry Andric ExtOp = ISD::SIGN_EXTEND; 50620b57cec5SDimitry Andric break; 50630b57cec5SDimitry Andric case ISD::UDIV: 50640b57cec5SDimitry Andric case ISD::UREM: 50650b57cec5SDimitry Andric ExtOp = ISD::ZERO_EXTEND; 50660b57cec5SDimitry Andric break; 50675f757f3fSDimitry Andric case ISD::UMIN: 50685f757f3fSDimitry Andric case ISD::UMAX: 50695f757f3fSDimitry Andric if (TLI.isSExtCheaperThanZExt(OVT, NVT)) 50705f757f3fSDimitry Andric ExtOp = ISD::SIGN_EXTEND; 50715f757f3fSDimitry Andric else 50725f757f3fSDimitry Andric ExtOp = ISD::ZERO_EXTEND; 50735f757f3fSDimitry Andric break; 50740b57cec5SDimitry Andric } 50750b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 50760b57cec5SDimitry Andric } 50770b57cec5SDimitry Andric // Promote each of the values to the new type. 50780b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 50790b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 50800b57cec5SDimitry Andric // Perform the larger operation, then convert back 50810b57cec5SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 50820b57cec5SDimitry Andric Results.push_back(DAG.getNode(TruncOp, dl, OVT, Tmp1)); 50830b57cec5SDimitry Andric break; 50840b57cec5SDimitry Andric } 50850b57cec5SDimitry Andric case ISD::UMUL_LOHI: 50860b57cec5SDimitry Andric case ISD::SMUL_LOHI: { 50870b57cec5SDimitry Andric // Promote to a multiply in a wider integer type. 50880b57cec5SDimitry Andric unsigned ExtOp = Node->getOpcode() == ISD::UMUL_LOHI ? ISD::ZERO_EXTEND 50890b57cec5SDimitry Andric : ISD::SIGN_EXTEND; 50900b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 50910b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 50920b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::MUL, dl, NVT, Tmp1, Tmp2); 50930b57cec5SDimitry Andric 50940b57cec5SDimitry Andric auto &DL = DAG.getDataLayout(); 50950b57cec5SDimitry Andric unsigned OriginalSize = OVT.getScalarSizeInBits(); 50960b57cec5SDimitry Andric Tmp2 = DAG.getNode( 50970b57cec5SDimitry Andric ISD::SRL, dl, NVT, Tmp1, 50980b57cec5SDimitry Andric DAG.getConstant(OriginalSize, dl, TLI.getScalarShiftAmountTy(DL, NVT))); 50990b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1)); 51000b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2)); 51010b57cec5SDimitry Andric break; 51020b57cec5SDimitry Andric } 51030b57cec5SDimitry Andric case ISD::SELECT: { 51040b57cec5SDimitry Andric unsigned ExtOp, TruncOp; 51050b57cec5SDimitry Andric if (Node->getValueType(0).isVector() || 51060b57cec5SDimitry Andric Node->getValueType(0).getSizeInBits() == NVT.getSizeInBits()) { 51070b57cec5SDimitry Andric ExtOp = ISD::BITCAST; 51080b57cec5SDimitry Andric TruncOp = ISD::BITCAST; 51090b57cec5SDimitry Andric } else if (Node->getValueType(0).isInteger()) { 51100b57cec5SDimitry Andric ExtOp = ISD::ANY_EXTEND; 51110b57cec5SDimitry Andric TruncOp = ISD::TRUNCATE; 51120b57cec5SDimitry Andric } else { 51130b57cec5SDimitry Andric ExtOp = ISD::FP_EXTEND; 51140b57cec5SDimitry Andric TruncOp = ISD::FP_ROUND; 51150b57cec5SDimitry Andric } 51160b57cec5SDimitry Andric Tmp1 = Node->getOperand(0); 51170b57cec5SDimitry Andric // Promote each of the values to the new type. 51180b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 51190b57cec5SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 51200b57cec5SDimitry Andric // Perform the larger operation, then round down. 51210b57cec5SDimitry Andric Tmp1 = DAG.getSelect(dl, NVT, Tmp1, Tmp2, Tmp3); 51220b57cec5SDimitry Andric Tmp1->setFlags(Node->getFlags()); 51230b57cec5SDimitry Andric if (TruncOp != ISD::FP_ROUND) 51240b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1); 51250b57cec5SDimitry Andric else 51260b57cec5SDimitry Andric Tmp1 = DAG.getNode(TruncOp, dl, Node->getValueType(0), Tmp1, 51270b57cec5SDimitry Andric DAG.getIntPtrConstant(0, dl)); 51280b57cec5SDimitry Andric Results.push_back(Tmp1); 51290b57cec5SDimitry Andric break; 51300b57cec5SDimitry Andric } 51310b57cec5SDimitry Andric case ISD::VECTOR_SHUFFLE: { 51320b57cec5SDimitry Andric ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Node)->getMask(); 51330b57cec5SDimitry Andric 51340b57cec5SDimitry Andric // Cast the two input vectors. 51350b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(0)); 51360b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::BITCAST, dl, NVT, Node->getOperand(1)); 51370b57cec5SDimitry Andric 51380b57cec5SDimitry Andric // Convert the shuffle mask to the right # elements. 51390b57cec5SDimitry Andric Tmp1 = ShuffleWithNarrowerEltType(NVT, OVT, dl, Tmp1, Tmp2, Mask); 51400b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::BITCAST, dl, OVT, Tmp1); 51410b57cec5SDimitry Andric Results.push_back(Tmp1); 51420b57cec5SDimitry Andric break; 51430b57cec5SDimitry Andric } 5144fe6060f1SDimitry Andric case ISD::VECTOR_SPLICE: { 5145fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(0)); 5146fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ISD::ANY_EXTEND, dl, NVT, Node->getOperand(1)); 5147fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ISD::VECTOR_SPLICE, dl, NVT, Tmp1, Tmp2, 5148fe6060f1SDimitry Andric Node->getOperand(2)); 5149fe6060f1SDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp3)); 5150fe6060f1SDimitry Andric break; 5151fe6060f1SDimitry Andric } 5152fe6060f1SDimitry Andric case ISD::SELECT_CC: { 5153fe6060f1SDimitry Andric SDValue Cond = Node->getOperand(4); 5154fe6060f1SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Cond)->get(); 5155fe6060f1SDimitry Andric // Type of the comparison operands. 5156fe6060f1SDimitry Andric MVT CVT = Node->getSimpleValueType(0); 5157fe6060f1SDimitry Andric assert(CVT == OVT && "not handled"); 5158fe6060f1SDimitry Andric 5159fe6060f1SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 5160fe6060f1SDimitry Andric if (NVT.isInteger()) { 5161fe6060f1SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 5162fe6060f1SDimitry Andric } 5163fe6060f1SDimitry Andric 5164fe6060f1SDimitry Andric // Promote the comparison operands, if needed. 5165fe6060f1SDimitry Andric if (TLI.isCondCodeLegal(CCCode, CVT)) { 5166fe6060f1SDimitry Andric Tmp1 = Node->getOperand(0); 5167fe6060f1SDimitry Andric Tmp2 = Node->getOperand(1); 5168fe6060f1SDimitry Andric } else { 5169fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 5170fe6060f1SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 5171fe6060f1SDimitry Andric } 5172fe6060f1SDimitry Andric // Cast the true/false operands. 5173fe6060f1SDimitry Andric Tmp3 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 5174fe6060f1SDimitry Andric Tmp4 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 5175fe6060f1SDimitry Andric 5176fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::SELECT_CC, dl, NVT, {Tmp1, Tmp2, Tmp3, Tmp4, Cond}, 5177fe6060f1SDimitry Andric Node->getFlags()); 5178fe6060f1SDimitry Andric 5179fe6060f1SDimitry Andric // Cast the result back to the original type. 5180fe6060f1SDimitry Andric if (ExtOp != ISD::FP_EXTEND) 5181fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp1); 5182fe6060f1SDimitry Andric else 5183fe6060f1SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp1, 5184bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true)); 5185fe6060f1SDimitry Andric 5186fe6060f1SDimitry Andric Results.push_back(Tmp1); 5187fe6060f1SDimitry Andric break; 5188fe6060f1SDimitry Andric } 5189e8d8bef9SDimitry Andric case ISD::SETCC: 5190e8d8bef9SDimitry Andric case ISD::STRICT_FSETCC: 5191e8d8bef9SDimitry Andric case ISD::STRICT_FSETCCS: { 51920b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 51930b57cec5SDimitry Andric if (NVT.isInteger()) { 5194e8d8bef9SDimitry Andric ISD::CondCode CCCode = cast<CondCodeSDNode>(Node->getOperand(2))->get(); 51955f757f3fSDimitry Andric if (isSignedIntSetCC(CCCode) || 51965f757f3fSDimitry Andric TLI.isSExtCheaperThanZExt(Node->getOperand(0).getValueType(), NVT)) 51975f757f3fSDimitry Andric ExtOp = ISD::SIGN_EXTEND; 51985f757f3fSDimitry Andric else 51995f757f3fSDimitry Andric ExtOp = ISD::ZERO_EXTEND; 52000b57cec5SDimitry Andric } 5201e8d8bef9SDimitry Andric if (Node->isStrictFPOpcode()) { 5202e8d8bef9SDimitry Andric SDValue InChain = Node->getOperand(0); 5203e8d8bef9SDimitry Andric std::tie(Tmp1, std::ignore) = 5204e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(1), InChain, dl, NVT); 5205e8d8bef9SDimitry Andric std::tie(Tmp2, std::ignore) = 5206e8d8bef9SDimitry Andric DAG.getStrictFPExtendOrRound(Node->getOperand(2), InChain, dl, NVT); 5207e8d8bef9SDimitry Andric SmallVector<SDValue, 2> TmpChains = {Tmp1.getValue(1), Tmp2.getValue(1)}; 5208e8d8bef9SDimitry Andric SDValue OutChain = DAG.getTokenFactor(dl, TmpChains); 5209e8d8bef9SDimitry Andric SDVTList VTs = DAG.getVTList(Node->getValueType(0), MVT::Other); 5210e8d8bef9SDimitry Andric Results.push_back(DAG.getNode(Node->getOpcode(), dl, VTs, 5211e8d8bef9SDimitry Andric {OutChain, Tmp1, Tmp2, Node->getOperand(3)}, 5212e8d8bef9SDimitry Andric Node->getFlags())); 5213e8d8bef9SDimitry Andric Results.push_back(Results.back().getValue(1)); 5214e8d8bef9SDimitry Andric break; 5215e8d8bef9SDimitry Andric } 52160b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(0)); 52170b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(1)); 52180b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::SETCC, dl, Node->getValueType(0), Tmp1, 52190b57cec5SDimitry Andric Tmp2, Node->getOperand(2), Node->getFlags())); 52200b57cec5SDimitry Andric break; 52210b57cec5SDimitry Andric } 52220b57cec5SDimitry Andric case ISD::BR_CC: { 52230b57cec5SDimitry Andric unsigned ExtOp = ISD::FP_EXTEND; 52240b57cec5SDimitry Andric if (NVT.isInteger()) { 52250b57cec5SDimitry Andric ISD::CondCode CCCode = 52260b57cec5SDimitry Andric cast<CondCodeSDNode>(Node->getOperand(1))->get(); 52270b57cec5SDimitry Andric ExtOp = isSignedIntSetCC(CCCode) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; 52280b57cec5SDimitry Andric } 52290b57cec5SDimitry Andric Tmp1 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(2)); 52300b57cec5SDimitry Andric Tmp2 = DAG.getNode(ExtOp, dl, NVT, Node->getOperand(3)); 52310b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BR_CC, dl, Node->getValueType(0), 52320b57cec5SDimitry Andric Node->getOperand(0), Node->getOperand(1), 52330b57cec5SDimitry Andric Tmp1, Tmp2, Node->getOperand(4))); 52340b57cec5SDimitry Andric break; 52350b57cec5SDimitry Andric } 52360b57cec5SDimitry Andric case ISD::FADD: 52370b57cec5SDimitry Andric case ISD::FSUB: 52380b57cec5SDimitry Andric case ISD::FMUL: 52390b57cec5SDimitry Andric case ISD::FDIV: 52400b57cec5SDimitry Andric case ISD::FREM: 52410b57cec5SDimitry Andric case ISD::FMINNUM: 52420b57cec5SDimitry Andric case ISD::FMAXNUM: 524306c3fb27SDimitry Andric case ISD::FMINIMUM: 524406c3fb27SDimitry Andric case ISD::FMAXIMUM: 52450b57cec5SDimitry Andric case ISD::FPOW: 52460b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 52470b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 52480b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, 52490b57cec5SDimitry Andric Node->getFlags()); 5250bdd1243dSDimitry Andric Results.push_back( 5251bdd1243dSDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3, 5252bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true))); 52530b57cec5SDimitry Andric break; 525481ad6265SDimitry Andric case ISD::STRICT_FADD: 525581ad6265SDimitry Andric case ISD::STRICT_FSUB: 525681ad6265SDimitry Andric case ISD::STRICT_FMUL: 525781ad6265SDimitry Andric case ISD::STRICT_FDIV: 525881ad6265SDimitry Andric case ISD::STRICT_FMINNUM: 525981ad6265SDimitry Andric case ISD::STRICT_FMAXNUM: 5260480093f4SDimitry Andric case ISD::STRICT_FREM: 5261480093f4SDimitry Andric case ISD::STRICT_FPOW: 5262480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 5263480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 5264480093f4SDimitry Andric Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 5265480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(2)}); 5266480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1), 5267480093f4SDimitry Andric Tmp2.getValue(1)); 5268480093f4SDimitry Andric Tmp1 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 5269480093f4SDimitry Andric {Tmp3, Tmp1, Tmp2}); 5270480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 5271480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1, DAG.getIntPtrConstant(0, dl)}); 5272480093f4SDimitry Andric Results.push_back(Tmp1); 5273480093f4SDimitry Andric Results.push_back(Tmp1.getValue(1)); 5274480093f4SDimitry Andric break; 52750b57cec5SDimitry Andric case ISD::FMA: 52760b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 52770b57cec5SDimitry Andric Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1)); 52780b57cec5SDimitry Andric Tmp3 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(2)); 52790b57cec5SDimitry Andric Results.push_back( 52800b57cec5SDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, 52810b57cec5SDimitry Andric DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2, Tmp3), 5282bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true))); 52830b57cec5SDimitry Andric break; 528481ad6265SDimitry Andric case ISD::STRICT_FMA: 528581ad6265SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 528681ad6265SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 528781ad6265SDimitry Andric Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 528881ad6265SDimitry Andric {Node->getOperand(0), Node->getOperand(2)}); 528981ad6265SDimitry Andric Tmp3 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 529081ad6265SDimitry Andric {Node->getOperand(0), Node->getOperand(3)}); 529181ad6265SDimitry Andric Tmp4 = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Tmp1.getValue(1), 529281ad6265SDimitry Andric Tmp2.getValue(1), Tmp3.getValue(1)); 529381ad6265SDimitry Andric Tmp4 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 529481ad6265SDimitry Andric {Tmp4, Tmp1, Tmp2, Tmp3}); 529581ad6265SDimitry Andric Tmp4 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 529681ad6265SDimitry Andric {Tmp4.getValue(1), Tmp4, DAG.getIntPtrConstant(0, dl)}); 529781ad6265SDimitry Andric Results.push_back(Tmp4); 529881ad6265SDimitry Andric Results.push_back(Tmp4.getValue(1)); 529981ad6265SDimitry Andric break; 53000b57cec5SDimitry Andric case ISD::FCOPYSIGN: 530106c3fb27SDimitry Andric case ISD::FLDEXP: 53020b57cec5SDimitry Andric case ISD::FPOWI: { 53030b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 53040b57cec5SDimitry Andric Tmp2 = Node->getOperand(1); 53050b57cec5SDimitry Andric Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2); 53060b57cec5SDimitry Andric 53070b57cec5SDimitry Andric // fcopysign doesn't change anything but the sign bit, so 53080b57cec5SDimitry Andric // (fp_round (fcopysign (fpext a), b)) 53090b57cec5SDimitry Andric // is as precise as 53100b57cec5SDimitry Andric // (fp_round (fpext a)) 53110b57cec5SDimitry Andric // which is a no-op. Mark it as a TRUNCating FP_ROUND. 53120b57cec5SDimitry Andric const bool isTrunc = (Node->getOpcode() == ISD::FCOPYSIGN); 5313bdd1243dSDimitry Andric Results.push_back( 5314bdd1243dSDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp3, 5315bdd1243dSDimitry Andric DAG.getIntPtrConstant(isTrunc, dl, /*isTarget=*/true))); 53160b57cec5SDimitry Andric break; 53170b57cec5SDimitry Andric } 531881ad6265SDimitry Andric case ISD::STRICT_FPOWI: 531981ad6265SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 532081ad6265SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 532181ad6265SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 532281ad6265SDimitry Andric {Tmp1.getValue(1), Tmp1, Node->getOperand(2)}); 532381ad6265SDimitry Andric Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 532481ad6265SDimitry Andric {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)}); 532581ad6265SDimitry Andric Results.push_back(Tmp3); 532681ad6265SDimitry Andric Results.push_back(Tmp3.getValue(1)); 532781ad6265SDimitry Andric break; 532806c3fb27SDimitry Andric case ISD::FFREXP: { 532906c3fb27SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 533006c3fb27SDimitry Andric Tmp2 = DAG.getNode(ISD::FFREXP, dl, {NVT, Node->getValueType(1)}, Tmp1); 533106c3fb27SDimitry Andric 533206c3fb27SDimitry Andric Results.push_back( 533306c3fb27SDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2, 533406c3fb27SDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true))); 533506c3fb27SDimitry Andric 533606c3fb27SDimitry Andric Results.push_back(Tmp2.getValue(1)); 533706c3fb27SDimitry Andric break; 533806c3fb27SDimitry Andric } 53390b57cec5SDimitry Andric case ISD::FFLOOR: 53400b57cec5SDimitry Andric case ISD::FCEIL: 53410b57cec5SDimitry Andric case ISD::FRINT: 53420b57cec5SDimitry Andric case ISD::FNEARBYINT: 53430b57cec5SDimitry Andric case ISD::FROUND: 53445ffd83dbSDimitry Andric case ISD::FROUNDEVEN: 53450b57cec5SDimitry Andric case ISD::FTRUNC: 53460b57cec5SDimitry Andric case ISD::FNEG: 53470b57cec5SDimitry Andric case ISD::FSQRT: 53480b57cec5SDimitry Andric case ISD::FSIN: 53490b57cec5SDimitry Andric case ISD::FCOS: 53500b57cec5SDimitry Andric case ISD::FLOG: 53510b57cec5SDimitry Andric case ISD::FLOG2: 53520b57cec5SDimitry Andric case ISD::FLOG10: 53530b57cec5SDimitry Andric case ISD::FABS: 53540b57cec5SDimitry Andric case ISD::FEXP: 53550b57cec5SDimitry Andric case ISD::FEXP2: 53565f757f3fSDimitry Andric case ISD::FEXP10: 5357*cb14a3feSDimitry Andric case ISD::FCANONICALIZE: 53580b57cec5SDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0)); 53590b57cec5SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 5360bdd1243dSDimitry Andric Results.push_back( 5361bdd1243dSDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2, 5362bdd1243dSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true))); 53630b57cec5SDimitry Andric break; 5364480093f4SDimitry Andric case ISD::STRICT_FFLOOR: 5365480093f4SDimitry Andric case ISD::STRICT_FCEIL: 536681ad6265SDimitry Andric case ISD::STRICT_FRINT: 536781ad6265SDimitry Andric case ISD::STRICT_FNEARBYINT: 5368349cc55cSDimitry Andric case ISD::STRICT_FROUND: 536981ad6265SDimitry Andric case ISD::STRICT_FROUNDEVEN: 537081ad6265SDimitry Andric case ISD::STRICT_FTRUNC: 537181ad6265SDimitry Andric case ISD::STRICT_FSQRT: 5372480093f4SDimitry Andric case ISD::STRICT_FSIN: 5373480093f4SDimitry Andric case ISD::STRICT_FCOS: 5374480093f4SDimitry Andric case ISD::STRICT_FLOG: 537581ad6265SDimitry Andric case ISD::STRICT_FLOG2: 5376480093f4SDimitry Andric case ISD::STRICT_FLOG10: 5377480093f4SDimitry Andric case ISD::STRICT_FEXP: 537881ad6265SDimitry Andric case ISD::STRICT_FEXP2: 5379480093f4SDimitry Andric Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other}, 5380480093f4SDimitry Andric {Node->getOperand(0), Node->getOperand(1)}); 5381480093f4SDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, {NVT, MVT::Other}, 5382480093f4SDimitry Andric {Tmp1.getValue(1), Tmp1}); 5383480093f4SDimitry Andric Tmp3 = DAG.getNode(ISD::STRICT_FP_ROUND, dl, {OVT, MVT::Other}, 5384480093f4SDimitry Andric {Tmp2.getValue(1), Tmp2, DAG.getIntPtrConstant(0, dl)}); 5385480093f4SDimitry Andric Results.push_back(Tmp3); 5386480093f4SDimitry Andric Results.push_back(Tmp3.getValue(1)); 5387480093f4SDimitry Andric break; 53880b57cec5SDimitry Andric case ISD::BUILD_VECTOR: { 53890b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 53900b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 53910b57cec5SDimitry Andric 53920b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 53930b57cec5SDimitry Andric // 53940b57cec5SDimitry Andric // e.g. v2i64 = build_vector i64:x, i64:y => v4i32 53950b57cec5SDimitry Andric // => 53960b57cec5SDimitry Andric // v4i32 = concat_vectors (v2i32 (bitcast i64:x)), (v2i32 (bitcast i64:y)) 53970b57cec5SDimitry Andric 53980b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 53990b57cec5SDimitry Andric "Invalid promote type for build_vector"); 5400*cb14a3feSDimitry Andric assert(NewEltVT.bitsLE(EltVT) && "not handled"); 54010b57cec5SDimitry Andric 54020b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 54030b57cec5SDimitry Andric 54040b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 54050b57cec5SDimitry Andric for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) { 54060b57cec5SDimitry Andric SDValue Op = Node->getOperand(I); 54070b57cec5SDimitry Andric NewOps.push_back(DAG.getNode(ISD::BITCAST, SDLoc(Op), MidVT, Op)); 54080b57cec5SDimitry Andric } 54090b57cec5SDimitry Andric 54100b57cec5SDimitry Andric SDLoc SL(Node); 5411*cb14a3feSDimitry Andric SDValue Concat = 5412*cb14a3feSDimitry Andric DAG.getNode(MidVT == NewEltVT ? ISD::BUILD_VECTOR : ISD::CONCAT_VECTORS, 5413*cb14a3feSDimitry Andric SL, NVT, NewOps); 54140b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 54150b57cec5SDimitry Andric Results.push_back(CvtVec); 54160b57cec5SDimitry Andric break; 54170b57cec5SDimitry Andric } 54180b57cec5SDimitry Andric case ISD::EXTRACT_VECTOR_ELT: { 54190b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 54200b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 54210b57cec5SDimitry Andric 54220b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size. 54230b57cec5SDimitry Andric // 54240b57cec5SDimitry Andric // e.g. v2i64 = extract_vector_elt x:v2i64, y:i32 54250b57cec5SDimitry Andric // => 54260b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 54270b57cec5SDimitry Andric // 54280b57cec5SDimitry Andric // i64 = bitcast 54290b57cec5SDimitry Andric // (v2i32 build_vector (i32 (extract_vector_elt castx, (2 * y))), 54300b57cec5SDimitry Andric // (i32 (extract_vector_elt castx, (2 * y + 1))) 54310b57cec5SDimitry Andric // 54320b57cec5SDimitry Andric 54330b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 54340b57cec5SDimitry Andric "Invalid promote type for extract_vector_elt"); 54350b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 54360b57cec5SDimitry Andric 54370b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 54380b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 54390b57cec5SDimitry Andric 54400b57cec5SDimitry Andric SDValue Idx = Node->getOperand(1); 54410b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 54420b57cec5SDimitry Andric SDLoc SL(Node); 54430b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SL, IdxVT); 54440b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 54450b57cec5SDimitry Andric 54460b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 54470b57cec5SDimitry Andric 54480b57cec5SDimitry Andric SmallVector<SDValue, 8> NewOps; 54490b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 54500b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 54510b57cec5SDimitry Andric SDValue TmpIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 54520b57cec5SDimitry Andric 54530b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 54540b57cec5SDimitry Andric CastVec, TmpIdx); 54550b57cec5SDimitry Andric NewOps.push_back(Elt); 54560b57cec5SDimitry Andric } 54570b57cec5SDimitry Andric 54580b57cec5SDimitry Andric SDValue NewVec = DAG.getBuildVector(MidVT, SL, NewOps); 54590b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, EltVT, NewVec)); 54600b57cec5SDimitry Andric break; 54610b57cec5SDimitry Andric } 54620b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: { 54630b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 54640b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 54650b57cec5SDimitry Andric 54660b57cec5SDimitry Andric // Handle bitcasts to a different vector type with the same total bit size 54670b57cec5SDimitry Andric // 54680b57cec5SDimitry Andric // e.g. v2i64 = insert_vector_elt x:v2i64, y:i64, z:i32 54690b57cec5SDimitry Andric // => 54700b57cec5SDimitry Andric // v4i32:castx = bitcast x:v2i64 54710b57cec5SDimitry Andric // v2i32:casty = bitcast y:i64 54720b57cec5SDimitry Andric // 54730b57cec5SDimitry Andric // v2i64 = bitcast 54740b57cec5SDimitry Andric // (v4i32 insert_vector_elt 54750b57cec5SDimitry Andric // (v4i32 insert_vector_elt v4i32:castx, 54760b57cec5SDimitry Andric // (extract_vector_elt casty, 0), 2 * z), 54770b57cec5SDimitry Andric // (extract_vector_elt casty, 1), (2 * z + 1)) 54780b57cec5SDimitry Andric 54790b57cec5SDimitry Andric assert(NVT.isVector() && OVT.getSizeInBits() == NVT.getSizeInBits() && 54800b57cec5SDimitry Andric "Invalid promote type for insert_vector_elt"); 54810b57cec5SDimitry Andric assert(NewEltVT.bitsLT(EltVT) && "not handled"); 54820b57cec5SDimitry Andric 54830b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 54840b57cec5SDimitry Andric unsigned NewEltsPerOldElt = MidVT.getVectorNumElements(); 54850b57cec5SDimitry Andric 54860b57cec5SDimitry Andric SDValue Val = Node->getOperand(1); 54870b57cec5SDimitry Andric SDValue Idx = Node->getOperand(2); 54880b57cec5SDimitry Andric EVT IdxVT = Idx.getValueType(); 54890b57cec5SDimitry Andric SDLoc SL(Node); 54900b57cec5SDimitry Andric 54910b57cec5SDimitry Andric SDValue Factor = DAG.getConstant(NewEltsPerOldElt, SDLoc(), IdxVT); 54920b57cec5SDimitry Andric SDValue NewBaseIdx = DAG.getNode(ISD::MUL, SL, IdxVT, Idx, Factor); 54930b57cec5SDimitry Andric 54940b57cec5SDimitry Andric SDValue CastVec = DAG.getNode(ISD::BITCAST, SL, NVT, Node->getOperand(0)); 54950b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 54960b57cec5SDimitry Andric 54970b57cec5SDimitry Andric SDValue NewVec = CastVec; 54980b57cec5SDimitry Andric for (unsigned I = 0; I < NewEltsPerOldElt; ++I) { 54990b57cec5SDimitry Andric SDValue IdxOffset = DAG.getConstant(I, SL, IdxVT); 55000b57cec5SDimitry Andric SDValue InEltIdx = DAG.getNode(ISD::ADD, SL, IdxVT, NewBaseIdx, IdxOffset); 55010b57cec5SDimitry Andric 55020b57cec5SDimitry Andric SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, NewEltVT, 55030b57cec5SDimitry Andric CastVal, IdxOffset); 55040b57cec5SDimitry Andric 55050b57cec5SDimitry Andric NewVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, SL, NVT, 55060b57cec5SDimitry Andric NewVec, Elt, InEltIdx); 55070b57cec5SDimitry Andric } 55080b57cec5SDimitry Andric 55090b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewVec)); 55100b57cec5SDimitry Andric break; 55110b57cec5SDimitry Andric } 55120b57cec5SDimitry Andric case ISD::SCALAR_TO_VECTOR: { 55130b57cec5SDimitry Andric MVT EltVT = OVT.getVectorElementType(); 55140b57cec5SDimitry Andric MVT NewEltVT = NVT.getVectorElementType(); 55150b57cec5SDimitry Andric 55160b57cec5SDimitry Andric // Handle bitcasts to different vector type with the same total bit size. 55170b57cec5SDimitry Andric // 55180b57cec5SDimitry Andric // e.g. v2i64 = scalar_to_vector x:i64 55190b57cec5SDimitry Andric // => 55200b57cec5SDimitry Andric // concat_vectors (v2i32 bitcast x:i64), (v2i32 undef) 55210b57cec5SDimitry Andric // 55220b57cec5SDimitry Andric 55230b57cec5SDimitry Andric MVT MidVT = getPromotedVectorElementType(TLI, EltVT, NewEltVT); 55240b57cec5SDimitry Andric SDValue Val = Node->getOperand(0); 55250b57cec5SDimitry Andric SDLoc SL(Node); 55260b57cec5SDimitry Andric 55270b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, MidVT, Val); 55280b57cec5SDimitry Andric SDValue Undef = DAG.getUNDEF(MidVT); 55290b57cec5SDimitry Andric 55300b57cec5SDimitry Andric SmallVector<SDValue, 8> NewElts; 55310b57cec5SDimitry Andric NewElts.push_back(CastVal); 55320b57cec5SDimitry Andric for (unsigned I = 1, NElts = OVT.getVectorNumElements(); I != NElts; ++I) 55330b57cec5SDimitry Andric NewElts.push_back(Undef); 55340b57cec5SDimitry Andric 55350b57cec5SDimitry Andric SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, SL, NVT, NewElts); 55360b57cec5SDimitry Andric SDValue CvtVec = DAG.getNode(ISD::BITCAST, SL, OVT, Concat); 55370b57cec5SDimitry Andric Results.push_back(CvtVec); 55380b57cec5SDimitry Andric break; 55390b57cec5SDimitry Andric } 55400b57cec5SDimitry Andric case ISD::ATOMIC_SWAP: { 55410b57cec5SDimitry Andric AtomicSDNode *AM = cast<AtomicSDNode>(Node); 55420b57cec5SDimitry Andric SDLoc SL(Node); 55430b57cec5SDimitry Andric SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NVT, AM->getVal()); 55440b57cec5SDimitry Andric assert(NVT.getSizeInBits() == OVT.getSizeInBits() && 55450b57cec5SDimitry Andric "unexpected promotion type"); 55460b57cec5SDimitry Andric assert(AM->getMemoryVT().getSizeInBits() == NVT.getSizeInBits() && 55470b57cec5SDimitry Andric "unexpected atomic_swap with illegal type"); 55480b57cec5SDimitry Andric 55490b57cec5SDimitry Andric SDValue NewAtomic 55500b57cec5SDimitry Andric = DAG.getAtomic(ISD::ATOMIC_SWAP, SL, NVT, 55510b57cec5SDimitry Andric DAG.getVTList(NVT, MVT::Other), 55520b57cec5SDimitry Andric { AM->getChain(), AM->getBasePtr(), CastVal }, 55530b57cec5SDimitry Andric AM->getMemOperand()); 55540b57cec5SDimitry Andric Results.push_back(DAG.getNode(ISD::BITCAST, SL, OVT, NewAtomic)); 55550b57cec5SDimitry Andric Results.push_back(NewAtomic.getValue(1)); 55560b57cec5SDimitry Andric break; 55570b57cec5SDimitry Andric } 55585f757f3fSDimitry Andric case ISD::SPLAT_VECTOR: { 55595f757f3fSDimitry Andric SDValue Scalar = Node->getOperand(0); 55605f757f3fSDimitry Andric MVT ScalarType = Scalar.getSimpleValueType(); 55615f757f3fSDimitry Andric MVT NewScalarType = NVT.getVectorElementType(); 55625f757f3fSDimitry Andric if (ScalarType.isInteger()) { 55635f757f3fSDimitry Andric Tmp1 = DAG.getNode(ISD::ANY_EXTEND, dl, NewScalarType, Scalar); 55645f757f3fSDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 55655f757f3fSDimitry Andric Results.push_back(DAG.getNode(ISD::TRUNCATE, dl, OVT, Tmp2)); 55665f757f3fSDimitry Andric break; 55675f757f3fSDimitry Andric } 55685f757f3fSDimitry Andric Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NewScalarType, Scalar); 55695f757f3fSDimitry Andric Tmp2 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1); 55705f757f3fSDimitry Andric Results.push_back( 55715f757f3fSDimitry Andric DAG.getNode(ISD::FP_ROUND, dl, OVT, Tmp2, 55725f757f3fSDimitry Andric DAG.getIntPtrConstant(0, dl, /*isTarget=*/true))); 55735f757f3fSDimitry Andric break; 55745f757f3fSDimitry Andric } 55750b57cec5SDimitry Andric } 55760b57cec5SDimitry Andric 55770b57cec5SDimitry Andric // Replace the original node with the legalized result. 55780b57cec5SDimitry Andric if (!Results.empty()) { 55790b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Successfully promoted node\n"); 55800b57cec5SDimitry Andric ReplaceNode(Node, Results.data()); 55810b57cec5SDimitry Andric } else 55820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Could not promote node\n"); 55830b57cec5SDimitry Andric } 55840b57cec5SDimitry Andric 55850b57cec5SDimitry Andric /// This is the entry point for the file. 55860b57cec5SDimitry Andric void SelectionDAG::Legalize() { 55870b57cec5SDimitry Andric AssignTopologicalOrder(); 55880b57cec5SDimitry Andric 55890b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 55900b57cec5SDimitry Andric // Use a delete listener to remove nodes which were deleted during 55910b57cec5SDimitry Andric // legalization from LegalizeNodes. This is needed to handle the situation 55920b57cec5SDimitry Andric // where a new node is allocated by the object pool to the same address of a 55930b57cec5SDimitry Andric // previously deleted node. 55940b57cec5SDimitry Andric DAGNodeDeletedListener DeleteListener( 55950b57cec5SDimitry Andric *this, 55960b57cec5SDimitry Andric [&LegalizedNodes](SDNode *N, SDNode *E) { LegalizedNodes.erase(N); }); 55970b57cec5SDimitry Andric 55980b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes); 55990b57cec5SDimitry Andric 56000b57cec5SDimitry Andric // Visit all the nodes. We start in topological order, so that we see 56010b57cec5SDimitry Andric // nodes with their original operands intact. Legalization can produce 56020b57cec5SDimitry Andric // new nodes which may themselves need to be legalized. Iterate until all 56030b57cec5SDimitry Andric // nodes have been legalized. 56040b57cec5SDimitry Andric while (true) { 56050b57cec5SDimitry Andric bool AnyLegalized = false; 56060b57cec5SDimitry Andric for (auto NI = allnodes_end(); NI != allnodes_begin();) { 56070b57cec5SDimitry Andric --NI; 56080b57cec5SDimitry Andric 56090b57cec5SDimitry Andric SDNode *N = &*NI; 56100b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 56110b57cec5SDimitry Andric ++NI; 56120b57cec5SDimitry Andric DeleteNode(N); 56130b57cec5SDimitry Andric continue; 56140b57cec5SDimitry Andric } 56150b57cec5SDimitry Andric 56160b57cec5SDimitry Andric if (LegalizedNodes.insert(N).second) { 56170b57cec5SDimitry Andric AnyLegalized = true; 56180b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 56190b57cec5SDimitry Andric 56200b57cec5SDimitry Andric if (N->use_empty() && N != getRoot().getNode()) { 56210b57cec5SDimitry Andric ++NI; 56220b57cec5SDimitry Andric DeleteNode(N); 56230b57cec5SDimitry Andric } 56240b57cec5SDimitry Andric } 56250b57cec5SDimitry Andric } 56260b57cec5SDimitry Andric if (!AnyLegalized) 56270b57cec5SDimitry Andric break; 56280b57cec5SDimitry Andric 56290b57cec5SDimitry Andric } 56300b57cec5SDimitry Andric 56310b57cec5SDimitry Andric // Remove dead nodes now. 56320b57cec5SDimitry Andric RemoveDeadNodes(); 56330b57cec5SDimitry Andric } 56340b57cec5SDimitry Andric 56350b57cec5SDimitry Andric bool SelectionDAG::LegalizeOp(SDNode *N, 56360b57cec5SDimitry Andric SmallSetVector<SDNode *, 16> &UpdatedNodes) { 56370b57cec5SDimitry Andric SmallPtrSet<SDNode *, 16> LegalizedNodes; 56380b57cec5SDimitry Andric SelectionDAGLegalize Legalizer(*this, LegalizedNodes, &UpdatedNodes); 56390b57cec5SDimitry Andric 56400b57cec5SDimitry Andric // Directly insert the node in question, and legalize it. This will recurse 56410b57cec5SDimitry Andric // as needed through operands. 56420b57cec5SDimitry Andric LegalizedNodes.insert(N); 56430b57cec5SDimitry Andric Legalizer.LegalizeOp(N); 56440b57cec5SDimitry Andric 56450b57cec5SDimitry Andric return LegalizedNodes.count(N); 56460b57cec5SDimitry Andric } 5647