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