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