blob: c6bb86f93f1e6852492876a342933558f84dbd79 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the SystemZTargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
Ulrich Weigand5f613df2013-05-06 16:15:19 +000014#include "SystemZISelLowering.h"
15#include "SystemZCallingConv.h"
16#include "SystemZConstantPoolValue.h"
17#include "SystemZMachineFunctionInfo.h"
18#include "SystemZTargetMachine.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineRegisterInfo.h"
22#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Ulrich Weigand57c85f52015-04-01 12:51:43 +000023#include "llvm/IR/Intrinsics.h"
Reid Kleckner0e8c4bb2017-09-07 23:27:44 +000024#include "llvm/IR/IntrinsicInst.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000025#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/KnownBits.h"
Will Dietz981af002013-10-12 00:55:57 +000027#include <cctype>
28
Ulrich Weigand5f613df2013-05-06 16:15:19 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "systemz-lower"
32
Richard Sandifordf722a8e302013-10-16 11:10:55 +000033namespace {
Richard Sandifordd420f732013-12-13 15:28:45 +000034// Represents information about a comparison.
35struct Comparison {
36 Comparison(SDValue Op0In, SDValue Op1In)
37 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
38
39 // The operands to the comparison.
40 SDValue Op0, Op1;
41
42 // The opcode that should be used to compare Op0 and Op1.
43 unsigned Opcode;
44
45 // A SystemZICMP value. Only used for integer comparisons.
46 unsigned ICmpType;
47
48 // The mask of CC values that Opcode can produce.
49 unsigned CCValid;
50
51 // The mask of CC values for which the original condition is true.
52 unsigned CCMask;
53};
Richard Sandifordc2312692014-03-06 10:38:30 +000054} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000055
Ulrich Weigand5f613df2013-05-06 16:15:19 +000056// Classify VT as either 32 or 64 bit.
57static bool is32Bit(EVT VT) {
58 switch (VT.getSimpleVT().SimpleTy) {
59 case MVT::i32:
60 return true;
61 case MVT::i64:
62 return false;
63 default:
64 llvm_unreachable("Unsupported type");
65 }
66}
67
68// Return a version of MachineOperand that can be safely used before the
69// final use.
70static MachineOperand earlyUseOperand(MachineOperand Op) {
71 if (Op.isReg())
72 Op.setIsKill(false);
73 return Op;
74}
75
Mehdi Amini44ede332015-07-09 02:09:04 +000076SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000077 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000078 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000079 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000080
81 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000082 if (Subtarget.hasHighWord())
83 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
84 else
85 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000086 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
87 if (Subtarget.hasVector()) {
88 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
89 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
90 } else {
91 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
92 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
93 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +000094 if (Subtarget.hasVectorEnhancements1())
95 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
96 else
97 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +000098
Ulrich Weigandce4c1092015-05-05 19:25:42 +000099 if (Subtarget.hasVector()) {
100 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
101 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
102 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
103 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000104 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000105 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000106 }
107
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000109 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000110
111 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000112 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
113
114 // TODO: It may be better to default to latency-oriented scheduling, however
115 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000116 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000117 // scheduler, because it can.
118 setSchedulingPreference(Sched::RegPressure);
119
120 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000121 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122
123 // Instructions are strings of 2-byte aligned 2-byte values.
124 setMinFunctionAlignment(2);
125
126 // Handle operations that are handled in a similar way for all types.
127 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
128 I <= MVT::LAST_FP_VALUETYPE;
129 ++I) {
130 MVT VT = MVT::SimpleValueType(I);
131 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000132 // Lower SET_CC into an IPM-based sequence.
133 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134
135 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
136 setOperationAction(ISD::SELECT, VT, Expand);
137
138 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
139 setOperationAction(ISD::SELECT_CC, VT, Custom);
140 setOperationAction(ISD::BR_CC, VT, Custom);
141 }
142 }
143
144 // Expand jump table branches as address arithmetic followed by an
145 // indirect jump.
146 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
147
148 // Expand BRCOND into a BR_CC (see above).
149 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
150
151 // Handle integer types.
152 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
153 I <= MVT::LAST_INTEGER_VALUETYPE;
154 ++I) {
155 MVT VT = MVT::SimpleValueType(I);
156 if (isTypeLegal(VT)) {
157 // Expand individual DIV and REMs into DIVREMs.
158 setOperationAction(ISD::SDIV, VT, Expand);
159 setOperationAction(ISD::UDIV, VT, Expand);
160 setOperationAction(ISD::SREM, VT, Expand);
161 setOperationAction(ISD::UREM, VT, Expand);
162 setOperationAction(ISD::SDIVREM, VT, Custom);
163 setOperationAction(ISD::UDIVREM, VT, Custom);
164
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000165 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
166 // stores, putting a serialization instruction after the stores.
167 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
168 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000169
Richard Sandiford41350a52013-12-24 15:18:04 +0000170 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
171 // available, or if the operand is constant.
172 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
173
Ulrich Weigandb4012182015-03-31 12:56:33 +0000174 // Use POPCNT on z196 and above.
175 if (Subtarget.hasPopulationCount())
176 setOperationAction(ISD::CTPOP, VT, Custom);
177 else
178 setOperationAction(ISD::CTPOP, VT, Expand);
179
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 setOperationAction(ISD::ROTR, VT, Expand);
183
Richard Sandiford7d86e472013-08-21 09:34:56 +0000184 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 setOperationAction(ISD::MULHS, VT, Expand);
186 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000187 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
188 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000189
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000190 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000191 // On z10, promoting to i64 doesn't generate an inexact condition for
192 // values that are outside the i32 range but in the i64 range, so use
193 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000194 if (!Subtarget.hasFPExtension())
195 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 }
197 }
198
199 // Type legalization will convert 8- and 16-bit atomic operations into
200 // forms that operate on i32s (but still keeping the original memory VT).
201 // Lower them into full i32 operations.
202 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
203 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
204 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
205 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
206 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000214 // Even though i128 is not a legal type, we still need to custom lower
215 // the atomic operations in order to exploit SystemZ instructions.
216 setOperationAction(ISD::ATOMIC_LOAD, MVT::i128, Custom);
217 setOperationAction(ISD::ATOMIC_STORE, MVT::i128, Custom);
Ulrich Weigand0f1de042017-09-28 16:22:54 +0000218
219 // We can use the CC result of compare-and-swap to implement
220 // the "success" result of ATOMIC_CMP_SWAP_WITH_SUCCESS.
221 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Custom);
223 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000224
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000225 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
226
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000227 // Traps are legal, as we will convert them to "j .+2".
228 setOperationAction(ISD::TRAP, MVT::Other, Legal);
229
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000230 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000232 if (!Subtarget.hasFPExtension()) {
233 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
234 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
235 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000236
237 // We have native support for a 64-bit CTLZ, via FLOGR.
238 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
239 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
240
241 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
242 setOperationAction(ISD::OR, MVT::i64, Custom);
243
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 // FIXME: Can we support these natively?
245 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
246 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
247 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
248
249 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000251 for (MVT VT : MVT::integer_valuetypes()) {
252 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
253 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
254 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
255 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000256
257 // Handle the various types of symbolic address.
258 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
259 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
260 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
261 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
262 setOperationAction(ISD::JumpTable, PtrVT, Custom);
263
264 // We need to handle dynamic allocations specially because of the
265 // 160-byte area at the bottom of the stack.
266 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000267 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000268
269 // Use custom expanders so that we can force the function to use
270 // a frame pointer.
271 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
272 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
273
Richard Sandiford03481332013-08-23 11:36:42 +0000274 // Handle prefetches with PFD or PFDRL.
275 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
276
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000277 for (MVT VT : MVT::vector_valuetypes()) {
278 // Assume by default that all vector operations need to be expanded.
279 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
280 if (getOperationAction(Opcode, VT) == Legal)
281 setOperationAction(Opcode, VT, Expand);
282
283 // Likewise all truncating stores and extending loads.
284 for (MVT InnerVT : MVT::vector_valuetypes()) {
285 setTruncStoreAction(VT, InnerVT, Expand);
286 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
287 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
288 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
289 }
290
291 if (isTypeLegal(VT)) {
292 // These operations are legal for anything that can be stored in a
293 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000294 // as such. In particular, we can do these for v4f32 even though there
295 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000296 setOperationAction(ISD::LOAD, VT, Legal);
297 setOperationAction(ISD::STORE, VT, Legal);
298 setOperationAction(ISD::VSELECT, VT, Legal);
299 setOperationAction(ISD::BITCAST, VT, Legal);
300 setOperationAction(ISD::UNDEF, VT, Legal);
301
302 // Likewise, except that we need to replace the nodes with something
303 // more specific.
304 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
305 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
306 }
307 }
308
309 // Handle integer vector types.
310 for (MVT VT : MVT::integer_vector_valuetypes()) {
311 if (isTypeLegal(VT)) {
312 // These operations have direct equivalents.
313 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
314 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
315 setOperationAction(ISD::ADD, VT, Legal);
316 setOperationAction(ISD::SUB, VT, Legal);
317 if (VT != MVT::v2i64)
318 setOperationAction(ISD::MUL, VT, Legal);
319 setOperationAction(ISD::AND, VT, Legal);
320 setOperationAction(ISD::OR, VT, Legal);
321 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000322 if (Subtarget.hasVectorEnhancements1())
323 setOperationAction(ISD::CTPOP, VT, Legal);
324 else
325 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000326 setOperationAction(ISD::CTTZ, VT, Legal);
327 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000328
329 // Convert a GPR scalar to a vector by inserting it into element 0.
330 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
331
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000332 // Use a series of unpacks for extensions.
333 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
334 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
335
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000336 // Detect shifts by a scalar amount and convert them into
337 // V*_BY_SCALAR.
338 setOperationAction(ISD::SHL, VT, Custom);
339 setOperationAction(ISD::SRA, VT, Custom);
340 setOperationAction(ISD::SRL, VT, Custom);
341
342 // At present ROTL isn't matched by DAGCombiner. ROTR should be
343 // converted into ROTL.
344 setOperationAction(ISD::ROTL, VT, Expand);
345 setOperationAction(ISD::ROTR, VT, Expand);
346
347 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
348 // and inverting the result as necessary.
349 setOperationAction(ISD::SETCC, VT, Custom);
350 }
351 }
352
Ulrich Weigandcd808232015-05-05 19:26:48 +0000353 if (Subtarget.hasVector()) {
354 // There should be no need to check for float types other than v2f64
355 // since <2 x f32> isn't a legal type.
356 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000357 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000358 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000359 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000360 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000361 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000362 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000363 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000364 }
365
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000366 // Handle floating-point types.
367 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
368 I <= MVT::LAST_FP_VALUETYPE;
369 ++I) {
370 MVT VT = MVT::SimpleValueType(I);
371 if (isTypeLegal(VT)) {
372 // We can use FI for FRINT.
373 setOperationAction(ISD::FRINT, VT, Legal);
374
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000375 // We can use the extended form of FI for other rounding operations.
376 if (Subtarget.hasFPExtension()) {
377 setOperationAction(ISD::FNEARBYINT, VT, Legal);
378 setOperationAction(ISD::FFLOOR, VT, Legal);
379 setOperationAction(ISD::FCEIL, VT, Legal);
380 setOperationAction(ISD::FTRUNC, VT, Legal);
381 setOperationAction(ISD::FROUND, VT, Legal);
382 }
383
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000384 // No special instructions for these.
385 setOperationAction(ISD::FSIN, VT, Expand);
386 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000387 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000388 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000389 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 }
391 }
392
Ulrich Weigandcd808232015-05-05 19:26:48 +0000393 // Handle floating-point vector types.
394 if (Subtarget.hasVector()) {
395 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000396 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000397 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
398
399 // Some insertions and extractions can be done directly but others
400 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000401 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000402 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000403 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000404 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
405
406 // These operations have direct equivalents.
407 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
408 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
409 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
410 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
411 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
412 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
413 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
414 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
415 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
416 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
417 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
418 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
419 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
420 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
421 }
422
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000423 // The vector enhancements facility 1 has instructions for these.
424 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000425 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
426 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
427 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
428 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
429 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
430 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
431 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
432 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
433 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
434 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
435 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
436 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
437 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
438 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
439
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000440 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
441 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
442 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
443 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
444
445 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
446 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
447 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
448 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000449
450 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
451 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
452 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
453 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
454
455 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
456 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
457 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
458 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000459
460 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
461 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
462 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
463 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000464 }
465
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 // We have fused multiply-addition for f32 and f64 but not f128.
467 setOperationAction(ISD::FMA, MVT::f32, Legal);
468 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000469 if (Subtarget.hasVectorEnhancements1())
470 setOperationAction(ISD::FMA, MVT::f128, Legal);
471 else
472 setOperationAction(ISD::FMA, MVT::f128, Expand);
473
474 // We don't have a copysign instruction on vector registers.
475 if (Subtarget.hasVectorEnhancements1())
476 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000477
478 // Needed so that we don't try to implement f128 constant loads using
479 // a load-and-extend of a f80 constant (in cases where the constant
480 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000481 for (MVT VT : MVT::fp_valuetypes())
482 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000484 // We don't have extending load instruction on vector registers.
485 if (Subtarget.hasVectorEnhancements1()) {
486 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
487 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
488 }
489
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000490 // Floating-point truncation and stores need to be done separately.
491 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
492 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
493 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
494
495 // We have 64-bit FPR<->GPR moves, but need special handling for
496 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000497 if (!Subtarget.hasVector()) {
498 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
499 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
500 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000501
502 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
503 // structure, but VAEND is a no-op.
504 setOperationAction(ISD::VASTART, MVT::Other, Custom);
505 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
506 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000507
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000508 // Codes for which we want to perform some z-specific combinations.
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000509 setTargetDAGCombine(ISD::ZERO_EXTEND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000510 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigand849a59f2018-01-19 20:52:04 +0000511 setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000512 setTargetDAGCombine(ISD::STORE);
513 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000514 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000515 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000516 setTargetDAGCombine(ISD::SHL);
517 setTargetDAGCombine(ISD::SRA);
518 setTargetDAGCombine(ISD::SRL);
519 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000520
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000521 // Handle intrinsics.
522 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000523 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000524
Richard Sandifordd131ff82013-07-08 09:35:23 +0000525 // We want to use MVC in preference to even a single load/store pair.
526 MaxStoresPerMemcpy = 0;
527 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000528
529 // The main memset sequence is a byte store followed by an MVC.
530 // Two STC or MV..I stores win over that, but the kind of fused stores
531 // generated by target-independent code don't when the byte value is
532 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
533 // than "STC;MVC". Handle the choice in target-specific code instead.
534 MaxStoresPerMemset = 0;
535 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000536}
537
Mehdi Amini44ede332015-07-09 02:09:04 +0000538EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
539 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000540 if (!VT.isVector())
541 return MVT::i32;
542 return VT.changeVectorElementTypeToInteger();
543}
544
545bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000546 VT = VT.getScalarType();
547
548 if (!VT.isSimple())
549 return false;
550
551 switch (VT.getSimpleVT().SimpleTy) {
552 case MVT::f32:
553 case MVT::f64:
554 return true;
555 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000556 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000557 default:
558 break;
559 }
560
561 return false;
562}
563
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000564bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
565 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
566 return Imm.isZero() || Imm.isNegZero();
567}
568
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000569bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
570 // We can use CGFI or CLGFI.
571 return isInt<32>(Imm) || isUInt<32>(Imm);
572}
573
574bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
575 // We can use ALGFI or SLGFI.
576 return isUInt<32>(Imm) || isUInt<32>(-Imm);
577}
578
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000579bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
580 unsigned,
581 unsigned,
582 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000583 // Unaligned accesses should never be slower than the expanded version.
584 // We check specifically for aligned accesses in the few cases where
585 // they are required.
586 if (Fast)
587 *Fast = true;
588 return true;
589}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000590
Jonas Paulsson024e3192017-07-21 11:59:37 +0000591// Information about the addressing mode for a memory access.
592struct AddressingMode {
593 // True if a long displacement is supported.
594 bool LongDisplacement;
595
596 // True if use of index register is supported.
597 bool IndexReg;
598
599 AddressingMode(bool LongDispl, bool IdxReg) :
600 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
601};
602
603// Return the desired addressing mode for a Load which has only one use (in
604// the same block) which is a Store.
605static AddressingMode getLoadStoreAddrMode(bool HasVector,
606 Type *Ty) {
607 // With vector support a Load->Store combination may be combined to either
608 // an MVC or vector operations and it seems to work best to allow the
609 // vector addressing mode.
610 if (HasVector)
611 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
612
613 // Otherwise only the MVC case is special.
614 bool MVC = Ty->isIntegerTy(8);
615 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
616}
617
618// Return the addressing mode which seems most desirable given an LLVM
619// Instruction pointer.
620static AddressingMode
621supportedAddressingMode(Instruction *I, bool HasVector) {
622 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
623 switch (II->getIntrinsicID()) {
624 default: break;
625 case Intrinsic::memset:
626 case Intrinsic::memmove:
627 case Intrinsic::memcpy:
628 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
629 }
630 }
631
632 if (isa<LoadInst>(I) && I->hasOneUse()) {
633 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
634 if (SingleUser->getParent() == I->getParent()) {
635 if (isa<ICmpInst>(SingleUser)) {
636 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
Jonas Paulssoncc5fe732018-01-31 12:41:25 +0000637 if (C->getBitWidth() <= 64 &&
638 (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue())))
Jonas Paulsson024e3192017-07-21 11:59:37 +0000639 // Comparison of memory with 16 bit signed / unsigned immediate
640 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
641 } else if (isa<StoreInst>(SingleUser))
642 // Load->Store
643 return getLoadStoreAddrMode(HasVector, I->getType());
644 }
645 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
646 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
647 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
648 // Load->Store
649 return getLoadStoreAddrMode(HasVector, LoadI->getType());
650 }
651
652 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
653
654 // * Use LDE instead of LE/LEY for z13 to avoid partial register
655 // dependencies (LDE only supports small offsets).
656 // * Utilize the vector registers to hold floating point
657 // values (vector load / store instructions only support small
658 // offsets).
659
660 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
661 I->getOperand(0)->getType());
662 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
663 bool IsVectorAccess = MemAccessTy->isVectorTy();
664
665 // A store of an extracted vector element will be combined into a VSTE type
666 // instruction.
667 if (!IsVectorAccess && isa<StoreInst>(I)) {
668 Value *DataOp = I->getOperand(0);
669 if (isa<ExtractElementInst>(DataOp))
670 IsVectorAccess = true;
671 }
672
673 // A load which gets inserted into a vector element will be combined into a
674 // VLE type instruction.
675 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
676 User *LoadUser = *I->user_begin();
677 if (isa<InsertElementInst>(LoadUser))
678 IsVectorAccess = true;
679 }
680
681 if (IsFPAccess || IsVectorAccess)
682 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
683 }
684
685 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
686}
687
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000688bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000689 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000690 // Punt on globals for now, although they can be used in limited
691 // RELATIVE LONG cases.
692 if (AM.BaseGV)
693 return false;
694
695 // Require a 20-bit signed offset.
696 if (!isInt<20>(AM.BaseOffs))
697 return false;
698
Jonas Paulsson6228aed2017-08-09 11:28:01 +0000699 AddressingMode SupportedAM(true, true);
700 if (I != nullptr)
701 SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
702
703 if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
704 return false;
705
706 if (!SupportedAM.IndexReg)
Jonas Paulsson024e3192017-07-21 11:59:37 +0000707 // No indexing allowed.
708 return AM.Scale == 0;
709 else
710 // Indexing is OK but no scale factor can be applied.
711 return AM.Scale == 0 || AM.Scale == 1;
Richard Sandiford791bea42013-07-31 12:58:26 +0000712}
713
Richard Sandiford709bda62013-08-19 12:42:31 +0000714bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
715 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
716 return false;
717 unsigned FromBits = FromType->getPrimitiveSizeInBits();
718 unsigned ToBits = ToType->getPrimitiveSizeInBits();
719 return FromBits > ToBits;
720}
721
722bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
723 if (!FromVT.isInteger() || !ToVT.isInteger())
724 return false;
725 unsigned FromBits = FromVT.getSizeInBits();
726 unsigned ToBits = ToVT.getSizeInBits();
727 return FromBits > ToBits;
728}
729
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000730//===----------------------------------------------------------------------===//
731// Inline asm support
732//===----------------------------------------------------------------------===//
733
734TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000735SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000736 if (Constraint.size() == 1) {
737 switch (Constraint[0]) {
738 case 'a': // Address register
739 case 'd': // Data register (equivalent to 'r')
740 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000741 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000742 case 'r': // General-purpose register
Ulrich Weigand18f69302018-03-02 20:36:34 +0000743 case 'v': // Vector register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000744 return C_RegisterClass;
745
746 case 'Q': // Memory with base and unsigned 12-bit displacement
747 case 'R': // Likewise, plus an index
748 case 'S': // Memory with base and signed 20-bit displacement
749 case 'T': // Likewise, plus an index
750 case 'm': // Equivalent to 'T'.
751 return C_Memory;
752
753 case 'I': // Unsigned 8-bit constant
754 case 'J': // Unsigned 12-bit constant
755 case 'K': // Signed 16-bit constant
756 case 'L': // Signed 20-bit displacement (on all targets we support)
757 case 'M': // 0x7fffffff
758 return C_Other;
759
760 default:
761 break;
762 }
763 }
764 return TargetLowering::getConstraintType(Constraint);
765}
766
767TargetLowering::ConstraintWeight SystemZTargetLowering::
768getSingleConstraintMatchWeight(AsmOperandInfo &info,
769 const char *constraint) const {
770 ConstraintWeight weight = CW_Invalid;
771 Value *CallOperandVal = info.CallOperandVal;
772 // If we don't have a value, we can't do a match,
773 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000774 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000775 return CW_Default;
776 Type *type = CallOperandVal->getType();
777 // Look at the constraint type.
778 switch (*constraint) {
779 default:
780 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
781 break;
782
783 case 'a': // Address register
784 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000785 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000786 case 'r': // General-purpose register
787 if (CallOperandVal->getType()->isIntegerTy())
788 weight = CW_Register;
789 break;
790
791 case 'f': // Floating-point register
792 if (type->isFloatingPointTy())
793 weight = CW_Register;
794 break;
795
Ulrich Weigand18f69302018-03-02 20:36:34 +0000796 case 'v': // Vector register
797 if ((type->isVectorTy() || type->isFloatingPointTy()) &&
798 Subtarget.hasVector())
799 weight = CW_Register;
800 break;
801
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000802 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000803 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000804 if (isUInt<8>(C->getZExtValue()))
805 weight = CW_Constant;
806 break;
807
808 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000809 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000810 if (isUInt<12>(C->getZExtValue()))
811 weight = CW_Constant;
812 break;
813
814 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000815 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000816 if (isInt<16>(C->getSExtValue()))
817 weight = CW_Constant;
818 break;
819
820 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000821 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000822 if (isInt<20>(C->getSExtValue()))
823 weight = CW_Constant;
824 break;
825
826 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000827 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000828 if (C->getZExtValue() == 0x7fffffff)
829 weight = CW_Constant;
830 break;
831 }
832 return weight;
833}
834
Richard Sandifordb8204052013-07-12 09:08:12 +0000835// Parse a "{tNNN}" register constraint for which the register type "t"
836// has already been verified. MC is the class associated with "t" and
837// Map maps 0-based register numbers to LLVM register numbers.
838static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000839parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000840 const unsigned *Map, unsigned Size) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000841 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
842 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000843 unsigned Index;
844 bool Failed =
845 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
Ulrich Weigand18f69302018-03-02 20:36:34 +0000846 if (!Failed && Index < Size && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000847 return std::make_pair(Map[Index], RC);
848 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000849 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000850}
851
Eric Christopher11e4df72015-02-26 22:38:43 +0000852std::pair<unsigned, const TargetRegisterClass *>
853SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000854 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000855 if (Constraint.size() == 1) {
856 // GCC Constraint Letters
857 switch (Constraint[0]) {
858 default: break;
859 case 'd': // Data register (equivalent to 'r')
860 case 'r': // General-purpose register
861 if (VT == MVT::i64)
862 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
863 else if (VT == MVT::i128)
864 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
865 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
866
867 case 'a': // Address register
868 if (VT == MVT::i64)
869 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
870 else if (VT == MVT::i128)
871 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
872 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
873
Richard Sandiford0755c932013-10-01 11:26:28 +0000874 case 'h': // High-part register (an LLVM extension)
875 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
876
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000877 case 'f': // Floating-point register
878 if (VT == MVT::f64)
879 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
880 else if (VT == MVT::f128)
881 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
882 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
Ulrich Weigand18f69302018-03-02 20:36:34 +0000883
884 case 'v': // Vector register
885 if (Subtarget.hasVector()) {
886 if (VT == MVT::f32)
887 return std::make_pair(0U, &SystemZ::VR32BitRegClass);
888 if (VT == MVT::f64)
889 return std::make_pair(0U, &SystemZ::VR64BitRegClass);
890 return std::make_pair(0U, &SystemZ::VR128BitRegClass);
891 }
892 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000893 }
894 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000895 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000896 // We need to override the default register parsing for GPRs and FPRs
897 // because the interpretation depends on VT. The internal names of
898 // the registers are also different from the external names
899 // (F0D and F0S instead of F0, etc.).
900 if (Constraint[1] == 'r') {
901 if (VT == MVT::i32)
902 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000903 SystemZMC::GR32Regs, 16);
Richard Sandifordb8204052013-07-12 09:08:12 +0000904 if (VT == MVT::i128)
905 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000906 SystemZMC::GR128Regs, 16);
Richard Sandifordb8204052013-07-12 09:08:12 +0000907 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000908 SystemZMC::GR64Regs, 16);
Richard Sandifordb8204052013-07-12 09:08:12 +0000909 }
910 if (Constraint[1] == 'f') {
911 if (VT == MVT::f32)
912 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000913 SystemZMC::FP32Regs, 16);
Richard Sandifordb8204052013-07-12 09:08:12 +0000914 if (VT == MVT::f128)
915 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000916 SystemZMC::FP128Regs, 16);
Richard Sandifordb8204052013-07-12 09:08:12 +0000917 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
Ulrich Weigand18f69302018-03-02 20:36:34 +0000918 SystemZMC::FP64Regs, 16);
919 }
920 if (Constraint[1] == 'v') {
921 if (VT == MVT::f32)
922 return parseRegisterNumber(Constraint, &SystemZ::VR32BitRegClass,
923 SystemZMC::VR32Regs, 32);
924 if (VT == MVT::f64)
925 return parseRegisterNumber(Constraint, &SystemZ::VR64BitRegClass,
926 SystemZMC::VR64Regs, 32);
927 return parseRegisterNumber(Constraint, &SystemZ::VR128BitRegClass,
928 SystemZMC::VR128Regs, 32);
Richard Sandifordb8204052013-07-12 09:08:12 +0000929 }
930 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000931 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000932}
933
934void SystemZTargetLowering::
935LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
936 std::vector<SDValue> &Ops,
937 SelectionDAG &DAG) const {
938 // Only support length 1 constraints for now.
939 if (Constraint.length() == 1) {
940 switch (Constraint[0]) {
941 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000942 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000943 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000944 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000945 Op.getValueType()));
946 return;
947
948 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000949 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000950 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000951 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000952 Op.getValueType()));
953 return;
954
955 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000956 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000957 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000958 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000959 Op.getValueType()));
960 return;
961
962 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000963 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000964 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000965 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000966 Op.getValueType()));
967 return;
968
969 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000970 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000971 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000972 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000973 Op.getValueType()));
974 return;
975 }
976 }
977 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
978}
979
980//===----------------------------------------------------------------------===//
981// Calling conventions
982//===----------------------------------------------------------------------===//
983
984#include "SystemZGenCallingConv.inc"
985
Ulrich Weigand5eb64112018-03-02 20:39:30 +0000986const MCPhysReg *SystemZTargetLowering::getScratchRegisters(
987 CallingConv::ID) const {
988 static const MCPhysReg ScratchRegs[] = { SystemZ::R0D, SystemZ::R1D,
989 SystemZ::R14D, 0 };
990 return ScratchRegs;
991}
992
Richard Sandiford709bda62013-08-19 12:42:31 +0000993bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
994 Type *ToType) const {
995 return isTruncateFree(FromType, ToType);
996}
997
Matt Arsenault31380752017-04-18 21:16:46 +0000998bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000999 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +00001000}
1001
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001002// We do not yet support 128-bit single-element vector types. If the user
1003// attempts to use such types as function argument or return type, prefer
1004// to error out instead of emitting code violating the ABI.
1005static void VerifyVectorType(MVT VT, EVT ArgVT) {
1006 if (ArgVT.isVector() && !VT.isVector())
1007 report_fatal_error("Unsupported vector argument or return type");
1008}
1009
1010static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
1011 for (unsigned i = 0; i < Ins.size(); ++i)
1012 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
1013}
1014
1015static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1016 for (unsigned i = 0; i < Outs.size(); ++i)
1017 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
1018}
1019
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001020// Value is a value that has been passed to us in the location described by VA
1021// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
1022// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001023static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001024 CCValAssign &VA, SDValue Chain,
1025 SDValue Value) {
1026 // If the argument has been promoted from a smaller type, insert an
1027 // assertion to capture this.
1028 if (VA.getLocInfo() == CCValAssign::SExt)
1029 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
1030 DAG.getValueType(VA.getValVT()));
1031 else if (VA.getLocInfo() == CCValAssign::ZExt)
1032 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
1033 DAG.getValueType(VA.getValVT()));
1034
1035 if (VA.isExtInLoc())
1036 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001037 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1038 // If this is a short vector argument loaded from the stack,
1039 // extend from i64 to full vector size and then bitcast.
1040 assert(VA.getLocVT() == MVT::i64);
1041 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001042 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001043 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1044 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001045 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1046 return Value;
1047}
1048
1049// Value is a value of type VA.getValVT() that we need to copy into
1050// the location described by VA. Return a copy of Value converted to
1051// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001052static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001053 CCValAssign &VA, SDValue Value) {
1054 switch (VA.getLocInfo()) {
1055 case CCValAssign::SExt:
1056 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1057 case CCValAssign::ZExt:
1058 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1059 case CCValAssign::AExt:
1060 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001061 case CCValAssign::BCvt:
1062 // If this is a short vector argument to be stored to the stack,
1063 // bitcast to v2i64 and then extract first element.
1064 assert(VA.getLocVT() == MVT::i64);
1065 assert(VA.getValVT().isVector());
1066 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1067 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1068 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001069 case CCValAssign::Full:
1070 return Value;
1071 default:
1072 llvm_unreachable("Unhandled getLocInfo()");
1073 }
1074}
1075
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001076SDValue SystemZTargetLowering::LowerFormalArguments(
1077 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1078 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1079 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001081 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001082 MachineRegisterInfo &MRI = MF.getRegInfo();
1083 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001084 MF.getInfo<SystemZMachineFunctionInfo>();
1085 auto *TFL =
1086 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001087 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001088
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001089 // Detect unsupported vector argument types.
1090 if (Subtarget.hasVector())
1091 VerifyVectorTypes(Ins);
1092
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001093 // Assign locations to all of the incoming arguments.
1094 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001095 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001096 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1097
1098 unsigned NumFixedGPRs = 0;
1099 unsigned NumFixedFPRs = 0;
1100 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1101 SDValue ArgValue;
1102 CCValAssign &VA = ArgLocs[I];
1103 EVT LocVT = VA.getLocVT();
1104 if (VA.isRegLoc()) {
1105 // Arguments passed in registers
1106 const TargetRegisterClass *RC;
1107 switch (LocVT.getSimpleVT().SimpleTy) {
1108 default:
1109 // Integers smaller than i64 should be promoted to i64.
1110 llvm_unreachable("Unexpected argument type");
1111 case MVT::i32:
1112 NumFixedGPRs += 1;
1113 RC = &SystemZ::GR32BitRegClass;
1114 break;
1115 case MVT::i64:
1116 NumFixedGPRs += 1;
1117 RC = &SystemZ::GR64BitRegClass;
1118 break;
1119 case MVT::f32:
1120 NumFixedFPRs += 1;
1121 RC = &SystemZ::FP32BitRegClass;
1122 break;
1123 case MVT::f64:
1124 NumFixedFPRs += 1;
1125 RC = &SystemZ::FP64BitRegClass;
1126 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001127 case MVT::v16i8:
1128 case MVT::v8i16:
1129 case MVT::v4i32:
1130 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001131 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001132 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001133 RC = &SystemZ::VR128BitRegClass;
1134 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001135 }
1136
1137 unsigned VReg = MRI.createVirtualRegister(RC);
1138 MRI.addLiveIn(VA.getLocReg(), VReg);
1139 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1140 } else {
1141 assert(VA.isMemLoc() && "Argument not register or memory");
1142
1143 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001144 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1145 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001146
1147 // Create the SelectionDAG nodes corresponding to a load
1148 // from this parameter. Unpromoted ints and floats are
1149 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001150 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1151 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001152 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1153 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001154 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001155 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001156 }
1157
1158 // Convert the value of the argument register into the value that's
1159 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001160 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001161 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1162 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001163 // If the original argument was split (e.g. i128), we need
1164 // to load all parts of it here (using the same address).
1165 unsigned ArgIndex = Ins[I].OrigArgIndex;
1166 assert (Ins[I].PartOffset == 0);
1167 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1168 CCValAssign &PartVA = ArgLocs[I + 1];
1169 unsigned PartOffset = Ins[I + 1].PartOffset;
1170 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1171 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001172 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1173 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001174 ++I;
1175 }
1176 } else
1177 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001178 }
1179
1180 if (IsVarArg) {
1181 // Save the number of non-varargs registers for later use by va_start, etc.
1182 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1183 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1184
1185 // Likewise the address (in the form of a frame index) of where the
1186 // first stack vararg would be. The 1-byte size here is arbitrary.
1187 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001188 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189
1190 // ...and a similar frame index for the caller-allocated save area
1191 // that will be used to store the incoming registers.
1192 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001193 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001194 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1195
1196 // Store the FPR varargs in the reserved frame slots. (We store the
1197 // GPRs as part of the prologue.)
1198 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1199 SDValue MemOps[SystemZ::NumArgFPRs];
1200 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1201 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001202 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001203 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001204 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1205 &SystemZ::FP64BitRegClass);
1206 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1207 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001208 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001209 }
1210 // Join the stores, which are independent of one another.
1211 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001212 makeArrayRef(&MemOps[NumFixedFPRs],
1213 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001214 }
1215 }
1216
1217 return Chain;
1218}
1219
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001220static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001221 SmallVectorImpl<CCValAssign> &ArgLocs,
1222 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001223 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001224 // needs the callee-saved argument register R6, or if the call uses
1225 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001226 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1227 CCValAssign &VA = ArgLocs[I];
1228 if (VA.getLocInfo() == CCValAssign::Indirect)
1229 return false;
1230 if (!VA.isRegLoc())
1231 return false;
1232 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001233 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001234 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001235 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1236 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001237 }
1238 return true;
1239}
1240
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001241SDValue
1242SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1243 SmallVectorImpl<SDValue> &InVals) const {
1244 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001245 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001246 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1247 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1248 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001249 SDValue Chain = CLI.Chain;
1250 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001251 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001252 CallingConv::ID CallConv = CLI.CallConv;
1253 bool IsVarArg = CLI.IsVarArg;
1254 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001255 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001256
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001257 // Detect unsupported vector argument and return types.
1258 if (Subtarget.hasVector()) {
1259 VerifyVectorTypes(Outs);
1260 VerifyVectorTypes(Ins);
1261 }
1262
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001263 // Analyze the operands of the call, assigning locations to each operand.
1264 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001265 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001266 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1267
Richard Sandiford709bda62013-08-19 12:42:31 +00001268 // We don't support GuaranteedTailCallOpt, only automatically-detected
1269 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001270 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001271 IsTailCall = false;
1272
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001273 // Get a count of how many bytes are to be pushed on the stack.
1274 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1275
1276 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001277 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001278 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001279
1280 // Copy argument values to their designated locations.
1281 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1282 SmallVector<SDValue, 8> MemOpChains;
1283 SDValue StackPtr;
1284 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1285 CCValAssign &VA = ArgLocs[I];
1286 SDValue ArgValue = OutVals[I];
1287
1288 if (VA.getLocInfo() == CCValAssign::Indirect) {
1289 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001290 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001291 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001292 MemOpChains.push_back(
1293 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1294 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001295 // If the original argument was split (e.g. i128), we need
1296 // to store all parts of it here (and pass just one address).
1297 unsigned ArgIndex = Outs[I].OrigArgIndex;
1298 assert (Outs[I].PartOffset == 0);
1299 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1300 SDValue PartValue = OutVals[I + 1];
1301 unsigned PartOffset = Outs[I + 1].PartOffset;
1302 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1303 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001304 MemOpChains.push_back(
1305 DAG.getStore(Chain, DL, PartValue, Address,
1306 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001307 ++I;
1308 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001309 ArgValue = SpillSlot;
1310 } else
1311 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1312
1313 if (VA.isRegLoc())
1314 // Queue up the argument copies and emit them at the end.
1315 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1316 else {
1317 assert(VA.isMemLoc() && "Argument not register or memory");
1318
1319 // Work out the address of the stack slot. Unpromoted ints and
1320 // floats are passed as right-justified 8-byte values.
1321 if (!StackPtr.getNode())
1322 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1323 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1324 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1325 Offset += 4;
1326 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001327 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001328
1329 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001330 MemOpChains.push_back(
1331 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001332 }
1333 }
1334
1335 // Join the stores, which are independent of one another.
1336 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001337 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001338
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001339 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001340 // associated Target* opcodes. Force %r1 to be used for indirect
1341 // tail calls.
1342 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001343 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001344 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1345 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001346 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001347 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1348 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001349 } else if (IsTailCall) {
1350 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1351 Glue = Chain.getValue(1);
1352 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1353 }
1354
1355 // Build a sequence of copy-to-reg nodes, chained and glued together.
1356 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1357 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1358 RegsToPass[I].second, Glue);
1359 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001360 }
1361
1362 // The first call operand is the chain and the second is the target address.
1363 SmallVector<SDValue, 8> Ops;
1364 Ops.push_back(Chain);
1365 Ops.push_back(Callee);
1366
1367 // Add argument registers to the end of the list so that they are
1368 // known live into the call.
1369 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1370 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1371 RegsToPass[I].second.getValueType()));
1372
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001373 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001374 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001375 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001376 assert(Mask && "Missing call preserved mask for calling convention");
1377 Ops.push_back(DAG.getRegisterMask(Mask));
1378
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001379 // Glue the call to the argument copies, if any.
1380 if (Glue.getNode())
1381 Ops.push_back(Glue);
1382
1383 // Emit the call.
1384 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001385 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001386 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1387 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001388 Glue = Chain.getValue(1);
1389
1390 // Mark the end of the call, which is glued to the call itself.
1391 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001392 DAG.getConstant(NumBytes, DL, PtrVT, true),
1393 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001394 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001395 Glue = Chain.getValue(1);
1396
1397 // Assign locations to each value returned by this call.
1398 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001399 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001400 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1401
1402 // Copy all of the result registers out of their specified physreg.
1403 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1404 CCValAssign &VA = RetLocs[I];
1405
1406 // Copy the value out, gluing the copy to the end of the call sequence.
1407 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1408 VA.getLocVT(), Glue);
1409 Chain = RetValue.getValue(1);
1410 Glue = RetValue.getValue(2);
1411
1412 // Convert the value of the return register into the value that's
1413 // being returned.
1414 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1415 }
1416
1417 return Chain;
1418}
1419
Ulrich Weiganda887f062015-08-13 13:37:06 +00001420bool SystemZTargetLowering::
1421CanLowerReturn(CallingConv::ID CallConv,
1422 MachineFunction &MF, bool isVarArg,
1423 const SmallVectorImpl<ISD::OutputArg> &Outs,
1424 LLVMContext &Context) const {
1425 // Detect unsupported vector return types.
1426 if (Subtarget.hasVector())
1427 VerifyVectorTypes(Outs);
1428
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001429 // Special case that we cannot easily detect in RetCC_SystemZ since
1430 // i128 is not a legal type.
1431 for (auto &Out : Outs)
1432 if (Out.ArgVT == MVT::i128)
1433 return false;
1434
Ulrich Weiganda887f062015-08-13 13:37:06 +00001435 SmallVector<CCValAssign, 16> RetLocs;
1436 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1437 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1438}
1439
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001440SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001441SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1442 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001443 const SmallVectorImpl<ISD::OutputArg> &Outs,
1444 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001445 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001446 MachineFunction &MF = DAG.getMachineFunction();
1447
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001448 // Detect unsupported vector return types.
1449 if (Subtarget.hasVector())
1450 VerifyVectorTypes(Outs);
1451
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001452 // Assign locations to each returned value.
1453 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001454 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001455 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1456
1457 // Quick exit for void returns
1458 if (RetLocs.empty())
1459 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1460
1461 // Copy the result values into the output registers.
1462 SDValue Glue;
1463 SmallVector<SDValue, 4> RetOps;
1464 RetOps.push_back(Chain);
1465 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1466 CCValAssign &VA = RetLocs[I];
1467 SDValue RetValue = OutVals[I];
1468
1469 // Make the return register live on exit.
1470 assert(VA.isRegLoc() && "Can only return in registers!");
1471
1472 // Promote the value as required.
1473 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1474
1475 // Chain and glue the copies together.
1476 unsigned Reg = VA.getLocReg();
1477 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1478 Glue = Chain.getValue(1);
1479 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1480 }
1481
1482 // Update chain and glue.
1483 RetOps[0] = Chain;
1484 if (Glue.getNode())
1485 RetOps.push_back(Glue);
1486
Craig Topper48d114b2014-04-26 18:35:24 +00001487 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001488}
1489
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001490// Return true if Op is an intrinsic node with chain that returns the CC value
1491// as its only (other) argument. Provide the associated SystemZISD opcode and
1492// the mask of valid CC values if so.
1493static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1494 unsigned &CCValid) {
1495 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1496 switch (Id) {
1497 case Intrinsic::s390_tbegin:
1498 Opcode = SystemZISD::TBEGIN;
1499 CCValid = SystemZ::CCMASK_TBEGIN;
1500 return true;
1501
1502 case Intrinsic::s390_tbegin_nofloat:
1503 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1504 CCValid = SystemZ::CCMASK_TBEGIN;
1505 return true;
1506
1507 case Intrinsic::s390_tend:
1508 Opcode = SystemZISD::TEND;
1509 CCValid = SystemZ::CCMASK_TEND;
1510 return true;
1511
1512 default:
1513 return false;
1514 }
1515}
1516
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001517// Return true if Op is an intrinsic node without chain that returns the
1518// CC value as its final argument. Provide the associated SystemZISD
1519// opcode and the mask of valid CC values if so.
1520static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1521 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1522 switch (Id) {
1523 case Intrinsic::s390_vpkshs:
1524 case Intrinsic::s390_vpksfs:
1525 case Intrinsic::s390_vpksgs:
1526 Opcode = SystemZISD::PACKS_CC;
1527 CCValid = SystemZ::CCMASK_VCMP;
1528 return true;
1529
1530 case Intrinsic::s390_vpklshs:
1531 case Intrinsic::s390_vpklsfs:
1532 case Intrinsic::s390_vpklsgs:
1533 Opcode = SystemZISD::PACKLS_CC;
1534 CCValid = SystemZ::CCMASK_VCMP;
1535 return true;
1536
1537 case Intrinsic::s390_vceqbs:
1538 case Intrinsic::s390_vceqhs:
1539 case Intrinsic::s390_vceqfs:
1540 case Intrinsic::s390_vceqgs:
1541 Opcode = SystemZISD::VICMPES;
1542 CCValid = SystemZ::CCMASK_VCMP;
1543 return true;
1544
1545 case Intrinsic::s390_vchbs:
1546 case Intrinsic::s390_vchhs:
1547 case Intrinsic::s390_vchfs:
1548 case Intrinsic::s390_vchgs:
1549 Opcode = SystemZISD::VICMPHS;
1550 CCValid = SystemZ::CCMASK_VCMP;
1551 return true;
1552
1553 case Intrinsic::s390_vchlbs:
1554 case Intrinsic::s390_vchlhs:
1555 case Intrinsic::s390_vchlfs:
1556 case Intrinsic::s390_vchlgs:
1557 Opcode = SystemZISD::VICMPHLS;
1558 CCValid = SystemZ::CCMASK_VCMP;
1559 return true;
1560
1561 case Intrinsic::s390_vtm:
1562 Opcode = SystemZISD::VTM;
1563 CCValid = SystemZ::CCMASK_VCMP;
1564 return true;
1565
1566 case Intrinsic::s390_vfaebs:
1567 case Intrinsic::s390_vfaehs:
1568 case Intrinsic::s390_vfaefs:
1569 Opcode = SystemZISD::VFAE_CC;
1570 CCValid = SystemZ::CCMASK_ANY;
1571 return true;
1572
1573 case Intrinsic::s390_vfaezbs:
1574 case Intrinsic::s390_vfaezhs:
1575 case Intrinsic::s390_vfaezfs:
1576 Opcode = SystemZISD::VFAEZ_CC;
1577 CCValid = SystemZ::CCMASK_ANY;
1578 return true;
1579
1580 case Intrinsic::s390_vfeebs:
1581 case Intrinsic::s390_vfeehs:
1582 case Intrinsic::s390_vfeefs:
1583 Opcode = SystemZISD::VFEE_CC;
1584 CCValid = SystemZ::CCMASK_ANY;
1585 return true;
1586
1587 case Intrinsic::s390_vfeezbs:
1588 case Intrinsic::s390_vfeezhs:
1589 case Intrinsic::s390_vfeezfs:
1590 Opcode = SystemZISD::VFEEZ_CC;
1591 CCValid = SystemZ::CCMASK_ANY;
1592 return true;
1593
1594 case Intrinsic::s390_vfenebs:
1595 case Intrinsic::s390_vfenehs:
1596 case Intrinsic::s390_vfenefs:
1597 Opcode = SystemZISD::VFENE_CC;
1598 CCValid = SystemZ::CCMASK_ANY;
1599 return true;
1600
1601 case Intrinsic::s390_vfenezbs:
1602 case Intrinsic::s390_vfenezhs:
1603 case Intrinsic::s390_vfenezfs:
1604 Opcode = SystemZISD::VFENEZ_CC;
1605 CCValid = SystemZ::CCMASK_ANY;
1606 return true;
1607
1608 case Intrinsic::s390_vistrbs:
1609 case Intrinsic::s390_vistrhs:
1610 case Intrinsic::s390_vistrfs:
1611 Opcode = SystemZISD::VISTR_CC;
1612 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1613 return true;
1614
1615 case Intrinsic::s390_vstrcbs:
1616 case Intrinsic::s390_vstrchs:
1617 case Intrinsic::s390_vstrcfs:
1618 Opcode = SystemZISD::VSTRC_CC;
1619 CCValid = SystemZ::CCMASK_ANY;
1620 return true;
1621
1622 case Intrinsic::s390_vstrczbs:
1623 case Intrinsic::s390_vstrczhs:
1624 case Intrinsic::s390_vstrczfs:
1625 Opcode = SystemZISD::VSTRCZ_CC;
1626 CCValid = SystemZ::CCMASK_ANY;
1627 return true;
1628
1629 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001630 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001631 Opcode = SystemZISD::VFCMPES;
1632 CCValid = SystemZ::CCMASK_VCMP;
1633 return true;
1634
1635 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001636 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001637 Opcode = SystemZISD::VFCMPHS;
1638 CCValid = SystemZ::CCMASK_VCMP;
1639 return true;
1640
1641 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001642 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001643 Opcode = SystemZISD::VFCMPHES;
1644 CCValid = SystemZ::CCMASK_VCMP;
1645 return true;
1646
1647 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001648 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001649 Opcode = SystemZISD::VFTCI;
1650 CCValid = SystemZ::CCMASK_VCMP;
1651 return true;
1652
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001653 case Intrinsic::s390_tdc:
1654 Opcode = SystemZISD::TDC;
1655 CCValid = SystemZ::CCMASK_TDC;
1656 return true;
1657
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001658 default:
1659 return false;
1660 }
1661}
1662
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001663// Emit an intrinsic with chain with a glued value instead of its CC result.
1664static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1665 unsigned Opcode) {
1666 // Copy all operands except the intrinsic ID.
1667 unsigned NumOps = Op.getNumOperands();
1668 SmallVector<SDValue, 6> Ops;
1669 Ops.reserve(NumOps - 1);
1670 Ops.push_back(Op.getOperand(0));
1671 for (unsigned I = 2; I < NumOps; ++I)
1672 Ops.push_back(Op.getOperand(I));
1673
1674 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1675 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1676 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1677 SDValue OldChain = SDValue(Op.getNode(), 1);
1678 SDValue NewChain = SDValue(Intr.getNode(), 0);
1679 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1680 return Intr;
1681}
1682
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001683// Emit an intrinsic with a glued value instead of its CC result.
1684static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1685 unsigned Opcode) {
1686 // Copy all operands except the intrinsic ID.
1687 unsigned NumOps = Op.getNumOperands();
1688 SmallVector<SDValue, 6> Ops;
1689 Ops.reserve(NumOps - 1);
1690 for (unsigned I = 1; I < NumOps; ++I)
1691 Ops.push_back(Op.getOperand(I));
1692
1693 if (Op->getNumValues() == 1)
1694 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1695 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1696 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1697 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1698}
1699
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001700// CC is a comparison that will be implemented using an integer or
1701// floating-point comparison. Return the condition code mask for
1702// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1703// unsigned comparisons and clear for signed ones. In the floating-point
1704// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1705static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1706#define CONV(X) \
1707 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1708 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1709 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1710
1711 switch (CC) {
1712 default:
1713 llvm_unreachable("Invalid integer condition!");
1714
1715 CONV(EQ);
1716 CONV(NE);
1717 CONV(GT);
1718 CONV(GE);
1719 CONV(LT);
1720 CONV(LE);
1721
1722 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1723 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1724 }
1725#undef CONV
1726}
1727
Richard Sandifordd420f732013-12-13 15:28:45 +00001728// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001729// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001730static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001731 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001732 return;
1733
Richard Sandiford21f5d682014-03-06 11:22:58 +00001734 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001735 if (!ConstOp1)
1736 return;
1737
1738 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001739 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1740 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1741 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1742 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1743 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001744 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001745 }
1746}
1747
Richard Sandifordd420f732013-12-13 15:28:45 +00001748// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1749// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001750static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1751 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001752 // For us to make any changes, it must a comparison between a single-use
1753 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001754 if (!C.Op0.hasOneUse() ||
1755 C.Op0.getOpcode() != ISD::LOAD ||
1756 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001757 return;
1758
1759 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001760 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001761 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1762 if (NumBits != 8 && NumBits != 16)
1763 return;
1764
1765 // The load must be an extending one and the constant must be within the
1766 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001767 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001768 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001769 uint64_t Mask = (1 << NumBits) - 1;
1770 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001771 // Make sure that ConstOp1 is in range of C.Op0.
1772 int64_t SignedValue = ConstOp1->getSExtValue();
1773 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001774 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001775 if (C.ICmpType != SystemZICMP::SignedOnly) {
1776 // Unsigned comparison between two sign-extended values is equivalent
1777 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001778 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001779 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001780 // Try to treat the comparison as unsigned, so that we can use CLI.
1781 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001782 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001783 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001784 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1785 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001786 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001787 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001788 else
1789 // No instruction exists for this combination.
1790 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001791 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001792 }
1793 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1794 if (Value > Mask)
1795 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001796 // If the constant is in range, we can use any comparison.
1797 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001798 } else
1799 return;
1800
1801 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001802 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1803 ISD::SEXTLOAD :
1804 ISD::ZEXTLOAD);
1805 if (C.Op0.getValueType() != MVT::i32 ||
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001806 Load->getExtensionType() != ExtType) {
Justin Lebar9c375812016-07-15 18:27:10 +00001807 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1808 Load->getBasePtr(), Load->getPointerInfo(),
1809 Load->getMemoryVT(), Load->getAlignment(),
1810 Load->getMemOperand()->getFlags());
Jonas Paulssonb9a24672017-11-30 08:18:50 +00001811 // Update the chain uses.
1812 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), C.Op0.getValue(1));
1813 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001814
1815 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001816 if (C.Op1.getValueType() != MVT::i32 ||
1817 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001818 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001819}
1820
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001821// Return true if Op is either an unextended load, or a load suitable
1822// for integer register-memory comparisons of type ICmpType.
1823static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001824 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001825 if (Load) {
1826 // There are no instructions to compare a register with a memory byte.
1827 if (Load->getMemoryVT() == MVT::i8)
1828 return false;
1829 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001830 switch (Load->getExtensionType()) {
1831 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001832 return true;
1833 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001834 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001835 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001836 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001837 default:
1838 break;
1839 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001840 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001841 return false;
1842}
1843
Richard Sandifordd420f732013-12-13 15:28:45 +00001844// Return true if it is better to swap the operands of C.
1845static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001846 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001847 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001848 return false;
1849
1850 // Always keep a floating-point constant second, since comparisons with
1851 // zero can use LOAD TEST and comparisons with other constants make a
1852 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001853 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001854 return false;
1855
1856 // Never swap comparisons with zero since there are many ways to optimize
1857 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001858 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001859 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001860 return false;
1861
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001862 // Also keep natural memory operands second if the loaded value is
1863 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001864 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001865 return false;
1866
Richard Sandiford24e597b2013-08-23 11:27:19 +00001867 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1868 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001869 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001870 // The only exceptions are when the second operand is a constant and
1871 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001872 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001873 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001874 // The unsigned memory-immediate instructions can handle 16-bit
1875 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001876 if (C.ICmpType != SystemZICMP::SignedOnly &&
1877 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001878 return false;
1879 // The signed memory-immediate instructions can handle 16-bit
1880 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001881 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1882 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001883 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001884 return true;
1885 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001886
1887 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001888 unsigned Opcode0 = C.Op0.getOpcode();
1889 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001890 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001891 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001892 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001893 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001894 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001895 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1896 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001897 return true;
1898
Richard Sandiford24e597b2013-08-23 11:27:19 +00001899 return false;
1900}
1901
Richard Sandiford73170f82013-12-11 11:45:08 +00001902// Return a version of comparison CC mask CCMask in which the LT and GT
1903// actions are swapped.
1904static unsigned reverseCCMask(unsigned CCMask) {
1905 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1906 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1907 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1908 (CCMask & SystemZ::CCMASK_CMP_UO));
1909}
1910
Richard Sandiford0847c452013-12-13 15:50:30 +00001911// Check whether C tests for equality between X and Y and whether X - Y
1912// or Y - X is also computed. In that case it's better to compare the
1913// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001914static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1915 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001916 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1917 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001918 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001919 SDNode *N = *I;
1920 if (N->getOpcode() == ISD::SUB &&
1921 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1922 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1923 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001924 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001925 return;
1926 }
1927 }
1928 }
1929}
1930
Richard Sandifordd420f732013-12-13 15:28:45 +00001931// Check whether C compares a floating-point value with zero and if that
1932// floating-point value is also negated. In this case we can use the
1933// negation to set CC, so avoiding separate LOAD AND TEST and
1934// LOAD (NEGATIVE/COMPLEMENT) instructions.
1935static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001936 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001937 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001938 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001939 SDNode *N = *I;
1940 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001941 C.Op0 = SDValue(N, 0);
1942 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001943 return;
1944 }
1945 }
1946 }
1947}
1948
Richard Sandifordd420f732013-12-13 15:28:45 +00001949// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001950// also sign-extended. In that case it is better to test the result
1951// of the sign extension using LTGFR.
1952//
1953// This case is important because InstCombine transforms a comparison
1954// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001955static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001956 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001957 if (C.Op0.getOpcode() == ISD::SHL &&
1958 C.Op0.getValueType() == MVT::i64 &&
1959 C.Op1.getOpcode() == ISD::Constant &&
1960 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001961 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001962 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001963 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001964 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001965 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001966 SDNode *N = *I;
1967 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1968 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001969 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001970 return;
1971 }
1972 }
1973 }
1974 }
1975}
1976
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001977// If C compares the truncation of an extending load, try to compare
1978// the untruncated value instead. This exposes more opportunities to
1979// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001980static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
1981 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001982 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1983 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1984 C.Op1.getOpcode() == ISD::Constant &&
1985 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001986 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001987 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001988 unsigned Type = L->getExtensionType();
1989 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1990 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1991 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001992 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001993 }
1994 }
1995 }
1996}
1997
Richard Sandiford030c1652013-09-13 09:09:50 +00001998// Return true if shift operation N has an in-range constant shift value.
1999// Store it in ShiftVal if so.
2000static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002001 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00002002 if (!Shift)
2003 return false;
2004
2005 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002006 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00002007 return false;
2008
2009 ShiftVal = Amount;
2010 return true;
2011}
2012
2013// Check whether an AND with Mask is suitable for a TEST UNDER MASK
2014// instruction and whether the CC value is descriptive enough to handle
2015// a comparison of type Opcode between the AND result and CmpVal.
2016// CCMask says which comparison result is being tested and BitSize is
2017// the number of bits in the operands. If TEST UNDER MASK can be used,
2018// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002019static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
2020 uint64_t Mask, uint64_t CmpVal,
2021 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002022 assert(Mask != 0 && "ANDs with zero should have been removed by now");
2023
Richard Sandiford030c1652013-09-13 09:09:50 +00002024 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
2025 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
2026 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
2027 return 0;
2028
Richard Sandiford113c8702013-09-03 15:38:35 +00002029 // Work out the masks for the lowest and highest bits.
2030 unsigned HighShift = 63 - countLeadingZeros(Mask);
2031 uint64_t High = uint64_t(1) << HighShift;
2032 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
2033
2034 // Signed ordered comparisons are effectively unsigned if the sign
2035 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002036 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002037
2038 // Check for equality comparisons with 0, or the equivalent.
2039 if (CmpVal == 0) {
2040 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2041 return SystemZ::CCMASK_TM_ALL_0;
2042 if (CCMask == SystemZ::CCMASK_CMP_NE)
2043 return SystemZ::CCMASK_TM_SOME_1;
2044 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002045 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002046 if (CCMask == SystemZ::CCMASK_CMP_LT)
2047 return SystemZ::CCMASK_TM_ALL_0;
2048 if (CCMask == SystemZ::CCMASK_CMP_GE)
2049 return SystemZ::CCMASK_TM_SOME_1;
2050 }
2051 if (EffectivelyUnsigned && CmpVal < Low) {
2052 if (CCMask == SystemZ::CCMASK_CMP_LE)
2053 return SystemZ::CCMASK_TM_ALL_0;
2054 if (CCMask == SystemZ::CCMASK_CMP_GT)
2055 return SystemZ::CCMASK_TM_SOME_1;
2056 }
2057
2058 // Check for equality comparisons with the mask, or the equivalent.
2059 if (CmpVal == Mask) {
2060 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2061 return SystemZ::CCMASK_TM_ALL_1;
2062 if (CCMask == SystemZ::CCMASK_CMP_NE)
2063 return SystemZ::CCMASK_TM_SOME_0;
2064 }
2065 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2066 if (CCMask == SystemZ::CCMASK_CMP_GT)
2067 return SystemZ::CCMASK_TM_ALL_1;
2068 if (CCMask == SystemZ::CCMASK_CMP_LE)
2069 return SystemZ::CCMASK_TM_SOME_0;
2070 }
2071 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2072 if (CCMask == SystemZ::CCMASK_CMP_GE)
2073 return SystemZ::CCMASK_TM_ALL_1;
2074 if (CCMask == SystemZ::CCMASK_CMP_LT)
2075 return SystemZ::CCMASK_TM_SOME_0;
2076 }
2077
2078 // Check for ordered comparisons with the top bit.
2079 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2080 if (CCMask == SystemZ::CCMASK_CMP_LE)
2081 return SystemZ::CCMASK_TM_MSB_0;
2082 if (CCMask == SystemZ::CCMASK_CMP_GT)
2083 return SystemZ::CCMASK_TM_MSB_1;
2084 }
2085 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2086 if (CCMask == SystemZ::CCMASK_CMP_LT)
2087 return SystemZ::CCMASK_TM_MSB_0;
2088 if (CCMask == SystemZ::CCMASK_CMP_GE)
2089 return SystemZ::CCMASK_TM_MSB_1;
2090 }
2091
2092 // If there are just two bits, we can do equality checks for Low and High
2093 // as well.
2094 if (Mask == Low + High) {
2095 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2096 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2097 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2098 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2099 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2100 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2101 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2102 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2103 }
2104
2105 // Looks like we've exhausted our options.
2106 return 0;
2107}
2108
Richard Sandifordd420f732013-12-13 15:28:45 +00002109// See whether C can be implemented as a TEST UNDER MASK instruction.
2110// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002111static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2112 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002113 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002114 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002115 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002116 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002117 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002118
2119 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002120 Comparison NewC(C);
2121 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002122 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002123 if (C.Op0.getOpcode() == ISD::AND) {
2124 NewC.Op0 = C.Op0.getOperand(0);
2125 NewC.Op1 = C.Op0.getOperand(1);
2126 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2127 if (!Mask)
2128 return;
2129 MaskVal = Mask->getZExtValue();
2130 } else {
2131 // There is no instruction to compare with a 64-bit immediate
2132 // so use TMHH instead if possible. We need an unsigned ordered
2133 // comparison with an i64 immediate.
2134 if (NewC.Op0.getValueType() != MVT::i64 ||
2135 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2136 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2137 NewC.ICmpType == SystemZICMP::SignedOnly)
2138 return;
2139 // Convert LE and GT comparisons into LT and GE.
2140 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2141 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2142 if (CmpVal == uint64_t(-1))
2143 return;
2144 CmpVal += 1;
2145 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2146 }
2147 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2148 // be masked off without changing the result.
2149 MaskVal = -(CmpVal & -CmpVal);
2150 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2151 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002152 if (!MaskVal)
2153 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002154
Richard Sandiford113c8702013-09-03 15:38:35 +00002155 // Check whether the combination of mask, comparison value and comparison
2156 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002157 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002158 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002159 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2160 NewC.Op0.getOpcode() == ISD::SHL &&
2161 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002162 (MaskVal >> ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002163 ((CmpVal >> ShiftVal) << ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002164 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2165 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002166 CmpVal >> ShiftVal,
2167 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002168 NewC.Op0 = NewC.Op0.getOperand(0);
2169 MaskVal >>= ShiftVal;
2170 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2171 NewC.Op0.getOpcode() == ISD::SRL &&
2172 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002173 (MaskVal << ShiftVal != 0) &&
Ulrich Weigand5bfed6c2017-12-05 19:42:07 +00002174 ((CmpVal << ShiftVal) >> ShiftVal) == CmpVal &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002175 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002176 MaskVal << ShiftVal,
2177 CmpVal << ShiftVal,
2178 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002179 NewC.Op0 = NewC.Op0.getOperand(0);
2180 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002181 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002182 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2183 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002184 if (!NewCCMask)
2185 return;
2186 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002187
Richard Sandiford35b9be22013-08-28 10:31:43 +00002188 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002189 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002190 C.Op0 = NewC.Op0;
2191 if (Mask && Mask->getZExtValue() == MaskVal)
2192 C.Op1 = SDValue(Mask, 0);
2193 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002194 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002195 C.CCValid = SystemZ::CCMASK_TM;
2196 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002197}
2198
Ulrich Weigand31112892018-01-19 20:54:18 +00002199// See whether the comparison argument contains a redundant AND
2200// and remove it if so. This sometimes happens due to the generic
2201// BRCOND expansion.
2202static void adjustForRedundantAnd(SelectionDAG &DAG, const SDLoc &DL,
2203 Comparison &C) {
2204 if (C.Op0.getOpcode() != ISD::AND)
2205 return;
2206 auto *Mask = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
2207 if (!Mask)
2208 return;
2209 KnownBits Known;
2210 DAG.computeKnownBits(C.Op0.getOperand(0), Known);
2211 if ((~Known.Zero).getZExtValue() & ~Mask->getZExtValue())
2212 return;
2213
2214 C.Op0 = C.Op0.getOperand(0);
2215}
2216
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002217// Return a Comparison that tests the condition-code result of intrinsic
2218// node Call against constant integer CC using comparison code Cond.
2219// Opcode is the opcode of the SystemZISD operation for the intrinsic
2220// and CCValid is the set of possible condition-code results.
2221static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2222 SDValue Call, unsigned CCValid, uint64_t CC,
2223 ISD::CondCode Cond) {
2224 Comparison C(Call, SDValue());
2225 C.Opcode = Opcode;
2226 C.CCValid = CCValid;
2227 if (Cond == ISD::SETEQ)
2228 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2229 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2230 else if (Cond == ISD::SETNE)
2231 // ...and the inverse of that.
2232 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2233 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2234 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2235 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002236 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002237 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2238 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002239 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002240 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2241 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2242 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002243 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002244 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2245 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002246 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002247 else
2248 llvm_unreachable("Unexpected integer comparison type");
2249 C.CCMask &= CCValid;
2250 return C;
2251}
2252
Richard Sandifordd420f732013-12-13 15:28:45 +00002253// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2254static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002255 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002256 if (CmpOp1.getOpcode() == ISD::Constant) {
2257 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2258 unsigned Opcode, CCValid;
2259 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2260 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2261 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2262 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002263 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2264 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2265 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2266 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002267 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002268 Comparison C(CmpOp0, CmpOp1);
2269 C.CCMask = CCMaskForCondCode(Cond);
2270 if (C.Op0.getValueType().isFloatingPoint()) {
2271 C.CCValid = SystemZ::CCMASK_FCMP;
2272 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002273 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002274 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002275 C.CCValid = SystemZ::CCMASK_ICMP;
2276 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002277 // Choose the type of comparison. Equality and inequality tests can
2278 // use either signed or unsigned comparisons. The choice also doesn't
2279 // matter if both sign bits are known to be clear. In those cases we
2280 // want to give the main isel code the freedom to choose whichever
2281 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002282 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2283 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2284 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2285 C.ICmpType = SystemZICMP::Any;
2286 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2287 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002288 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002289 C.ICmpType = SystemZICMP::SignedOnly;
2290 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Ulrich Weigand31112892018-01-19 20:54:18 +00002291 adjustForRedundantAnd(DAG, DL, C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002292 adjustZeroCmp(DAG, DL, C);
2293 adjustSubwordCmp(DAG, DL, C);
2294 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002295 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002296 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002297 }
2298
Richard Sandifordd420f732013-12-13 15:28:45 +00002299 if (shouldSwapCmpOperands(C)) {
2300 std::swap(C.Op0, C.Op1);
2301 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002302 }
2303
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002304 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002305 return C;
2306}
2307
2308// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002309static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002310 if (!C.Op1.getNode()) {
2311 SDValue Op;
2312 switch (C.Op0.getOpcode()) {
2313 case ISD::INTRINSIC_W_CHAIN:
2314 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2315 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002316 case ISD::INTRINSIC_WO_CHAIN:
2317 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2318 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002319 default:
2320 llvm_unreachable("Invalid comparison operands");
2321 }
2322 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2323 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002324 if (C.Opcode == SystemZISD::ICMP)
2325 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002327 if (C.Opcode == SystemZISD::TM) {
2328 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2329 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2330 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002331 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002332 }
2333 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002334}
2335
Richard Sandiford7d86e472013-08-21 09:34:56 +00002336// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2337// 64 bits. Extend is the extension type to use. Store the high part
2338// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002339static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2340 SDValue Op0, SDValue Op1, SDValue &Hi,
2341 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002342 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2343 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2344 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002345 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2346 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002347 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2348 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2349}
2350
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002351// Lower a binary operation that produces two VT results, one in each
2352// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002353// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002354// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002355static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002356 unsigned Opcode, SDValue Op0, SDValue Op1,
2357 SDValue &Even, SDValue &Odd) {
2358 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002359 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002360 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2361 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002362}
2363
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002364// Return an i32 value that is 1 if the CC value produced by Glue is
2365// in the mask CCMask and 0 otherwise. CC is known to have a value
2366// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002367static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002368 unsigned CCValid, unsigned CCMask) {
Ulrich Weigand849a59f2018-01-19 20:52:04 +00002369 SDValue Ops[] = { DAG.getConstant(1, DL, MVT::i32),
2370 DAG.getConstant(0, DL, MVT::i32),
2371 DAG.getConstant(CCValid, DL, MVT::i32),
2372 DAG.getConstant(CCMask, DL, MVT::i32), Glue };
2373 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, MVT::i32, Ops);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002374}
2375
Ulrich Weigandcd808232015-05-05 19:26:48 +00002376// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2377// be done directly. IsFP is true if CC is for a floating-point rather than
2378// integer comparison.
2379static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002380 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002381 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002382 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002383 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002384
Ulrich Weigandcd808232015-05-05 19:26:48 +00002385 case ISD::SETOGE:
2386 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002387 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002388
2389 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002390 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002391 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002392
2393 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002394 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002395
2396 default:
2397 return 0;
2398 }
2399}
2400
2401// Return the SystemZISD vector comparison operation for CC or its inverse,
2402// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002403// result is for the inverse of CC. IsFP is true if CC is for a
2404// floating-point rather than integer comparison.
2405static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2406 bool &Invert) {
2407 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002408 Invert = false;
2409 return Opcode;
2410 }
2411
Ulrich Weigandcd808232015-05-05 19:26:48 +00002412 CC = ISD::getSetCCInverse(CC, !IsFP);
2413 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002414 Invert = true;
2415 return Opcode;
2416 }
2417
2418 return 0;
2419}
2420
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002421// Return a v2f64 that contains the extended form of elements Start and Start+1
2422// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002423static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002424 SDValue Op) {
2425 int Mask[] = { Start, -1, Start + 1, -1 };
2426 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2427 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2428}
2429
2430// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2431// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002432SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2433 const SDLoc &DL, EVT VT,
2434 SDValue CmpOp0,
2435 SDValue CmpOp1) const {
2436 // There is no hardware support for v4f32 (unless we have the vector
2437 // enhancements facility 1), so extend the vector into two v2f64s
2438 // and compare those.
2439 if (CmpOp0.getValueType() == MVT::v4f32 &&
2440 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002441 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2442 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2443 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2444 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2445 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2446 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2447 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2448 }
2449 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2450}
2451
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002452// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2453// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002454SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2455 const SDLoc &DL, EVT VT,
2456 ISD::CondCode CC,
2457 SDValue CmpOp0,
2458 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002459 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002460 bool Invert = false;
2461 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002462 switch (CC) {
2463 // Handle tests for order using (or (ogt y x) (oge x y)).
2464 case ISD::SETUO:
2465 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002466 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002467 case ISD::SETO: {
2468 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002469 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2470 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002471 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2472 break;
2473 }
2474
2475 // Handle <> tests using (or (ogt y x) (ogt x y)).
2476 case ISD::SETUEQ:
2477 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002478 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002479 case ISD::SETONE: {
2480 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002481 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2482 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002483 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2484 break;
2485 }
2486
2487 // Otherwise a single comparison is enough. It doesn't really
2488 // matter whether we try the inversion or the swap first, since
2489 // there are no cases where both work.
2490 default:
2491 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002492 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002493 else {
2494 CC = ISD::getSetCCSwappedOperands(CC);
2495 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002496 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002497 else
2498 llvm_unreachable("Unhandled comparison");
2499 }
2500 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002501 }
2502 if (Invert) {
2503 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2504 DAG.getConstant(65535, DL, MVT::i32));
2505 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2506 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2507 }
2508 return Cmp;
2509}
2510
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002511SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2512 SelectionDAG &DAG) const {
2513 SDValue CmpOp0 = Op.getOperand(0);
2514 SDValue CmpOp1 = Op.getOperand(1);
2515 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2516 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002517 EVT VT = Op.getValueType();
2518 if (VT.isVector())
2519 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002520
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002521 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002522 SDValue Glue = emitCmp(DAG, DL, C);
2523 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002524}
2525
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002526SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002527 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2528 SDValue CmpOp0 = Op.getOperand(2);
2529 SDValue CmpOp1 = Op.getOperand(3);
2530 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002531 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002532
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002533 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002534 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002535 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002536 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2537 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002538}
2539
Richard Sandiford57485472013-12-13 15:35:00 +00002540// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2541// allowing Pos and Neg to be wider than CmpOp.
2542static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2543 return (Neg.getOpcode() == ISD::SUB &&
2544 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2545 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2546 Neg.getOperand(1) == Pos &&
2547 (Pos == CmpOp ||
2548 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2549 Pos.getOperand(0) == CmpOp)));
2550}
2551
2552// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002553static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002554 bool IsNegative) {
2555 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2556 if (IsNegative)
2557 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002558 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002559 return Op;
2560}
2561
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002562SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2563 SelectionDAG &DAG) const {
2564 SDValue CmpOp0 = Op.getOperand(0);
2565 SDValue CmpOp1 = Op.getOperand(1);
2566 SDValue TrueOp = Op.getOperand(2);
2567 SDValue FalseOp = Op.getOperand(3);
2568 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002569 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002570
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002571 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002572
2573 // Check for absolute and negative-absolute selections, including those
2574 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2575 // This check supplements the one in DAGCombiner.
2576 if (C.Opcode == SystemZISD::ICMP &&
2577 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2578 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2579 C.Op1.getOpcode() == ISD::Constant &&
2580 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2581 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2582 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2583 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2584 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2585 }
2586
Richard Sandifordd420f732013-12-13 15:28:45 +00002587 SDValue Glue = emitCmp(DAG, DL, C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002588 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2589 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002590
Ulrich Weigand849a59f2018-01-19 20:52:04 +00002591 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, Op.getValueType(), Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002592}
2593
2594SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2595 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002596 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002597 const GlobalValue *GV = Node->getGlobal();
2598 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002599 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002600 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002601
2602 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002603 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002604 // Assign anchors at 1<<12 byte boundaries.
2605 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2606 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2607 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2608
2609 // The offset can be folded into the address if it is aligned to a halfword.
2610 Offset -= Anchor;
2611 if (Offset != 0 && (Offset & 1) == 0) {
2612 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2613 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002614 Offset = 0;
2615 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002616 } else {
2617 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2618 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2619 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002620 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002621 }
2622
2623 // If there was a non-zero offset that we didn't fold, create an explicit
2624 // addition for it.
2625 if (Offset != 0)
2626 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002627 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002628
2629 return Result;
2630}
2631
Ulrich Weigand7db69182015-02-18 09:13:27 +00002632SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2633 SelectionDAG &DAG,
2634 unsigned Opcode,
2635 SDValue GOTOffset) const {
2636 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002637 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002638 SDValue Chain = DAG.getEntryNode();
2639 SDValue Glue;
2640
2641 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2642 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2643 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2644 Glue = Chain.getValue(1);
2645 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2646 Glue = Chain.getValue(1);
2647
2648 // The first call operand is the chain and the second is the TLS symbol.
2649 SmallVector<SDValue, 8> Ops;
2650 Ops.push_back(Chain);
2651 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2652 Node->getValueType(0),
2653 0, 0));
2654
2655 // Add argument registers to the end of the list so that they are
2656 // known live into the call.
2657 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2658 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2659
2660 // Add a register mask operand representing the call-preserved registers.
2661 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002662 const uint32_t *Mask =
2663 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002664 assert(Mask && "Missing call preserved mask for calling convention");
2665 Ops.push_back(DAG.getRegisterMask(Mask));
2666
2667 // Glue the call to the argument copies.
2668 Ops.push_back(Glue);
2669
2670 // Emit the call.
2671 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2672 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2673 Glue = Chain.getValue(1);
2674
2675 // Copy the return value from %r2.
2676 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2677}
2678
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002679SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2680 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002681 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002682 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002683
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002684 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002685 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002686 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2687
2688 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002689 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002690 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2691
2692 // Merge them into a single 64-bit address.
2693 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002694 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002695 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2696}
2697
2698SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2699 SelectionDAG &DAG) const {
Chih-Hung Hsieh9f9e4682018-02-28 17:48:55 +00002700 if (DAG.getTarget().useEmulatedTLS())
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002701 return LowerToTLSEmulatedModel(Node, DAG);
2702 SDLoc DL(Node);
2703 const GlobalValue *GV = Node->getGlobal();
2704 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2705 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2706
2707 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002708
Ulrich Weigand7db69182015-02-18 09:13:27 +00002709 // Get the offset of GA from the thread pointer, based on the TLS model.
2710 SDValue Offset;
2711 switch (model) {
2712 case TLSModel::GeneralDynamic: {
2713 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2714 SystemZConstantPoolValue *CPV =
2715 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002716
Ulrich Weigand7db69182015-02-18 09:13:27 +00002717 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002718 Offset = DAG.getLoad(
2719 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002720 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002721
2722 // Call __tls_get_offset to retrieve the offset.
2723 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2724 break;
2725 }
2726
2727 case TLSModel::LocalDynamic: {
2728 // Load the GOT offset of the module ID.
2729 SystemZConstantPoolValue *CPV =
2730 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2731
2732 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002733 Offset = DAG.getLoad(
2734 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002735 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002736
2737 // Call __tls_get_offset to retrieve the module base offset.
2738 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2739
2740 // Note: The SystemZLDCleanupPass will remove redundant computations
2741 // of the module base offset. Count total number of local-dynamic
2742 // accesses to trigger execution of that pass.
2743 SystemZMachineFunctionInfo* MFI =
2744 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2745 MFI->incNumLocalDynamicTLSAccesses();
2746
2747 // Add the per-symbol offset.
2748 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2749
2750 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002751 DTPOffset = DAG.getLoad(
2752 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002753 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002754
2755 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2756 break;
2757 }
2758
2759 case TLSModel::InitialExec: {
2760 // Load the offset from the GOT.
2761 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2762 SystemZII::MO_INDNTPOFF);
2763 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002764 Offset =
2765 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2766 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002767 break;
2768 }
2769
2770 case TLSModel::LocalExec: {
2771 // Force the offset into the constant pool and load it from there.
2772 SystemZConstantPoolValue *CPV =
2773 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2774
2775 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002776 Offset = DAG.getLoad(
2777 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002778 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002779 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002780 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002781 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002782
2783 // Add the base and offset together.
2784 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2785}
2786
2787SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2788 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002789 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002790 const BlockAddress *BA = Node->getBlockAddress();
2791 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002792 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002793
2794 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2795 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2796 return Result;
2797}
2798
2799SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2800 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002801 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002802 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002803 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2804
2805 // Use LARL to load the address of the table.
2806 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2807}
2808
2809SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2810 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002811 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002812 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002813
2814 SDValue Result;
2815 if (CP->isMachineConstantPoolEntry())
2816 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002817 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002818 else
2819 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002820 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002821
2822 // Use LARL to load the address of the constant pool entry.
2823 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2824}
2825
Ulrich Weigandf557d082016-04-04 12:44:55 +00002826SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2827 SelectionDAG &DAG) const {
2828 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002829 MachineFrameInfo &MFI = MF.getFrameInfo();
2830 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002831
2832 SDLoc DL(Op);
2833 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2834 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2835
2836 // If the back chain frame index has not been allocated yet, do so.
2837 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2838 int BackChainIdx = FI->getFramePointerSaveIndex();
2839 if (!BackChainIdx) {
2840 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002841 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002842 FI->setFramePointerSaveIndex(BackChainIdx);
2843 }
2844 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2845
2846 // FIXME The frontend should detect this case.
2847 if (Depth > 0) {
2848 report_fatal_error("Unsupported stack frame traversal count");
2849 }
2850
2851 return BackChain;
2852}
2853
2854SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2855 SelectionDAG &DAG) const {
2856 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002857 MachineFrameInfo &MFI = MF.getFrameInfo();
2858 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002859
2860 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2861 return SDValue();
2862
2863 SDLoc DL(Op);
2864 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2865 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2866
2867 // FIXME The frontend should detect this case.
2868 if (Depth > 0) {
2869 report_fatal_error("Unsupported stack frame traversal count");
2870 }
2871
2872 // Return R14D, which has the return address. Mark it an implicit live-in.
2873 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2874 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2875}
2876
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002877SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2878 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002879 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002880 SDValue In = Op.getOperand(0);
2881 EVT InVT = In.getValueType();
2882 EVT ResVT = Op.getValueType();
2883
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002884 // Convert loads directly. This is normally done by DAGCombiner,
2885 // but we need this case for bitcasts that are created during lowering
2886 // and which are then lowered themselves.
2887 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Jonas Paulssonb9a24672017-11-30 08:18:50 +00002888 if (ISD::isNormalLoad(LoadN)) {
2889 SDValue NewLoad = DAG.getLoad(ResVT, DL, LoadN->getChain(),
2890 LoadN->getBasePtr(), LoadN->getMemOperand());
2891 // Update the chain uses.
2892 DAG.ReplaceAllUsesOfValueWith(SDValue(LoadN, 1), NewLoad.getValue(1));
2893 return NewLoad;
2894 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002895
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002896 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002897 SDValue In64;
2898 if (Subtarget.hasHighWord()) {
2899 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2900 MVT::i64);
2901 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2902 MVT::i64, SDValue(U64, 0), In);
2903 } else {
2904 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2905 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002906 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002907 }
2908 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002909 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002910 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002911 }
2912 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2913 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002914 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002915 MVT::f64, SDValue(U64, 0), In);
2916 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002917 if (Subtarget.hasHighWord())
2918 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2919 MVT::i32, Out64);
2920 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002921 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002922 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002923 }
2924 llvm_unreachable("Unexpected bitcast combination");
2925}
2926
2927SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2928 SelectionDAG &DAG) const {
2929 MachineFunction &MF = DAG.getMachineFunction();
2930 SystemZMachineFunctionInfo *FuncInfo =
2931 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002932 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002933
2934 SDValue Chain = Op.getOperand(0);
2935 SDValue Addr = Op.getOperand(1);
2936 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002937 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002938
2939 // The initial values of each field.
2940 const unsigned NumFields = 4;
2941 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002942 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2943 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002944 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2945 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2946 };
2947
2948 // Store each field into its respective slot.
2949 SDValue MemOps[NumFields];
2950 unsigned Offset = 0;
2951 for (unsigned I = 0; I < NumFields; ++I) {
2952 SDValue FieldAddr = Addr;
2953 if (Offset != 0)
2954 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002955 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002956 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00002957 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002958 Offset += 8;
2959 }
Craig Topper48d114b2014-04-26 18:35:24 +00002960 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002961}
2962
2963SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2964 SelectionDAG &DAG) const {
2965 SDValue Chain = Op.getOperand(0);
2966 SDValue DstPtr = Op.getOperand(1);
2967 SDValue SrcPtr = Op.getOperand(2);
2968 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2969 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002970 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002971
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002972 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002973 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002974 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002975 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2976}
2977
2978SDValue SystemZTargetLowering::
2979lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002980 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00002981 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braunf1caa282017-12-15 22:22:58 +00002982 bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
2983 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002984
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002985 SDValue Chain = Op.getOperand(0);
2986 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002987 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002988 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002989
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002990 // If user has set the no alignment function attribute, ignore
2991 // alloca alignments.
2992 uint64_t AlignVal = (RealignOpt ?
2993 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2994
2995 uint64_t StackAlign = TFI->getStackAlignment();
2996 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2997 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2998
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002999 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003000 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003001
3002 // Get a reference to the stack pointer.
3003 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
3004
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003005 // If we need a backchain, save it now.
3006 SDValue Backchain;
3007 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003008 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003009
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003010 // Add extra space for alignment if needed.
3011 if (ExtraAlignSpace)
3012 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00003013 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003014
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003015 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003016 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003017
3018 // Copy the new stack pointer back.
3019 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
3020
3021 // The allocated data lives above the 160 bytes allocated for the standard
3022 // frame, plus any outgoing stack arguments. We don't know how much that
3023 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
3024 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3025 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
3026
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003027 // Dynamically realign if needed.
3028 if (RequiredAlign > StackAlign) {
3029 Result =
3030 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
3031 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
3032 Result =
3033 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
3034 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3035 }
3036
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003037 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003038 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003039
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003040 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003041 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003042}
3043
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003044SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3045 SDValue Op, SelectionDAG &DAG) const {
3046 SDLoc DL(Op);
3047
3048 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3049}
3050
Richard Sandiford7d86e472013-08-21 09:34:56 +00003051SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3052 SelectionDAG &DAG) const {
3053 EVT VT = Op.getValueType();
3054 SDLoc DL(Op);
3055 SDValue Ops[2];
3056 if (is32Bit(VT))
3057 // Just do a normal 64-bit multiplication and extract the results.
3058 // We define this so that it can be used for constant division.
3059 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3060 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003061 else if (Subtarget.hasMiscellaneousExtensions2())
3062 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3063 // the high result in the even register. ISD::SMUL_LOHI is defined to
3064 // return the low half first, so the results are in reverse order.
3065 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3066 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003067 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003068 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003069 //
3070 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3071 //
3072 // but using the fact that the upper halves are either all zeros
3073 // or all ones:
3074 //
3075 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3076 //
3077 // and grouping the right terms together since they are quicker than the
3078 // multiplication:
3079 //
3080 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003081 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003082 SDValue LL = Op.getOperand(0);
3083 SDValue RL = Op.getOperand(1);
3084 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3085 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003086 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3087 // the high result in the even register. ISD::SMUL_LOHI is defined to
3088 // return the low half first, so the results are in reverse order.
3089 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003090 LL, RL, Ops[1], Ops[0]);
3091 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3092 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3093 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3094 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3095 }
Craig Topper64941d92014-04-27 19:20:57 +00003096 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003097}
3098
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003099SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3100 SelectionDAG &DAG) const {
3101 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003102 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003103 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003104 if (is32Bit(VT))
3105 // Just do a normal 64-bit multiplication and extract the results.
3106 // We define this so that it can be used for constant division.
3107 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3108 Op.getOperand(1), Ops[1], Ops[0]);
3109 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003110 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3111 // the high result in the even register. ISD::UMUL_LOHI is defined to
3112 // return the low half first, so the results are in reverse order.
3113 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003114 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003115 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003116}
3117
3118SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3119 SelectionDAG &DAG) const {
3120 SDValue Op0 = Op.getOperand(0);
3121 SDValue Op1 = Op.getOperand(1);
3122 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003123 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003124
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003125 // We use DSGF for 32-bit division. This means the first operand must
3126 // always be 64-bit, and the second operand should be 32-bit whenever
3127 // that is possible, to improve performance.
3128 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003129 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003130 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003131 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003132
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003133 // DSG(F) returns the remainder in the even register and the
3134 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003135 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003136 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003137 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003138}
3139
3140SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3141 SelectionDAG &DAG) const {
3142 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003143 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003144
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003145 // DL(G) returns the remainder in the even register and the
3146 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003147 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003148 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3149 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003150 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003151}
3152
3153SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3154 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3155
3156 // Get the known-zero masks for each operand.
3157 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003158 KnownBits Known[2];
3159 DAG.computeKnownBits(Ops[0], Known[0]);
3160 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003161
3162 // See if the upper 32 bits of one operand and the lower 32 bits of the
3163 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003164 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3165 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003166 unsigned High, Low;
3167 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3168 High = 1, Low = 0;
3169 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3170 High = 0, Low = 1;
3171 else
3172 return Op;
3173
3174 SDValue LowOp = Ops[Low];
3175 SDValue HighOp = Ops[High];
3176
3177 // If the high part is a constant, we're better off using IILH.
3178 if (HighOp.getOpcode() == ISD::Constant)
3179 return Op;
3180
3181 // If the low part is a constant that is outside the range of LHI,
3182 // then we're better off using IILF.
3183 if (LowOp.getOpcode() == ISD::Constant) {
3184 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3185 if (!isInt<16>(Value))
3186 return Op;
3187 }
3188
3189 // Check whether the high part is an AND that doesn't change the
3190 // high 32 bits and just masks out low bits. We can skip it if so.
3191 if (HighOp.getOpcode() == ISD::AND &&
3192 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003193 SDValue HighOp0 = HighOp.getOperand(0);
3194 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3195 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3196 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003197 }
3198
3199 // Take advantage of the fact that all GR32 operations only change the
3200 // low 32 bits by truncating Low to an i32 and inserting it directly
3201 // using a subreg. The interesting cases are those where the truncation
3202 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003203 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003204 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003205 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003206 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003207}
3208
Ulrich Weigandb4012182015-03-31 12:56:33 +00003209SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3210 SelectionDAG &DAG) const {
3211 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003212 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003213 Op = Op.getOperand(0);
3214
3215 // Handle vector types via VPOPCT.
3216 if (VT.isVector()) {
3217 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3218 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003219 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003220 case 8:
3221 break;
3222 case 16: {
3223 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3224 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3225 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3226 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3227 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3228 break;
3229 }
3230 case 32: {
3231 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3232 DAG.getConstant(0, DL, MVT::i32));
3233 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3234 break;
3235 }
3236 case 64: {
3237 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3238 DAG.getConstant(0, DL, MVT::i32));
3239 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3240 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3241 break;
3242 }
3243 default:
3244 llvm_unreachable("Unexpected type");
3245 }
3246 return Op;
3247 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003248
3249 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003250 KnownBits Known;
3251 DAG.computeKnownBits(Op, Known);
3252 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003253 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003254 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003255
3256 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003257 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003258 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3259 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003260
3261 // The POPCNT instruction counts the number of bits in each byte.
3262 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3263 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3264 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3265
3266 // Add up per-byte counts in a binary tree. All bits of Op at
3267 // position larger than BitSize remain zero throughout.
3268 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003269 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003270 if (BitSize != OrigBitSize)
3271 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003272 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003273 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3274 }
3275
3276 // Extract overall result from high byte.
3277 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003278 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3279 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003280
3281 return Op;
3282}
3283
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003284SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3285 SelectionDAG &DAG) const {
3286 SDLoc DL(Op);
3287 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3288 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003289 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003290 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3291
3292 // The only fence that needs an instruction is a sequentially-consistent
3293 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003294 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003295 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003296 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003297 Op.getOperand(0)),
3298 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003299 }
3300
3301 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3302 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3303}
3304
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003305// Op is an atomic load. Lower it into a normal volatile load.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003306SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3307 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003308 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003309 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003310 Node->getChain(), Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003311 Node->getMemoryVT(), Node->getMemOperand());
3312}
3313
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003314// Op is an atomic store. Lower it into a normal volatile store.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003315SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3316 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003317 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003318 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3319 Node->getBasePtr(), Node->getMemoryVT(),
3320 Node->getMemOperand());
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003321 // We have to enforce sequential consistency by performing a
3322 // serialization operation after the store.
3323 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent)
3324 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3325 MVT::Other, Chain), 0);
3326 return Chain;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003327}
3328
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003329// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3330// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003331SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3332 SelectionDAG &DAG,
3333 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003334 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003335
3336 // 32-bit operations need no code outside the main loop.
3337 EVT NarrowVT = Node->getMemoryVT();
3338 EVT WideVT = MVT::i32;
3339 if (NarrowVT == WideVT)
3340 return Op;
3341
3342 int64_t BitSize = NarrowVT.getSizeInBits();
3343 SDValue ChainIn = Node->getChain();
3344 SDValue Addr = Node->getBasePtr();
3345 SDValue Src2 = Node->getVal();
3346 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003347 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003348 EVT PtrVT = Addr.getValueType();
3349
3350 // Convert atomic subtracts of constants into additions.
3351 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003352 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003353 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003354 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003355 }
3356
3357 // Get the address of the containing word.
3358 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003359 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003360
3361 // Get the number of bits that the word must be rotated left in order
3362 // to bring the field to the top bits of a GR32.
3363 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003364 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003365 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3366
3367 // Get the complementing shift amount, for rotating a field in the top
3368 // bits back to its proper position.
3369 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003370 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003371
3372 // Extend the source operand to 32 bits and prepare it for the inner loop.
3373 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3374 // operations require the source to be shifted in advance. (This shift
3375 // can be folded if the source is constant.) For AND and NAND, the lower
3376 // bits must be set, while for other opcodes they should be left clear.
3377 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3378 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003379 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003380 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3381 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3382 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003383 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003384
3385 // Construct the ATOMIC_LOADW_* node.
3386 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3387 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003388 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003389 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003390 NarrowVT, MMO);
3391
3392 // Rotate the result of the final CS so that the field is in the lower
3393 // bits of a GR32, then truncate it.
3394 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003395 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003396 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3397
3398 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003399 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003400}
3401
Richard Sandiford41350a52013-12-24 15:18:04 +00003402// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003403// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003404// operations into additions.
3405SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3406 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003407 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003408 EVT MemVT = Node->getMemoryVT();
3409 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3410 // A full-width operation.
3411 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3412 SDValue Src2 = Node->getVal();
3413 SDValue NegSrc2;
3414 SDLoc DL(Src2);
3415
Richard Sandiford21f5d682014-03-06 11:22:58 +00003416 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003417 // Use an addition if the operand is constant and either LAA(G) is
3418 // available or the negative value is in the range of A(G)FHI.
3419 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003420 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003421 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003422 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003423 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003424 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003425 Src2);
3426
3427 if (NegSrc2.getNode())
3428 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3429 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003430 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003431
3432 // Use the node as-is.
3433 return Op;
3434 }
3435
3436 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3437}
3438
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003439// Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003440SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3441 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003442 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003443 SDValue ChainIn = Node->getOperand(0);
3444 SDValue Addr = Node->getOperand(1);
3445 SDValue CmpVal = Node->getOperand(2);
3446 SDValue SwapVal = Node->getOperand(3);
3447 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003448 SDLoc DL(Node);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003449
3450 // We have native support for 32-bit and 64-bit compare and swap, but we
3451 // still need to expand extracting the "success" result from the CC.
3452 EVT NarrowVT = Node->getMemoryVT();
3453 EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32;
3454 if (NarrowVT == WideVT) {
3455 SDVTList Tys = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
3456 SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal };
3457 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP,
3458 DL, Tys, Ops, NarrowVT, MMO);
3459 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3460 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
3461
3462 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3463 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3464 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3465 return SDValue();
3466 }
3467
3468 // Convert 8-bit and 16-bit compare and swap to a loop, implemented
3469 // via a fullword ATOMIC_CMP_SWAPW operation.
3470 int64_t BitSize = NarrowVT.getSizeInBits();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003471 EVT PtrVT = Addr.getValueType();
3472
3473 // Get the address of the containing word.
3474 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003475 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003476
3477 // Get the number of bits that the word must be rotated left in order
3478 // to bring the field to the top bits of a GR32.
3479 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003480 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003481 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3482
3483 // Get the complementing shift amount, for rotating a field in the top
3484 // bits back to its proper position.
3485 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003486 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003487
3488 // Construct the ATOMIC_CMP_SWAPW node.
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003489 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other, MVT::Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003490 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003491 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003492 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003493 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00003494 SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(2),
3495 SystemZ::CCMASK_ICMP, SystemZ::CCMASK_CMP_EQ);
3496
3497 DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
3498 DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
3499 DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(1));
3500 return SDValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003501}
3502
3503SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3504 SelectionDAG &DAG) const {
3505 MachineFunction &MF = DAG.getMachineFunction();
3506 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003507 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003508 SystemZ::R15D, Op.getValueType());
3509}
3510
3511SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3512 SelectionDAG &DAG) const {
3513 MachineFunction &MF = DAG.getMachineFunction();
3514 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Matthias Braunf1caa282017-12-15 22:22:58 +00003515 bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003516
3517 SDValue Chain = Op.getOperand(0);
3518 SDValue NewSP = Op.getOperand(1);
3519 SDValue Backchain;
3520 SDLoc DL(Op);
3521
3522 if (StoreBackchain) {
3523 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003524 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003525 }
3526
3527 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3528
3529 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003530 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003531
3532 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003533}
3534
Richard Sandiford03481332013-08-23 11:36:42 +00003535SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3536 SelectionDAG &DAG) const {
3537 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3538 if (!IsData)
3539 // Just preserve the chain.
3540 return Op.getOperand(0);
3541
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003542 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003543 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3544 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003545 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003546 SDValue Ops[] = {
3547 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003548 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003549 Op.getOperand(1)
3550 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003551 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003552 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003553 Node->getMemoryVT(), Node->getMemOperand());
3554}
3555
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003556// Return an i32 that contains the value of CC immediately after After,
3557// whose final operand must be MVT::Glue.
3558static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003559 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003560 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003561 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3562 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3563 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003564}
3565
3566SDValue
3567SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3568 SelectionDAG &DAG) const {
3569 unsigned Opcode, CCValid;
3570 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3571 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3572 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3573 SDValue CC = getCCResult(DAG, Glued.getNode());
3574 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3575 return SDValue();
3576 }
3577
3578 return SDValue();
3579}
3580
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003581SDValue
3582SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3583 SelectionDAG &DAG) const {
3584 unsigned Opcode, CCValid;
3585 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3586 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3587 SDValue CC = getCCResult(DAG, Glued.getNode());
3588 if (Op->getNumValues() == 1)
3589 return CC;
3590 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003591 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3592 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003593 }
3594
3595 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3596 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003597 case Intrinsic::thread_pointer:
3598 return lowerThreadPointer(SDLoc(Op), DAG);
3599
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003600 case Intrinsic::s390_vpdi:
3601 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3602 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3603
3604 case Intrinsic::s390_vperm:
3605 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3606 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3607
3608 case Intrinsic::s390_vuphb:
3609 case Intrinsic::s390_vuphh:
3610 case Intrinsic::s390_vuphf:
3611 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3612 Op.getOperand(1));
3613
3614 case Intrinsic::s390_vuplhb:
3615 case Intrinsic::s390_vuplhh:
3616 case Intrinsic::s390_vuplhf:
3617 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3618 Op.getOperand(1));
3619
3620 case Intrinsic::s390_vuplb:
3621 case Intrinsic::s390_vuplhw:
3622 case Intrinsic::s390_vuplf:
3623 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3624 Op.getOperand(1));
3625
3626 case Intrinsic::s390_vupllb:
3627 case Intrinsic::s390_vupllh:
3628 case Intrinsic::s390_vupllf:
3629 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3630 Op.getOperand(1));
3631
3632 case Intrinsic::s390_vsumb:
3633 case Intrinsic::s390_vsumh:
3634 case Intrinsic::s390_vsumgh:
3635 case Intrinsic::s390_vsumgf:
3636 case Intrinsic::s390_vsumqf:
3637 case Intrinsic::s390_vsumqg:
3638 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3639 Op.getOperand(1), Op.getOperand(2));
3640 }
3641
3642 return SDValue();
3643}
3644
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003645namespace {
3646// Says that SystemZISD operation Opcode can be used to perform the equivalent
3647// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3648// Operand is the constant third operand, otherwise it is the number of
3649// bytes in each element of the result.
3650struct Permute {
3651 unsigned Opcode;
3652 unsigned Operand;
3653 unsigned char Bytes[SystemZ::VectorBytes];
3654};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003655}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003656
3657static const Permute PermuteForms[] = {
3658 // VMRHG
3659 { SystemZISD::MERGE_HIGH, 8,
3660 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3661 // VMRHF
3662 { SystemZISD::MERGE_HIGH, 4,
3663 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3664 // VMRHH
3665 { SystemZISD::MERGE_HIGH, 2,
3666 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3667 // VMRHB
3668 { SystemZISD::MERGE_HIGH, 1,
3669 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3670 // VMRLG
3671 { SystemZISD::MERGE_LOW, 8,
3672 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3673 // VMRLF
3674 { SystemZISD::MERGE_LOW, 4,
3675 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3676 // VMRLH
3677 { SystemZISD::MERGE_LOW, 2,
3678 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3679 // VMRLB
3680 { SystemZISD::MERGE_LOW, 1,
3681 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3682 // VPKG
3683 { SystemZISD::PACK, 4,
3684 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3685 // VPKF
3686 { SystemZISD::PACK, 2,
3687 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3688 // VPKH
3689 { SystemZISD::PACK, 1,
3690 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3691 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3692 { SystemZISD::PERMUTE_DWORDS, 4,
3693 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3694 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3695 { SystemZISD::PERMUTE_DWORDS, 1,
3696 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3697};
3698
3699// Called after matching a vector shuffle against a particular pattern.
3700// Both the original shuffle and the pattern have two vector operands.
3701// OpNos[0] is the operand of the original shuffle that should be used for
3702// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3703// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3704// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3705// for operands 0 and 1 of the pattern.
3706static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3707 if (OpNos[0] < 0) {
3708 if (OpNos[1] < 0)
3709 return false;
3710 OpNo0 = OpNo1 = OpNos[1];
3711 } else if (OpNos[1] < 0) {
3712 OpNo0 = OpNo1 = OpNos[0];
3713 } else {
3714 OpNo0 = OpNos[0];
3715 OpNo1 = OpNos[1];
3716 }
3717 return true;
3718}
3719
3720// Bytes is a VPERM-like permute vector, except that -1 is used for
3721// undefined bytes. Return true if the VPERM can be implemented using P.
3722// When returning true set OpNo0 to the VPERM operand that should be
3723// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3724//
3725// For example, if swapping the VPERM operands allows P to match, OpNo0
3726// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3727// operand, but rewriting it to use two duplicated operands allows it to
3728// match P, then OpNo0 and OpNo1 will be the same.
3729static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3730 unsigned &OpNo0, unsigned &OpNo1) {
3731 int OpNos[] = { -1, -1 };
3732 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3733 int Elt = Bytes[I];
3734 if (Elt >= 0) {
3735 // Make sure that the two permute vectors use the same suboperand
3736 // byte number. Only the operand numbers (the high bits) are
3737 // allowed to differ.
3738 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3739 return false;
3740 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3741 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3742 // Make sure that the operand mappings are consistent with previous
3743 // elements.
3744 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3745 return false;
3746 OpNos[ModelOpNo] = RealOpNo;
3747 }
3748 }
3749 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3750}
3751
3752// As above, but search for a matching permute.
3753static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3754 unsigned &OpNo0, unsigned &OpNo1) {
3755 for (auto &P : PermuteForms)
3756 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3757 return &P;
3758 return nullptr;
3759}
3760
3761// Bytes is a VPERM-like permute vector, except that -1 is used for
3762// undefined bytes. This permute is an operand of an outer permute.
3763// See whether redistributing the -1 bytes gives a shuffle that can be
3764// implemented using P. If so, set Transform to a VPERM-like permute vector
3765// that, when applied to the result of P, gives the original permute in Bytes.
3766static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3767 const Permute &P,
3768 SmallVectorImpl<int> &Transform) {
3769 unsigned To = 0;
3770 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3771 int Elt = Bytes[From];
3772 if (Elt < 0)
3773 // Byte number From of the result is undefined.
3774 Transform[From] = -1;
3775 else {
3776 while (P.Bytes[To] != Elt) {
3777 To += 1;
3778 if (To == SystemZ::VectorBytes)
3779 return false;
3780 }
3781 Transform[From] = To;
3782 }
3783 }
3784 return true;
3785}
3786
3787// As above, but search for a matching permute.
3788static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3789 SmallVectorImpl<int> &Transform) {
3790 for (auto &P : PermuteForms)
3791 if (matchDoublePermute(Bytes, P, Transform))
3792 return &P;
3793 return nullptr;
3794}
3795
3796// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3797// as if it had type vNi8.
3798static void getVPermMask(ShuffleVectorSDNode *VSN,
3799 SmallVectorImpl<int> &Bytes) {
3800 EVT VT = VSN->getValueType(0);
3801 unsigned NumElements = VT.getVectorNumElements();
3802 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3803 Bytes.resize(NumElements * BytesPerElement, -1);
3804 for (unsigned I = 0; I < NumElements; ++I) {
3805 int Index = VSN->getMaskElt(I);
3806 if (Index >= 0)
3807 for (unsigned J = 0; J < BytesPerElement; ++J)
3808 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3809 }
3810}
3811
3812// Bytes is a VPERM-like permute vector, except that -1 is used for
3813// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3814// the result come from a contiguous sequence of bytes from one input.
3815// Set Base to the selector for the first byte if so.
3816static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3817 unsigned BytesPerElement, int &Base) {
3818 Base = -1;
3819 for (unsigned I = 0; I < BytesPerElement; ++I) {
3820 if (Bytes[Start + I] >= 0) {
3821 unsigned Elem = Bytes[Start + I];
3822 if (Base < 0) {
3823 Base = Elem - I;
3824 // Make sure the bytes would come from one input operand.
3825 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3826 return false;
3827 } else if (unsigned(Base) != Elem - I)
3828 return false;
3829 }
3830 }
3831 return true;
3832}
3833
3834// Bytes is a VPERM-like permute vector, except that -1 is used for
3835// undefined bytes. Return true if it can be performed using VSLDI.
3836// When returning true, set StartIndex to the shift amount and OpNo0
3837// and OpNo1 to the VPERM operands that should be used as the first
3838// and second shift operand respectively.
3839static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3840 unsigned &StartIndex, unsigned &OpNo0,
3841 unsigned &OpNo1) {
3842 int OpNos[] = { -1, -1 };
3843 int Shift = -1;
3844 for (unsigned I = 0; I < 16; ++I) {
3845 int Index = Bytes[I];
3846 if (Index >= 0) {
3847 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3848 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3849 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3850 if (Shift < 0)
3851 Shift = ExpectedShift;
3852 else if (Shift != ExpectedShift)
3853 return false;
3854 // Make sure that the operand mappings are consistent with previous
3855 // elements.
3856 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3857 return false;
3858 OpNos[ModelOpNo] = RealOpNo;
3859 }
3860 }
3861 StartIndex = Shift;
3862 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3863}
3864
3865// Create a node that performs P on operands Op0 and Op1, casting the
3866// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003867static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003868 const Permute &P, SDValue Op0, SDValue Op1) {
3869 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3870 // elements of a PACK are twice as wide as the outputs.
3871 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3872 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3873 P.Operand);
3874 // Cast both operands to the appropriate type.
3875 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3876 SystemZ::VectorBytes / InBytes);
3877 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3878 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3879 SDValue Op;
3880 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3881 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3882 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3883 } else if (P.Opcode == SystemZISD::PACK) {
3884 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3885 SystemZ::VectorBytes / P.Operand);
3886 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3887 } else {
3888 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3889 }
3890 return Op;
3891}
3892
3893// Bytes is a VPERM-like permute vector, except that -1 is used for
3894// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3895// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003896static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3897 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003898 const SmallVectorImpl<int> &Bytes) {
3899 for (unsigned I = 0; I < 2; ++I)
3900 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3901
3902 // First see whether VSLDI can be used.
3903 unsigned StartIndex, OpNo0, OpNo1;
3904 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3905 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3906 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3907
3908 // Fall back on VPERM. Construct an SDNode for the permute vector.
3909 SDValue IndexNodes[SystemZ::VectorBytes];
3910 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3911 if (Bytes[I] >= 0)
3912 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3913 else
3914 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003915 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003916 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3917}
3918
3919namespace {
3920// Describes a general N-operand vector shuffle.
3921struct GeneralShuffle {
3922 GeneralShuffle(EVT vt) : VT(vt) {}
3923 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003924 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003925 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003926
3927 // The operands of the shuffle.
3928 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3929
3930 // Index I is -1 if byte I of the result is undefined. Otherwise the
3931 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3932 // Bytes[I] / SystemZ::VectorBytes.
3933 SmallVector<int, SystemZ::VectorBytes> Bytes;
3934
3935 // The type of the shuffle result.
3936 EVT VT;
3937};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003938}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003939
3940// Add an extra undefined element to the shuffle.
3941void GeneralShuffle::addUndef() {
3942 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3943 for (unsigned I = 0; I < BytesPerElement; ++I)
3944 Bytes.push_back(-1);
3945}
3946
3947// Add an extra element to the shuffle, taking it from element Elem of Op.
3948// A null Op indicates a vector input whose value will be calculated later;
3949// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003950// type as the result. Aborts and returns false if the source vector elements
3951// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
3952// LLVM they become implicitly extended, but this is rare and not optimized.
3953bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003954 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3955
3956 // The source vector can have wider elements than the result,
3957 // either through an explicit TRUNCATE or because of type legalization.
3958 // We want the least significant part.
3959 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3960 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003961
3962 // Return false if the source elements are smaller than their destination
3963 // elements.
3964 if (FromBytesPerElement < BytesPerElement)
3965 return false;
3966
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003967 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3968 (FromBytesPerElement - BytesPerElement));
3969
3970 // Look through things like shuffles and bitcasts.
3971 while (Op.getNode()) {
3972 if (Op.getOpcode() == ISD::BITCAST)
3973 Op = Op.getOperand(0);
3974 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3975 // See whether the bytes we need come from a contiguous part of one
3976 // operand.
3977 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3978 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3979 int NewByte;
3980 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3981 break;
3982 if (NewByte < 0) {
3983 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003984 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003985 }
3986 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3987 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003988 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003989 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003990 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003991 } else
3992 break;
3993 }
3994
3995 // Make sure that the source of the extraction is in Ops.
3996 unsigned OpNo = 0;
3997 for (; OpNo < Ops.size(); ++OpNo)
3998 if (Ops[OpNo] == Op)
3999 break;
4000 if (OpNo == Ops.size())
4001 Ops.push_back(Op);
4002
4003 // Add the element to Bytes.
4004 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
4005 for (unsigned I = 0; I < BytesPerElement; ++I)
4006 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004007
4008 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004009}
4010
4011// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004012SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004013 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
4014
4015 if (Ops.size() == 0)
4016 return DAG.getUNDEF(VT);
4017
4018 // Make sure that there are at least two shuffle operands.
4019 if (Ops.size() == 1)
4020 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
4021
4022 // Create a tree of shuffles, deferring root node until after the loop.
4023 // Try to redistribute the undefined elements of non-root nodes so that
4024 // the non-root shuffles match something like a pack or merge, then adjust
4025 // the parent node's permute vector to compensate for the new order.
4026 // Among other things, this copes with vectors like <2 x i16> that were
4027 // padded with undefined elements during type legalization.
4028 //
4029 // In the best case this redistribution will lead to the whole tree
4030 // using packs and merges. It should rarely be a loss in other cases.
4031 unsigned Stride = 1;
4032 for (; Stride * 2 < Ops.size(); Stride *= 2) {
4033 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
4034 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4035
4036 // Create a mask for just these two operands.
4037 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4038 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4039 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4040 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4041 if (OpNo == I)
4042 NewBytes[J] = Byte;
4043 else if (OpNo == I + Stride)
4044 NewBytes[J] = SystemZ::VectorBytes + Byte;
4045 else
4046 NewBytes[J] = -1;
4047 }
4048 // See if it would be better to reorganize NewMask to avoid using VPERM.
4049 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4050 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4051 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4052 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4053 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4054 if (NewBytes[J] >= 0) {
4055 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4056 "Invalid double permute");
4057 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4058 } else
4059 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4060 }
4061 } else {
4062 // Just use NewBytes on the operands.
4063 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4064 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4065 if (NewBytes[J] >= 0)
4066 Bytes[J] = I * SystemZ::VectorBytes + J;
4067 }
4068 }
4069 }
4070
4071 // Now we just have 2 inputs. Put the second operand in Ops[1].
4072 if (Stride > 1) {
4073 Ops[1] = Ops[Stride];
4074 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4075 if (Bytes[I] >= int(SystemZ::VectorBytes))
4076 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4077 }
4078
4079 // Look for an instruction that can do the permute without resorting
4080 // to VPERM.
4081 unsigned OpNo0, OpNo1;
4082 SDValue Op;
4083 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4084 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4085 else
4086 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4087 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4088}
4089
Ulrich Weigandcd808232015-05-05 19:26:48 +00004090// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4091static bool isScalarToVector(SDValue Op) {
4092 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004093 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004094 return false;
4095 return true;
4096}
4097
4098// Return a vector of type VT that contains Value in the first element.
4099// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004100static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004101 SDValue Value) {
4102 // If we have a constant, replicate it to all elements and let the
4103 // BUILD_VECTOR lowering take care of it.
4104 if (Value.getOpcode() == ISD::Constant ||
4105 Value.getOpcode() == ISD::ConstantFP) {
4106 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004107 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004108 }
Sanjay Patel57195842016-03-14 17:28:46 +00004109 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004110 return DAG.getUNDEF(VT);
4111 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4112}
4113
4114// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4115// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004116static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004117 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004118 if (Op0.isUndef()) {
4119 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004120 return DAG.getUNDEF(VT);
4121 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4122 }
Sanjay Patel57195842016-03-14 17:28:46 +00004123 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004124 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4125 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4126 buildScalarToVector(DAG, DL, VT, Op0),
4127 buildScalarToVector(DAG, DL, VT, Op1));
4128}
4129
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004130// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4131// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004132static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004133 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004134 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004135 return DAG.getUNDEF(MVT::v2i64);
4136 // If one of the two inputs is undefined then replicate the other one,
4137 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004138 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004139 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004140 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004141 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4142 else {
4143 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4144 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4145 }
4146 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4147}
4148
4149// Try to represent constant BUILD_VECTOR node BVN using a
4150// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4151// on success.
4152static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4153 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4154 unsigned BytesPerElement = ElemVT.getStoreSize();
4155 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4156 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004157 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004158 uint64_t Value;
4159 if (Op.getOpcode() == ISD::Constant)
4160 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4161 else if (Op.getOpcode() == ISD::ConstantFP)
4162 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4163 .getZExtValue());
4164 else
4165 return false;
4166 for (unsigned J = 0; J < BytesPerElement; ++J) {
4167 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4168 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004169 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004170 else if (Byte != 0)
4171 return false;
4172 }
4173 }
4174 }
4175 return true;
4176}
4177
4178// Try to load a vector constant in which BitsPerElement-bit value Value
4179// is replicated to fill the vector. VT is the type of the resulting
4180// constant, which may have elements of a different size from BitsPerElement.
4181// Return the SDValue of the constant on success, otherwise return
4182// an empty value.
4183static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4184 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004185 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004186 unsigned BitsPerElement) {
4187 // Signed 16-bit values can be replicated using VREPI.
4188 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4189 if (isInt<16>(SignedValue)) {
4190 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4191 SystemZ::VectorBits / BitsPerElement);
4192 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4193 DAG.getConstant(SignedValue, DL, MVT::i32));
4194 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4195 }
4196 // See whether rotating the constant left some N places gives a value that
4197 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4198 // If so we can use VGM.
4199 unsigned Start, End;
4200 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4201 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4202 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4203 // bit numbers for an BitsPerElement value, so that 0 denotes
4204 // 1 << (BitsPerElement-1).
4205 Start -= 64 - BitsPerElement;
4206 End -= 64 - BitsPerElement;
4207 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4208 SystemZ::VectorBits / BitsPerElement);
4209 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4210 DAG.getConstant(Start, DL, MVT::i32),
4211 DAG.getConstant(End, DL, MVT::i32));
4212 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4213 }
4214 return SDValue();
4215}
4216
4217// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4218// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4219// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4220// would benefit from this representation and return it if so.
4221static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4222 BuildVectorSDNode *BVN) {
4223 EVT VT = BVN->getValueType(0);
4224 unsigned NumElements = VT.getVectorNumElements();
4225
4226 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4227 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4228 // need a BUILD_VECTOR, add an additional placeholder operand for that
4229 // BUILD_VECTOR and store its operands in ResidueOps.
4230 GeneralShuffle GS(VT);
4231 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4232 bool FoundOne = false;
4233 for (unsigned I = 0; I < NumElements; ++I) {
4234 SDValue Op = BVN->getOperand(I);
4235 if (Op.getOpcode() == ISD::TRUNCATE)
4236 Op = Op.getOperand(0);
4237 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4238 Op.getOperand(1).getOpcode() == ISD::Constant) {
4239 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004240 if (!GS.add(Op.getOperand(0), Elem))
4241 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004242 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004243 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004244 GS.addUndef();
4245 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004246 if (!GS.add(SDValue(), ResidueOps.size()))
4247 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004248 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004249 }
4250 }
4251
4252 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4253 if (!FoundOne)
4254 return SDValue();
4255
4256 // Create the BUILD_VECTOR for the remaining elements, if any.
4257 if (!ResidueOps.empty()) {
4258 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004259 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004260 for (auto &Op : GS.Ops) {
4261 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004262 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004263 break;
4264 }
4265 }
4266 }
4267 return GS.getNode(DAG, SDLoc(BVN));
4268}
4269
4270// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004271static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004272 SmallVectorImpl<SDValue> &Elems) {
4273 // See whether there is a single replicated value.
4274 SDValue Single;
4275 unsigned int NumElements = Elems.size();
4276 unsigned int Count = 0;
4277 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004278 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004279 if (!Single.getNode())
4280 Single = Elem;
4281 else if (Elem != Single) {
4282 Single = SDValue();
4283 break;
4284 }
4285 Count += 1;
4286 }
4287 }
4288 // There are three cases here:
4289 //
4290 // - if the only defined element is a loaded one, the best sequence
4291 // is a replicating load.
4292 //
4293 // - otherwise, if the only defined element is an i64 value, we will
4294 // end up with the same VLVGP sequence regardless of whether we short-cut
4295 // for replication or fall through to the later code.
4296 //
4297 // - otherwise, if the only defined element is an i32 or smaller value,
4298 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4299 // This is only a win if the single defined element is used more than once.
4300 // In other cases we're better off using a single VLVGx.
4301 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4302 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4303
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004304 // If all elements are loads, use VLREP/VLEs (below).
4305 bool AllLoads = true;
4306 for (auto Elem : Elems)
4307 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4308 AllLoads = false;
4309 break;
4310 }
4311
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004312 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004313 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004314 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4315
Ulrich Weigandcd808232015-05-05 19:26:48 +00004316 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004317 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004318 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4319
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004320 // Build v4f32 values directly from the FPRs:
4321 //
4322 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4323 // V V VMRHF
4324 // <ABxx> <CDxx>
4325 // V VMRHG
4326 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004327 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004328 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4329 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4330 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004331 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004332 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004333 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004334 Op23 = Op01;
4335 // Merging identical replications is a no-op.
4336 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4337 return Op01;
4338 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4339 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4340 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4341 DL, MVT::v2i64, Op01, Op23);
4342 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4343 }
4344
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004345 // Collect the constant terms.
4346 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4347 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4348
4349 unsigned NumConstants = 0;
4350 for (unsigned I = 0; I < NumElements; ++I) {
4351 SDValue Elem = Elems[I];
4352 if (Elem.getOpcode() == ISD::Constant ||
4353 Elem.getOpcode() == ISD::ConstantFP) {
4354 NumConstants += 1;
4355 Constants[I] = Elem;
4356 Done[I] = true;
4357 }
4358 }
4359 // If there was at least one constant, fill in the other elements of
4360 // Constants with undefs to get a full vector constant and use that
4361 // as the starting point.
4362 SDValue Result;
4363 if (NumConstants > 0) {
4364 for (unsigned I = 0; I < NumElements; ++I)
4365 if (!Constants[I].getNode())
4366 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004367 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004368 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004369 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004370 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004371 // register.
4372
4373 // Use a VLREP if at least one element is a load.
4374 unsigned LoadElIdx = UINT_MAX;
4375 for (unsigned I = 0; I < NumElements; ++I)
4376 if (Elems[I].getOpcode() == ISD::LOAD &&
4377 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4378 LoadElIdx = I;
4379 break;
4380 }
4381 if (LoadElIdx != UINT_MAX) {
4382 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4383 Done[LoadElIdx] = true;
4384 } else {
4385 // Try to use VLVGP.
4386 unsigned I1 = NumElements / 2 - 1;
4387 unsigned I2 = NumElements - 1;
4388 bool Def1 = !Elems[I1].isUndef();
4389 bool Def2 = !Elems[I2].isUndef();
4390 if (Def1 || Def2) {
4391 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4392 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4393 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4394 joinDwords(DAG, DL, Elem1, Elem2));
4395 Done[I1] = true;
4396 Done[I2] = true;
4397 } else
4398 Result = DAG.getUNDEF(VT);
4399 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004400 }
4401
4402 // Use VLVGx to insert the other elements.
4403 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004404 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004405 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4406 DAG.getConstant(I, DL, MVT::i32));
4407 return Result;
4408}
4409
4410SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4411 SelectionDAG &DAG) const {
4412 const SystemZInstrInfo *TII =
4413 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4414 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4415 SDLoc DL(Op);
4416 EVT VT = Op.getValueType();
4417
4418 if (BVN->isConstant()) {
4419 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4420 // preferred way of creating all-zero and all-one vectors so give it
4421 // priority over other methods below.
4422 uint64_t Mask = 0;
4423 if (tryBuildVectorByteMask(BVN, Mask)) {
4424 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4425 DAG.getConstant(Mask, DL, MVT::i32));
4426 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4427 }
4428
4429 // Try using some form of replication.
4430 APInt SplatBits, SplatUndef;
4431 unsigned SplatBitSize;
4432 bool HasAnyUndefs;
4433 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4434 8, true) &&
4435 SplatBitSize <= 64) {
4436 // First try assuming that any undefined bits above the highest set bit
4437 // and below the lowest set bit are 1s. This increases the likelihood of
4438 // being able to use a sign-extended element value in VECTOR REPLICATE
4439 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4440 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4441 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4442 uint64_t Lower = (SplatUndefZ
4443 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4444 uint64_t Upper = (SplatUndefZ
4445 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4446 uint64_t Value = SplatBitsZ | Upper | Lower;
4447 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4448 SplatBitSize);
4449 if (Op.getNode())
4450 return Op;
4451
4452 // Now try assuming that any undefined bits between the first and
4453 // last defined set bits are set. This increases the chances of
4454 // using a non-wraparound mask.
4455 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4456 Value = SplatBitsZ | Middle;
4457 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4458 if (Op.getNode())
4459 return Op;
4460 }
4461
4462 // Fall back to loading it from memory.
4463 return SDValue();
4464 }
4465
4466 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004467 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004468 return Res;
4469
Ulrich Weigandcd808232015-05-05 19:26:48 +00004470 // Detect SCALAR_TO_VECTOR conversions.
4471 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4472 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4473
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004474 // Otherwise use buildVector to build the vector up from GPRs.
4475 unsigned NumElements = Op.getNumOperands();
4476 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4477 for (unsigned I = 0; I < NumElements; ++I)
4478 Ops[I] = Op.getOperand(I);
4479 return buildVector(DAG, DL, VT, Ops);
4480}
4481
4482SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4483 SelectionDAG &DAG) const {
4484 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4485 SDLoc DL(Op);
4486 EVT VT = Op.getValueType();
4487 unsigned NumElements = VT.getVectorNumElements();
4488
4489 if (VSN->isSplat()) {
4490 SDValue Op0 = Op.getOperand(0);
4491 unsigned Index = VSN->getSplatIndex();
4492 assert(Index < VT.getVectorNumElements() &&
4493 "Splat index should be defined and in first operand");
4494 // See whether the value we're splatting is directly available as a scalar.
4495 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4496 Op0.getOpcode() == ISD::BUILD_VECTOR)
4497 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4498 // Otherwise keep it as a vector-to-vector operation.
4499 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4500 DAG.getConstant(Index, DL, MVT::i32));
4501 }
4502
4503 GeneralShuffle GS(VT);
4504 for (unsigned I = 0; I < NumElements; ++I) {
4505 int Elt = VSN->getMaskElt(I);
4506 if (Elt < 0)
4507 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004508 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4509 unsigned(Elt) % NumElements))
4510 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004511 }
4512 return GS.getNode(DAG, SDLoc(VSN));
4513}
4514
4515SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4516 SelectionDAG &DAG) const {
4517 SDLoc DL(Op);
4518 // Just insert the scalar into element 0 of an undefined vector.
4519 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4520 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4521 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4522}
4523
Ulrich Weigandcd808232015-05-05 19:26:48 +00004524SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4525 SelectionDAG &DAG) const {
4526 // Handle insertions of floating-point values.
4527 SDLoc DL(Op);
4528 SDValue Op0 = Op.getOperand(0);
4529 SDValue Op1 = Op.getOperand(1);
4530 SDValue Op2 = Op.getOperand(2);
4531 EVT VT = Op.getValueType();
4532
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004533 // Insertions into constant indices of a v2f64 can be done using VPDI.
4534 // However, if the inserted value is a bitcast or a constant then it's
4535 // better to use GPRs, as below.
4536 if (VT == MVT::v2f64 &&
4537 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004538 Op1.getOpcode() != ISD::ConstantFP &&
4539 Op2.getOpcode() == ISD::Constant) {
4540 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4541 unsigned Mask = VT.getVectorNumElements() - 1;
4542 if (Index <= Mask)
4543 return Op;
4544 }
4545
4546 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004547 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004548 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4549 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4550 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4551 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4552 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4553}
4554
4555SDValue
4556SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4557 SelectionDAG &DAG) const {
4558 // Handle extractions of floating-point values.
4559 SDLoc DL(Op);
4560 SDValue Op0 = Op.getOperand(0);
4561 SDValue Op1 = Op.getOperand(1);
4562 EVT VT = Op.getValueType();
4563 EVT VecVT = Op0.getValueType();
4564
4565 // Extractions of constant indices can be done directly.
4566 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4567 uint64_t Index = CIndexN->getZExtValue();
4568 unsigned Mask = VecVT.getVectorNumElements() - 1;
4569 if (Index <= Mask)
4570 return Op;
4571 }
4572
4573 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4574 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4575 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4576 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4577 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4578 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4579}
4580
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004581SDValue
4582SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004583 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004584 SDValue PackedOp = Op.getOperand(0);
4585 EVT OutVT = Op.getValueType();
4586 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004587 unsigned ToBits = OutVT.getScalarSizeInBits();
4588 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004589 do {
4590 FromBits *= 2;
4591 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4592 SystemZ::VectorBits / FromBits);
4593 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4594 } while (FromBits != ToBits);
4595 return PackedOp;
4596}
4597
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004598SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4599 unsigned ByScalar) const {
4600 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4601 SDValue Op0 = Op.getOperand(0);
4602 SDValue Op1 = Op.getOperand(1);
4603 SDLoc DL(Op);
4604 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004605 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004606
4607 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4608 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4609 APInt SplatBits, SplatUndef;
4610 unsigned SplatBitSize;
4611 bool HasAnyUndefs;
4612 // Check for constant splats. Use ElemBitSize as the minimum element
4613 // width and reject splats that need wider elements.
4614 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4615 ElemBitSize, true) &&
4616 SplatBitSize == ElemBitSize) {
4617 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4618 DL, MVT::i32);
4619 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4620 }
4621 // Check for variable splats.
4622 BitVector UndefElements;
4623 SDValue Splat = BVN->getSplatValue(&UndefElements);
4624 if (Splat) {
4625 // Since i32 is the smallest legal type, we either need a no-op
4626 // or a truncation.
4627 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4628 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4629 }
4630 }
4631
4632 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4633 // and the shift amount is directly available in a GPR.
4634 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4635 if (VSN->isSplat()) {
4636 SDValue VSNOp0 = VSN->getOperand(0);
4637 unsigned Index = VSN->getSplatIndex();
4638 assert(Index < VT.getVectorNumElements() &&
4639 "Splat index should be defined and in first operand");
4640 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4641 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4642 // Since i32 is the smallest legal type, we either need a no-op
4643 // or a truncation.
4644 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4645 VSNOp0.getOperand(Index));
4646 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4647 }
4648 }
4649 }
4650
4651 // Otherwise just treat the current form as legal.
4652 return Op;
4653}
4654
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004655SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4656 SelectionDAG &DAG) const {
4657 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004658 case ISD::FRAMEADDR:
4659 return lowerFRAMEADDR(Op, DAG);
4660 case ISD::RETURNADDR:
4661 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004662 case ISD::BR_CC:
4663 return lowerBR_CC(Op, DAG);
4664 case ISD::SELECT_CC:
4665 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004666 case ISD::SETCC:
4667 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004668 case ISD::GlobalAddress:
4669 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4670 case ISD::GlobalTLSAddress:
4671 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4672 case ISD::BlockAddress:
4673 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4674 case ISD::JumpTable:
4675 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4676 case ISD::ConstantPool:
4677 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4678 case ISD::BITCAST:
4679 return lowerBITCAST(Op, DAG);
4680 case ISD::VASTART:
4681 return lowerVASTART(Op, DAG);
4682 case ISD::VACOPY:
4683 return lowerVACOPY(Op, DAG);
4684 case ISD::DYNAMIC_STACKALLOC:
4685 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004686 case ISD::GET_DYNAMIC_AREA_OFFSET:
4687 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004688 case ISD::SMUL_LOHI:
4689 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004690 case ISD::UMUL_LOHI:
4691 return lowerUMUL_LOHI(Op, DAG);
4692 case ISD::SDIVREM:
4693 return lowerSDIVREM(Op, DAG);
4694 case ISD::UDIVREM:
4695 return lowerUDIVREM(Op, DAG);
4696 case ISD::OR:
4697 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004698 case ISD::CTPOP:
4699 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004700 case ISD::ATOMIC_FENCE:
4701 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004702 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004703 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4704 case ISD::ATOMIC_STORE:
4705 return lowerATOMIC_STORE(Op, DAG);
4706 case ISD::ATOMIC_LOAD:
4707 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004708 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004709 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004710 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004711 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004712 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004713 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004714 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004715 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004716 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004717 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004718 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004719 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004720 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004721 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004722 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004723 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004724 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004725 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004726 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004727 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004728 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004729 return lowerATOMIC_CMP_SWAP(Op, DAG);
4730 case ISD::STACKSAVE:
4731 return lowerSTACKSAVE(Op, DAG);
4732 case ISD::STACKRESTORE:
4733 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004734 case ISD::PREFETCH:
4735 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004736 case ISD::INTRINSIC_W_CHAIN:
4737 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004738 case ISD::INTRINSIC_WO_CHAIN:
4739 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004740 case ISD::BUILD_VECTOR:
4741 return lowerBUILD_VECTOR(Op, DAG);
4742 case ISD::VECTOR_SHUFFLE:
4743 return lowerVECTOR_SHUFFLE(Op, DAG);
4744 case ISD::SCALAR_TO_VECTOR:
4745 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004746 case ISD::INSERT_VECTOR_ELT:
4747 return lowerINSERT_VECTOR_ELT(Op, DAG);
4748 case ISD::EXTRACT_VECTOR_ELT:
4749 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004750 case ISD::SIGN_EXTEND_VECTOR_INREG:
4751 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4752 case ISD::ZERO_EXTEND_VECTOR_INREG:
4753 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004754 case ISD::SHL:
4755 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4756 case ISD::SRL:
4757 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4758 case ISD::SRA:
4759 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004760 default:
4761 llvm_unreachable("Unexpected node to lower");
4762 }
4763}
4764
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004765// Lower operations with invalid operand or result types (currently used
4766// only for 128-bit integer types).
4767
4768static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
4769 SDLoc DL(In);
4770 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4771 DAG.getIntPtrConstant(0, DL));
4772 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4773 DAG.getIntPtrConstant(1, DL));
4774 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
4775 MVT::Untyped, Hi, Lo);
4776 return SDValue(Pair, 0);
4777}
4778
4779static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
4780 SDLoc DL(In);
4781 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
4782 DL, MVT::i64, In);
4783 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
4784 DL, MVT::i64, In);
4785 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
4786}
4787
4788void
4789SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
4790 SmallVectorImpl<SDValue> &Results,
4791 SelectionDAG &DAG) const {
4792 switch (N->getOpcode()) {
4793 case ISD::ATOMIC_LOAD: {
4794 SDLoc DL(N);
4795 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4796 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
4797 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4798 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
4799 DL, Tys, Ops, MVT::i128, MMO);
4800 Results.push_back(lowerGR128ToI128(DAG, Res));
4801 Results.push_back(Res.getValue(1));
4802 break;
4803 }
4804 case ISD::ATOMIC_STORE: {
4805 SDLoc DL(N);
4806 SDVTList Tys = DAG.getVTList(MVT::Other);
4807 SDValue Ops[] = { N->getOperand(0),
4808 lowerI128ToGR128(DAG, N->getOperand(2)),
4809 N->getOperand(1) };
4810 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4811 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
4812 DL, Tys, Ops, MVT::i128, MMO);
4813 // We have to enforce sequential consistency by performing a
4814 // serialization operation after the store.
4815 if (cast<AtomicSDNode>(N)->getOrdering() ==
4816 AtomicOrdering::SequentiallyConsistent)
4817 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
4818 MVT::Other, Res), 0);
4819 Results.push_back(Res);
4820 break;
4821 }
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004822 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004823 SDLoc DL(N);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004824 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other, MVT::Glue);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004825 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
4826 lowerI128ToGR128(DAG, N->getOperand(2)),
4827 lowerI128ToGR128(DAG, N->getOperand(3)) };
4828 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4829 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
4830 DL, Tys, Ops, MVT::i128, MMO);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004831 SDValue Success = emitSETCC(DAG, DL, Res.getValue(2),
4832 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
4833 Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1));
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004834 Results.push_back(lowerGR128ToI128(DAG, Res));
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004835 Results.push_back(Success);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004836 Results.push_back(Res.getValue(1));
4837 break;
4838 }
4839 default:
4840 llvm_unreachable("Unexpected node to lower");
4841 }
4842}
4843
4844void
4845SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
4846 SmallVectorImpl<SDValue> &Results,
4847 SelectionDAG &DAG) const {
4848 return LowerOperationWrapper(N, Results, DAG);
4849}
4850
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004851const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4852#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004853 switch ((SystemZISD::NodeType)Opcode) {
4854 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004855 OPCODE(RET_FLAG);
4856 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004857 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004858 OPCODE(TLS_GDCALL);
4859 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004860 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004861 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004862 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004863 OPCODE(ICMP);
4864 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004865 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004866 OPCODE(BR_CCMASK);
4867 OPCODE(SELECT_CCMASK);
4868 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004869 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004870 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004871 OPCODE(UMUL_LOHI);
4872 OPCODE(SDIVREM);
4873 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004874 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004875 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004876 OPCODE(NC);
4877 OPCODE(NC_LOOP);
4878 OPCODE(OC);
4879 OPCODE(OC_LOOP);
4880 OPCODE(XC);
4881 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004882 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004883 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004884 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004885 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004886 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004887 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004888 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004889 OPCODE(TBEGIN);
4890 OPCODE(TBEGIN_NOFLOAT);
4891 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004892 OPCODE(BYTE_MASK);
4893 OPCODE(ROTATE_MASK);
4894 OPCODE(REPLICATE);
4895 OPCODE(JOIN_DWORDS);
4896 OPCODE(SPLAT);
4897 OPCODE(MERGE_HIGH);
4898 OPCODE(MERGE_LOW);
4899 OPCODE(SHL_DOUBLE);
4900 OPCODE(PERMUTE_DWORDS);
4901 OPCODE(PERMUTE);
4902 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004903 OPCODE(PACKS_CC);
4904 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004905 OPCODE(UNPACK_HIGH);
4906 OPCODE(UNPACKL_HIGH);
4907 OPCODE(UNPACK_LOW);
4908 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004909 OPCODE(VSHL_BY_SCALAR);
4910 OPCODE(VSRL_BY_SCALAR);
4911 OPCODE(VSRA_BY_SCALAR);
4912 OPCODE(VSUM);
4913 OPCODE(VICMPE);
4914 OPCODE(VICMPH);
4915 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004916 OPCODE(VICMPES);
4917 OPCODE(VICMPHS);
4918 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004919 OPCODE(VFCMPE);
4920 OPCODE(VFCMPH);
4921 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004922 OPCODE(VFCMPES);
4923 OPCODE(VFCMPHS);
4924 OPCODE(VFCMPHES);
4925 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004926 OPCODE(VEXTEND);
4927 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004928 OPCODE(VTM);
4929 OPCODE(VFAE_CC);
4930 OPCODE(VFAEZ_CC);
4931 OPCODE(VFEE_CC);
4932 OPCODE(VFEEZ_CC);
4933 OPCODE(VFENE_CC);
4934 OPCODE(VFENEZ_CC);
4935 OPCODE(VISTR_CC);
4936 OPCODE(VSTRC_CC);
4937 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004938 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004939 OPCODE(ATOMIC_SWAPW);
4940 OPCODE(ATOMIC_LOADW_ADD);
4941 OPCODE(ATOMIC_LOADW_SUB);
4942 OPCODE(ATOMIC_LOADW_AND);
4943 OPCODE(ATOMIC_LOADW_OR);
4944 OPCODE(ATOMIC_LOADW_XOR);
4945 OPCODE(ATOMIC_LOADW_NAND);
4946 OPCODE(ATOMIC_LOADW_MIN);
4947 OPCODE(ATOMIC_LOADW_MAX);
4948 OPCODE(ATOMIC_LOADW_UMIN);
4949 OPCODE(ATOMIC_LOADW_UMAX);
4950 OPCODE(ATOMIC_CMP_SWAPW);
Ulrich Weigand0f1de042017-09-28 16:22:54 +00004951 OPCODE(ATOMIC_CMP_SWAP);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004952 OPCODE(ATOMIC_LOAD_128);
4953 OPCODE(ATOMIC_STORE_128);
4954 OPCODE(ATOMIC_CMP_SWAP_128);
Bryan Chan28b759c2016-05-16 20:32:22 +00004955 OPCODE(LRV);
4956 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004957 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004958 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004959 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004960#undef OPCODE
4961}
4962
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004963// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00004964// in width. Also check for presence of vector support.
4965bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
4966 if (!Subtarget.hasVector())
4967 return false;
4968
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00004969 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004970}
4971
4972// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4973// producing a result of type ResVT. Op is a possibly bitcast version
4974// of the input vector and Index is the index (based on type VecVT) that
4975// should be extracted. Return the new extraction if a simplification
4976// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004977SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
4978 EVT VecVT, SDValue Op,
4979 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004980 DAGCombinerInfo &DCI,
4981 bool Force) const {
4982 SelectionDAG &DAG = DCI.DAG;
4983
4984 // The number of bytes being extracted.
4985 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4986
4987 for (;;) {
4988 unsigned Opcode = Op.getOpcode();
4989 if (Opcode == ISD::BITCAST)
4990 // Look through bitcasts.
4991 Op = Op.getOperand(0);
4992 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4993 canTreatAsByteVector(Op.getValueType())) {
4994 // Get a VPERM-like permute mask and see whether the bytes covered
4995 // by the extracted element are a contiguous sequence from one
4996 // source operand.
4997 SmallVector<int, SystemZ::VectorBytes> Bytes;
4998 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4999 int First;
5000 if (!getShuffleInput(Bytes, Index * BytesPerElement,
5001 BytesPerElement, First))
5002 break;
5003 if (First < 0)
5004 return DAG.getUNDEF(ResVT);
5005 // Make sure the contiguous sequence starts at a multiple of the
5006 // original element size.
5007 unsigned Byte = unsigned(First) % Bytes.size();
5008 if (Byte % BytesPerElement != 0)
5009 break;
5010 // We can get the extracted value directly from an input.
5011 Index = Byte / BytesPerElement;
5012 Op = Op.getOperand(unsigned(First) / Bytes.size());
5013 Force = true;
5014 } else if (Opcode == ISD::BUILD_VECTOR &&
5015 canTreatAsByteVector(Op.getValueType())) {
5016 // We can only optimize this case if the BUILD_VECTOR elements are
5017 // at least as wide as the extracted value.
5018 EVT OpVT = Op.getValueType();
5019 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5020 if (OpBytesPerElement < BytesPerElement)
5021 break;
5022 // Make sure that the least-significant bit of the extracted value
5023 // is the least significant bit of an input.
5024 unsigned End = (Index + 1) * BytesPerElement;
5025 if (End % OpBytesPerElement != 0)
5026 break;
5027 // We're extracting the low part of one operand of the BUILD_VECTOR.
5028 Op = Op.getOperand(End / OpBytesPerElement - 1);
5029 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005030 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005031 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
5032 DCI.AddToWorklist(Op.getNode());
5033 }
5034 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
5035 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5036 if (VT != ResVT) {
5037 DCI.AddToWorklist(Op.getNode());
5038 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
5039 }
5040 return Op;
5041 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005042 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
5043 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
5044 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005045 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
5046 // Make sure that only the unextended bits are significant.
5047 EVT ExtVT = Op.getValueType();
5048 EVT OpVT = Op.getOperand(0).getValueType();
5049 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
5050 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5051 unsigned Byte = Index * BytesPerElement;
5052 unsigned SubByte = Byte % ExtBytesPerElement;
5053 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
5054 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005055 SubByte + BytesPerElement > ExtBytesPerElement)
5056 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005057 // Get the byte offset of the unextended element
5058 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
5059 // ...then add the byte offset relative to that element.
5060 Byte += SubByte - MinSubByte;
5061 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005062 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005063 Op = Op.getOperand(0);
5064 Index = Byte / BytesPerElement;
5065 Force = true;
5066 } else
5067 break;
5068 }
5069 if (Force) {
5070 if (Op.getValueType() != VecVT) {
5071 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5072 DCI.AddToWorklist(Op.getNode());
5073 }
5074 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5075 DAG.getConstant(Index, DL, MVT::i32));
5076 }
5077 return SDValue();
5078}
5079
5080// Optimize vector operations in scalar value Op on the basis that Op
5081// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005082SDValue SystemZTargetLowering::combineTruncateExtract(
5083 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005084 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5085 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5086 // of type TruncVT.
5087 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5088 TruncVT.getSizeInBits() % 8 == 0) {
5089 SDValue Vec = Op.getOperand(0);
5090 EVT VecVT = Vec.getValueType();
5091 if (canTreatAsByteVector(VecVT)) {
5092 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5093 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5094 unsigned TruncBytes = TruncVT.getStoreSize();
5095 if (BytesPerElement % TruncBytes == 0) {
5096 // Calculate the value of Y' in the above description. We are
5097 // splitting the original elements into Scale equal-sized pieces
5098 // and for truncation purposes want the last (least-significant)
5099 // of these pieces for IndexN. This is easiest to do by calculating
5100 // the start index of the following element and then subtracting 1.
5101 unsigned Scale = BytesPerElement / TruncBytes;
5102 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5103
5104 // Defer the creation of the bitcast from X to combineExtract,
5105 // which might be able to optimize the extraction.
5106 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5107 VecVT.getStoreSize() / TruncBytes);
5108 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5109 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5110 }
5111 }
5112 }
5113 }
5114 return SDValue();
5115}
5116
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005117SDValue SystemZTargetLowering::combineZERO_EXTEND(
5118 SDNode *N, DAGCombinerInfo &DCI) const {
5119 // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2')
5120 SelectionDAG &DAG = DCI.DAG;
5121 SDValue N0 = N->getOperand(0);
5122 EVT VT = N->getValueType(0);
5123 if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) {
5124 auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0));
5125 auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5126 if (TrueOp && FalseOp) {
5127 SDLoc DL(N0);
5128 SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT),
5129 DAG.getConstant(FalseOp->getZExtValue(), DL, VT),
5130 N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) };
5131 SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops);
5132 // If N0 has multiple uses, change other uses as well.
5133 if (!N0.hasOneUse()) {
5134 SDValue TruncSelect =
5135 DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect);
5136 DCI.CombineTo(N0.getNode(), TruncSelect);
5137 }
5138 return NewSelect;
5139 }
5140 }
5141 return SDValue();
5142}
5143
5144SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG(
5145 SDNode *N, DAGCombinerInfo &DCI) const {
5146 // Convert (sext_in_reg (setcc LHS, RHS, COND), i1)
5147 // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1)
5148 // into (select_cc LHS, RHS, -1, 0, COND)
5149 SelectionDAG &DAG = DCI.DAG;
5150 SDValue N0 = N->getOperand(0);
5151 EVT VT = N->getValueType(0);
5152 EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
5153 if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND)
5154 N0 = N0.getOperand(0);
5155 if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) {
5156 SDLoc DL(N0);
5157 SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1),
5158 DAG.getConstant(-1, DL, VT), DAG.getConstant(0, DL, VT),
5159 N0.getOperand(2) };
5160 return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops);
5161 }
5162 return SDValue();
5163}
5164
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005165SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5166 SDNode *N, DAGCombinerInfo &DCI) const {
5167 // Convert (sext (ashr (shl X, C1), C2)) to
5168 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5169 // cheap as narrower ones.
5170 SelectionDAG &DAG = DCI.DAG;
5171 SDValue N0 = N->getOperand(0);
5172 EVT VT = N->getValueType(0);
5173 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5174 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5175 SDValue Inner = N0.getOperand(0);
5176 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5177 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005178 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005179 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5180 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5181 EVT ShiftVT = N0.getOperand(1).getValueType();
5182 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5183 Inner.getOperand(0));
5184 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5185 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5186 ShiftVT));
5187 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5188 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5189 }
5190 }
5191 }
5192 return SDValue();
5193}
5194
5195SDValue SystemZTargetLowering::combineMERGE(
5196 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005197 SelectionDAG &DAG = DCI.DAG;
5198 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005199 SDValue Op0 = N->getOperand(0);
5200 SDValue Op1 = N->getOperand(1);
5201 if (Op0.getOpcode() == ISD::BITCAST)
5202 Op0 = Op0.getOperand(0);
5203 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5204 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5205 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5206 // for v4f32.
5207 if (Op1 == N->getOperand(0))
5208 return Op1;
5209 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5210 EVT VT = Op1.getValueType();
5211 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5212 if (ElemBytes <= 4) {
5213 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5214 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5215 EVT InVT = VT.changeVectorElementTypeToInteger();
5216 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5217 SystemZ::VectorBytes / ElemBytes / 2);
5218 if (VT != InVT) {
5219 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5220 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005221 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005222 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5223 DCI.AddToWorklist(Op.getNode());
5224 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005225 }
5226 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005227 return SDValue();
5228}
5229
5230SDValue SystemZTargetLowering::combineSTORE(
5231 SDNode *N, DAGCombinerInfo &DCI) const {
5232 SelectionDAG &DAG = DCI.DAG;
5233 auto *SN = cast<StoreSDNode>(N);
5234 auto &Op1 = N->getOperand(1);
5235 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005236 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5237 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5238 // If X has wider elements then convert it to:
5239 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005240 if (MemVT.isInteger()) {
5241 if (SDValue Value =
5242 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5243 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005244
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005245 // Rewrite the store with the new form of stored value.
5246 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5247 SN->getBasePtr(), SN->getMemoryVT(),
5248 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005249 }
5250 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005251 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
Ulrich Weigand59a01a92017-09-19 20:50:05 +00005252 if (!SN->isTruncatingStore() &&
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005253 Op1.getOpcode() == ISD::BSWAP &&
5254 Op1.getNode()->hasOneUse() &&
5255 (Op1.getValueType() == MVT::i16 ||
5256 Op1.getValueType() == MVT::i32 ||
5257 Op1.getValueType() == MVT::i64)) {
5258
5259 SDValue BSwapOp = Op1.getOperand(0);
5260
5261 if (BSwapOp.getValueType() == MVT::i16)
5262 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5263
5264 SDValue Ops[] = {
5265 N->getOperand(0), BSwapOp, N->getOperand(2),
5266 DAG.getValueType(Op1.getValueType())
5267 };
5268
5269 return
5270 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5271 Ops, MemVT, SN->getMemOperand());
5272 }
5273 return SDValue();
5274}
5275
5276SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5277 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005278
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005279 if (!Subtarget.hasVector())
5280 return SDValue();
5281
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005282 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005283 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5284 SDValue Op0 = N->getOperand(0);
5285 EVT VecVT = Op0.getValueType();
5286 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5287 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005288 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005289 return SDValue();
5290}
5291
5292SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5293 SDNode *N, DAGCombinerInfo &DCI) const {
5294 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005295 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005296 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005297 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5298 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005299 return SDValue();
5300}
5301
5302SDValue SystemZTargetLowering::combineFP_ROUND(
5303 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005304 // (fpround (extract_vector_elt X 0))
5305 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005306 // (extract_vector_elt (VROUND X) 0)
5307 // (extract_vector_elt (VROUND X) 1)
5308 //
5309 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005310 SelectionDAG &DAG = DCI.DAG;
5311 SDValue Op0 = N->getOperand(0);
5312 if (N->getValueType(0) == MVT::f32 &&
5313 Op0.hasOneUse() &&
5314 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5315 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5316 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5317 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5318 SDValue Vec = Op0.getOperand(0);
5319 for (auto *U : Vec->uses()) {
5320 if (U != Op0.getNode() &&
5321 U->hasOneUse() &&
5322 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5323 U->getOperand(0) == Vec &&
5324 U->getOperand(1).getOpcode() == ISD::Constant &&
5325 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5326 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5327 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5328 OtherRound.getOperand(0) == SDValue(U, 0) &&
5329 OtherRound.getValueType() == MVT::f32) {
5330 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5331 MVT::v4f32, Vec);
5332 DCI.AddToWorklist(VRound.getNode());
5333 SDValue Extract1 =
5334 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5335 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5336 DCI.AddToWorklist(Extract1.getNode());
5337 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5338 SDValue Extract0 =
5339 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5340 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5341 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005342 }
5343 }
5344 }
5345 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005346 return SDValue();
5347}
Bryan Chan28b759c2016-05-16 20:32:22 +00005348
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005349SDValue SystemZTargetLowering::combineBSWAP(
5350 SDNode *N, DAGCombinerInfo &DCI) const {
5351 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005352 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005353 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5354 N->getOperand(0).hasOneUse() &&
5355 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
Ulrich Weiganddb16bee2018-03-02 20:51:59 +00005356 N->getValueType(0) == MVT::i64)) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005357 SDValue Load = N->getOperand(0);
5358 LoadSDNode *LD = cast<LoadSDNode>(Load);
5359
5360 // Create the byte-swapping load.
5361 SDValue Ops[] = {
5362 LD->getChain(), // Chain
5363 LD->getBasePtr(), // Ptr
5364 DAG.getValueType(N->getValueType(0)) // VT
5365 };
5366 SDValue BSLoad =
5367 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5368 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5369 MVT::i64 : MVT::i32, MVT::Other),
5370 Ops, LD->getMemoryVT(), LD->getMemOperand());
5371
5372 // If this is an i16 load, insert the truncate.
5373 SDValue ResVal = BSLoad;
5374 if (N->getValueType(0) == MVT::i16)
5375 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5376
5377 // First, combine the bswap away. This makes the value produced by the
5378 // load dead.
5379 DCI.CombineTo(N, ResVal);
5380
5381 // Next, combine the load away, we give it a bogus result value but a real
5382 // chain result. The result value is dead because the bswap is dead.
5383 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5384
5385 // Return N so it doesn't get rechecked!
5386 return SDValue(N, 0);
5387 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005388 return SDValue();
5389}
Bryan Chan28b759c2016-05-16 20:32:22 +00005390
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005391SDValue SystemZTargetLowering::combineSHIFTROT(
5392 SDNode *N, DAGCombinerInfo &DCI) const {
5393
5394 SelectionDAG &DAG = DCI.DAG;
5395
5396 // Shift/rotate instructions only use the last 6 bits of the second operand
5397 // register. If the second operand is the result of an AND with an immediate
5398 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005399 //
5400 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005401 // entirely, but we can still truncate it to a 16-bit value. This prevents
5402 // us from ending up with a NILL with a signed operand, which will cause the
5403 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005404 SDValue N1 = N->getOperand(1);
5405 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005406 SDValue AndMaskOp = N1->getOperand(1);
5407 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005408
5409 // The AND mask is constant
5410 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005411 auto AmtVal = AndMask->getZExtValue();
5412
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005413 // Bottom 6 bits are set
5414 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005415 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005416
5417 // This is the only use, so remove the node
5418 if (N1.hasOneUse()) {
5419 // Combine the AND away
5420 DCI.CombineTo(N1.getNode(), AndOp);
5421
5422 // Return N so it isn't rechecked
5423 return SDValue(N, 0);
5424
5425 // The node will be reused, so create a new node for this one use
5426 } else {
5427 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5428 N->getValueType(0), N->getOperand(0),
5429 AndOp);
5430 DCI.AddToWorklist(Replace.getNode());
5431
5432 return Replace;
5433 }
Elliot Colp687691a2016-08-18 18:04:26 +00005434
Elliot Colpa4092102016-08-23 14:03:02 +00005435 // We can't remove the AND, but we can use NILL here (normally we would
5436 // use NILF). Only keep the last 16 bits of the mask. The actual
5437 // transformation will be handled by .td definitions.
5438 } else if (AmtVal >> 16 != 0) {
5439 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005440
Elliot Colpa4092102016-08-23 14:03:02 +00005441 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5442 SDLoc(AndMaskOp),
5443 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005444
Elliot Colpa4092102016-08-23 14:03:02 +00005445 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5446 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005447
Elliot Colpa4092102016-08-23 14:03:02 +00005448 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5449 N->getValueType(0), N->getOperand(0),
5450 NewAnd);
5451 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005452
Elliot Colpa4092102016-08-23 14:03:02 +00005453 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005454 }
5455 }
5456 }
5457
5458 return SDValue();
5459}
5460
Ulrich Weigand31112892018-01-19 20:54:18 +00005461static bool combineCCMask(SDValue &Glue, int &CCValid, int &CCMask) {
5462 // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code
5463 // set by the glued instruction using the CCValid / CCMask masks,
5464 // If the glued instruction is itself a (ICMP (SELECT_CCMASK)) testing
5465 // the condition code set by some other instruction, see whether we
5466 // can directly use that condition code.
5467 bool Invert = false;
5468
5469 // Verify that we have an appropriate mask for a EQ or NE comparison.
5470 if (CCValid != SystemZ::CCMASK_ICMP)
5471 return false;
5472 if (CCMask == SystemZ::CCMASK_CMP_NE)
5473 Invert = !Invert;
5474 else if (CCMask != SystemZ::CCMASK_CMP_EQ)
5475 return false;
5476
5477 // Verify that we have an ICMP that is the single user of a SELECT_CCMASK.
5478 SDNode *ICmp = Glue.getNode();
5479 if (ICmp->getOpcode() != SystemZISD::ICMP)
5480 return false;
5481 SDNode *Select = ICmp->getOperand(0).getNode();
5482 if (Select->getOpcode() != SystemZISD::SELECT_CCMASK)
5483 return false;
5484 if (!Select->hasOneUse())
5485 return false;
5486
5487 // Verify that the ICMP compares against one of select values.
5488 auto *CompareVal = dyn_cast<ConstantSDNode>(ICmp->getOperand(1));
5489 if (!CompareVal)
5490 return false;
5491 auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0));
5492 if (!TrueVal)
5493 return false;
5494 auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1));
5495 if (!FalseVal)
5496 return false;
5497 if (CompareVal->getZExtValue() == FalseVal->getZExtValue())
5498 Invert = !Invert;
5499 else if (CompareVal->getZExtValue() != TrueVal->getZExtValue())
5500 return false;
5501
5502 // Compute the effective CC mask for the new branch or select.
5503 auto *NewCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2));
5504 auto *NewCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3));
5505 if (!NewCCValid || !NewCCMask)
5506 return false;
5507 CCValid = NewCCValid->getZExtValue();
5508 CCMask = NewCCMask->getZExtValue();
5509 if (Invert)
5510 CCMask ^= CCValid;
5511
5512 // Return the updated Glue link.
5513 Glue = Select->getOperand(4);
5514 return true;
5515}
5516
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005517static bool combineMergeChains(SDValue &Chain, SDValue Glue) {
5518 // We are about to glue an instruction with input chain Chain to the
5519 // instruction Glue. Verify that this would not create an invalid
5520 // topological sort due to intervening chain nodes.
5521
5522 SDNode *Node = Glue.getNode();
5523 for (int ResNo = Node->getNumValues() - 1; ResNo >= 0; --ResNo)
5524 if (Node->getValueType(ResNo) == MVT::Other) {
5525 SDValue OutChain = SDValue(Node, ResNo);
5526 // FIXME: We should be able to at least handle an intervening
5527 // TokenFactor node by swapping chains around a bit ...
5528 return Chain == OutChain;
5529 }
5530
5531 return true;
5532}
5533
Ulrich Weigand31112892018-01-19 20:54:18 +00005534SDValue SystemZTargetLowering::combineBR_CCMASK(
5535 SDNode *N, DAGCombinerInfo &DCI) const {
5536 SelectionDAG &DAG = DCI.DAG;
5537
5538 // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK.
5539 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
5540 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
5541 if (!CCValid || !CCMask)
5542 return SDValue();
5543
5544 int CCValidVal = CCValid->getZExtValue();
5545 int CCMaskVal = CCMask->getZExtValue();
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005546 SDValue Chain = N->getOperand(0);
Ulrich Weigand31112892018-01-19 20:54:18 +00005547 SDValue Glue = N->getOperand(4);
5548
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005549 if (combineCCMask(Glue, CCValidVal, CCMaskVal)
5550 && combineMergeChains(Chain, Glue))
Ulrich Weigand31112892018-01-19 20:54:18 +00005551 return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0),
Ulrich Weigand145d63f2018-01-22 15:41:49 +00005552 Chain,
Ulrich Weigand31112892018-01-19 20:54:18 +00005553 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32),
5554 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32),
5555 N->getOperand(3), Glue);
5556 return SDValue();
5557}
5558
5559SDValue SystemZTargetLowering::combineSELECT_CCMASK(
5560 SDNode *N, DAGCombinerInfo &DCI) const {
5561 SelectionDAG &DAG = DCI.DAG;
5562
5563 // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK.
5564 auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2));
5565 auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3));
5566 if (!CCValid || !CCMask)
5567 return SDValue();
5568
5569 int CCValidVal = CCValid->getZExtValue();
5570 int CCMaskVal = CCMask->getZExtValue();
5571 SDValue Glue = N->getOperand(4);
5572
5573 if (combineCCMask(Glue, CCValidVal, CCMaskVal))
5574 return DAG.getNode(SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0),
5575 N->getOperand(0),
5576 N->getOperand(1),
5577 DAG.getConstant(CCValidVal, SDLoc(N), MVT::i32),
5578 DAG.getConstant(CCMaskVal, SDLoc(N), MVT::i32),
5579 Glue);
5580 return SDValue();
5581}
5582
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005583SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5584 DAGCombinerInfo &DCI) const {
5585 switch(N->getOpcode()) {
5586 default: break;
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005587 case ISD::ZERO_EXTEND: return combineZERO_EXTEND(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005588 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
Ulrich Weigand849a59f2018-01-19 20:52:04 +00005589 case ISD::SIGN_EXTEND_INREG: return combineSIGN_EXTEND_INREG(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005590 case SystemZISD::MERGE_HIGH:
5591 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5592 case ISD::STORE: return combineSTORE(N, DCI);
5593 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5594 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5595 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5596 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005597 case ISD::SHL:
5598 case ISD::SRA:
5599 case ISD::SRL:
5600 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Ulrich Weigand31112892018-01-19 20:54:18 +00005601 case SystemZISD::BR_CCMASK: return combineBR_CCMASK(N, DCI);
5602 case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005603 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005604
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005605 return SDValue();
5606}
5607
Ulrich Weigand9eb858c2018-01-19 20:49:05 +00005608void
5609SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
5610 KnownBits &Known,
5611 const APInt &DemandedElts,
5612 const SelectionDAG &DAG,
5613 unsigned Depth) const {
5614 unsigned BitWidth = Known.getBitWidth();
5615
5616 Known.resetAll();
5617 switch (Op.getOpcode()) {
5618 case SystemZISD::SELECT_CCMASK: {
5619 KnownBits TrueKnown(BitWidth), FalseKnown(BitWidth);
5620 DAG.computeKnownBits(Op.getOperand(0), TrueKnown, Depth + 1);
5621 DAG.computeKnownBits(Op.getOperand(1), FalseKnown, Depth + 1);
5622 Known.Zero = TrueKnown.Zero & FalseKnown.Zero;
5623 Known.One = TrueKnown.One & FalseKnown.One;
5624 break;
5625 }
5626
5627 default:
5628 break;
5629 }
5630}
5631
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005632//===----------------------------------------------------------------------===//
5633// Custom insertion
5634//===----------------------------------------------------------------------===//
5635
5636// Create a new basic block after MBB.
5637static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5638 MachineFunction &MF = *MBB->getParent();
5639 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005640 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005641 return NewMBB;
5642}
5643
Richard Sandifordbe133a82013-08-28 09:01:51 +00005644// Split MBB after MI and return the new block (the one that contains
5645// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005646static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005647 MachineBasicBlock *MBB) {
5648 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5649 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005650 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005651 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5652 return NewMBB;
5653}
5654
Richard Sandiford5e318f02013-08-27 09:54:29 +00005655// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005656static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005657 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005658 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005659 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005660 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5661 return NewMBB;
5662}
5663
Richard Sandiford5e318f02013-08-27 09:54:29 +00005664// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005665static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005666 const SystemZInstrInfo *TII) {
5667 if (Base.isReg())
5668 return Base.getReg();
5669
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005670 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005671 MachineFunction &MF = *MBB->getParent();
5672 MachineRegisterInfo &MRI = MF.getRegInfo();
5673
5674 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005675 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005676 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005677 .addImm(0)
5678 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005679 return Reg;
5680}
5681
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005682// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5683MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005684SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005685 MachineBasicBlock *MBB,
5686 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005687 const SystemZInstrInfo *TII =
5688 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005689
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005690 unsigned DestReg = MI.getOperand(0).getReg();
5691 unsigned TrueReg = MI.getOperand(1).getReg();
5692 unsigned FalseReg = MI.getOperand(2).getReg();
5693 unsigned CCValid = MI.getOperand(3).getImm();
5694 unsigned CCMask = MI.getOperand(4).getImm();
5695 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005696
Ulrich Weigand524f2762016-11-28 13:34:08 +00005697 // Use LOCROpcode if possible.
5698 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5699 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5700 .addReg(FalseReg).addReg(TrueReg)
5701 .addImm(CCValid).addImm(CCMask);
5702 MI.eraseFromParent();
5703 return MBB;
5704 }
5705
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005706 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005707 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005708 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5709
5710 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005711 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005712 // # fallthrough to FalseMBB
5713 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005714 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5715 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005716 MBB->addSuccessor(JoinMBB);
5717 MBB->addSuccessor(FalseMBB);
5718
5719 // FalseMBB:
5720 // # fallthrough to JoinMBB
5721 MBB = FalseMBB;
5722 MBB->addSuccessor(JoinMBB);
5723
5724 // JoinMBB:
5725 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5726 // ...
5727 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005728 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005729 .addReg(TrueReg).addMBB(StartMBB)
5730 .addReg(FalseReg).addMBB(FalseMBB);
5731
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005732 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005733 return JoinMBB;
5734}
5735
Richard Sandifordb86a8342013-06-27 09:27:40 +00005736// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5737// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005738// happen when the condition is false rather than true. If a STORE ON
5739// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005740MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5741 MachineBasicBlock *MBB,
5742 unsigned StoreOpcode,
5743 unsigned STOCOpcode,
5744 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005745 const SystemZInstrInfo *TII =
5746 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005747
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005748 unsigned SrcReg = MI.getOperand(0).getReg();
5749 MachineOperand Base = MI.getOperand(1);
5750 int64_t Disp = MI.getOperand(2).getImm();
5751 unsigned IndexReg = MI.getOperand(3).getReg();
5752 unsigned CCValid = MI.getOperand(4).getImm();
5753 unsigned CCMask = MI.getOperand(5).getImm();
5754 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005755
5756 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5757
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005758 // Use STOCOpcode if possible. We could use different store patterns in
5759 // order to avoid matching the index register, but the performance trade-offs
5760 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005761 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005762 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005763 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005764
5765 // ISel pattern matching also adds a load memory operand of the same
5766 // address, so take special care to find the storing memory operand.
5767 MachineMemOperand *MMO = nullptr;
5768 for (auto *I : MI.memoperands())
5769 if (I->isStore()) {
5770 MMO = I;
5771 break;
5772 }
5773
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005774 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005775 .addReg(SrcReg)
5776 .add(Base)
5777 .addImm(Disp)
5778 .addImm(CCValid)
5779 .addImm(CCMask)
5780 .addMemOperand(MMO);
5781
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005782 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005783 return MBB;
5784 }
5785
Richard Sandifordb86a8342013-06-27 09:27:40 +00005786 // Get the condition needed to branch around the store.
5787 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005788 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005789
5790 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005791 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005792 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5793
5794 // StartMBB:
5795 // BRC CCMask, JoinMBB
5796 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005797 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005798 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5799 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005800 MBB->addSuccessor(JoinMBB);
5801 MBB->addSuccessor(FalseMBB);
5802
5803 // FalseMBB:
5804 // store %SrcReg, %Disp(%Index,%Base)
5805 // # fallthrough to JoinMBB
5806 MBB = FalseMBB;
5807 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005808 .addReg(SrcReg)
5809 .add(Base)
5810 .addImm(Disp)
5811 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005812 MBB->addSuccessor(JoinMBB);
5813
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005814 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005815 return JoinMBB;
5816}
5817
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005818// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5819// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5820// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5821// BitSize is the width of the field in bits, or 0 if this is a partword
5822// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5823// is one of the operands. Invert says whether the field should be
5824// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005825MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5826 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5827 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005828 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005829 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005830 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005831 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005832 bool IsSubWord = (BitSize < 32);
5833
5834 // Extract the operands. Base can be a register or a frame index.
5835 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005836 unsigned Dest = MI.getOperand(0).getReg();
5837 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5838 int64_t Disp = MI.getOperand(2).getImm();
5839 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5840 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5841 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5842 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005843 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005844 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005845
5846 // Subword operations use 32-bit registers.
5847 const TargetRegisterClass *RC = (BitSize <= 32 ?
5848 &SystemZ::GR32BitRegClass :
5849 &SystemZ::GR64BitRegClass);
5850 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5851 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5852
5853 // Get the right opcodes for the displacement.
5854 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5855 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5856 assert(LOpcode && CSOpcode && "Displacement out of range");
5857
5858 // Create virtual registers for temporary results.
5859 unsigned OrigVal = MRI.createVirtualRegister(RC);
5860 unsigned OldVal = MRI.createVirtualRegister(RC);
5861 unsigned NewVal = (BinOpcode || IsSubWord ?
5862 MRI.createVirtualRegister(RC) : Src2.getReg());
5863 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5864 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5865
5866 // Insert a basic block for the main loop.
5867 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005868 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005869 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5870
5871 // StartMBB:
5872 // ...
5873 // %OrigVal = L Disp(%Base)
5874 // # fall through to LoopMMB
5875 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005876 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005877 MBB->addSuccessor(LoopMBB);
5878
5879 // LoopMBB:
5880 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5881 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5882 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5883 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5884 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5885 // JNE LoopMBB
5886 // # fall through to DoneMMB
5887 MBB = LoopMBB;
5888 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5889 .addReg(OrigVal).addMBB(StartMBB)
5890 .addReg(Dest).addMBB(LoopMBB);
5891 if (IsSubWord)
5892 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5893 .addReg(OldVal).addReg(BitShift).addImm(0);
5894 if (Invert) {
5895 // Perform the operation normally and then invert every bit of the field.
5896 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005897 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005898 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005899 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005900 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005901 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005902 else {
5903 // Use LCGR and add -1 to the result, which is more compact than
5904 // an XILF, XILH pair.
5905 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5906 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5907 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5908 .addReg(Tmp2).addImm(-1);
5909 }
5910 } else if (BinOpcode)
5911 // A simply binary operation.
5912 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005913 .addReg(RotatedOldVal)
5914 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005915 else if (IsSubWord)
5916 // Use RISBG to rotate Src2 into position and use it to replace the
5917 // field in RotatedOldVal.
5918 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5919 .addReg(RotatedOldVal).addReg(Src2.getReg())
5920 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5921 if (IsSubWord)
5922 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5923 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5924 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005925 .addReg(OldVal)
5926 .addReg(NewVal)
5927 .add(Base)
5928 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005929 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5930 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005931 MBB->addSuccessor(LoopMBB);
5932 MBB->addSuccessor(DoneMBB);
5933
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005934 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005935 return DoneMBB;
5936}
5937
5938// Implement EmitInstrWithCustomInserter for pseudo
5939// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5940// instruction that should be used to compare the current field with the
5941// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5942// for when the current field should be kept. BitSize is the width of
5943// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005944MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5945 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5946 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005947 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005948 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005949 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005950 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005951 bool IsSubWord = (BitSize < 32);
5952
5953 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005954 unsigned Dest = MI.getOperand(0).getReg();
5955 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5956 int64_t Disp = MI.getOperand(2).getImm();
5957 unsigned Src2 = MI.getOperand(3).getReg();
5958 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5959 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5960 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005961 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005962 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005963
5964 // Subword operations use 32-bit registers.
5965 const TargetRegisterClass *RC = (BitSize <= 32 ?
5966 &SystemZ::GR32BitRegClass :
5967 &SystemZ::GR64BitRegClass);
5968 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5969 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5970
5971 // Get the right opcodes for the displacement.
5972 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5973 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5974 assert(LOpcode && CSOpcode && "Displacement out of range");
5975
5976 // Create virtual registers for temporary results.
5977 unsigned OrigVal = MRI.createVirtualRegister(RC);
5978 unsigned OldVal = MRI.createVirtualRegister(RC);
5979 unsigned NewVal = MRI.createVirtualRegister(RC);
5980 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5981 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5982 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5983
5984 // Insert 3 basic blocks for the loop.
5985 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005986 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005987 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5988 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5989 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5990
5991 // StartMBB:
5992 // ...
5993 // %OrigVal = L Disp(%Base)
5994 // # fall through to LoopMMB
5995 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005996 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005997 MBB->addSuccessor(LoopMBB);
5998
5999 // LoopMBB:
6000 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
6001 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
6002 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00006003 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006004 MBB = LoopMBB;
6005 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
6006 .addReg(OrigVal).addMBB(StartMBB)
6007 .addReg(Dest).addMBB(UpdateMBB);
6008 if (IsSubWord)
6009 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
6010 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006011 BuildMI(MBB, DL, TII->get(CompareOpcode))
6012 .addReg(RotatedOldVal).addReg(Src2);
6013 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00006014 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006015 MBB->addSuccessor(UpdateMBB);
6016 MBB->addSuccessor(UseAltMBB);
6017
6018 // UseAltMBB:
6019 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
6020 // # fall through to UpdateMMB
6021 MBB = UseAltMBB;
6022 if (IsSubWord)
6023 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
6024 .addReg(RotatedOldVal).addReg(Src2)
6025 .addImm(32).addImm(31 + BitSize).addImm(0);
6026 MBB->addSuccessor(UpdateMBB);
6027
6028 // UpdateMBB:
6029 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
6030 // [ %RotatedAltVal, UseAltMBB ]
6031 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
6032 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
6033 // JNE LoopMBB
6034 // # fall through to DoneMMB
6035 MBB = UpdateMBB;
6036 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
6037 .addReg(RotatedOldVal).addMBB(LoopMBB)
6038 .addReg(RotatedAltVal).addMBB(UseAltMBB);
6039 if (IsSubWord)
6040 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
6041 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
6042 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00006043 .addReg(OldVal)
6044 .addReg(NewVal)
6045 .add(Base)
6046 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006047 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6048 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006049 MBB->addSuccessor(LoopMBB);
6050 MBB->addSuccessor(DoneMBB);
6051
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006052 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006053 return DoneMBB;
6054}
6055
6056// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
6057// instruction MI.
6058MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006059SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006060 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00006061
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006062 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006063 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006064 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006065 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006066
6067 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006068 unsigned Dest = MI.getOperand(0).getReg();
6069 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
6070 int64_t Disp = MI.getOperand(2).getImm();
6071 unsigned OrigCmpVal = MI.getOperand(3).getReg();
6072 unsigned OrigSwapVal = MI.getOperand(4).getReg();
6073 unsigned BitShift = MI.getOperand(5).getReg();
6074 unsigned NegBitShift = MI.getOperand(6).getReg();
6075 int64_t BitSize = MI.getOperand(7).getImm();
6076 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006077
6078 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
6079
6080 // Get the right opcodes for the displacement.
6081 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
6082 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
6083 assert(LOpcode && CSOpcode && "Displacement out of range");
6084
6085 // Create virtual registers for temporary results.
6086 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
6087 unsigned OldVal = MRI.createVirtualRegister(RC);
6088 unsigned CmpVal = MRI.createVirtualRegister(RC);
6089 unsigned SwapVal = MRI.createVirtualRegister(RC);
6090 unsigned StoreVal = MRI.createVirtualRegister(RC);
6091 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
6092 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
6093 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
6094
6095 // Insert 2 basic blocks for the loop.
6096 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006097 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006098 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6099 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
6100
6101 // StartMBB:
6102 // ...
6103 // %OrigOldVal = L Disp(%Base)
6104 // # fall through to LoopMMB
6105 MBB = StartMBB;
6106 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00006107 .add(Base)
6108 .addImm(Disp)
6109 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006110 MBB->addSuccessor(LoopMBB);
6111
6112 // LoopMBB:
6113 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
6114 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
6115 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
6116 // %Dest = RLL %OldVal, BitSize(%BitShift)
6117 // ^^ The low BitSize bits contain the field
6118 // of interest.
6119 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
6120 // ^^ Replace the upper 32-BitSize bits of the
6121 // comparison value with those that we loaded,
6122 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006123 // CR %Dest, %RetryCmpVal
6124 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006125 // # Fall through to SetMBB
6126 MBB = LoopMBB;
6127 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
6128 .addReg(OrigOldVal).addMBB(StartMBB)
6129 .addReg(RetryOldVal).addMBB(SetMBB);
6130 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
6131 .addReg(OrigCmpVal).addMBB(StartMBB)
6132 .addReg(RetryCmpVal).addMBB(SetMBB);
6133 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
6134 .addReg(OrigSwapVal).addMBB(StartMBB)
6135 .addReg(RetrySwapVal).addMBB(SetMBB);
6136 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
6137 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
6138 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
6139 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00006140 BuildMI(MBB, DL, TII->get(SystemZ::CR))
6141 .addReg(Dest).addReg(RetryCmpVal);
6142 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00006143 .addImm(SystemZ::CCMASK_ICMP)
6144 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006145 MBB->addSuccessor(DoneMBB);
6146 MBB->addSuccessor(SetMBB);
6147
6148 // SetMBB:
6149 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
6150 // ^^ Replace the upper 32-BitSize bits of the new
6151 // value with those that we loaded.
6152 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
6153 // ^^ Rotate the new field to its proper position.
6154 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
6155 // JNE LoopMBB
6156 // # fall through to ExitMMB
6157 MBB = SetMBB;
6158 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
6159 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
6160 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
6161 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
6162 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00006163 .addReg(OldVal)
6164 .addReg(StoreVal)
6165 .add(Base)
6166 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006167 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6168 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006169 MBB->addSuccessor(LoopMBB);
6170 MBB->addSuccessor(DoneMBB);
6171
Ulrich Weiganddf868552017-09-28 22:08:25 +00006172 // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in
6173 // to the block after the loop. At this point, CC may have been defined
6174 // either by the CR in LoopMBB or by the CS in SetMBB.
6175 if (!MI.registerDefIsDead(SystemZ::CC))
6176 DoneMBB->addLiveIn(SystemZ::CC);
6177
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006178 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006179 return DoneMBB;
6180}
6181
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006182// Emit a move from two GR64s to a GR128.
6183MachineBasicBlock *
6184SystemZTargetLowering::emitPair128(MachineInstr &MI,
6185 MachineBasicBlock *MBB) const {
6186 MachineFunction &MF = *MBB->getParent();
6187 const SystemZInstrInfo *TII =
6188 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
6189 MachineRegisterInfo &MRI = MF.getRegInfo();
6190 DebugLoc DL = MI.getDebugLoc();
6191
6192 unsigned Dest = MI.getOperand(0).getReg();
6193 unsigned Hi = MI.getOperand(1).getReg();
6194 unsigned Lo = MI.getOperand(2).getReg();
6195 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6196 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6197
6198 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
6199 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
6200 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
6201 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
6202 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
6203
6204 MI.eraseFromParent();
6205 return MBB;
6206}
6207
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006208// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006209// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006210// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006211MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
6212 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006213 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006214 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006215 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006216 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006217 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006218 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006219
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006220 unsigned Dest = MI.getOperand(0).getReg();
6221 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006222 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6223
6224 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
6225 if (ClearEven) {
6226 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6227 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
6228
6229 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
6230 .addImm(0);
6231 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00006232 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006233 In128 = NewIn128;
6234 }
6235 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006236 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006237
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006238 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006239 return MBB;
6240}
6241
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006242MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
6243 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006244 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006245 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006246 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006247 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006248 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006249
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006250 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
6251 uint64_t DestDisp = MI.getOperand(1).getImm();
6252 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
6253 uint64_t SrcDisp = MI.getOperand(3).getImm();
6254 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006255
Richard Sandifordbe133a82013-08-28 09:01:51 +00006256 // When generating more than one CLC, all but the last will need to
6257 // branch to the end when a difference is found.
6258 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00006259 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006260
Richard Sandiford5e318f02013-08-27 09:54:29 +00006261 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006262 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006263 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
6264
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006265 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00006266 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
6267 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
6268 forceReg(MI, DestBase, TII));
6269
6270 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
6271 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
6272 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
6273 MRI.createVirtualRegister(RC));
6274 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
6275 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
6276 MRI.createVirtualRegister(RC));
6277
6278 RC = &SystemZ::GR64BitRegClass;
6279 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
6280 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
6281
6282 MachineBasicBlock *StartMBB = MBB;
6283 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
6284 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006285 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006286
6287 // StartMBB:
6288 // # fall through to LoopMMB
6289 MBB->addSuccessor(LoopMBB);
6290
6291 // LoopMBB:
6292 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006293 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006294 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006295 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006296 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006297 // [ %NextCountReg, NextMBB ]
6298 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006299 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006300 // ( JLH EndMBB )
6301 //
6302 // The prefetch is used only for MVC. The JLH is used only for CLC.
6303 MBB = LoopMBB;
6304
6305 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6306 .addReg(StartDestReg).addMBB(StartMBB)
6307 .addReg(NextDestReg).addMBB(NextMBB);
6308 if (!HaveSingleBase)
6309 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6310 .addReg(StartSrcReg).addMBB(StartMBB)
6311 .addReg(NextSrcReg).addMBB(NextMBB);
6312 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6313 .addReg(StartCountReg).addMBB(StartMBB)
6314 .addReg(NextCountReg).addMBB(NextMBB);
6315 if (Opcode == SystemZ::MVC)
6316 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6317 .addImm(SystemZ::PFD_WRITE)
6318 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6319 BuildMI(MBB, DL, TII->get(Opcode))
6320 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6321 .addReg(ThisSrcReg).addImm(SrcDisp);
6322 if (EndMBB) {
6323 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6324 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6325 .addMBB(EndMBB);
6326 MBB->addSuccessor(EndMBB);
6327 MBB->addSuccessor(NextMBB);
6328 }
6329
6330 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006331 // %NextDestReg = LA 256(%ThisDestReg)
6332 // %NextSrcReg = LA 256(%ThisSrcReg)
6333 // %NextCountReg = AGHI %ThisCountReg, -1
6334 // CGHI %NextCountReg, 0
6335 // JLH LoopMBB
6336 // # fall through to DoneMMB
6337 //
6338 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006339 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006340
Richard Sandiford5e318f02013-08-27 09:54:29 +00006341 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6342 .addReg(ThisDestReg).addImm(256).addReg(0);
6343 if (!HaveSingleBase)
6344 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6345 .addReg(ThisSrcReg).addImm(256).addReg(0);
6346 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6347 .addReg(ThisCountReg).addImm(-1);
6348 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6349 .addReg(NextCountReg).addImm(0);
6350 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6351 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6352 .addMBB(LoopMBB);
6353 MBB->addSuccessor(LoopMBB);
6354 MBB->addSuccessor(DoneMBB);
6355
6356 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6357 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6358 Length &= 255;
6359 MBB = DoneMBB;
6360 }
6361 // Handle any remaining bytes with straight-line code.
6362 while (Length > 0) {
6363 uint64_t ThisLength = std::min(Length, uint64_t(256));
6364 // The previous iteration might have created out-of-range displacements.
6365 // Apply them using LAY if so.
6366 if (!isUInt<12>(DestDisp)) {
6367 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006368 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006369 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006370 .addImm(DestDisp)
6371 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006372 DestBase = MachineOperand::CreateReg(Reg, false);
6373 DestDisp = 0;
6374 }
6375 if (!isUInt<12>(SrcDisp)) {
6376 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006377 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006378 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006379 .addImm(SrcDisp)
6380 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006381 SrcBase = MachineOperand::CreateReg(Reg, false);
6382 SrcDisp = 0;
6383 }
6384 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006385 .add(DestBase)
6386 .addImm(DestDisp)
6387 .addImm(ThisLength)
6388 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006389 .addImm(SrcDisp)
6390 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006391 DestDisp += ThisLength;
6392 SrcDisp += ThisLength;
6393 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006394 // If there's another CLC to go, branch to the end if a difference
6395 // was found.
6396 if (EndMBB && Length > 0) {
6397 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6398 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6399 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6400 .addMBB(EndMBB);
6401 MBB->addSuccessor(EndMBB);
6402 MBB->addSuccessor(NextMBB);
6403 MBB = NextMBB;
6404 }
6405 }
6406 if (EndMBB) {
6407 MBB->addSuccessor(EndMBB);
6408 MBB = EndMBB;
6409 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006410 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006411
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006412 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006413 return MBB;
6414}
6415
Richard Sandifordca232712013-08-16 11:21:54 +00006416// Decompose string pseudo-instruction MI into a loop that continually performs
6417// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006418MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6419 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006420 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006421 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006422 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006423 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006424 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006425
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006426 uint64_t End1Reg = MI.getOperand(0).getReg();
6427 uint64_t Start1Reg = MI.getOperand(1).getReg();
6428 uint64_t Start2Reg = MI.getOperand(2).getReg();
6429 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006430
6431 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6432 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6433 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6434 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6435
6436 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006437 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006438 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6439
6440 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006441 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006442 MBB->addSuccessor(LoopMBB);
6443
6444 // LoopMBB:
6445 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6446 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006447 // R0L = %CharReg
6448 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006449 // JO LoopMBB
6450 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006451 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006452 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006453 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006454
6455 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6456 .addReg(Start1Reg).addMBB(StartMBB)
6457 .addReg(End1Reg).addMBB(LoopMBB);
6458 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6459 .addReg(Start2Reg).addMBB(StartMBB)
6460 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006461 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006462 BuildMI(MBB, DL, TII->get(Opcode))
6463 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6464 .addReg(This1Reg).addReg(This2Reg);
6465 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6466 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6467 MBB->addSuccessor(LoopMBB);
6468 MBB->addSuccessor(DoneMBB);
6469
6470 DoneMBB->addLiveIn(SystemZ::CC);
6471
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006472 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006473 return DoneMBB;
6474}
6475
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006476// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006477MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6478 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6479 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006480 MachineFunction &MF = *MBB->getParent();
6481 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6482 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6483
6484 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006485 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006486
6487 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6488 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006489 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006490 static const unsigned GPRControlBit[16] = {
6491 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6492 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6493 };
6494 Control |= GPRControlBit[15];
6495 if (TFI->hasFP(MF))
6496 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006497 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006498
6499 // Add GPR clobbers.
6500 for (int I = 0; I < 16; I++) {
6501 if ((Control & GPRControlBit[I]) == 0) {
6502 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006503 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006504 }
6505 }
6506
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006507 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006508 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006509 if (Subtarget.hasVector()) {
6510 for (int I = 0; I < 32; I++) {
6511 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006512 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006513 }
6514 } else {
6515 for (int I = 0; I < 16; I++) {
6516 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006517 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006518 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006519 }
6520 }
6521
6522 return MBB;
6523}
6524
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006525MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6526 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006527 MachineFunction &MF = *MBB->getParent();
6528 MachineRegisterInfo *MRI = &MF.getRegInfo();
6529 const SystemZInstrInfo *TII =
6530 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006531 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006532
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006533 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006534
6535 // Create new virtual register of the same class as source.
6536 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6537 unsigned DstReg = MRI->createVirtualRegister(RC);
6538
6539 // Replace pseudo with a normal load-and-test that models the def as
6540 // well.
6541 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6542 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006543 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006544
6545 return MBB;
6546}
6547
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006548MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6549 MachineInstr &MI, MachineBasicBlock *MBB) const {
6550 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006551 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006552 return emitSelect(MI, MBB,
6553 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006554 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006555 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006556 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006557 return emitSelect(MI, MBB, SystemZ::LOCGR);
6558 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006559 case SystemZ::SelectF64:
6560 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006561 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006562 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006563
Richard Sandiford2896d042013-10-01 14:33:55 +00006564 case SystemZ::CondStore8Mux:
6565 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6566 case SystemZ::CondStore8MuxInv:
6567 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6568 case SystemZ::CondStore16Mux:
6569 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6570 case SystemZ::CondStore16MuxInv:
6571 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006572 case SystemZ::CondStore32Mux:
6573 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6574 case SystemZ::CondStore32MuxInv:
6575 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006576 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006577 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006578 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006579 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006580 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006581 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006582 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006583 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006584 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006585 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006586 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006587 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006588 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006589 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006590 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006591 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006592 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006593 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006594 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006595 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006596 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006597 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006598 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006599 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006600
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006601 case SystemZ::PAIR128:
6602 return emitPair128(MI, MBB);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006603 case SystemZ::AEXT128:
6604 return emitExt128(MI, MBB, false);
6605 case SystemZ::ZEXT128:
6606 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006607
6608 case SystemZ::ATOMIC_SWAPW:
6609 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6610 case SystemZ::ATOMIC_SWAP_32:
6611 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6612 case SystemZ::ATOMIC_SWAP_64:
6613 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6614
6615 case SystemZ::ATOMIC_LOADW_AR:
6616 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6617 case SystemZ::ATOMIC_LOADW_AFI:
6618 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6619 case SystemZ::ATOMIC_LOAD_AR:
6620 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6621 case SystemZ::ATOMIC_LOAD_AHI:
6622 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6623 case SystemZ::ATOMIC_LOAD_AFI:
6624 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6625 case SystemZ::ATOMIC_LOAD_AGR:
6626 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6627 case SystemZ::ATOMIC_LOAD_AGHI:
6628 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6629 case SystemZ::ATOMIC_LOAD_AGFI:
6630 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6631
6632 case SystemZ::ATOMIC_LOADW_SR:
6633 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6634 case SystemZ::ATOMIC_LOAD_SR:
6635 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6636 case SystemZ::ATOMIC_LOAD_SGR:
6637 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6638
6639 case SystemZ::ATOMIC_LOADW_NR:
6640 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6641 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006642 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006643 case SystemZ::ATOMIC_LOAD_NR:
6644 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006645 case SystemZ::ATOMIC_LOAD_NILL:
6646 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6647 case SystemZ::ATOMIC_LOAD_NILH:
6648 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6649 case SystemZ::ATOMIC_LOAD_NILF:
6650 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006651 case SystemZ::ATOMIC_LOAD_NGR:
6652 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006653 case SystemZ::ATOMIC_LOAD_NILL64:
6654 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6655 case SystemZ::ATOMIC_LOAD_NILH64:
6656 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006657 case SystemZ::ATOMIC_LOAD_NIHL64:
6658 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6659 case SystemZ::ATOMIC_LOAD_NIHH64:
6660 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006661 case SystemZ::ATOMIC_LOAD_NILF64:
6662 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006663 case SystemZ::ATOMIC_LOAD_NIHF64:
6664 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006665
6666 case SystemZ::ATOMIC_LOADW_OR:
6667 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6668 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006669 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006670 case SystemZ::ATOMIC_LOAD_OR:
6671 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006672 case SystemZ::ATOMIC_LOAD_OILL:
6673 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6674 case SystemZ::ATOMIC_LOAD_OILH:
6675 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6676 case SystemZ::ATOMIC_LOAD_OILF:
6677 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006678 case SystemZ::ATOMIC_LOAD_OGR:
6679 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006680 case SystemZ::ATOMIC_LOAD_OILL64:
6681 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6682 case SystemZ::ATOMIC_LOAD_OILH64:
6683 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006684 case SystemZ::ATOMIC_LOAD_OIHL64:
6685 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6686 case SystemZ::ATOMIC_LOAD_OIHH64:
6687 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006688 case SystemZ::ATOMIC_LOAD_OILF64:
6689 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006690 case SystemZ::ATOMIC_LOAD_OIHF64:
6691 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006692
6693 case SystemZ::ATOMIC_LOADW_XR:
6694 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6695 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006696 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006697 case SystemZ::ATOMIC_LOAD_XR:
6698 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006699 case SystemZ::ATOMIC_LOAD_XILF:
6700 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006701 case SystemZ::ATOMIC_LOAD_XGR:
6702 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006703 case SystemZ::ATOMIC_LOAD_XILF64:
6704 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006705 case SystemZ::ATOMIC_LOAD_XIHF64:
6706 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006707
6708 case SystemZ::ATOMIC_LOADW_NRi:
6709 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6710 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006711 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006712 case SystemZ::ATOMIC_LOAD_NRi:
6713 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006714 case SystemZ::ATOMIC_LOAD_NILLi:
6715 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6716 case SystemZ::ATOMIC_LOAD_NILHi:
6717 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6718 case SystemZ::ATOMIC_LOAD_NILFi:
6719 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006720 case SystemZ::ATOMIC_LOAD_NGRi:
6721 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006722 case SystemZ::ATOMIC_LOAD_NILL64i:
6723 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6724 case SystemZ::ATOMIC_LOAD_NILH64i:
6725 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006726 case SystemZ::ATOMIC_LOAD_NIHL64i:
6727 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6728 case SystemZ::ATOMIC_LOAD_NIHH64i:
6729 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006730 case SystemZ::ATOMIC_LOAD_NILF64i:
6731 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006732 case SystemZ::ATOMIC_LOAD_NIHF64i:
6733 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006734
6735 case SystemZ::ATOMIC_LOADW_MIN:
6736 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6737 SystemZ::CCMASK_CMP_LE, 0);
6738 case SystemZ::ATOMIC_LOAD_MIN_32:
6739 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6740 SystemZ::CCMASK_CMP_LE, 32);
6741 case SystemZ::ATOMIC_LOAD_MIN_64:
6742 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6743 SystemZ::CCMASK_CMP_LE, 64);
6744
6745 case SystemZ::ATOMIC_LOADW_MAX:
6746 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6747 SystemZ::CCMASK_CMP_GE, 0);
6748 case SystemZ::ATOMIC_LOAD_MAX_32:
6749 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6750 SystemZ::CCMASK_CMP_GE, 32);
6751 case SystemZ::ATOMIC_LOAD_MAX_64:
6752 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6753 SystemZ::CCMASK_CMP_GE, 64);
6754
6755 case SystemZ::ATOMIC_LOADW_UMIN:
6756 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6757 SystemZ::CCMASK_CMP_LE, 0);
6758 case SystemZ::ATOMIC_LOAD_UMIN_32:
6759 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6760 SystemZ::CCMASK_CMP_LE, 32);
6761 case SystemZ::ATOMIC_LOAD_UMIN_64:
6762 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6763 SystemZ::CCMASK_CMP_LE, 64);
6764
6765 case SystemZ::ATOMIC_LOADW_UMAX:
6766 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6767 SystemZ::CCMASK_CMP_GE, 0);
6768 case SystemZ::ATOMIC_LOAD_UMAX_32:
6769 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6770 SystemZ::CCMASK_CMP_GE, 32);
6771 case SystemZ::ATOMIC_LOAD_UMAX_64:
6772 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6773 SystemZ::CCMASK_CMP_GE, 64);
6774
6775 case SystemZ::ATOMIC_CMP_SWAPW:
6776 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006777 case SystemZ::MVCSequence:
6778 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006779 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006780 case SystemZ::NCSequence:
6781 case SystemZ::NCLoop:
6782 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6783 case SystemZ::OCSequence:
6784 case SystemZ::OCLoop:
6785 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6786 case SystemZ::XCSequence:
6787 case SystemZ::XCLoop:
6788 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006789 case SystemZ::CLCSequence:
6790 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006791 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006792 case SystemZ::CLSTLoop:
6793 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006794 case SystemZ::MVSTLoop:
6795 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006796 case SystemZ::SRSTLoop:
6797 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006798 case SystemZ::TBEGIN:
6799 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6800 case SystemZ::TBEGIN_nofloat:
6801 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6802 case SystemZ::TBEGINC:
6803 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006804 case SystemZ::LTEBRCompare_VecPseudo:
6805 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6806 case SystemZ::LTDBRCompare_VecPseudo:
6807 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6808 case SystemZ::LTXBRCompare_VecPseudo:
6809 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6810
Ulrich Weigand5eb64112018-03-02 20:39:30 +00006811 case TargetOpcode::STACKMAP:
6812 case TargetOpcode::PATCHPOINT:
6813 return emitPatchPoint(MI, MBB);
6814
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006815 default:
6816 llvm_unreachable("Unexpected instr type to insert");
6817 }
6818}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006819
6820// This is only used by the isel schedulers, and is needed only to prevent
6821// compiler from crashing when list-ilp is used.
6822const TargetRegisterClass *
6823SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6824 if (VT == MVT::Untyped)
6825 return &SystemZ::ADDR128BitRegClass;
6826 return TargetLowering::getRepRegClassFor(VT);
6827}