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 // Emit a mem-mem operation after subtracting one from size, which will be
21 // added back during pseudo expansion. As the Reg case emitted here may be
22 // converted by DAGCombiner into having an Imm length, they are both emitted
23 // the same way.
24 static SDValue emitMemMemImm(SelectionDAG &DAG, const SDLoc &DL, unsigned Op,
25                              SDValue Chain, SDValue Dst, SDValue Src,
26                              uint64_t Size) {
27   return DAG.getNode(Op, DL, MVT::Other, Chain, Dst, Src,
28                      DAG.getConstant(Size - 1, DL, Src.getValueType()));
29 }
30 
31 static SDValue emitMemMemReg(SelectionDAG &DAG, const SDLoc &DL, unsigned Op,
32                              SDValue Chain, SDValue Dst, SDValue Src,
33                              SDValue Size) {
34   SDValue LenMinus1 = DAG.getNode(ISD::ADD, DL, MVT::i64,
35                                   DAG.getZExtOrTrunc(Size, DL, MVT::i64),
36                                   DAG.getConstant(-1, DL, MVT::i64));
37   return DAG.getNode(Op, DL, MVT::Other, Chain, Dst, Src, LenMinus1);
38 }
39 
40 // Use CLC to compare [Src1, Src1 + Size) with [Src2, Src2 + Size).
41 // One is subtracted from size also here, per above.
42 static SDValue emitCLC(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
43                        SDValue Src1, SDValue Src2, uint64_t Size) {
44   SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
45   EVT PtrVT = Src1.getValueType();
46   return DAG.getNode(SystemZISD::CLC, DL, VTs, Chain, Src1, Src2,
47                      DAG.getConstant(Size - 1, DL, PtrVT));
48 }
49 
50 SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemcpy(
51     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, SDValue Src,
52     SDValue Size, Align Alignment, bool IsVolatile, bool AlwaysInline,
53     MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
54   if (IsVolatile)
55     return SDValue();
56 
57   if (auto *CSize = dyn_cast<ConstantSDNode>(Size))
58     return emitMemMemImm(DAG, DL, SystemZISD::MVC, Chain, Dst, Src,
59                          CSize->getZExtValue());
60   return SDValue();
61 }
62 
63 // Handle a memset of 1, 2, 4 or 8 bytes with the operands given by
64 // Chain, Dst, ByteVal and Size.  These cases are expected to use
65 // MVI, MVHHI, MVHI and MVGHI respectively.
66 static SDValue memsetStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
67                            SDValue Dst, uint64_t ByteVal, uint64_t Size,
68                            unsigned Align, MachinePointerInfo DstPtrInfo) {
69   uint64_t StoreVal = ByteVal;
70   for (unsigned I = 1; I < Size; ++I)
71     StoreVal |= ByteVal << (I * 8);
72   return DAG.getStore(
73       Chain, DL, DAG.getConstant(StoreVal, DL, MVT::getIntegerVT(Size * 8)),
74       Dst, DstPtrInfo, Align);
75 }
76 
77 SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemset(
78     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst,
79     SDValue Byte, SDValue Size, Align Alignment, bool IsVolatile,
80     MachinePointerInfo DstPtrInfo) const {
81   EVT PtrVT = Dst.getValueType();
82 
83   if (IsVolatile)
84     return SDValue();
85 
86   auto *CByte = dyn_cast<ConstantSDNode>(Byte);
87   if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
88     uint64_t Bytes = CSize->getZExtValue();
89     if (Bytes == 0)
90       return SDValue();
91     if (CByte) {
92       // Handle cases that can be done using at most two of
93       // MVI, MVHI, MVHHI and MVGHI.  The latter two can only be
94       // used if ByteVal is all zeros or all ones; in other casees,
95       // we can move at most 2 halfwords.
96       uint64_t ByteVal = CByte->getZExtValue();
97       if (ByteVal == 0 || ByteVal == 255 ?
98           Bytes <= 16 && countPopulation(Bytes) <= 2 :
99           Bytes <= 4) {
100         unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes);
101         unsigned Size2 = Bytes - Size1;
102         SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1,
103                                      Alignment.value(), DstPtrInfo);
104         if (Size2 == 0)
105           return Chain1;
106         Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
107                           DAG.getConstant(Size1, DL, PtrVT));
108         DstPtrInfo = DstPtrInfo.getWithOffset(Size1);
109         SDValue Chain2 = memsetStore(
110             DAG, DL, Chain, Dst, ByteVal, Size2,
111             std::min((unsigned)Alignment.value(), Size1), DstPtrInfo);
112         return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
113       }
114     } else {
115       // Handle one and two bytes using STC.
116       if (Bytes <= 2) {
117         SDValue Chain1 =
118             DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Alignment);
119         if (Bytes == 1)
120           return Chain1;
121         SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
122                                    DAG.getConstant(1, DL, PtrVT));
123         SDValue Chain2 = DAG.getStore(Chain, DL, Byte, Dst2,
124                                       DstPtrInfo.getWithOffset(1), Align(1));
125         return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
126       }
127     }
128     assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already");
129 
130     // Handle the special case of a memset of 0, which can use XC.
131     if (CByte && CByte->getZExtValue() == 0)
132       return emitMemMemImm(DAG, DL, SystemZISD::XC, Chain, Dst, Dst, Bytes);
133 
134     // Copy the byte to the first location and then use MVC to copy
135     // it to the rest.
136     Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Alignment);
137     SDValue DstPlus1 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
138                                    DAG.getConstant(1, DL, PtrVT));
139     return emitMemMemImm(DAG, DL, SystemZISD::MVC, Chain, DstPlus1, Dst,
140                          Bytes - 1);
141   }
142 
143   // Variable length
144   if (CByte && CByte->getZExtValue() == 0)
145     // Handle the special case of a variable length memset of 0 with XC.
146     return emitMemMemReg(DAG, DL, SystemZISD::XC, Chain, Dst, Dst, Size);
147 
148   return SDValue();
149 }
150 
151 // Convert the current CC value into an integer that is 0 if CC == 0,
152 // greater than zero if CC == 1 and less than zero if CC >= 2.
153 // The sequence starts with IPM, which puts CC into bits 29 and 28
154 // of an integer and clears bits 30 and 31.
155 static SDValue addIPMSequence(const SDLoc &DL, SDValue CCReg,
156                               SelectionDAG &DAG) {
157   SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
158   SDValue SHL = DAG.getNode(ISD::SHL, DL, MVT::i32, IPM,
159                             DAG.getConstant(30 - SystemZ::IPM_CC, DL, MVT::i32));
160   SDValue SRA = DAG.getNode(ISD::SRA, DL, MVT::i32, SHL,
161                             DAG.getConstant(30, DL, MVT::i32));
162   return SRA;
163 }
164 
165 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp(
166     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1,
167     SDValue Src2, SDValue Size, MachinePointerInfo Op1PtrInfo,
168     MachinePointerInfo Op2PtrInfo) const {
169   if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
170     uint64_t Bytes = CSize->getZExtValue();
171     assert(Bytes > 0 && "Caller should have handled 0-size case");
172     // Swap operands to invert CC == 1 vs. CC == 2 cases.
173     SDValue CCReg = emitCLC(DAG, DL, Chain, Src2, Src1, Bytes);
174     Chain = CCReg.getValue(1);
175     return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain);
176   }
177   return std::make_pair(SDValue(), SDValue());
178 }
179 
180 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemchr(
181     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
182     SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const {
183   // Use SRST to find the character.  End is its address on success.
184   EVT PtrVT = Src.getValueType();
185   SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other);
186   Length = DAG.getZExtOrTrunc(Length, DL, PtrVT);
187   Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32);
188   Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char,
189                      DAG.getConstant(255, DL, MVT::i32));
190   SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length);
191   SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
192                             Limit, Src, Char);
193   SDValue CCReg = End.getValue(1);
194   Chain = End.getValue(2);
195 
196   // Now select between End and null, depending on whether the character
197   // was found.
198   SDValue Ops[] = {
199       End, DAG.getConstant(0, DL, PtrVT),
200       DAG.getTargetConstant(SystemZ::CCMASK_SRST, DL, MVT::i32),
201       DAG.getTargetConstant(SystemZ::CCMASK_SRST_FOUND, DL, MVT::i32), CCReg};
202   End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, PtrVT, Ops);
203   return std::make_pair(End, Chain);
204 }
205 
206 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcpy(
207     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest,
208     SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo,
209     bool isStpcpy) const {
210   SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other);
211   SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src,
212                                 DAG.getConstant(0, DL, MVT::i32));
213   return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1));
214 }
215 
216 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcmp(
217     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1,
218     SDValue Src2, MachinePointerInfo Op1PtrInfo,
219     MachinePointerInfo Op2PtrInfo) const {
220   SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::i32, MVT::Other);
221   // Swap operands to invert CC == 1 vs. CC == 2 cases.
222   SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src2, Src1,
223                                DAG.getConstant(0, DL, MVT::i32));
224   SDValue CCReg = Unused.getValue(1);
225   Chain = Unused.getValue(2);
226   return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain);
227 }
228 
229 // Search from Src for a null character, stopping once Src reaches Limit.
230 // Return a pair of values, the first being the number of nonnull characters
231 // and the second being the out chain.
232 //
233 // This can be used for strlen by setting Limit to 0.
234 static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG,
235                                                     const SDLoc &DL,
236                                                     SDValue Chain, SDValue Src,
237                                                     SDValue Limit) {
238   EVT PtrVT = Src.getValueType();
239   SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other);
240   SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
241                             Limit, Src, DAG.getConstant(0, DL, MVT::i32));
242   Chain = End.getValue(2);
243   SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src);
244   return std::make_pair(Len, Chain);
245 }
246 
247 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrlen(
248     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
249     MachinePointerInfo SrcPtrInfo) const {
250   EVT PtrVT = Src.getValueType();
251   return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, DL, PtrVT));
252 }
253 
254 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrnlen(
255     SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
256     SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const {
257   EVT PtrVT = Src.getValueType();
258   MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT);
259   SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength);
260   return getBoundedStrlen(DAG, DL, Chain, Src, Limit);
261 }
262