blob: 910128daebc3199daee9ec42f64734cac413d457 [file] [log] [blame]
Richard Sandifordd131ff82013-07-08 09:35:23 +00001//===-- SystemZSelectionDAGInfo.cpp - SystemZ SelectionDAG Info -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Richard Sandifordd131ff82013-07-08 09:35:23 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the SystemZSelectionDAGInfo class.
10//
11//===----------------------------------------------------------------------===//
12
Richard Sandifordd131ff82013-07-08 09:35:23 +000013#include "SystemZTargetMachine.h"
14#include "llvm/CodeGen/SelectionDAG.h"
15
16using namespace llvm;
17
Chandler Carruth84e68b22014-04-22 02:41:26 +000018#define DEBUG_TYPE "systemz-selectiondag-info"
19
Richard Sandiford4943bc32013-09-06 10:25:07 +000020// 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.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000025static SDValue emitMemMem(SelectionDAG &DAG, const SDLoc &DL, unsigned Sequence,
Richard Sandiford4943bc32013-09-06 10:25:07 +000026 unsigned Loop, SDValue Chain, SDValue Dst,
27 SDValue Src, uint64_t Size) {
Richard Sandiford5e318f02013-08-27 09:54:29 +000028 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)
Richard Sandiford4943bc32013-09-06 10:25:07 +000041 return DAG.getNode(Loop, DL, MVT::Other, Chain, Dst, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000042 DAG.getConstant(Size, DL, PtrVT),
43 DAG.getConstant(Size / 256, DL, PtrVT));
Richard Sandiford4943bc32013-09-06 10:25:07 +000044 return DAG.getNode(Sequence, DL, MVT::Other, Chain, Dst, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000045 DAG.getConstant(Size, DL, PtrVT));
Richard Sandiford5e318f02013-08-27 09:54:29 +000046}
47
Benjamin Kramerbdc49562016-06-12 15:39:02 +000048SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemcpy(
49 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst, SDValue Src,
50 SDValue Size, unsigned Align, bool IsVolatile, bool AlwaysInline,
51 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
Richard Sandifordd131ff82013-07-08 09:35:23 +000052 if (IsVolatile)
53 return SDValue();
54
Richard Sandiford21f5d682014-03-06 11:22:58 +000055 if (auto *CSize = dyn_cast<ConstantSDNode>(Size))
Richard Sandiford4943bc32013-09-06 10:25:07 +000056 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP,
57 Chain, Dst, Src, CSize->getZExtValue());
Richard Sandifordd131ff82013-07-08 09:35:23 +000058 return SDValue();
59}
Richard Sandiford47660c12013-07-09 09:32:42 +000060
61// Handle a memset of 1, 2, 4 or 8 bytes with the operands given by
62// Chain, Dst, ByteVal and Size. These cases are expected to use
63// MVI, MVHHI, MVHI and MVGHI respectively.
Benjamin Kramerbdc49562016-06-12 15:39:02 +000064static SDValue memsetStore(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
Richard Sandiford47660c12013-07-09 09:32:42 +000065 SDValue Dst, uint64_t ByteVal, uint64_t Size,
Benjamin Kramerbdc49562016-06-12 15:39:02 +000066 unsigned Align, MachinePointerInfo DstPtrInfo) {
Richard Sandiford47660c12013-07-09 09:32:42 +000067 uint64_t StoreVal = ByteVal;
68 for (unsigned I = 1; I < Size; ++I)
69 StoreVal |= ByteVal << (I * 8);
Justin Lebar9c375812016-07-15 18:27:10 +000070 return DAG.getStore(
71 Chain, DL, DAG.getConstant(StoreVal, DL, MVT::getIntegerVT(Size * 8)),
72 Dst, DstPtrInfo, Align);
Richard Sandiford47660c12013-07-09 09:32:42 +000073}
74
Benjamin Kramerbdc49562016-06-12 15:39:02 +000075SDValue SystemZSelectionDAGInfo::EmitTargetCodeForMemset(
76 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dst,
77 SDValue Byte, SDValue Size, unsigned Align, bool IsVolatile,
78 MachinePointerInfo DstPtrInfo) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +000079 EVT PtrVT = Dst.getValueType();
Richard Sandiford47660c12013-07-09 09:32:42 +000080
81 if (IsVolatile)
82 return SDValue();
83
Richard Sandiford21f5d682014-03-06 11:22:58 +000084 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
Richard Sandiford47660c12013-07-09 09:32:42 +000085 uint64_t Bytes = CSize->getZExtValue();
86 if (Bytes == 0)
87 return SDValue();
Richard Sandiford21f5d682014-03-06 11:22:58 +000088 if (auto *CByte = dyn_cast<ConstantSDNode>(Byte)) {
Richard Sandiford47660c12013-07-09 09:32:42 +000089 // Handle cases that can be done using at most two of
90 // MVI, MVHI, MVHHI and MVGHI. The latter two can only be
91 // used if ByteVal is all zeros or all ones; in other casees,
92 // we can move at most 2 halfwords.
93 uint64_t ByteVal = CByte->getZExtValue();
94 if (ByteVal == 0 || ByteVal == 255 ?
Benjamin Kramer5f6a9072015-02-12 15:35:40 +000095 Bytes <= 16 && countPopulation(Bytes) <= 2 :
Richard Sandiford47660c12013-07-09 09:32:42 +000096 Bytes <= 4) {
97 unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes);
98 unsigned Size2 = Bytes - Size1;
99 SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1,
100 Align, DstPtrInfo);
101 if (Size2 == 0)
102 return Chain1;
Richard Sandiford5e318f02013-08-27 09:54:29 +0000103 Dst = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000104 DAG.getConstant(Size1, DL, PtrVT));
Richard Sandiford47660c12013-07-09 09:32:42 +0000105 DstPtrInfo = DstPtrInfo.getWithOffset(Size1);
106 SDValue Chain2 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size2,
107 std::min(Align, Size1), DstPtrInfo);
108 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
109 }
110 } else {
111 // Handle one and two bytes using STC.
112 if (Bytes <= 2) {
Justin Lebar9c375812016-07-15 18:27:10 +0000113 SDValue Chain1 = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Align);
Richard Sandiford47660c12013-07-09 09:32:42 +0000114 if (Bytes == 1)
115 return Chain1;
Richard Sandiford5e318f02013-08-27 09:54:29 +0000116 SDValue Dst2 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000117 DAG.getConstant(1, DL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +0000118 SDValue Chain2 =
119 DAG.getStore(Chain, DL, Byte, Dst2, DstPtrInfo.getWithOffset(1),
120 /* Alignment = */ 1);
Richard Sandiford47660c12013-07-09 09:32:42 +0000121 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
122 }
123 }
124 assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already");
Richard Sandiford4943bc32013-09-06 10:25:07 +0000125
126 // Handle the special case of a memset of 0, which can use XC.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000127 auto *CByte = dyn_cast<ConstantSDNode>(Byte);
Richard Sandiford4943bc32013-09-06 10:25:07 +0000128 if (CByte && CByte->getZExtValue() == 0)
129 return emitMemMem(DAG, DL, SystemZISD::XC, SystemZISD::XC_LOOP,
130 Chain, Dst, Dst, Bytes);
131
Richard Sandiford5e318f02013-08-27 09:54:29 +0000132 // Copy the byte to the first location and then use MVC to copy
133 // it to the rest.
Justin Lebar9c375812016-07-15 18:27:10 +0000134 Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo, Align);
Richard Sandiford5e318f02013-08-27 09:54:29 +0000135 SDValue DstPlus1 = DAG.getNode(ISD::ADD, DL, PtrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000136 DAG.getConstant(1, DL, PtrVT));
Richard Sandiford4943bc32013-09-06 10:25:07 +0000137 return emitMemMem(DAG, DL, SystemZISD::MVC, SystemZISD::MVC_LOOP,
138 Chain, DstPlus1, Dst, Bytes - 1);
Richard Sandiford47660c12013-07-09 09:32:42 +0000139 }
140 return SDValue();
141}
Richard Sandiford564681c2013-08-12 10:28:10 +0000142
Richard Sandifordbe133a82013-08-28 09:01:51 +0000143// Use CLC to compare [Src1, Src1 + Size) with [Src2, Src2 + Size),
144// deciding whether to use a loop or straight-line code.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000145static SDValue emitCLC(SelectionDAG &DAG, const SDLoc &DL, SDValue Chain,
Richard Sandifordbe133a82013-08-28 09:01:51 +0000146 SDValue Src1, SDValue Src2, uint64_t Size) {
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000147 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
Richard Sandifordbe133a82013-08-28 09:01:51 +0000148 EVT PtrVT = Src1.getValueType();
149 // A two-CLC sequence is a clear win over a loop, not least because it
150 // needs only one branch. A three-CLC sequence needs the same number
151 // of branches as a loop (i.e. 2), but is shorter. That brings us to
152 // lengths greater than 768 bytes. It seems relatively likely that
153 // a difference will be found within the first 768 bytes, so we just
154 // optimize for the smallest number of branch instructions, in order
155 // to avoid polluting the prediction buffer too much. A loop only ever
156 // needs 2 branches, whereas a straight-line sequence would need 3 or more.
157 if (Size > 3 * 256)
158 return DAG.getNode(SystemZISD::CLC_LOOP, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000159 DAG.getConstant(Size, DL, PtrVT),
160 DAG.getConstant(Size / 256, DL, PtrVT));
Richard Sandifordbe133a82013-08-28 09:01:51 +0000161 return DAG.getNode(SystemZISD::CLC, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000162 DAG.getConstant(Size, DL, PtrVT));
Richard Sandifordbe133a82013-08-28 09:01:51 +0000163}
164
Richard Sandifordca232712013-08-16 11:21:54 +0000165// Convert the current CC value into an integer that is 0 if CC == 0,
166// less than zero if CC == 1 and greater than zero if CC >= 2.
167// The sequence starts with IPM, which puts CC into bits 29 and 28
168// of an integer and clears bits 30 and 31.
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000169static SDValue addIPMSequence(const SDLoc &DL, SDValue CCReg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000170 SelectionDAG &DAG) {
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000171 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
Richard Sandifordca232712013-08-16 11:21:54 +0000172 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000173 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Richard Sandifordca232712013-08-16 11:21:54 +0000174 SDValue ROTL = DAG.getNode(ISD::ROTL, DL, MVT::i32, SRL,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000175 DAG.getConstant(31, DL, MVT::i32));
Richard Sandifordca232712013-08-16 11:21:54 +0000176 return ROTL;
177}
178
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000179std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemcmp(
180 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1,
181 SDValue Src2, SDValue Size, MachinePointerInfo Op1PtrInfo,
182 MachinePointerInfo Op2PtrInfo) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000183 if (auto *CSize = dyn_cast<ConstantSDNode>(Size)) {
Richard Sandiford564681c2013-08-12 10:28:10 +0000184 uint64_t Bytes = CSize->getZExtValue();
Richard Sandifordbe133a82013-08-28 09:01:51 +0000185 assert(Bytes > 0 && "Caller should have handled 0-size case");
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000186 SDValue CCReg = emitCLC(DAG, DL, Chain, Src1, Src2, Bytes);
187 Chain = CCReg.getValue(1);
188 return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain);
Richard Sandiford564681c2013-08-12 10:28:10 +0000189 }
190 return std::make_pair(SDValue(), SDValue());
191}
Richard Sandifordca232712013-08-16 11:21:54 +0000192
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000193std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForMemchr(
194 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
195 SDValue Char, SDValue Length, MachinePointerInfo SrcPtrInfo) const {
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000196 // Use SRST to find the character. End is its address on success.
197 EVT PtrVT = Src.getValueType();
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000198 SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other);
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000199 Length = DAG.getZExtOrTrunc(Length, DL, PtrVT);
200 Char = DAG.getZExtOrTrunc(Char, DL, MVT::i32);
201 Char = DAG.getNode(ISD::AND, DL, MVT::i32, Char,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000202 DAG.getConstant(255, DL, MVT::i32));
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000203 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, Length);
204 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
205 Limit, Src, Char);
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000206 SDValue CCReg = End.getValue(1);
207 Chain = End.getValue(2);
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000208
209 // Now select between End and null, depending on whether the character
210 // was found.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000211 SDValue Ops[] = {End, DAG.getConstant(0, DL, PtrVT),
212 DAG.getConstant(SystemZ::CCMASK_SRST, DL, MVT::i32),
213 DAG.getConstant(SystemZ::CCMASK_SRST_FOUND, DL, MVT::i32),
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000214 CCReg};
215 End = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, PtrVT, Ops);
Richard Sandiford6f6d5512013-08-20 09:38:48 +0000216 return std::make_pair(End, Chain);
217}
218
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000219std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcpy(
220 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Dest,
221 SDValue Src, MachinePointerInfo DestPtrInfo, MachinePointerInfo SrcPtrInfo,
222 bool isStpcpy) const {
Richard Sandifordbb83a502013-08-16 11:29:37 +0000223 SDVTList VTs = DAG.getVTList(Dest.getValueType(), MVT::Other);
224 SDValue EndDest = DAG.getNode(SystemZISD::STPCPY, DL, VTs, Chain, Dest, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000225 DAG.getConstant(0, DL, MVT::i32));
Richard Sandifordbb83a502013-08-16 11:29:37 +0000226 return std::make_pair(isStpcpy ? EndDest : Dest, EndDest.getValue(1));
227}
228
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000229std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrcmp(
230 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src1,
231 SDValue Src2, MachinePointerInfo Op1PtrInfo,
232 MachinePointerInfo Op2PtrInfo) const {
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000233 SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::i32, MVT::Other);
Richard Sandifordca232712013-08-16 11:21:54 +0000234 SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src1, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000235 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000236 SDValue CCReg = Unused.getValue(1);
237 Chain = Unused.getValue(2);
238 return std::make_pair(addIPMSequence(DL, CCReg, DAG), Chain);
Richard Sandifordca232712013-08-16 11:21:54 +0000239}
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000240
241// Search from Src for a null character, stopping once Src reaches Limit.
242// Return a pair of values, the first being the number of nonnull characters
243// and the second being the out chain.
244//
245// This can be used for strlen by setting Limit to 0.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000246static std::pair<SDValue, SDValue> getBoundedStrlen(SelectionDAG &DAG,
247 const SDLoc &DL,
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000248 SDValue Chain, SDValue Src,
249 SDValue Limit) {
250 EVT PtrVT = Src.getValueType();
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000251 SDVTList VTs = DAG.getVTList(PtrVT, MVT::i32, MVT::Other);
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000252 SDValue End = DAG.getNode(SystemZISD::SEARCH_STRING, DL, VTs, Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000253 Limit, Src, DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigandb32f3652018-04-30 17:52:32 +0000254 Chain = End.getValue(2);
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000255 SDValue Len = DAG.getNode(ISD::SUB, DL, PtrVT, End, Src);
256 return std::make_pair(Len, Chain);
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000257}
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000258
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000259std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrlen(
260 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
261 MachinePointerInfo SrcPtrInfo) const {
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000262 EVT PtrVT = Src.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000263 return getBoundedStrlen(DAG, DL, Chain, Src, DAG.getConstant(0, DL, PtrVT));
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000264}
265
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000266std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::EmitTargetCodeForStrnlen(
267 SelectionDAG &DAG, const SDLoc &DL, SDValue Chain, SDValue Src,
268 SDValue MaxLength, MachinePointerInfo SrcPtrInfo) const {
Richard Sandiford0dec06a2013-08-16 11:41:43 +0000269 EVT PtrVT = Src.getValueType();
270 MaxLength = DAG.getZExtOrTrunc(MaxLength, DL, PtrVT);
271 SDValue Limit = DAG.getNode(ISD::ADD, DL, PtrVT, Src, MaxLength);
272 return getBoundedStrlen(DAG, DL, Chain, Src, Limit);
273}