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