blob: 178aa381731127cf4c4997033d9ae7fc912685f7 [file] [log] [blame]
Richard Sandifordd131ff82013-07-08 09:35:23 +00001//===-- 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 Sandifordd131ff82013-07-08 09:35:23 +000014#include "SystemZTargetMachine.h"
15#include "llvm/CodeGen/SelectionDAG.h"
16
17using namespace llvm;
18
Chandler Carruth84e68b22014-04-22 02:41:26 +000019#define DEBUG_TYPE "systemz-selectiondag-info"
20
Richard Sandiford4943bc32013-09-06 10:25:07 +000021// 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.
26static SDValue emitMemMem(SelectionDAG &DAG, SDLoc DL, unsigned Sequence,
27 unsigned Loop, SDValue Chain, SDValue Dst,
28 SDValue Src, uint64_t Size) {
Richard Sandiford5e318f02013-08-27 09:54:29 +000029 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 Sandiford4943bc32013-09-06 10:25:07 +000042 return DAG.getNode(Loop, DL, MVT::Other, Chain, Dst, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000043 DAG.getConstant(Size, DL, PtrVT),
44 DAG.getConstant(Size / 256, DL, PtrVT));
Richard Sandiford4943bc32013-09-06 10:25:07 +000045 return DAG.getNode(Sequence, DL, MVT::Other, Chain, Dst, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000046 DAG.getConstant(Size, DL, PtrVT));
Richard Sandiford5e318f02013-08-27 09:54:29 +000047}
48
Richard Sandifordd131ff82013-07-08 09:35:23 +000049SDValue SystemZSelectionDAGInfo::
50EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
51 SDValue Dst, SDValue Src, SDValue Size, unsigned Align,
52 bool IsVolatile, bool AlwaysInline,
53 MachinePointerInfo DstPtrInfo,
54 MachinePointerInfo SrcPtrInfo) const {
55 if (IsVolatile)
56 return SDValue();
57
Richard Sandiford21f5d682014-03-06 11:22:58 +000058 if (auto *CSize = dyn_cast<ConstantSDNode>(Size))
Richard Sandiford4943bc32013-09-06 10:25:07 +000059 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP,
60 Chain, Dst, Src, CSize->getZExtValue());
Richard Sandifordd131ff82013-07-08 09:35:23 +000061 return SDValue();
62}
Richard Sandiford47660c12013-07-09 09:32:42 +000063
64// Handle a memset of 1, 2, 4 or 8 bytes with the operands given by
65// Chain, Dst, ByteVal and Size. These cases are expected to use
66// MVI, MVHHI, MVHI and MVGHI respectively.
67static SDValue memsetStore(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
68 SDValue Dst, uint64_t ByteVal, uint64_t Size,
69 unsigned Align,
70 MachinePointerInfo DstPtrInfo) {
71 uint64_t StoreVal = ByteVal;
72 for (unsigned I = 1; I < Size; ++I)
73 StoreVal |= ByteVal << (I * 8);
74 return DAG.getStore(Chain, DL,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000075 DAG.getConstant(StoreVal, DL,
76 MVT::getIntegerVT(Size * 8)),
Richard Sandiford47660c12013-07-09 09:32:42 +000077 Dst, DstPtrInfo, false, false, Align);
78}
79
80SDValue SystemZSelectionDAGInfo::
81EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
82 SDValue Dst, SDValue Byte, SDValue Size,
83 unsigned Align, bool IsVolatile,
84 MachinePointerInfo DstPtrInfo) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +000085 EVT PtrVT = Dst.getValueType();
Richard Sandiford47660c12013-07-09 09:32:42 +000086
87 if (IsVolatile)
88 return SDValue();
89
Richard Sandiford21f5d682014-03-06 11:22:58 +000090 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
Richard Sandiford47660c12013-07-09 09:32:42 +000091 uint64_t Bytes = CSize->getZExtValue();
92 if (Bytes == 0)
93 return SDValue();
Richard Sandiford21f5d682014-03-06 11:22:58 +000094 if (auto *CByte = dyn_cast<ConstantSDNode>(Byte)) {
Richard Sandiford47660c12013-07-09 09:32:42 +000095 // Handle cases that can be done using at most two of
96 // MVI, MVHI, MVHHI and MVGHI. The latter two can only be
97 // used if ByteVal is all zeros or all ones; in other casees,
98 // we can move at most 2 halfwords.
99 uint64_t ByteVal = CByte->getZExtValue();
100 if (ByteVal == 0 || ByteVal == 255 ?
Benjamin Kramer5f6a9072015-02-12 15:35:40 +0000101 Bytes <= 16 && countPopulation(Bytes) <= 2 :
Richard Sandiford47660c12013-07-09 09:32:42 +0000102 Bytes <= 4) {
103 unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes);
104 unsigned Size2 = Bytes - Size1;
105 SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1,
106 Align, DstPtrInfo);
107 if (Size2 == 0)
108 return Chain1;
Richard Sandiford5e318f02013-08-27 09:54:29 +0000109 Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000110 DAG.getConstant(Size1, DL, PtrVT));
Richard Sandiford47660c12013-07-09 09:32:42 +0000111 DstPtrInfo = DstPtrInfo.getWithOffset(Size1);
112 SDValue Chain2 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size2,
113 std::min(Align, Size1), DstPtrInfo);
114 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
115 }
116 } else {
117 // Handle one and two bytes using STC.
118 if (Bytes <= 2) {
119 SDValue Chain1 = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo,
120 false, false, Align);
121 if (Bytes == 1)
122 return Chain1;
Richard Sandiford5e318f02013-08-27 09:54:29 +0000123 SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000124 DAG.getConstant(1, DL, PtrVT));
Richard Sandiford47660c12013-07-09 09:32:42 +0000125 SDValue Chain2 = DAG.getStore(Chain, DL, Byte, Dst2,
126 DstPtrInfo.getWithOffset(1),
127 false, false, 1);
128 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
129 }
130 }
131 assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already");
Richard Sandiford4943bc32013-09-06 10:25:07 +0000132
133 // Handle the special case of a memset of 0, which can use XC.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000134 auto *CByte = dyn_cast<ConstantSDNode>(Byte);
Richard Sandiford4943bc32013-09-06 10:25:07 +0000135 if (CByte && CByte->getZExtValue() == 0)
136 return emitMemMem(DAG, DL, SystemZISD::XC, SystemZISD::XC_LOOP,
137 Chain, Dst, Dst, Bytes);
138
Richard Sandiford5e318f02013-08-27 09:54:29 +0000139 // Copy the byte to the first location and then use MVC to copy
140 // it to the rest.
141 Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo,
142 false, false, Align);
143 SDValue DstPlus1 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000144 DAG.getConstant(1, DL, PtrVT));
Richard Sandiford4943bc32013-09-06 10:25:07 +0000145 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP,
146 Chain, DstPlus1, Dst, Bytes - 1);
Richard Sandiford47660c12013-07-09 09:32:42 +0000147 }
148 return SDValue();
149}
Richard Sandiford564681c2013-08-12 10:28:10 +0000150
Richard Sandifordbe133a82013-08-28 09:01:51 +0000151// Use CLC to compare [Src1, Src1 + Size) with [Src2, Src2 + Size),
152// deciding whether to use a loop or straight-line code.
153static SDValue emitCLC(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
154 SDValue Src1, SDValue Src2, uint64_t Size) {
155 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
156 EVT PtrVT = Src1.getValueType();
157 // A two-CLC sequence is a clear win over a loop, not least because it
158 // needs only one branch. A three-CLC sequence needs the same number
159 // of branches as a loop (i.e. 2), but is shorter. That brings us to
160 // lengths greater than 768 bytes. It seems relatively likely that
161 // a difference will be found within the first 768 bytes, so we just
162 // optimize for the smallest number of branch instructions, in order
163 // to avoid polluting the prediction buffer too much. A loop only ever
164 // needs 2 branches, whereas a straight-line sequence would need 3 or more.
165 if (Size > 3 * 256)
166 return DAG.getNode(SystemZISD::CLC_LOOP, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000167 DAG.getConstant(Size, DL, PtrVT),
168 DAG.getConstant(Size / 256, DL, PtrVT));
Richard Sandifordbe133a82013-08-28 09:01:51 +0000169 return DAG.getNode(SystemZISD::CLC, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000170 DAG.getConstant(Size, DL, PtrVT));
Richard Sandifordbe133a82013-08-28 09:01:51 +0000171}
172
Richard Sandifordca232712013-08-16 11:21:54 +0000173// Convert the current CC value into an integer that is 0 if CC == 0,
174// less than zero if CC == 1 and greater than zero if CC >= 2.
175// The sequence starts with IPM, which puts CC into bits 29 and 28
176// of an integer and clears bits 30 and 31.
177static SDValue addIPMSequence(SDLoc DL, SDValue Glue, SelectionDAG &DAG) {
178 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
179 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000180 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Richard Sandifordca232712013-08-16 11:21:54 +0000181 SDValue ROTL = DAG.getNode(ISD::ROTL, DL, MVT::i32, SRL,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000182 DAG.getConstant(31, DL, MVT::i32));
Richard Sandifordca232712013-08-16 11:21:54 +0000183 return ROTL;
184}
185
Richard Sandiford564681c2013-08-12 10:28:10 +0000186std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
187EmitTargetCodeForMemcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
188 SDValue Src1, SDValue Src2, SDValue Size,
189 MachinePointerInfo Op1PtrInfo,
190 MachinePointerInfo Op2PtrInfo) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000191 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000192 uint64_t Bytes = CSize->getZExtValue();
Richard Sandifordbe133a82013-08-28 09:01:51 +0000193 assert(Bytes > 0 && "Caller should have handled 0-size case");
194 Chain = emitCLC(DAG, DL, Chain, Src1, Src2, Bytes);
195 SDValue Glue = Chain.getValue(1);
196 return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain);
Richard Sandiford564681c2013-08-12 10:28:10 +0000197 }
198 return std::make_pair(SDValue(), SDValue());
199}
Richard Sandifordca232712013-08-16 11:21:54 +0000200
201std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000202EmitTargetCodeForMemchr(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
203 SDValue Src, SDValue Char, SDValue Length,
204 MachinePointerInfo SrcPtrInfo) const {
205 // Use SRST to find the character. End is its address on success.
206 EVT PtrVT = Src.getValueType();
207 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other, MVT::Glue);
208 Length = DAG.getZExtOrTrunc(Length, DL, PtrVT);
209 Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32);
210 Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000211 DAG.getConstant(255, DL, MVT::i32));
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000212 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length);
213 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
214 Limit, Src, Char);
215 Chain = End.getValue(1);
216 SDValue Glue = End.getValue(2);
217
218 // Now select between End and null, depending on whether the character
219 // was found.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000220 SDValue Ops[] = {End, DAG.getConstant(0, DL, PtrVT),
221 DAG.getConstant(SystemZ::CCMASK_SRST, DL, MVT::i32),
222 DAG.getConstant(SystemZ::CCMASK_SRST_FOUND, DL, MVT::i32),
223 Glue};
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000224 VTs = DAG.getVTList(PtrVT, MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +0000225 End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000226 return std::make_pair(End, Chain);
227}
228
229std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
Richard Sandifordbb83a502013-08-16 11:29:37 +0000230EmitTargetCodeForStrcpy(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
231 SDValue Dest, SDValue Src,
232 MachinePointerInfo DestPtrInfo,
233 MachinePointerInfo SrcPtrInfo, bool isStpcpy) const {
234 SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other);
235 SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000236 DAG.getConstant(0, DL, MVT::i32));
Richard Sandifordbb83a502013-08-16 11:29:37 +0000237 return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1));
238}
239
240std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
Richard Sandifordca232712013-08-16 11:21:54 +0000241EmitTargetCodeForStrcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
242 SDValue Src1, SDValue Src2,
243 MachinePointerInfo Op1PtrInfo,
244 MachinePointerInfo Op2PtrInfo) const {
245 SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::Other, MVT::Glue);
246 SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000247 DAG.getConstant(0, DL, MVT::i32));
Richard Sandifordca232712013-08-16 11:21:54 +0000248 Chain = Unused.getValue(1);
249 SDValue Glue = Chain.getValue(2);
250 return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain);
251}
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000252
253// Search from Src for a null character, stopping once Src reaches Limit.
254// Return a pair of values, the first being the number of nonnull characters
255// and the second being the out chain.
256//
257// This can be used for strlen by setting Limit to 0.
258static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG, SDLoc DL,
259 SDValue Chain, SDValue Src,
260 SDValue Limit) {
261 EVT PtrVT = Src.getValueType();
262 SDVTList VTs = DAG.getVTList(PtrVT, MVT::Other, MVT::Glue);
263 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000264 Limit, Src, DAG.getConstant(0, DL, MVT::i32));
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000265 Chain = End.getValue(1);
266 SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src);
267 return std::make_pair(Len, Chain);
268}
269
270std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
271EmitTargetCodeForStrlen(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
272 SDValue Src, MachinePointerInfo SrcPtrInfo) const {
273 EVT PtrVT = Src.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000274 return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, DL, PtrVT));
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000275}
276
277std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
278EmitTargetCodeForStrnlen(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
279 SDValue Src, SDValue MaxLength,
280 MachinePointerInfo SrcPtrInfo) const {
281 EVT PtrVT = Src.getValueType();
282 MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT);
283 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength);
284 return getBoundedStrlen(DAG, DL, Chain, Src, Limit);
285}