10b57cec5SDimitry Andric //===-- SystemZSelectionDAGInfo.cpp - SystemZ SelectionDAG Info -----------===// 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 SystemZSelectionDAGInfo class. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "SystemZTargetMachine.h" 140b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric using namespace llvm; 170b57cec5SDimitry Andric 180b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-selectiondag-info" 190b57cec5SDimitry Andric 20*0eae32dcSDimitry Andric static unsigned getMemMemLenAdj(unsigned Op) { 21*0eae32dcSDimitry Andric return Op == SystemZISD::MEMSET_MVC ? 2 : 1; 22349cc55cSDimitry Andric } 23349cc55cSDimitry Andric 24*0eae32dcSDimitry Andric static SDValue createMemMemNode(SelectionDAG &DAG, const SDLoc &DL, unsigned Op, 25*0eae32dcSDimitry Andric SDValue Chain, SDValue Dst, SDValue Src, 26*0eae32dcSDimitry Andric SDValue LenAdj, SDValue Byte) { 27*0eae32dcSDimitry Andric SDVTList VTs = Op == SystemZISD::CLC ? DAG.getVTList(MVT::i32, MVT::Other) 28*0eae32dcSDimitry Andric : DAG.getVTList(MVT::Other); 29*0eae32dcSDimitry Andric SmallVector<SDValue, 6> Ops; 30*0eae32dcSDimitry Andric if (Op == SystemZISD::MEMSET_MVC) 31*0eae32dcSDimitry Andric Ops = { Chain, Dst, LenAdj, Byte }; 32*0eae32dcSDimitry Andric else 33*0eae32dcSDimitry Andric Ops = { Chain, Dst, Src, LenAdj }; 34*0eae32dcSDimitry Andric return DAG.getNode(Op, DL, VTs, Ops); 35*0eae32dcSDimitry Andric } 36*0eae32dcSDimitry Andric 37*0eae32dcSDimitry Andric // Emit a mem-mem operation after subtracting one (or two for memset) from 38*0eae32dcSDimitry Andric // size, which will be added back during pseudo expansion. As the Reg case 39*0eae32dcSDimitry Andric // emitted here may be converted by DAGCombiner into having an Imm length, 40*0eae32dcSDimitry Andric // they are both emitted the same way. 41349cc55cSDimitry Andric static SDValue emitMemMemImm(SelectionDAG &DAG, const SDLoc &DL, unsigned Op, 42349cc55cSDimitry Andric SDValue Chain, SDValue Dst, SDValue Src, 43*0eae32dcSDimitry Andric uint64_t Size, SDValue Byte = SDValue()) { 44*0eae32dcSDimitry Andric unsigned Adj = getMemMemLenAdj(Op); 45*0eae32dcSDimitry Andric assert(Size >= Adj && "Adjusted length overflow."); 46*0eae32dcSDimitry Andric SDValue LenAdj = DAG.getConstant(Size - Adj, DL, Dst.getValueType()); 47*0eae32dcSDimitry Andric return createMemMemNode(DAG, DL, Op, Chain, Dst, Src, LenAdj, Byte); 48349cc55cSDimitry Andric } 49349cc55cSDimitry Andric 50349cc55cSDimitry Andric static SDValue emitMemMemReg(SelectionDAG &DAG, const SDLoc &DL, unsigned Op, 51349cc55cSDimitry Andric SDValue Chain, SDValue Dst, SDValue Src, 52*0eae32dcSDimitry Andric SDValue Size, SDValue Byte = SDValue()) { 53*0eae32dcSDimitry Andric int64_t Adj = getMemMemLenAdj(Op); 54*0eae32dcSDimitry Andric SDValue LenAdj = DAG.getNode(ISD::ADD, DL, MVT::i64, 55349cc55cSDimitry Andric DAG.getZExtOrTrunc(Size, DL, MVT::i64), 56*0eae32dcSDimitry Andric DAG.getConstant(0 - Adj, DL, MVT::i64)); 57*0eae32dcSDimitry Andric return createMemMemNode(DAG, DL, Op, Chain, Dst, Src, LenAdj, Byte); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric 600b57cec5SDimitry Andric SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemcpy( 610b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, SDValue Src, 625ffd83dbSDimitry Andric SDValue Size, Align Alignment, bool IsVolatile, bool AlwaysInline, 630b57cec5SDimitry Andric MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const { 640b57cec5SDimitry Andric if (IsVolatile) 650b57cec5SDimitry Andric return SDValue(); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) 68349cc55cSDimitry Andric return emitMemMemImm(DAG, DL, SystemZISD::MVC, Chain, Dst, Src, 69349cc55cSDimitry Andric CSize->getZExtValue()); 70349cc55cSDimitry Andric 71349cc55cSDimitry Andric return emitMemMemReg(DAG, DL, SystemZISD::MVC, Chain, Dst, Src, Size); 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 740b57cec5SDimitry Andric // Handle a memset of 1, 2, 4 or 8 bytes with the operands given by 750b57cec5SDimitry Andric // Chain, Dst, ByteVal and Size. These cases are expected to use 760b57cec5SDimitry Andric // MVI, MVHHI, MVHI and MVGHI respectively. 770b57cec5SDimitry Andric static SDValue memsetStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, 780b57cec5SDimitry Andric SDValue Dst, uint64_t ByteVal, uint64_t Size, 790b57cec5SDimitry Andric unsigned Align, MachinePointerInfo DstPtrInfo) { 800b57cec5SDimitry Andric uint64_t StoreVal = ByteVal; 810b57cec5SDimitry Andric for (unsigned I = 1; I < Size; ++I) 820b57cec5SDimitry Andric StoreVal |= ByteVal << (I * 8); 830b57cec5SDimitry Andric return DAG.getStore( 840b57cec5SDimitry Andric Chain, DL, DAG.getConstant(StoreVal, DL, MVT::getIntegerVT(Size * 8)), 850b57cec5SDimitry Andric Dst, DstPtrInfo, Align); 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemset( 890b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, 905ffd83dbSDimitry Andric SDValue Byte, SDValue Size, Align Alignment, bool IsVolatile, 910b57cec5SDimitry Andric MachinePointerInfo DstPtrInfo) const { 920b57cec5SDimitry Andric EVT PtrVT = Dst.getValueType(); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric if (IsVolatile) 950b57cec5SDimitry Andric return SDValue(); 960b57cec5SDimitry Andric 97fe6060f1SDimitry Andric auto *CByte = dyn_cast<ConstantSDNode>(Byte); 980b57cec5SDimitry Andric if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) { 990b57cec5SDimitry Andric uint64_t Bytes = CSize->getZExtValue(); 1000b57cec5SDimitry Andric if (Bytes == 0) 1010b57cec5SDimitry Andric return SDValue(); 102fe6060f1SDimitry Andric if (CByte) { 1030b57cec5SDimitry Andric // Handle cases that can be done using at most two of 1040b57cec5SDimitry Andric // MVI, MVHI, MVHHI and MVGHI. The latter two can only be 1050b57cec5SDimitry Andric // used if ByteVal is all zeros or all ones; in other casees, 1060b57cec5SDimitry Andric // we can move at most 2 halfwords. 1070b57cec5SDimitry Andric uint64_t ByteVal = CByte->getZExtValue(); 1080b57cec5SDimitry Andric if (ByteVal == 0 || ByteVal == 255 ? 1090b57cec5SDimitry Andric Bytes <= 16 && countPopulation(Bytes) <= 2 : 1100b57cec5SDimitry Andric Bytes <= 4) { 1110b57cec5SDimitry Andric unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes); 1120b57cec5SDimitry Andric unsigned Size2 = Bytes - Size1; 1130b57cec5SDimitry Andric SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1, 1145ffd83dbSDimitry Andric Alignment.value(), DstPtrInfo); 1150b57cec5SDimitry Andric if (Size2 == 0) 1160b57cec5SDimitry Andric return Chain1; 1170b57cec5SDimitry Andric Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, 1180b57cec5SDimitry Andric DAG.getConstant(Size1, DL, PtrVT)); 1190b57cec5SDimitry Andric DstPtrInfo = DstPtrInfo.getWithOffset(Size1); 1205ffd83dbSDimitry Andric SDValue Chain2 = memsetStore( 1215ffd83dbSDimitry Andric DAG, DL, Chain, Dst, ByteVal, Size2, 1225ffd83dbSDimitry Andric std::min((unsigned)Alignment.value(), Size1), DstPtrInfo); 1230b57cec5SDimitry Andric return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric } else { 1260b57cec5SDimitry Andric // Handle one and two bytes using STC. 1270b57cec5SDimitry Andric if (Bytes <= 2) { 1285ffd83dbSDimitry Andric SDValue Chain1 = 1295ffd83dbSDimitry Andric DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Alignment); 1300b57cec5SDimitry Andric if (Bytes == 1) 1310b57cec5SDimitry Andric return Chain1; 1320b57cec5SDimitry Andric SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, 1330b57cec5SDimitry Andric DAG.getConstant(1, DL, PtrVT)); 134e8d8bef9SDimitry Andric SDValue Chain2 = DAG.getStore(Chain, DL, Byte, Dst2, 135e8d8bef9SDimitry Andric DstPtrInfo.getWithOffset(1), Align(1)); 1360b57cec5SDimitry Andric return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already"); 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric // Handle the special case of a memset of 0, which can use XC. 1420b57cec5SDimitry Andric if (CByte && CByte->getZExtValue() == 0) 143349cc55cSDimitry Andric return emitMemMemImm(DAG, DL, SystemZISD::XC, Chain, Dst, Dst, Bytes); 1440b57cec5SDimitry Andric 145*0eae32dcSDimitry Andric return emitMemMemImm(DAG, DL, SystemZISD::MEMSET_MVC, Chain, Dst, SDValue(), 146*0eae32dcSDimitry Andric Bytes, DAG.getAnyExtOrTrunc(Byte, DL, MVT::i32)); 1470b57cec5SDimitry Andric } 148fe6060f1SDimitry Andric 149fe6060f1SDimitry Andric // Variable length 150349cc55cSDimitry Andric if (CByte && CByte->getZExtValue() == 0) 151fe6060f1SDimitry Andric // Handle the special case of a variable length memset of 0 with XC. 152349cc55cSDimitry Andric return emitMemMemReg(DAG, DL, SystemZISD::XC, Chain, Dst, Dst, Size); 1530b57cec5SDimitry Andric 154*0eae32dcSDimitry Andric return emitMemMemReg(DAG, DL, SystemZISD::MEMSET_MVC, Chain, Dst, SDValue(), 155*0eae32dcSDimitry Andric Size, DAG.getAnyExtOrTrunc(Byte, DL, MVT::i32)); 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric 1580b57cec5SDimitry Andric // Convert the current CC value into an integer that is 0 if CC == 0, 1590b57cec5SDimitry Andric // greater than zero if CC == 1 and less than zero if CC >= 2. 1600b57cec5SDimitry Andric // The sequence starts with IPM, which puts CC into bits 29 and 28 1610b57cec5SDimitry Andric // of an integer and clears bits 30 and 31. 1620b57cec5SDimitry Andric static SDValue addIPMSequence(const SDLoc &DL, SDValue CCReg, 1630b57cec5SDimitry Andric SelectionDAG &DAG) { 1640b57cec5SDimitry Andric SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); 1650b57cec5SDimitry Andric SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, IPM, 1660b57cec5SDimitry Andric DAG.getConstant(30 - SystemZ::IPM_CC, DL, MVT::i32)); 1670b57cec5SDimitry Andric SDValue SRA = DAG.getNode(ISD::SRA, DL, MVT::i32, SHL, 1680b57cec5SDimitry Andric DAG.getConstant(30, DL, MVT::i32)); 1690b57cec5SDimitry Andric return SRA; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp( 1730b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1, 1740b57cec5SDimitry Andric SDValue Src2, SDValue Size, MachinePointerInfo Op1PtrInfo, 1750b57cec5SDimitry Andric MachinePointerInfo Op2PtrInfo) const { 176349cc55cSDimitry Andric SDValue CCReg; 177349cc55cSDimitry Andric // Swap operands to invert CC == 1 vs. CC == 2 cases. 1780b57cec5SDimitry Andric if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) { 1790b57cec5SDimitry Andric uint64_t Bytes = CSize->getZExtValue(); 1800b57cec5SDimitry Andric assert(Bytes > 0 && "Caller should have handled 0-size case"); 181349cc55cSDimitry Andric CCReg = emitMemMemImm(DAG, DL, SystemZISD::CLC, Chain, Src2, Src1, Bytes); 182349cc55cSDimitry Andric } else 183349cc55cSDimitry Andric CCReg = emitMemMemReg(DAG, DL, SystemZISD::CLC, Chain, Src2, Src1, Size); 1840b57cec5SDimitry Andric Chain = CCReg.getValue(1); 1850b57cec5SDimitry Andric return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain); 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemchr( 1890b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 1900b57cec5SDimitry Andric SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const { 1910b57cec5SDimitry Andric // Use SRST to find the character. End is its address on success. 1920b57cec5SDimitry Andric EVT PtrVT = Src.getValueType(); 1930b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other); 1940b57cec5SDimitry Andric Length = DAG.getZExtOrTrunc(Length, DL, PtrVT); 1950b57cec5SDimitry Andric Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32); 1960b57cec5SDimitry Andric Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char, 1970b57cec5SDimitry Andric DAG.getConstant(255, DL, MVT::i32)); 1980b57cec5SDimitry Andric SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length); 1990b57cec5SDimitry Andric SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, 2000b57cec5SDimitry Andric Limit, Src, Char); 2010b57cec5SDimitry Andric SDValue CCReg = End.getValue(1); 2020b57cec5SDimitry Andric Chain = End.getValue(2); 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Now select between End and null, depending on whether the character 2050b57cec5SDimitry Andric // was found. 2068bcb0991SDimitry Andric SDValue Ops[] = { 2078bcb0991SDimitry Andric End, DAG.getConstant(0, DL, PtrVT), 2088bcb0991SDimitry Andric DAG.getTargetConstant(SystemZ::CCMASK_SRST, DL, MVT::i32), 2098bcb0991SDimitry Andric DAG.getTargetConstant(SystemZ::CCMASK_SRST_FOUND, DL, MVT::i32), CCReg}; 2100b57cec5SDimitry Andric End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, PtrVT, Ops); 2110b57cec5SDimitry Andric return std::make_pair(End, Chain); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcpy( 2150b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, 2160b57cec5SDimitry Andric SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, 2170b57cec5SDimitry Andric bool isStpcpy) const { 2180b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other); 2190b57cec5SDimitry Andric SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src, 2200b57cec5SDimitry Andric DAG.getConstant(0, DL, MVT::i32)); 2210b57cec5SDimitry Andric return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1)); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcmp( 2250b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1, 2260b57cec5SDimitry Andric SDValue Src2, MachinePointerInfo Op1PtrInfo, 2270b57cec5SDimitry Andric MachinePointerInfo Op2PtrInfo) const { 2280b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::i32, MVT::Other); 2290b57cec5SDimitry Andric // Swap operands to invert CC == 1 vs. CC == 2 cases. 2300b57cec5SDimitry Andric SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src2, Src1, 2310b57cec5SDimitry Andric DAG.getConstant(0, DL, MVT::i32)); 2320b57cec5SDimitry Andric SDValue CCReg = Unused.getValue(1); 2330b57cec5SDimitry Andric Chain = Unused.getValue(2); 2340b57cec5SDimitry Andric return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain); 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Search from Src for a null character, stopping once Src reaches Limit. 2380b57cec5SDimitry Andric // Return a pair of values, the first being the number of nonnull characters 2390b57cec5SDimitry Andric // and the second being the out chain. 2400b57cec5SDimitry Andric // 2410b57cec5SDimitry Andric // This can be used for strlen by setting Limit to 0. 2420b57cec5SDimitry Andric static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG, 2430b57cec5SDimitry Andric const SDLoc &DL, 2440b57cec5SDimitry Andric SDValue Chain, SDValue Src, 2450b57cec5SDimitry Andric SDValue Limit) { 2460b57cec5SDimitry Andric EVT PtrVT = Src.getValueType(); 2470b57cec5SDimitry Andric SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other); 2480b57cec5SDimitry Andric SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, 2490b57cec5SDimitry Andric Limit, Src, DAG.getConstant(0, DL, MVT::i32)); 2500b57cec5SDimitry Andric Chain = End.getValue(2); 2510b57cec5SDimitry Andric SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src); 2520b57cec5SDimitry Andric return std::make_pair(Len, Chain); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrlen( 2560b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 2570b57cec5SDimitry Andric MachinePointerInfo SrcPtrInfo) const { 2580b57cec5SDimitry Andric EVT PtrVT = Src.getValueType(); 2590b57cec5SDimitry Andric return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, DL, PtrVT)); 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrnlen( 2630b57cec5SDimitry Andric SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 2640b57cec5SDimitry Andric SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const { 2650b57cec5SDimitry Andric EVT PtrVT = Src.getValueType(); 2660b57cec5SDimitry Andric MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT); 2670b57cec5SDimitry Andric SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength); 2680b57cec5SDimitry Andric return getBoundedStrlen(DAG, DL, Chain, Src, Limit); 2690b57cec5SDimitry Andric } 270