1 //===-- SystemZSelectionDAGInfo.cpp - SystemZ SelectionDAG Info -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the SystemZSelectionDAGInfo class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "SystemZTargetMachine.h" 14 #include "llvm/CodeGen/SelectionDAG.h" 15 16 using namespace llvm; 17 18 #define DEBUG_TYPE "systemz-selectiondag-info" 19 20 // Decide whether it is best to use a loop or straight-line code for 21 // a block operation of Size bytes with source address Src and destination 22 // address Dest. Sequence is the opcode to use for straight-line code 23 // (such as MVC) and Loop is the opcode to use for loops (such as MVC_LOOP). 24 // Return the chain for the completed operation. 25 static SDValue emitMemMem(SelectionDAG &DAG, const SDLoc &DL, unsigned Sequence, 26 unsigned Loop, SDValue Chain, SDValue Dst, 27 SDValue Src, uint64_t Size) { 28 EVT PtrVT = Src.getValueType(); 29 // The heuristic we use is to prefer loops for anything that would 30 // require 7 or more MVCs. With these kinds of sizes there isn't 31 // much to choose between straight-line code and looping code, 32 // since the time will be dominated by the MVCs themselves. 33 // However, the loop has 4 or 5 instructions (depending on whether 34 // the base addresses can be proved equal), so there doesn't seem 35 // much point using a loop for 5 * 256 bytes or fewer. Anything in 36 // the range (5 * 256, 6 * 256) will need another instruction after 37 // the loop, so it doesn't seem worth using a loop then either. 38 // The next value up, 6 * 256, can be implemented in the same 39 // number of straight-line MVCs as 6 * 256 - 1. 40 if (Size > 6 * 256) 41 return DAG.getNode(Loop, DL, MVT::Other, Chain, Dst, Src, 42 DAG.getConstant(Size, DL, PtrVT), 43 DAG.getConstant(Size / 256, DL, PtrVT)); 44 return DAG.getNode(Sequence, DL, MVT::Other, Chain, Dst, Src, 45 DAG.getConstant(Size, DL, PtrVT)); 46 } 47 48 SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemcpy( 49 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, SDValue Src, 50 SDValue Size, Align Alignment, bool IsVolatile, bool AlwaysInline, 51 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const { 52 if (IsVolatile) 53 return SDValue(); 54 55 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) 56 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP, 57 Chain, Dst, Src, CSize->getZExtValue()); 58 return SDValue(); 59 } 60 61 // Handle a memset of 1, 2, 4 or 8 bytes with the operands given by 62 // Chain, Dst, ByteVal and Size. These cases are expected to use 63 // MVI, MVHHI, MVHI and MVGHI respectively. 64 static SDValue memsetStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, 65 SDValue Dst, uint64_t ByteVal, uint64_t Size, 66 unsigned Align, MachinePointerInfo DstPtrInfo) { 67 uint64_t StoreVal = ByteVal; 68 for (unsigned I = 1; I < Size; ++I) 69 StoreVal |= ByteVal << (I * 8); 70 return DAG.getStore( 71 Chain, DL, DAG.getConstant(StoreVal, DL, MVT::getIntegerVT(Size * 8)), 72 Dst, DstPtrInfo, Align); 73 } 74 75 SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemset( 76 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, 77 SDValue Byte, SDValue Size, Align Alignment, bool IsVolatile, 78 MachinePointerInfo DstPtrInfo) const { 79 EVT PtrVT = Dst.getValueType(); 80 81 if (IsVolatile) 82 return SDValue(); 83 84 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) { 85 uint64_t Bytes = CSize->getZExtValue(); 86 if (Bytes == 0) 87 return SDValue(); 88 if (auto *CByte = dyn_cast<ConstantSDNode>(Byte)) { 89 // Handle cases that can be done using at most two of 90 // MVI, MVHI, MVHHI and MVGHI. The latter two can only be 91 // used if ByteVal is all zeros or all ones; in other casees, 92 // we can move at most 2 halfwords. 93 uint64_t ByteVal = CByte->getZExtValue(); 94 if (ByteVal == 0 || ByteVal == 255 ? 95 Bytes <= 16 && countPopulation(Bytes) <= 2 : 96 Bytes <= 4) { 97 unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes); 98 unsigned Size2 = Bytes - Size1; 99 SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1, 100 Alignment.value(), DstPtrInfo); 101 if (Size2 == 0) 102 return Chain1; 103 Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, 104 DAG.getConstant(Size1, DL, PtrVT)); 105 DstPtrInfo = DstPtrInfo.getWithOffset(Size1); 106 SDValue Chain2 = memsetStore( 107 DAG, DL, Chain, Dst, ByteVal, Size2, 108 std::min((unsigned)Alignment.value(), Size1), DstPtrInfo); 109 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); 110 } 111 } else { 112 // Handle one and two bytes using STC. 113 if (Bytes <= 2) { 114 SDValue Chain1 = 115 DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Alignment); 116 if (Bytes == 1) 117 return Chain1; 118 SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, 119 DAG.getConstant(1, DL, PtrVT)); 120 SDValue Chain2 = 121 DAG.getStore(Chain, DL, Byte, Dst2, DstPtrInfo.getWithOffset(1), 122 /* Alignment = */ 1); 123 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); 124 } 125 } 126 assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already"); 127 128 // Handle the special case of a memset of 0, which can use XC. 129 auto *CByte = dyn_cast<ConstantSDNode>(Byte); 130 if (CByte && CByte->getZExtValue() == 0) 131 return emitMemMem(DAG, DL, SystemZISD::XC, SystemZISD::XC_LOOP, 132 Chain, Dst, Dst, Bytes); 133 134 // Copy the byte to the first location and then use MVC to copy 135 // it to the rest. 136 Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Alignment); 137 SDValue DstPlus1 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, 138 DAG.getConstant(1, DL, PtrVT)); 139 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP, 140 Chain, DstPlus1, Dst, Bytes - 1); 141 } 142 return SDValue(); 143 } 144 145 // Use CLC to compare [Src1, Src1 + Size) with [Src2, Src2 + Size), 146 // deciding whether to use a loop or straight-line code. 147 static SDValue emitCLC(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, 148 SDValue Src1, SDValue Src2, uint64_t Size) { 149 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); 150 EVT PtrVT = Src1.getValueType(); 151 // A two-CLC sequence is a clear win over a loop, not least because it 152 // needs only one branch. A three-CLC sequence needs the same number 153 // of branches as a loop (i.e. 2), but is shorter. That brings us to 154 // lengths greater than 768 bytes. It seems relatively likely that 155 // a difference will be found within the first 768 bytes, so we just 156 // optimize for the smallest number of branch instructions, in order 157 // to avoid polluting the prediction buffer too much. A loop only ever 158 // needs 2 branches, whereas a straight-line sequence would need 3 or more. 159 if (Size > 3 * 256) 160 return DAG.getNode(SystemZISD::CLC_LOOP, DL, VTs, Chain, Src1, Src2, 161 DAG.getConstant(Size, DL, PtrVT), 162 DAG.getConstant(Size / 256, DL, PtrVT)); 163 return DAG.getNode(SystemZISD::CLC, DL, VTs, Chain, Src1, Src2, 164 DAG.getConstant(Size, DL, PtrVT)); 165 } 166 167 // Convert the current CC value into an integer that is 0 if CC == 0, 168 // greater than zero if CC == 1 and less than zero if CC >= 2. 169 // The sequence starts with IPM, which puts CC into bits 29 and 28 170 // of an integer and clears bits 30 and 31. 171 static SDValue addIPMSequence(const SDLoc &DL, SDValue CCReg, 172 SelectionDAG &DAG) { 173 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); 174 SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, IPM, 175 DAG.getConstant(30 - SystemZ::IPM_CC, DL, MVT::i32)); 176 SDValue SRA = DAG.getNode(ISD::SRA, DL, MVT::i32, SHL, 177 DAG.getConstant(30, DL, MVT::i32)); 178 return SRA; 179 } 180 181 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp( 182 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1, 183 SDValue Src2, SDValue Size, MachinePointerInfo Op1PtrInfo, 184 MachinePointerInfo Op2PtrInfo) const { 185 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) { 186 uint64_t Bytes = CSize->getZExtValue(); 187 assert(Bytes > 0 && "Caller should have handled 0-size case"); 188 // Swap operands to invert CC == 1 vs. CC == 2 cases. 189 SDValue CCReg = emitCLC(DAG, DL, Chain, Src2, Src1, Bytes); 190 Chain = CCReg.getValue(1); 191 return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain); 192 } 193 return std::make_pair(SDValue(), SDValue()); 194 } 195 196 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemchr( 197 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 198 SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const { 199 // Use SRST to find the character. End is its address on success. 200 EVT PtrVT = Src.getValueType(); 201 SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other); 202 Length = DAG.getZExtOrTrunc(Length, DL, PtrVT); 203 Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32); 204 Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char, 205 DAG.getConstant(255, DL, MVT::i32)); 206 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length); 207 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, 208 Limit, Src, Char); 209 SDValue CCReg = End.getValue(1); 210 Chain = End.getValue(2); 211 212 // Now select between End and null, depending on whether the character 213 // was found. 214 SDValue Ops[] = { 215 End, DAG.getConstant(0, DL, PtrVT), 216 DAG.getTargetConstant(SystemZ::CCMASK_SRST, DL, MVT::i32), 217 DAG.getTargetConstant(SystemZ::CCMASK_SRST_FOUND, DL, MVT::i32), CCReg}; 218 End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, PtrVT, Ops); 219 return std::make_pair(End, Chain); 220 } 221 222 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcpy( 223 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest, 224 SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo, 225 bool isStpcpy) const { 226 SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other); 227 SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src, 228 DAG.getConstant(0, DL, MVT::i32)); 229 return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1)); 230 } 231 232 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcmp( 233 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1, 234 SDValue Src2, MachinePointerInfo Op1PtrInfo, 235 MachinePointerInfo Op2PtrInfo) const { 236 SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::i32, MVT::Other); 237 // Swap operands to invert CC == 1 vs. CC == 2 cases. 238 SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src2, Src1, 239 DAG.getConstant(0, DL, MVT::i32)); 240 SDValue CCReg = Unused.getValue(1); 241 Chain = Unused.getValue(2); 242 return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain); 243 } 244 245 // Search from Src for a null character, stopping once Src reaches Limit. 246 // Return a pair of values, the first being the number of nonnull characters 247 // and the second being the out chain. 248 // 249 // This can be used for strlen by setting Limit to 0. 250 static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG, 251 const SDLoc &DL, 252 SDValue Chain, SDValue Src, 253 SDValue Limit) { 254 EVT PtrVT = Src.getValueType(); 255 SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other); 256 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, 257 Limit, Src, DAG.getConstant(0, DL, MVT::i32)); 258 Chain = End.getValue(2); 259 SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src); 260 return std::make_pair(Len, Chain); 261 } 262 263 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrlen( 264 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 265 MachinePointerInfo SrcPtrInfo) const { 266 EVT PtrVT = Src.getValueType(); 267 return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, DL, PtrVT)); 268 } 269 270 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrnlen( 271 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src, 272 SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const { 273 EVT PtrVT = Src.getValueType(); 274 MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT); 275 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength); 276 return getBoundedStrlen(DAG, DL, Chain, Src, Limit); 277 } 278