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