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 | |
| 14 | #define DEBUG_TYPE "systemz-selectiondag-info" |
| 15 | #include "SystemZTargetMachine.h" |
| 16 | #include "llvm/CodeGen/SelectionDAG.h" |
| 17 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | SystemZSelectionDAGInfo:: |
| 21 | SystemZSelectionDAGInfo(const SystemZTargetMachine &TM) |
| 22 | : TargetSelectionDAGInfo(TM) { |
| 23 | } |
| 24 | |
| 25 | SystemZSelectionDAGInfo::~SystemZSelectionDAGInfo() { |
| 26 | } |
| 27 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 28 | // Use MVC to copy Size bytes from Src to Dest, deciding whether to use |
| 29 | // a loop or straight-line code. |
| 30 | static SDValue emitMVC(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 31 | SDValue Dst, SDValue Src, uint64_t Size) { |
| 32 | EVT PtrVT = Src.getValueType(); |
| 33 | // The heuristic we use is to prefer loops for anything that would |
| 34 | // require 7 or more MVCs. With these kinds of sizes there isn't |
| 35 | // much to choose between straight-line code and looping code, |
| 36 | // since the time will be dominated by the MVCs themselves. |
| 37 | // However, the loop has 4 or 5 instructions (depending on whether |
| 38 | // the base addresses can be proved equal), so there doesn't seem |
| 39 | // much point using a loop for 5 * 256 bytes or fewer. Anything in |
| 40 | // the range (5 * 256, 6 * 256) will need another instruction after |
| 41 | // the loop, so it doesn't seem worth using a loop then either. |
| 42 | // The next value up, 6 * 256, can be implemented in the same |
| 43 | // number of straight-line MVCs as 6 * 256 - 1. |
| 44 | if (Size > 6 * 256) |
| 45 | return DAG.getNode(SystemZISD::MVC_LOOP, DL, MVT::Other, Chain, Dst, Src, |
| 46 | DAG.getConstant(Size, PtrVT), |
| 47 | DAG.getConstant(Size / 256, PtrVT)); |
| 48 | return DAG.getNode(SystemZISD::MVC, DL, MVT::Other, Chain, Dst, Src, |
| 49 | DAG.getConstant(Size, PtrVT)); |
| 50 | } |
| 51 | |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 52 | SDValue SystemZSelectionDAGInfo:: |
| 53 | EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 54 | SDValue Dst, SDValue Src, SDValue Size, unsigned Align, |
| 55 | bool IsVolatile, bool AlwaysInline, |
| 56 | MachinePointerInfo DstPtrInfo, |
| 57 | MachinePointerInfo SrcPtrInfo) const { |
| 58 | if (IsVolatile) |
| 59 | return SDValue(); |
| 60 | |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 61 | if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) |
| 62 | return emitMVC(DAG, DL, Chain, Dst, Src, CSize->getZExtValue()); |
Richard Sandiford | d131ff8 | 2013-07-08 09:35:23 +0000 | [diff] [blame] | 63 | return SDValue(); |
| 64 | } |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 65 | |
| 66 | // Handle a memset of 1, 2, 4 or 8 bytes with the operands given by |
| 67 | // Chain, Dst, ByteVal and Size. These cases are expected to use |
| 68 | // MVI, MVHHI, MVHI and MVGHI respectively. |
| 69 | static SDValue memsetStore(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 70 | SDValue Dst, uint64_t ByteVal, uint64_t Size, |
| 71 | unsigned Align, |
| 72 | MachinePointerInfo DstPtrInfo) { |
| 73 | uint64_t StoreVal = ByteVal; |
| 74 | for (unsigned I = 1; I < Size; ++I) |
| 75 | StoreVal |= ByteVal << (I * 8); |
| 76 | return DAG.getStore(Chain, DL, |
| 77 | DAG.getConstant(StoreVal, MVT::getIntegerVT(Size * 8)), |
| 78 | Dst, DstPtrInfo, false, false, Align); |
| 79 | } |
| 80 | |
| 81 | SDValue SystemZSelectionDAGInfo:: |
| 82 | EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 83 | SDValue Dst, SDValue Byte, SDValue Size, |
| 84 | unsigned Align, bool IsVolatile, |
| 85 | MachinePointerInfo DstPtrInfo) const { |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 86 | EVT PtrVT = Dst.getValueType(); |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 87 | |
| 88 | if (IsVolatile) |
| 89 | return SDValue(); |
| 90 | |
| 91 | if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) { |
| 92 | uint64_t Bytes = CSize->getZExtValue(); |
| 93 | if (Bytes == 0) |
| 94 | return SDValue(); |
| 95 | if (ConstantSDNode *CByte = dyn_cast<ConstantSDNode>(Byte)) { |
| 96 | // Handle cases that can be done using at most two of |
| 97 | // MVI, MVHI, MVHHI and MVGHI. The latter two can only be |
| 98 | // used if ByteVal is all zeros or all ones; in other casees, |
| 99 | // we can move at most 2 halfwords. |
| 100 | uint64_t ByteVal = CByte->getZExtValue(); |
| 101 | if (ByteVal == 0 || ByteVal == 255 ? |
| 102 | Bytes <= 16 && CountPopulation_64(Bytes) <= 2 : |
| 103 | Bytes <= 4) { |
| 104 | unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes); |
| 105 | unsigned Size2 = Bytes - Size1; |
| 106 | SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1, |
| 107 | Align, DstPtrInfo); |
| 108 | if (Size2 == 0) |
| 109 | return Chain1; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 110 | Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, |
| 111 | DAG.getConstant(Size1, PtrVT)); |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 112 | DstPtrInfo = DstPtrInfo.getWithOffset(Size1); |
| 113 | SDValue Chain2 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size2, |
| 114 | std::min(Align, Size1), DstPtrInfo); |
| 115 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); |
| 116 | } |
| 117 | } else { |
| 118 | // Handle one and two bytes using STC. |
| 119 | if (Bytes <= 2) { |
| 120 | SDValue Chain1 = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, |
| 121 | false, false, Align); |
| 122 | if (Bytes == 1) |
| 123 | return Chain1; |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 124 | SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, |
| 125 | DAG.getConstant(1, PtrVT)); |
Richard Sandiford | 47660c1 | 2013-07-09 09:32:42 +0000 | [diff] [blame] | 126 | SDValue Chain2 = DAG.getStore(Chain, DL, Byte, Dst2, |
| 127 | DstPtrInfo.getWithOffset(1), |
| 128 | false, false, 1); |
| 129 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2); |
| 130 | } |
| 131 | } |
| 132 | assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already"); |
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. |
| 135 | Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, |
| 136 | false, false, Align); |
| 137 | SDValue DstPlus1 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst, |
| 138 | DAG.getConstant(1, PtrVT)); |
| 139 | return emitMVC(DAG, DL, 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 | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 144 | // Convert the current CC value into an integer that is 0 if CC == 0, |
| 145 | // less than zero if CC == 1 and greater than zero if CC >= 2. |
| 146 | // The sequence starts with IPM, which puts CC into bits 29 and 28 |
| 147 | // of an integer and clears bits 30 and 31. |
| 148 | static SDValue addIPMSequence(SDLoc DL, SDValue Glue, SelectionDAG &DAG) { |
| 149 | SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 150 | SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i32, IPM, |
| 151 | DAG.getConstant(28, MVT::i32)); |
| 152 | SDValue ROTL = DAG.getNode(ISD::ROTL, DL, MVT::i32, SRL, |
| 153 | DAG.getConstant(31, MVT::i32)); |
| 154 | return ROTL; |
| 155 | } |
| 156 | |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 157 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
| 158 | EmitTargetCodeForMemcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 159 | SDValue Src1, SDValue Src2, SDValue Size, |
| 160 | MachinePointerInfo Op1PtrInfo, |
| 161 | MachinePointerInfo Op2PtrInfo) const { |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 162 | EVT PtrVT = Src1.getValueType(); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 163 | if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) { |
| 164 | uint64_t Bytes = CSize->getZExtValue(); |
| 165 | if (Bytes >= 1 && Bytes <= 0x100) { |
| 166 | // A single CLC. |
| 167 | SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); |
| 168 | Chain = DAG.getNode(SystemZISD::CLC, DL, VTs, Chain, |
Richard Sandiford | 5e318f0 | 2013-08-27 09:54:29 +0000 | [diff] [blame] | 169 | Src1, Src2, Size, DAG.getConstant(0, PtrVT)); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 170 | SDValue Glue = Chain.getValue(1); |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 171 | return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain); |
Richard Sandiford | 564681c | 2013-08-12 10:28:10 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | return std::make_pair(SDValue(), SDValue()); |
| 175 | } |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 176 | |
| 177 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
Richard Sandiford | 6f6d551 | 2013-08-20 09:38:48 +0000 | [diff] [blame] | 178 | EmitTargetCodeForMemchr(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 179 | SDValue Src, SDValue Char, SDValue Length, |
| 180 | MachinePointerInfo SrcPtrInfo) const { |
| 181 | // Use SRST to find the character. End is its address on success. |
| 182 | EVT PtrVT = Src.getValueType(); |
| 183 | SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other, MVT::Glue); |
| 184 | Length = DAG.getZExtOrTrunc(Length, DL, PtrVT); |
| 185 | Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32); |
| 186 | Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char, |
| 187 | DAG.getConstant(255, MVT::i32)); |
| 188 | SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length); |
| 189 | SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, |
| 190 | Limit, Src, Char); |
| 191 | Chain = End.getValue(1); |
| 192 | SDValue Glue = End.getValue(2); |
| 193 | |
| 194 | // Now select between End and null, depending on whether the character |
| 195 | // was found. |
| 196 | SmallVector<SDValue, 5> Ops; |
| 197 | Ops.push_back(End); |
| 198 | Ops.push_back(DAG.getConstant(0, PtrVT)); |
| 199 | Ops.push_back(DAG.getConstant(SystemZ::CCMASK_SRST, MVT::i32)); |
| 200 | Ops.push_back(DAG.getConstant(SystemZ::CCMASK_SRST_FOUND, MVT::i32)); |
| 201 | Ops.push_back(Glue); |
| 202 | VTs = DAG.getVTList(PtrVT, MVT::Glue); |
| 203 | End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size()); |
| 204 | return std::make_pair(End, Chain); |
| 205 | } |
| 206 | |
| 207 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
Richard Sandiford | bb83a50 | 2013-08-16 11:29:37 +0000 | [diff] [blame] | 208 | EmitTargetCodeForStrcpy(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 209 | SDValue Dest, SDValue Src, |
| 210 | MachinePointerInfo DestPtrInfo, |
| 211 | MachinePointerInfo SrcPtrInfo, bool isStpcpy) const { |
| 212 | SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other); |
| 213 | SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src, |
| 214 | DAG.getConstant(0, MVT::i32)); |
| 215 | return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1)); |
| 216 | } |
| 217 | |
| 218 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
Richard Sandiford | ca23271 | 2013-08-16 11:21:54 +0000 | [diff] [blame] | 219 | EmitTargetCodeForStrcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 220 | SDValue Src1, SDValue Src2, |
| 221 | MachinePointerInfo Op1PtrInfo, |
| 222 | MachinePointerInfo Op2PtrInfo) const { |
| 223 | SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::Other, MVT::Glue); |
| 224 | SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src1, Src2, |
| 225 | DAG.getConstant(0, MVT::i32)); |
| 226 | Chain = Unused.getValue(1); |
| 227 | SDValue Glue = Chain.getValue(2); |
| 228 | return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain); |
| 229 | } |
Richard Sandiford | 0dec06a | 2013-08-16 11:41:43 +0000 | [diff] [blame] | 230 | |
| 231 | // Search from Src for a null character, stopping once Src reaches Limit. |
| 232 | // Return a pair of values, the first being the number of nonnull characters |
| 233 | // and the second being the out chain. |
| 234 | // |
| 235 | // This can be used for strlen by setting Limit to 0. |
| 236 | static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG, SDLoc DL, |
| 237 | SDValue Chain, SDValue Src, |
| 238 | SDValue Limit) { |
| 239 | EVT PtrVT = Src.getValueType(); |
| 240 | SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other, MVT::Glue); |
| 241 | SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain, |
| 242 | Limit, Src, DAG.getConstant(0, MVT::i32)); |
| 243 | Chain = End.getValue(1); |
| 244 | SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src); |
| 245 | return std::make_pair(Len, Chain); |
| 246 | } |
| 247 | |
| 248 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
| 249 | EmitTargetCodeForStrlen(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 250 | SDValue Src, MachinePointerInfo SrcPtrInfo) const { |
| 251 | EVT PtrVT = Src.getValueType(); |
| 252 | return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, PtrVT)); |
| 253 | } |
| 254 | |
| 255 | std::pair<SDValue, SDValue> SystemZSelectionDAGInfo:: |
| 256 | EmitTargetCodeForStrnlen(SelectionDAG &DAG, SDLoc DL, SDValue Chain, |
| 257 | SDValue Src, SDValue MaxLength, |
| 258 | MachinePointerInfo SrcPtrInfo) const { |
| 259 | EVT PtrVT = Src.getValueType(); |
| 260 | MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT); |
| 261 | SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength); |
| 262 | return getBoundedStrlen(DAG, DL, Chain, Src, Limit); |
| 263 | } |