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