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