blob: dc0de5fd3c7d7b2e8596bbc27e8aebd6cc96cf4e [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"
Craig Topperd0af7e82017-04-28 05:31:46 +000024#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/KnownBits.h"
Will Dietz981af002013-10-12 00:55:57 +000026#include <cctype>
27
Ulrich Weigand5f613df2013-05-06 16:15:19 +000028using namespace llvm;
29
Chandler Carruth84e68b22014-04-22 02:41:26 +000030#define DEBUG_TYPE "systemz-lower"
31
Richard Sandifordf722a8e302013-10-16 11:10:55 +000032namespace {
33// Represents a sequence for extracting a 0/1 value from an IPM result:
34// (((X ^ XORValue) + AddValue) >> Bit)
35struct IPMConversion {
36 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
37 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
38
39 int64_t XORValue;
40 int64_t AddValue;
41 unsigned Bit;
42};
Richard Sandifordd420f732013-12-13 15:28:45 +000043
44// Represents information about a comparison.
45struct Comparison {
46 Comparison(SDValue Op0In, SDValue Op1In)
47 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
48
49 // The operands to the comparison.
50 SDValue Op0, Op1;
51
52 // The opcode that should be used to compare Op0 and Op1.
53 unsigned Opcode;
54
55 // A SystemZICMP value. Only used for integer comparisons.
56 unsigned ICmpType;
57
58 // The mask of CC values that Opcode can produce.
59 unsigned CCValid;
60
61 // The mask of CC values for which the original condition is true.
62 unsigned CCMask;
63};
Richard Sandifordc2312692014-03-06 10:38:30 +000064} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000065
Ulrich Weigand5f613df2013-05-06 16:15:19 +000066// Classify VT as either 32 or 64 bit.
67static bool is32Bit(EVT VT) {
68 switch (VT.getSimpleVT().SimpleTy) {
69 case MVT::i32:
70 return true;
71 case MVT::i64:
72 return false;
73 default:
74 llvm_unreachable("Unsupported type");
75 }
76}
77
78// Return a version of MachineOperand that can be safely used before the
79// final use.
80static MachineOperand earlyUseOperand(MachineOperand Op) {
81 if (Op.isReg())
82 Op.setIsKill(false);
83 return Op;
84}
85
Mehdi Amini44ede332015-07-09 02:09:04 +000086SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000087 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000088 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000089 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000090
91 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000092 if (Subtarget.hasHighWord())
93 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
94 else
95 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000096 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
97 if (Subtarget.hasVector()) {
98 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
99 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
100 } else {
101 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
102 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
103 }
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000104 if (Subtarget.hasVectorEnhancements1())
105 addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
106 else
107 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000108
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000109 if (Subtarget.hasVector()) {
110 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
111 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
112 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
113 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000114 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000115 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000116 }
117
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000118 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000119 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000120
121 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
123
124 // TODO: It may be better to default to latency-oriented scheduling, however
125 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000126 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127 // scheduler, because it can.
128 setSchedulingPreference(Sched::RegPressure);
129
130 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000131 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132
133 // Instructions are strings of 2-byte aligned 2-byte values.
134 setMinFunctionAlignment(2);
135
136 // Handle operations that are handled in a similar way for all types.
137 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
138 I <= MVT::LAST_FP_VALUETYPE;
139 ++I) {
140 MVT VT = MVT::SimpleValueType(I);
141 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000142 // Lower SET_CC into an IPM-based sequence.
143 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000144
145 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
146 setOperationAction(ISD::SELECT, VT, Expand);
147
148 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
149 setOperationAction(ISD::SELECT_CC, VT, Custom);
150 setOperationAction(ISD::BR_CC, VT, Custom);
151 }
152 }
153
154 // Expand jump table branches as address arithmetic followed by an
155 // indirect jump.
156 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
157
158 // Expand BRCOND into a BR_CC (see above).
159 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
160
161 // Handle integer types.
162 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
163 I <= MVT::LAST_INTEGER_VALUETYPE;
164 ++I) {
165 MVT VT = MVT::SimpleValueType(I);
166 if (isTypeLegal(VT)) {
167 // Expand individual DIV and REMs into DIVREMs.
168 setOperationAction(ISD::SDIV, VT, Expand);
169 setOperationAction(ISD::UDIV, VT, Expand);
170 setOperationAction(ISD::SREM, VT, Expand);
171 setOperationAction(ISD::UREM, VT, Expand);
172 setOperationAction(ISD::SDIVREM, VT, Custom);
173 setOperationAction(ISD::UDIVREM, VT, Custom);
174
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000175 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
176 // stores, putting a serialization instruction after the stores.
177 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
178 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000179
Richard Sandiford41350a52013-12-24 15:18:04 +0000180 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
181 // available, or if the operand is constant.
182 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
183
Ulrich Weigandb4012182015-03-31 12:56:33 +0000184 // Use POPCNT on z196 and above.
185 if (Subtarget.hasPopulationCount())
186 setOperationAction(ISD::CTPOP, VT, Custom);
187 else
188 setOperationAction(ISD::CTPOP, VT, Expand);
189
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000190 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 setOperationAction(ISD::CTTZ, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::ROTR, VT, Expand);
193
Richard Sandiford7d86e472013-08-21 09:34:56 +0000194 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000195 setOperationAction(ISD::MULHS, VT, Expand);
196 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000197 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
198 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000200 // Only z196 and above have native support for conversions to unsigned.
Jonas Paulssonb7a2ef82017-02-02 15:42:14 +0000201 // On z10, promoting to i64 doesn't generate an inexact condition for
202 // values that are outside the i32 range but in the i64 range, so use
203 // the default expansion.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000204 if (!Subtarget.hasFPExtension())
205 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000206 }
207 }
208
209 // Type legalization will convert 8- and 16-bit atomic operations into
210 // forms that operate on i32s (but still keeping the original memory VT).
211 // Lower them into full i32 operations.
212 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
218 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
219 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
220 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
221 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
222 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
223 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
224
Ulrich Weiganda11f63a2017-08-04 18:57:58 +0000225 // Even though i128 is not a legal type, we still need to custom lower
226 // the atomic operations in order to exploit SystemZ instructions.
227 setOperationAction(ISD::ATOMIC_LOAD, MVT::i128, Custom);
228 setOperationAction(ISD::ATOMIC_STORE, MVT::i128, Custom);
229 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i128, Custom);
230
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000231 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
232
Zhan Jun Liauab42cbc2016-06-10 19:58:10 +0000233 // Traps are legal, as we will convert them to "j .+2".
234 setOperationAction(ISD::TRAP, MVT::Other, Legal);
235
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000236 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000238 if (!Subtarget.hasFPExtension()) {
239 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
240 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
241 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000242
243 // We have native support for a 64-bit CTLZ, via FLOGR.
244 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
245 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
246
247 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
248 setOperationAction(ISD::OR, MVT::i64, Custom);
249
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000250 // FIXME: Can we support these natively?
251 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
252 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
253 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
254
255 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000256 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000257 for (MVT VT : MVT::integer_valuetypes()) {
258 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
259 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
260 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
261 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000262
263 // Handle the various types of symbolic address.
264 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
265 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
266 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
267 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
268 setOperationAction(ISD::JumpTable, PtrVT, Custom);
269
270 // We need to handle dynamic allocations specially because of the
271 // 160-byte area at the bottom of the stack.
272 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000273 setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000274
275 // Use custom expanders so that we can force the function to use
276 // a frame pointer.
277 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
278 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
279
Richard Sandiford03481332013-08-23 11:36:42 +0000280 // Handle prefetches with PFD or PFDRL.
281 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
282
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000283 for (MVT VT : MVT::vector_valuetypes()) {
284 // Assume by default that all vector operations need to be expanded.
285 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
286 if (getOperationAction(Opcode, VT) == Legal)
287 setOperationAction(Opcode, VT, Expand);
288
289 // Likewise all truncating stores and extending loads.
290 for (MVT InnerVT : MVT::vector_valuetypes()) {
291 setTruncStoreAction(VT, InnerVT, Expand);
292 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
293 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
294 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
295 }
296
297 if (isTypeLegal(VT)) {
298 // These operations are legal for anything that can be stored in a
299 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000300 // as such. In particular, we can do these for v4f32 even though there
301 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000302 setOperationAction(ISD::LOAD, VT, Legal);
303 setOperationAction(ISD::STORE, VT, Legal);
304 setOperationAction(ISD::VSELECT, VT, Legal);
305 setOperationAction(ISD::BITCAST, VT, Legal);
306 setOperationAction(ISD::UNDEF, VT, Legal);
307
308 // Likewise, except that we need to replace the nodes with something
309 // more specific.
310 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
311 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
312 }
313 }
314
315 // Handle integer vector types.
316 for (MVT VT : MVT::integer_vector_valuetypes()) {
317 if (isTypeLegal(VT)) {
318 // These operations have direct equivalents.
319 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
320 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
321 setOperationAction(ISD::ADD, VT, Legal);
322 setOperationAction(ISD::SUB, VT, Legal);
323 if (VT != MVT::v2i64)
324 setOperationAction(ISD::MUL, VT, Legal);
325 setOperationAction(ISD::AND, VT, Legal);
326 setOperationAction(ISD::OR, VT, Legal);
327 setOperationAction(ISD::XOR, VT, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000328 if (Subtarget.hasVectorEnhancements1())
329 setOperationAction(ISD::CTPOP, VT, Legal);
330 else
331 setOperationAction(ISD::CTPOP, VT, Custom);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000332 setOperationAction(ISD::CTTZ, VT, Legal);
333 setOperationAction(ISD::CTLZ, VT, Legal);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000334
335 // Convert a GPR scalar to a vector by inserting it into element 0.
336 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
337
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000338 // Use a series of unpacks for extensions.
339 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
340 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
341
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000342 // Detect shifts by a scalar amount and convert them into
343 // V*_BY_SCALAR.
344 setOperationAction(ISD::SHL, VT, Custom);
345 setOperationAction(ISD::SRA, VT, Custom);
346 setOperationAction(ISD::SRL, VT, Custom);
347
348 // At present ROTL isn't matched by DAGCombiner. ROTR should be
349 // converted into ROTL.
350 setOperationAction(ISD::ROTL, VT, Expand);
351 setOperationAction(ISD::ROTR, VT, Expand);
352
353 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
354 // and inverting the result as necessary.
355 setOperationAction(ISD::SETCC, VT, Custom);
356 }
357 }
358
Ulrich Weigandcd808232015-05-05 19:26:48 +0000359 if (Subtarget.hasVector()) {
360 // There should be no need to check for float types other than v2f64
361 // since <2 x f32> isn't a legal type.
362 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000363 setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000364 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000365 setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000366 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000367 setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000368 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
Jonas Paulssonfccc7d62017-04-12 11:49:08 +0000369 setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000370 }
371
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000372 // Handle floating-point types.
373 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
374 I <= MVT::LAST_FP_VALUETYPE;
375 ++I) {
376 MVT VT = MVT::SimpleValueType(I);
377 if (isTypeLegal(VT)) {
378 // We can use FI for FRINT.
379 setOperationAction(ISD::FRINT, VT, Legal);
380
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000381 // We can use the extended form of FI for other rounding operations.
382 if (Subtarget.hasFPExtension()) {
383 setOperationAction(ISD::FNEARBYINT, VT, Legal);
384 setOperationAction(ISD::FFLOOR, VT, Legal);
385 setOperationAction(ISD::FCEIL, VT, Legal);
386 setOperationAction(ISD::FTRUNC, VT, Legal);
387 setOperationAction(ISD::FROUND, VT, Legal);
388 }
389
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000390 // No special instructions for these.
391 setOperationAction(ISD::FSIN, VT, Expand);
392 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000393 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000394 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000395 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000396 }
397 }
398
Ulrich Weigandcd808232015-05-05 19:26:48 +0000399 // Handle floating-point vector types.
400 if (Subtarget.hasVector()) {
401 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000402 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000403 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
404
405 // Some insertions and extractions can be done directly but others
406 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000407 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000408 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000409 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000410 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
411
412 // These operations have direct equivalents.
413 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
414 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
415 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
416 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
417 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
418 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
419 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
420 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
421 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
422 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
423 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
424 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
425 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
426 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
427 }
428
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000429 // The vector enhancements facility 1 has instructions for these.
430 if (Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand33435c42017-07-17 17:42:48 +0000431 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
432 setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
433 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
434 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
435 setOperationAction(ISD::FMA, MVT::v4f32, Legal);
436 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
437 setOperationAction(ISD::FABS, MVT::v4f32, Legal);
438 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
439 setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
440 setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
441 setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
442 setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
443 setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
444 setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
445
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000446 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
447 setOperationAction(ISD::FMAXNAN, MVT::f64, Legal);
448 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
449 setOperationAction(ISD::FMINNAN, MVT::f64, Legal);
450
451 setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
452 setOperationAction(ISD::FMAXNAN, MVT::v2f64, Legal);
453 setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
454 setOperationAction(ISD::FMINNAN, MVT::v2f64, Legal);
Ulrich Weigand33435c42017-07-17 17:42:48 +0000455
456 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
457 setOperationAction(ISD::FMAXNAN, MVT::f32, Legal);
458 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
459 setOperationAction(ISD::FMINNAN, MVT::f32, Legal);
460
461 setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
462 setOperationAction(ISD::FMAXNAN, MVT::v4f32, Legal);
463 setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
464 setOperationAction(ISD::FMINNAN, MVT::v4f32, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000465
466 setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
467 setOperationAction(ISD::FMAXNAN, MVT::f128, Legal);
468 setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
469 setOperationAction(ISD::FMINNAN, MVT::f128, Legal);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +0000470 }
471
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000472 // We have fused multiply-addition for f32 and f64 but not f128.
473 setOperationAction(ISD::FMA, MVT::f32, Legal);
474 setOperationAction(ISD::FMA, MVT::f64, Legal);
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000475 if (Subtarget.hasVectorEnhancements1())
476 setOperationAction(ISD::FMA, MVT::f128, Legal);
477 else
478 setOperationAction(ISD::FMA, MVT::f128, Expand);
479
480 // We don't have a copysign instruction on vector registers.
481 if (Subtarget.hasVectorEnhancements1())
482 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000483
484 // Needed so that we don't try to implement f128 constant loads using
485 // a load-and-extend of a f80 constant (in cases where the constant
486 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000487 for (MVT VT : MVT::fp_valuetypes())
488 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000489
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000490 // We don't have extending load instruction on vector registers.
491 if (Subtarget.hasVectorEnhancements1()) {
492 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
493 setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
494 }
495
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000496 // Floating-point truncation and stores need to be done separately.
497 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
498 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
499 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
500
501 // We have 64-bit FPR<->GPR moves, but need special handling for
502 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000503 if (!Subtarget.hasVector()) {
504 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
505 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
506 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000507
508 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
509 // structure, but VAEND is a no-op.
510 setOperationAction(ISD::VASTART, MVT::Other, Custom);
511 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
512 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000513
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000514 // Codes for which we want to perform some z-specific combinations.
515 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000516 setTargetDAGCombine(ISD::STORE);
517 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000518 setTargetDAGCombine(ISD::FP_ROUND);
Bryan Chan28b759c2016-05-16 20:32:22 +0000519 setTargetDAGCombine(ISD::BSWAP);
Elliot Colpbc2cfc22016-07-06 18:13:11 +0000520 setTargetDAGCombine(ISD::SHL);
521 setTargetDAGCombine(ISD::SRA);
522 setTargetDAGCombine(ISD::SRL);
523 setTargetDAGCombine(ISD::ROTL);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000524
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000525 // Handle intrinsics.
526 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000527 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000528
Richard Sandifordd131ff82013-07-08 09:35:23 +0000529 // We want to use MVC in preference to even a single load/store pair.
530 MaxStoresPerMemcpy = 0;
531 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000532
533 // The main memset sequence is a byte store followed by an MVC.
534 // Two STC or MV..I stores win over that, but the kind of fused stores
535 // generated by target-independent code don't when the byte value is
536 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
537 // than "STC;MVC". Handle the choice in target-specific code instead.
538 MaxStoresPerMemset = 0;
539 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000540}
541
Mehdi Amini44ede332015-07-09 02:09:04 +0000542EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
543 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000544 if (!VT.isVector())
545 return MVT::i32;
546 return VT.changeVectorElementTypeToInteger();
547}
548
549bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000550 VT = VT.getScalarType();
551
552 if (!VT.isSimple())
553 return false;
554
555 switch (VT.getSimpleVT().SimpleTy) {
556 case MVT::f32:
557 case MVT::f64:
558 return true;
559 case MVT::f128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +0000560 return Subtarget.hasVectorEnhancements1();
Stephen Lin73de7bf2013-07-09 18:16:56 +0000561 default:
562 break;
563 }
564
565 return false;
566}
567
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000568bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
569 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
570 return Imm.isZero() || Imm.isNegZero();
571}
572
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000573bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
574 // We can use CGFI or CLGFI.
575 return isInt<32>(Imm) || isUInt<32>(Imm);
576}
577
578bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
579 // We can use ALGFI or SLGFI.
580 return isUInt<32>(Imm) || isUInt<32>(-Imm);
581}
582
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000583bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
584 unsigned,
585 unsigned,
586 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000587 // Unaligned accesses should never be slower than the expanded version.
588 // We check specifically for aligned accesses in the few cases where
589 // they are required.
590 if (Fast)
591 *Fast = true;
592 return true;
593}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000594
Jonas Paulsson024e3192017-07-21 11:59:37 +0000595// Information about the addressing mode for a memory access.
596struct AddressingMode {
597 // True if a long displacement is supported.
598 bool LongDisplacement;
599
600 // True if use of index register is supported.
601 bool IndexReg;
602
603 AddressingMode(bool LongDispl, bool IdxReg) :
604 LongDisplacement(LongDispl), IndexReg(IdxReg) {}
605};
606
607// Return the desired addressing mode for a Load which has only one use (in
608// the same block) which is a Store.
609static AddressingMode getLoadStoreAddrMode(bool HasVector,
610 Type *Ty) {
611 // With vector support a Load->Store combination may be combined to either
612 // an MVC or vector operations and it seems to work best to allow the
613 // vector addressing mode.
614 if (HasVector)
615 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
616
617 // Otherwise only the MVC case is special.
618 bool MVC = Ty->isIntegerTy(8);
619 return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
620}
621
622// Return the addressing mode which seems most desirable given an LLVM
623// Instruction pointer.
624static AddressingMode
625supportedAddressingMode(Instruction *I, bool HasVector) {
626 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
627 switch (II->getIntrinsicID()) {
628 default: break;
629 case Intrinsic::memset:
630 case Intrinsic::memmove:
631 case Intrinsic::memcpy:
632 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
633 }
634 }
635
636 if (isa<LoadInst>(I) && I->hasOneUse()) {
637 auto *SingleUser = dyn_cast<Instruction>(*I->user_begin());
638 if (SingleUser->getParent() == I->getParent()) {
639 if (isa<ICmpInst>(SingleUser)) {
640 if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
641 if (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue()))
642 // Comparison of memory with 16 bit signed / unsigned immediate
643 return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
644 } else if (isa<StoreInst>(SingleUser))
645 // Load->Store
646 return getLoadStoreAddrMode(HasVector, I->getType());
647 }
648 } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
649 if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
650 if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
651 // Load->Store
652 return getLoadStoreAddrMode(HasVector, LoadI->getType());
653 }
654
655 if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
656
657 // * Use LDE instead of LE/LEY for z13 to avoid partial register
658 // dependencies (LDE only supports small offsets).
659 // * Utilize the vector registers to hold floating point
660 // values (vector load / store instructions only support small
661 // offsets).
662
663 Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
664 I->getOperand(0)->getType());
665 bool IsFPAccess = MemAccessTy->isFloatingPointTy();
666 bool IsVectorAccess = MemAccessTy->isVectorTy();
667
668 // A store of an extracted vector element will be combined into a VSTE type
669 // instruction.
670 if (!IsVectorAccess && isa<StoreInst>(I)) {
671 Value *DataOp = I->getOperand(0);
672 if (isa<ExtractElementInst>(DataOp))
673 IsVectorAccess = true;
674 }
675
676 // A load which gets inserted into a vector element will be combined into a
677 // VLE type instruction.
678 if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
679 User *LoadUser = *I->user_begin();
680 if (isa<InsertElementInst>(LoadUser))
681 IsVectorAccess = true;
682 }
683
684 if (IsFPAccess || IsVectorAccess)
685 return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
686 }
687
688 return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
689}
690
691// TODO: This method should also check for the displacement when *I is
692// passed. It may also be possible to merge with isFoldableMemAccessOffset()
693// now that both methods get the *I.
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000694bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
Jonas Paulsson024e3192017-07-21 11:59:37 +0000695 const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000696 // Punt on globals for now, although they can be used in limited
697 // RELATIVE LONG cases.
698 if (AM.BaseGV)
699 return false;
700
701 // Require a 20-bit signed offset.
702 if (!isInt<20>(AM.BaseOffs))
703 return false;
704
Jonas Paulsson024e3192017-07-21 11:59:37 +0000705 if (I != nullptr &&
706 !supportedAddressingMode(I, Subtarget.hasVector()).IndexReg)
707 // 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
Jonas Paulsson024e3192017-07-21 11:59:37 +0000714// TODO: Should we check for isInt<20> also?
Jonas Paulsson7a794222016-08-17 13:24:19 +0000715bool SystemZTargetLowering::isFoldableMemAccessOffset(Instruction *I,
716 int64_t Offset) const {
Jonas Paulsson024e3192017-07-21 11:59:37 +0000717 if (!supportedAddressingMode(I, Subtarget.hasVector()).LongDisplacement)
718 return (isUInt<12>(Offset));
Jonas Paulsson7a794222016-08-17 13:24:19 +0000719
720 return true;
721}
722
Richard Sandiford709bda62013-08-19 12:42:31 +0000723bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
724 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
725 return false;
726 unsigned FromBits = FromType->getPrimitiveSizeInBits();
727 unsigned ToBits = ToType->getPrimitiveSizeInBits();
728 return FromBits > ToBits;
729}
730
731bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
732 if (!FromVT.isInteger() || !ToVT.isInteger())
733 return false;
734 unsigned FromBits = FromVT.getSizeInBits();
735 unsigned ToBits = ToVT.getSizeInBits();
736 return FromBits > ToBits;
737}
738
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000739//===----------------------------------------------------------------------===//
740// Inline asm support
741//===----------------------------------------------------------------------===//
742
743TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000744SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 if (Constraint.size() == 1) {
746 switch (Constraint[0]) {
747 case 'a': // Address register
748 case 'd': // Data register (equivalent to 'r')
749 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000750 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000751 case 'r': // General-purpose register
752 return C_RegisterClass;
753
754 case 'Q': // Memory with base and unsigned 12-bit displacement
755 case 'R': // Likewise, plus an index
756 case 'S': // Memory with base and signed 20-bit displacement
757 case 'T': // Likewise, plus an index
758 case 'm': // Equivalent to 'T'.
759 return C_Memory;
760
761 case 'I': // Unsigned 8-bit constant
762 case 'J': // Unsigned 12-bit constant
763 case 'K': // Signed 16-bit constant
764 case 'L': // Signed 20-bit displacement (on all targets we support)
765 case 'M': // 0x7fffffff
766 return C_Other;
767
768 default:
769 break;
770 }
771 }
772 return TargetLowering::getConstraintType(Constraint);
773}
774
775TargetLowering::ConstraintWeight SystemZTargetLowering::
776getSingleConstraintMatchWeight(AsmOperandInfo &info,
777 const char *constraint) const {
778 ConstraintWeight weight = CW_Invalid;
779 Value *CallOperandVal = info.CallOperandVal;
780 // If we don't have a value, we can't do a match,
781 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000782 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000783 return CW_Default;
784 Type *type = CallOperandVal->getType();
785 // Look at the constraint type.
786 switch (*constraint) {
787 default:
788 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
789 break;
790
791 case 'a': // Address register
792 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000793 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000794 case 'r': // General-purpose register
795 if (CallOperandVal->getType()->isIntegerTy())
796 weight = CW_Register;
797 break;
798
799 case 'f': // Floating-point register
800 if (type->isFloatingPointTy())
801 weight = CW_Register;
802 break;
803
804 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000805 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000806 if (isUInt<8>(C->getZExtValue()))
807 weight = CW_Constant;
808 break;
809
810 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000811 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000812 if (isUInt<12>(C->getZExtValue()))
813 weight = CW_Constant;
814 break;
815
816 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000817 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000818 if (isInt<16>(C->getSExtValue()))
819 weight = CW_Constant;
820 break;
821
822 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000823 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000824 if (isInt<20>(C->getSExtValue()))
825 weight = CW_Constant;
826 break;
827
828 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000829 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000830 if (C->getZExtValue() == 0x7fffffff)
831 weight = CW_Constant;
832 break;
833 }
834 return weight;
835}
836
Richard Sandifordb8204052013-07-12 09:08:12 +0000837// Parse a "{tNNN}" register constraint for which the register type "t"
838// has already been verified. MC is the class associated with "t" and
839// Map maps 0-based register numbers to LLVM register numbers.
840static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000841parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
842 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000843 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
844 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000845 unsigned Index;
846 bool Failed =
847 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
848 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000849 return std::make_pair(Map[Index], RC);
850 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000851 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000852}
853
Eric Christopher11e4df72015-02-26 22:38:43 +0000854std::pair<unsigned, const TargetRegisterClass *>
855SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000856 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000857 if (Constraint.size() == 1) {
858 // GCC Constraint Letters
859 switch (Constraint[0]) {
860 default: break;
861 case 'd': // Data register (equivalent to 'r')
862 case 'r': // General-purpose register
863 if (VT == MVT::i64)
864 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
865 else if (VT == MVT::i128)
866 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
867 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
868
869 case 'a': // Address register
870 if (VT == MVT::i64)
871 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
872 else if (VT == MVT::i128)
873 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
874 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
875
Richard Sandiford0755c932013-10-01 11:26:28 +0000876 case 'h': // High-part register (an LLVM extension)
877 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
878
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000879 case 'f': // Floating-point register
880 if (VT == MVT::f64)
881 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
882 else if (VT == MVT::f128)
883 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
884 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
885 }
886 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000887 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000888 // We need to override the default register parsing for GPRs and FPRs
889 // because the interpretation depends on VT. The internal names of
890 // the registers are also different from the external names
891 // (F0D and F0S instead of F0, etc.).
892 if (Constraint[1] == 'r') {
893 if (VT == MVT::i32)
894 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
895 SystemZMC::GR32Regs);
896 if (VT == MVT::i128)
897 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
898 SystemZMC::GR128Regs);
899 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
900 SystemZMC::GR64Regs);
901 }
902 if (Constraint[1] == 'f') {
903 if (VT == MVT::f32)
904 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
905 SystemZMC::FP32Regs);
906 if (VT == MVT::f128)
907 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
908 SystemZMC::FP128Regs);
909 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
910 SystemZMC::FP64Regs);
911 }
912 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000913 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000914}
915
916void SystemZTargetLowering::
917LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
918 std::vector<SDValue> &Ops,
919 SelectionDAG &DAG) const {
920 // Only support length 1 constraints for now.
921 if (Constraint.length() == 1) {
922 switch (Constraint[0]) {
923 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000924 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000925 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000926 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000927 Op.getValueType()));
928 return;
929
930 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000931 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000932 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000933 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000934 Op.getValueType()));
935 return;
936
937 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000938 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000939 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000940 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000941 Op.getValueType()));
942 return;
943
944 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000945 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000946 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000947 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000948 Op.getValueType()));
949 return;
950
951 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000952 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000953 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000954 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000955 Op.getValueType()));
956 return;
957 }
958 }
959 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
960}
961
962//===----------------------------------------------------------------------===//
963// Calling conventions
964//===----------------------------------------------------------------------===//
965
966#include "SystemZGenCallingConv.inc"
967
Richard Sandiford709bda62013-08-19 12:42:31 +0000968bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
969 Type *ToType) const {
970 return isTruncateFree(FromType, ToType);
971}
972
Matt Arsenault31380752017-04-18 21:16:46 +0000973bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000974 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000975}
976
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000977// We do not yet support 128-bit single-element vector types. If the user
978// attempts to use such types as function argument or return type, prefer
979// to error out instead of emitting code violating the ABI.
980static void VerifyVectorType(MVT VT, EVT ArgVT) {
981 if (ArgVT.isVector() && !VT.isVector())
982 report_fatal_error("Unsupported vector argument or return type");
983}
984
985static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
986 for (unsigned i = 0; i < Ins.size(); ++i)
987 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
988}
989
990static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
991 for (unsigned i = 0; i < Outs.size(); ++i)
992 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
993}
994
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000995// Value is a value that has been passed to us in the location described by VA
996// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
997// any loads onto Chain.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000998static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000999 CCValAssign &VA, SDValue Chain,
1000 SDValue Value) {
1001 // If the argument has been promoted from a smaller type, insert an
1002 // assertion to capture this.
1003 if (VA.getLocInfo() == CCValAssign::SExt)
1004 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
1005 DAG.getValueType(VA.getValVT()));
1006 else if (VA.getLocInfo() == CCValAssign::ZExt)
1007 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
1008 DAG.getValueType(VA.getValVT()));
1009
1010 if (VA.isExtInLoc())
1011 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001012 else if (VA.getLocInfo() == CCValAssign::BCvt) {
1013 // If this is a short vector argument loaded from the stack,
1014 // extend from i64 to full vector size and then bitcast.
1015 assert(VA.getLocVT() == MVT::i64);
1016 assert(VA.getValVT().isVector());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001017 Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001018 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
1019 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001020 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
1021 return Value;
1022}
1023
1024// Value is a value of type VA.getValVT() that we need to copy into
1025// the location described by VA. Return a copy of Value converted to
1026// VA.getValVT(). The caller is responsible for handling indirect values.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001027static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001028 CCValAssign &VA, SDValue Value) {
1029 switch (VA.getLocInfo()) {
1030 case CCValAssign::SExt:
1031 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
1032 case CCValAssign::ZExt:
1033 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
1034 case CCValAssign::AExt:
1035 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00001036 case CCValAssign::BCvt:
1037 // If this is a short vector argument to be stored to the stack,
1038 // bitcast to v2i64 and then extract first element.
1039 assert(VA.getLocVT() == MVT::i64);
1040 assert(VA.getValVT().isVector());
1041 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
1042 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
1043 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001044 case CCValAssign::Full:
1045 return Value;
1046 default:
1047 llvm_unreachable("Unhandled getLocInfo()");
1048 }
1049}
1050
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001051SDValue SystemZTargetLowering::LowerFormalArguments(
1052 SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
1053 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
1054 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001055 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00001056 MachineFrameInfo &MFI = MF.getFrameInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001057 MachineRegisterInfo &MRI = MF.getRegInfo();
1058 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +00001059 MF.getInfo<SystemZMachineFunctionInfo>();
1060 auto *TFL =
1061 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001062 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001063
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001064 // Detect unsupported vector argument types.
1065 if (Subtarget.hasVector())
1066 VerifyVectorTypes(Ins);
1067
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001068 // Assign locations to all of the incoming arguments.
1069 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001070 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001071 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
1072
1073 unsigned NumFixedGPRs = 0;
1074 unsigned NumFixedFPRs = 0;
1075 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1076 SDValue ArgValue;
1077 CCValAssign &VA = ArgLocs[I];
1078 EVT LocVT = VA.getLocVT();
1079 if (VA.isRegLoc()) {
1080 // Arguments passed in registers
1081 const TargetRegisterClass *RC;
1082 switch (LocVT.getSimpleVT().SimpleTy) {
1083 default:
1084 // Integers smaller than i64 should be promoted to i64.
1085 llvm_unreachable("Unexpected argument type");
1086 case MVT::i32:
1087 NumFixedGPRs += 1;
1088 RC = &SystemZ::GR32BitRegClass;
1089 break;
1090 case MVT::i64:
1091 NumFixedGPRs += 1;
1092 RC = &SystemZ::GR64BitRegClass;
1093 break;
1094 case MVT::f32:
1095 NumFixedFPRs += 1;
1096 RC = &SystemZ::FP32BitRegClass;
1097 break;
1098 case MVT::f64:
1099 NumFixedFPRs += 1;
1100 RC = &SystemZ::FP64BitRegClass;
1101 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001102 case MVT::v16i8:
1103 case MVT::v8i16:
1104 case MVT::v4i32:
1105 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +00001106 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +00001107 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001108 RC = &SystemZ::VR128BitRegClass;
1109 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001110 }
1111
1112 unsigned VReg = MRI.createVirtualRegister(RC);
1113 MRI.addLiveIn(VA.getLocReg(), VReg);
1114 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
1115 } else {
1116 assert(VA.isMemLoc() && "Argument not register or memory");
1117
1118 // Create the frame index object for this incoming parameter.
Matthias Braun941a7052016-07-28 18:40:00 +00001119 int FI = MFI.CreateFixedObject(LocVT.getSizeInBits() / 8,
1120 VA.getLocMemOffset(), true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001121
1122 // Create the SelectionDAG nodes corresponding to a load
1123 // from this parameter. Unpromoted ints and floats are
1124 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
1126 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001127 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
1128 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001129 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001130 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001131 }
1132
1133 // Convert the value of the argument register into the value that's
1134 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001135 if (VA.getLocInfo() == CCValAssign::Indirect) {
Justin Lebar9c375812016-07-15 18:27:10 +00001136 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
1137 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001138 // If the original argument was split (e.g. i128), we need
1139 // to load all parts of it here (using the same address).
1140 unsigned ArgIndex = Ins[I].OrigArgIndex;
1141 assert (Ins[I].PartOffset == 0);
1142 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
1143 CCValAssign &PartVA = ArgLocs[I + 1];
1144 unsigned PartOffset = Ins[I + 1].PartOffset;
1145 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
1146 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001147 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
1148 MachinePointerInfo()));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001149 ++I;
1150 }
1151 } else
1152 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001153 }
1154
1155 if (IsVarArg) {
1156 // Save the number of non-varargs registers for later use by va_start, etc.
1157 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
1158 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
1159
1160 // Likewise the address (in the form of a frame index) of where the
1161 // first stack vararg would be. The 1-byte size here is arbitrary.
1162 int64_t StackSize = CCInfo.getNextStackOffset();
Matthias Braun941a7052016-07-28 18:40:00 +00001163 FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001164
1165 // ...and a similar frame index for the caller-allocated save area
1166 // that will be used to store the incoming registers.
1167 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
Matthias Braun941a7052016-07-28 18:40:00 +00001168 unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001169 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
1170
1171 // Store the FPR varargs in the reserved frame slots. (We store the
1172 // GPRs as part of the prologue.)
1173 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
1174 SDValue MemOps[SystemZ::NumArgFPRs];
1175 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
1176 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
Matthias Braun941a7052016-07-28 18:40:00 +00001177 int FI = MFI.CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +00001178 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
1180 &SystemZ::FP64BitRegClass);
1181 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
1182 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Justin Lebar9c375812016-07-15 18:27:10 +00001183 MachinePointerInfo::getFixedStack(MF, FI));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001184 }
1185 // Join the stores, which are independent of one another.
1186 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +00001187 makeArrayRef(&MemOps[NumFixedFPRs],
1188 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001189 }
1190 }
1191
1192 return Chain;
1193}
1194
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001195static bool canUseSiblingCall(const CCState &ArgCCInfo,
Bryan Chan893110e2016-04-28 00:17:23 +00001196 SmallVectorImpl<CCValAssign> &ArgLocs,
1197 SmallVectorImpl<ISD::OutputArg> &Outs) {
Richard Sandiford709bda62013-08-19 12:42:31 +00001198 // Punt if there are any indirect or stack arguments, or if the call
Bryan Chan893110e2016-04-28 00:17:23 +00001199 // needs the callee-saved argument register R6, or if the call uses
1200 // the callee-saved register arguments SwiftSelf and SwiftError.
Richard Sandiford709bda62013-08-19 12:42:31 +00001201 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1202 CCValAssign &VA = ArgLocs[I];
1203 if (VA.getLocInfo() == CCValAssign::Indirect)
1204 return false;
1205 if (!VA.isRegLoc())
1206 return false;
1207 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001208 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001209 return false;
Bryan Chan893110e2016-04-28 00:17:23 +00001210 if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
1211 return false;
Richard Sandiford709bda62013-08-19 12:42:31 +00001212 }
1213 return true;
1214}
1215
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001216SDValue
1217SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1218 SmallVectorImpl<SDValue> &InVals) const {
1219 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001220 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001221 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1222 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1223 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001224 SDValue Chain = CLI.Chain;
1225 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001226 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001227 CallingConv::ID CallConv = CLI.CallConv;
1228 bool IsVarArg = CLI.IsVarArg;
1229 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001230 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001231
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001232 // Detect unsupported vector argument and return types.
1233 if (Subtarget.hasVector()) {
1234 VerifyVectorTypes(Outs);
1235 VerifyVectorTypes(Ins);
1236 }
1237
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001238 // Analyze the operands of the call, assigning locations to each operand.
1239 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001240 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001241 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1242
Richard Sandiford709bda62013-08-19 12:42:31 +00001243 // We don't support GuaranteedTailCallOpt, only automatically-detected
1244 // sibling calls.
Bryan Chan893110e2016-04-28 00:17:23 +00001245 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
Richard Sandiford709bda62013-08-19 12:42:31 +00001246 IsTailCall = false;
1247
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001248 // Get a count of how many bytes are to be pushed on the stack.
1249 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1250
1251 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001252 if (!IsTailCall)
Serge Pavlovd526b132017-05-09 13:35:13 +00001253 Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001254
1255 // Copy argument values to their designated locations.
1256 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1257 SmallVector<SDValue, 8> MemOpChains;
1258 SDValue StackPtr;
1259 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1260 CCValAssign &VA = ArgLocs[I];
1261 SDValue ArgValue = OutVals[I];
1262
1263 if (VA.getLocInfo() == CCValAssign::Indirect) {
1264 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001265 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001266 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Justin Lebar9c375812016-07-15 18:27:10 +00001267 MemOpChains.push_back(
1268 DAG.getStore(Chain, DL, ArgValue, SpillSlot,
1269 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001270 // If the original argument was split (e.g. i128), we need
1271 // to store all parts of it here (and pass just one address).
1272 unsigned ArgIndex = Outs[I].OrigArgIndex;
1273 assert (Outs[I].PartOffset == 0);
1274 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1275 SDValue PartValue = OutVals[I + 1];
1276 unsigned PartOffset = Outs[I + 1].PartOffset;
1277 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1278 DAG.getIntPtrConstant(PartOffset, DL));
Justin Lebar9c375812016-07-15 18:27:10 +00001279 MemOpChains.push_back(
1280 DAG.getStore(Chain, DL, PartValue, Address,
1281 MachinePointerInfo::getFixedStack(MF, FI)));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001282 ++I;
1283 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001284 ArgValue = SpillSlot;
1285 } else
1286 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1287
1288 if (VA.isRegLoc())
1289 // Queue up the argument copies and emit them at the end.
1290 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1291 else {
1292 assert(VA.isMemLoc() && "Argument not register or memory");
1293
1294 // Work out the address of the stack slot. Unpromoted ints and
1295 // floats are passed as right-justified 8-byte values.
1296 if (!StackPtr.getNode())
1297 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1298 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1299 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1300 Offset += 4;
1301 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001302 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001303
1304 // Emit the store.
Justin Lebar9c375812016-07-15 18:27:10 +00001305 MemOpChains.push_back(
1306 DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001307 }
1308 }
1309
1310 // Join the stores, which are independent of one another.
1311 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001312 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001313
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001314 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001315 // associated Target* opcodes. Force %r1 to be used for indirect
1316 // tail calls.
1317 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001318 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001319 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1320 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001321 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001322 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1323 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001324 } else if (IsTailCall) {
1325 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1326 Glue = Chain.getValue(1);
1327 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1328 }
1329
1330 // Build a sequence of copy-to-reg nodes, chained and glued together.
1331 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1332 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1333 RegsToPass[I].second, Glue);
1334 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001335 }
1336
1337 // The first call operand is the chain and the second is the target address.
1338 SmallVector<SDValue, 8> Ops;
1339 Ops.push_back(Chain);
1340 Ops.push_back(Callee);
1341
1342 // Add argument registers to the end of the list so that they are
1343 // known live into the call.
1344 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1345 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1346 RegsToPass[I].second.getValueType()));
1347
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001348 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001349 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001350 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001351 assert(Mask && "Missing call preserved mask for calling convention");
1352 Ops.push_back(DAG.getRegisterMask(Mask));
1353
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001354 // Glue the call to the argument copies, if any.
1355 if (Glue.getNode())
1356 Ops.push_back(Glue);
1357
1358 // Emit the call.
1359 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001360 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001361 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1362 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001363 Glue = Chain.getValue(1);
1364
1365 // Mark the end of the call, which is glued to the call itself.
1366 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001367 DAG.getConstant(NumBytes, DL, PtrVT, true),
1368 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001369 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001370 Glue = Chain.getValue(1);
1371
1372 // Assign locations to each value returned by this call.
1373 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001374 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001375 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1376
1377 // Copy all of the result registers out of their specified physreg.
1378 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1379 CCValAssign &VA = RetLocs[I];
1380
1381 // Copy the value out, gluing the copy to the end of the call sequence.
1382 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1383 VA.getLocVT(), Glue);
1384 Chain = RetValue.getValue(1);
1385 Glue = RetValue.getValue(2);
1386
1387 // Convert the value of the return register into the value that's
1388 // being returned.
1389 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1390 }
1391
1392 return Chain;
1393}
1394
Ulrich Weiganda887f062015-08-13 13:37:06 +00001395bool SystemZTargetLowering::
1396CanLowerReturn(CallingConv::ID CallConv,
1397 MachineFunction &MF, bool isVarArg,
1398 const SmallVectorImpl<ISD::OutputArg> &Outs,
1399 LLVMContext &Context) const {
1400 // Detect unsupported vector return types.
1401 if (Subtarget.hasVector())
1402 VerifyVectorTypes(Outs);
1403
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001404 // Special case that we cannot easily detect in RetCC_SystemZ since
1405 // i128 is not a legal type.
1406 for (auto &Out : Outs)
1407 if (Out.ArgVT == MVT::i128)
1408 return false;
1409
Ulrich Weiganda887f062015-08-13 13:37:06 +00001410 SmallVector<CCValAssign, 16> RetLocs;
1411 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1412 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1413}
1414
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001415SDValue
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001416SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1417 bool IsVarArg,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001418 const SmallVectorImpl<ISD::OutputArg> &Outs,
1419 const SmallVectorImpl<SDValue> &OutVals,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001420 const SDLoc &DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001421 MachineFunction &MF = DAG.getMachineFunction();
1422
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001423 // Detect unsupported vector return types.
1424 if (Subtarget.hasVector())
1425 VerifyVectorTypes(Outs);
1426
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001427 // Assign locations to each returned value.
1428 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001429 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001430 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1431
1432 // Quick exit for void returns
1433 if (RetLocs.empty())
1434 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1435
1436 // Copy the result values into the output registers.
1437 SDValue Glue;
1438 SmallVector<SDValue, 4> RetOps;
1439 RetOps.push_back(Chain);
1440 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1441 CCValAssign &VA = RetLocs[I];
1442 SDValue RetValue = OutVals[I];
1443
1444 // Make the return register live on exit.
1445 assert(VA.isRegLoc() && "Can only return in registers!");
1446
1447 // Promote the value as required.
1448 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1449
1450 // Chain and glue the copies together.
1451 unsigned Reg = VA.getLocReg();
1452 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1453 Glue = Chain.getValue(1);
1454 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1455 }
1456
1457 // Update chain and glue.
1458 RetOps[0] = Chain;
1459 if (Glue.getNode())
1460 RetOps.push_back(Glue);
1461
Craig Topper48d114b2014-04-26 18:35:24 +00001462 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001463}
1464
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001465// Return true if Op is an intrinsic node with chain that returns the CC value
1466// as its only (other) argument. Provide the associated SystemZISD opcode and
1467// the mask of valid CC values if so.
1468static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1469 unsigned &CCValid) {
1470 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1471 switch (Id) {
1472 case Intrinsic::s390_tbegin:
1473 Opcode = SystemZISD::TBEGIN;
1474 CCValid = SystemZ::CCMASK_TBEGIN;
1475 return true;
1476
1477 case Intrinsic::s390_tbegin_nofloat:
1478 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1479 CCValid = SystemZ::CCMASK_TBEGIN;
1480 return true;
1481
1482 case Intrinsic::s390_tend:
1483 Opcode = SystemZISD::TEND;
1484 CCValid = SystemZ::CCMASK_TEND;
1485 return true;
1486
1487 default:
1488 return false;
1489 }
1490}
1491
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001492// Return true if Op is an intrinsic node without chain that returns the
1493// CC value as its final argument. Provide the associated SystemZISD
1494// opcode and the mask of valid CC values if so.
1495static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1496 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1497 switch (Id) {
1498 case Intrinsic::s390_vpkshs:
1499 case Intrinsic::s390_vpksfs:
1500 case Intrinsic::s390_vpksgs:
1501 Opcode = SystemZISD::PACKS_CC;
1502 CCValid = SystemZ::CCMASK_VCMP;
1503 return true;
1504
1505 case Intrinsic::s390_vpklshs:
1506 case Intrinsic::s390_vpklsfs:
1507 case Intrinsic::s390_vpklsgs:
1508 Opcode = SystemZISD::PACKLS_CC;
1509 CCValid = SystemZ::CCMASK_VCMP;
1510 return true;
1511
1512 case Intrinsic::s390_vceqbs:
1513 case Intrinsic::s390_vceqhs:
1514 case Intrinsic::s390_vceqfs:
1515 case Intrinsic::s390_vceqgs:
1516 Opcode = SystemZISD::VICMPES;
1517 CCValid = SystemZ::CCMASK_VCMP;
1518 return true;
1519
1520 case Intrinsic::s390_vchbs:
1521 case Intrinsic::s390_vchhs:
1522 case Intrinsic::s390_vchfs:
1523 case Intrinsic::s390_vchgs:
1524 Opcode = SystemZISD::VICMPHS;
1525 CCValid = SystemZ::CCMASK_VCMP;
1526 return true;
1527
1528 case Intrinsic::s390_vchlbs:
1529 case Intrinsic::s390_vchlhs:
1530 case Intrinsic::s390_vchlfs:
1531 case Intrinsic::s390_vchlgs:
1532 Opcode = SystemZISD::VICMPHLS;
1533 CCValid = SystemZ::CCMASK_VCMP;
1534 return true;
1535
1536 case Intrinsic::s390_vtm:
1537 Opcode = SystemZISD::VTM;
1538 CCValid = SystemZ::CCMASK_VCMP;
1539 return true;
1540
1541 case Intrinsic::s390_vfaebs:
1542 case Intrinsic::s390_vfaehs:
1543 case Intrinsic::s390_vfaefs:
1544 Opcode = SystemZISD::VFAE_CC;
1545 CCValid = SystemZ::CCMASK_ANY;
1546 return true;
1547
1548 case Intrinsic::s390_vfaezbs:
1549 case Intrinsic::s390_vfaezhs:
1550 case Intrinsic::s390_vfaezfs:
1551 Opcode = SystemZISD::VFAEZ_CC;
1552 CCValid = SystemZ::CCMASK_ANY;
1553 return true;
1554
1555 case Intrinsic::s390_vfeebs:
1556 case Intrinsic::s390_vfeehs:
1557 case Intrinsic::s390_vfeefs:
1558 Opcode = SystemZISD::VFEE_CC;
1559 CCValid = SystemZ::CCMASK_ANY;
1560 return true;
1561
1562 case Intrinsic::s390_vfeezbs:
1563 case Intrinsic::s390_vfeezhs:
1564 case Intrinsic::s390_vfeezfs:
1565 Opcode = SystemZISD::VFEEZ_CC;
1566 CCValid = SystemZ::CCMASK_ANY;
1567 return true;
1568
1569 case Intrinsic::s390_vfenebs:
1570 case Intrinsic::s390_vfenehs:
1571 case Intrinsic::s390_vfenefs:
1572 Opcode = SystemZISD::VFENE_CC;
1573 CCValid = SystemZ::CCMASK_ANY;
1574 return true;
1575
1576 case Intrinsic::s390_vfenezbs:
1577 case Intrinsic::s390_vfenezhs:
1578 case Intrinsic::s390_vfenezfs:
1579 Opcode = SystemZISD::VFENEZ_CC;
1580 CCValid = SystemZ::CCMASK_ANY;
1581 return true;
1582
1583 case Intrinsic::s390_vistrbs:
1584 case Intrinsic::s390_vistrhs:
1585 case Intrinsic::s390_vistrfs:
1586 Opcode = SystemZISD::VISTR_CC;
1587 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1588 return true;
1589
1590 case Intrinsic::s390_vstrcbs:
1591 case Intrinsic::s390_vstrchs:
1592 case Intrinsic::s390_vstrcfs:
1593 Opcode = SystemZISD::VSTRC_CC;
1594 CCValid = SystemZ::CCMASK_ANY;
1595 return true;
1596
1597 case Intrinsic::s390_vstrczbs:
1598 case Intrinsic::s390_vstrczhs:
1599 case Intrinsic::s390_vstrczfs:
1600 Opcode = SystemZISD::VSTRCZ_CC;
1601 CCValid = SystemZ::CCMASK_ANY;
1602 return true;
1603
1604 case Intrinsic::s390_vfcedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001605 case Intrinsic::s390_vfcesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001606 Opcode = SystemZISD::VFCMPES;
1607 CCValid = SystemZ::CCMASK_VCMP;
1608 return true;
1609
1610 case Intrinsic::s390_vfchdbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001611 case Intrinsic::s390_vfchsbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001612 Opcode = SystemZISD::VFCMPHS;
1613 CCValid = SystemZ::CCMASK_VCMP;
1614 return true;
1615
1616 case Intrinsic::s390_vfchedbs:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001617 case Intrinsic::s390_vfchesbs:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001618 Opcode = SystemZISD::VFCMPHES;
1619 CCValid = SystemZ::CCMASK_VCMP;
1620 return true;
1621
1622 case Intrinsic::s390_vftcidb:
Ulrich Weigand33435c42017-07-17 17:42:48 +00001623 case Intrinsic::s390_vftcisb:
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001624 Opcode = SystemZISD::VFTCI;
1625 CCValid = SystemZ::CCMASK_VCMP;
1626 return true;
1627
Marcin Koscielnickicf7cc722016-07-10 14:41:22 +00001628 case Intrinsic::s390_tdc:
1629 Opcode = SystemZISD::TDC;
1630 CCValid = SystemZ::CCMASK_TDC;
1631 return true;
1632
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001633 default:
1634 return false;
1635 }
1636}
1637
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001638// Emit an intrinsic with chain with a glued value instead of its CC result.
1639static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1640 unsigned Opcode) {
1641 // Copy all operands except the intrinsic ID.
1642 unsigned NumOps = Op.getNumOperands();
1643 SmallVector<SDValue, 6> Ops;
1644 Ops.reserve(NumOps - 1);
1645 Ops.push_back(Op.getOperand(0));
1646 for (unsigned I = 2; I < NumOps; ++I)
1647 Ops.push_back(Op.getOperand(I));
1648
1649 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1650 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1651 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1652 SDValue OldChain = SDValue(Op.getNode(), 1);
1653 SDValue NewChain = SDValue(Intr.getNode(), 0);
1654 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1655 return Intr;
1656}
1657
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001658// Emit an intrinsic with a glued value instead of its CC result.
1659static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1660 unsigned Opcode) {
1661 // Copy all operands except the intrinsic ID.
1662 unsigned NumOps = Op.getNumOperands();
1663 SmallVector<SDValue, 6> Ops;
1664 Ops.reserve(NumOps - 1);
1665 for (unsigned I = 1; I < NumOps; ++I)
1666 Ops.push_back(Op.getOperand(I));
1667
1668 if (Op->getNumValues() == 1)
1669 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1670 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1671 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1672 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1673}
1674
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001675// CC is a comparison that will be implemented using an integer or
1676// floating-point comparison. Return the condition code mask for
1677// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1678// unsigned comparisons and clear for signed ones. In the floating-point
1679// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1680static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1681#define CONV(X) \
1682 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1683 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1684 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1685
1686 switch (CC) {
1687 default:
1688 llvm_unreachable("Invalid integer condition!");
1689
1690 CONV(EQ);
1691 CONV(NE);
1692 CONV(GT);
1693 CONV(GE);
1694 CONV(LT);
1695 CONV(LE);
1696
1697 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1698 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1699 }
1700#undef CONV
1701}
1702
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001703// Return a sequence for getting a 1 from an IPM result when CC has a
1704// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1705// The handling of CC values outside CCValid doesn't matter.
1706static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1707 // Deal with cases where the result can be taken directly from a bit
1708 // of the IPM result.
1709 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1710 return IPMConversion(0, 0, SystemZ::IPM_CC);
1711 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1712 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1713
1714 // Deal with cases where we can add a value to force the sign bit
1715 // to contain the right value. Putting the bit in 31 means we can
1716 // use SRL rather than RISBG(L), and also makes it easier to get a
1717 // 0/-1 value, so it has priority over the other tests below.
1718 //
1719 // These sequences rely on the fact that the upper two bits of the
1720 // IPM result are zero.
1721 uint64_t TopBit = uint64_t(1) << 31;
1722 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1723 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1724 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1725 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1726 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1727 | SystemZ::CCMASK_1
1728 | SystemZ::CCMASK_2)))
1729 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1730 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1731 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1732 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1733 | SystemZ::CCMASK_2
1734 | SystemZ::CCMASK_3)))
1735 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1736
1737 // Next try inverting the value and testing a bit. 0/1 could be
1738 // handled this way too, but we dealt with that case above.
1739 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1740 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1741
1742 // Handle cases where adding a value forces a non-sign bit to contain
1743 // the right value.
1744 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1745 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1746 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1747 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1748
Alp Tokercb402912014-01-24 17:20:08 +00001749 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001750 // can be done by inverting the low CC bit and applying one of the
1751 // sign-based extractions above.
1752 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1753 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1754 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1755 return IPMConversion(1 << SystemZ::IPM_CC,
1756 TopBit - (3 << SystemZ::IPM_CC), 31);
1757 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1758 | SystemZ::CCMASK_1
1759 | SystemZ::CCMASK_3)))
1760 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1761 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1762 | SystemZ::CCMASK_2
1763 | SystemZ::CCMASK_3)))
1764 return IPMConversion(1 << SystemZ::IPM_CC,
1765 TopBit - (1 << SystemZ::IPM_CC), 31);
1766
1767 llvm_unreachable("Unexpected CC combination");
1768}
1769
Richard Sandifordd420f732013-12-13 15:28:45 +00001770// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001771// as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001772static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001773 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001774 return;
1775
Richard Sandiford21f5d682014-03-06 11:22:58 +00001776 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001777 if (!ConstOp1)
1778 return;
1779
1780 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001781 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1782 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1783 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1784 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1785 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001786 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001787 }
1788}
1789
Richard Sandifordd420f732013-12-13 15:28:45 +00001790// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1791// adjust the operands as necessary.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001792static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
1793 Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001794 // For us to make any changes, it must a comparison between a single-use
1795 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001796 if (!C.Op0.hasOneUse() ||
1797 C.Op0.getOpcode() != ISD::LOAD ||
1798 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001799 return;
1800
1801 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001802 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001803 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1804 if (NumBits != 8 && NumBits != 16)
1805 return;
1806
1807 // The load must be an extending one and the constant must be within the
1808 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001809 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001810 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001811 uint64_t Mask = (1 << NumBits) - 1;
1812 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001813 // Make sure that ConstOp1 is in range of C.Op0.
1814 int64_t SignedValue = ConstOp1->getSExtValue();
1815 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001816 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001817 if (C.ICmpType != SystemZICMP::SignedOnly) {
1818 // Unsigned comparison between two sign-extended values is equivalent
1819 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001820 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001821 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001822 // Try to treat the comparison as unsigned, so that we can use CLI.
1823 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001824 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001825 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001826 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1827 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001828 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001829 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001830 else
1831 // No instruction exists for this combination.
1832 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001833 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001834 }
1835 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1836 if (Value > Mask)
1837 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001838 // If the constant is in range, we can use any comparison.
1839 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001840 } else
1841 return;
1842
1843 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001844 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1845 ISD::SEXTLOAD :
1846 ISD::ZEXTLOAD);
1847 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001848 Load->getExtensionType() != ExtType)
Justin Lebar9c375812016-07-15 18:27:10 +00001849 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
1850 Load->getBasePtr(), Load->getPointerInfo(),
1851 Load->getMemoryVT(), Load->getAlignment(),
1852 Load->getMemOperand()->getFlags());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001853
1854 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001855 if (C.Op1.getValueType() != MVT::i32 ||
1856 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001857 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001858}
1859
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001860// Return true if Op is either an unextended load, or a load suitable
1861// for integer register-memory comparisons of type ICmpType.
1862static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001863 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001864 if (Load) {
1865 // There are no instructions to compare a register with a memory byte.
1866 if (Load->getMemoryVT() == MVT::i8)
1867 return false;
1868 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001869 switch (Load->getExtensionType()) {
1870 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001871 return true;
1872 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001873 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001874 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001875 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001876 default:
1877 break;
1878 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001879 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001880 return false;
1881}
1882
Richard Sandifordd420f732013-12-13 15:28:45 +00001883// Return true if it is better to swap the operands of C.
1884static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001885 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001886 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001887 return false;
1888
1889 // Always keep a floating-point constant second, since comparisons with
1890 // zero can use LOAD TEST and comparisons with other constants make a
1891 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001892 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001893 return false;
1894
1895 // Never swap comparisons with zero since there are many ways to optimize
1896 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001897 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001898 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001899 return false;
1900
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001901 // Also keep natural memory operands second if the loaded value is
1902 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001903 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001904 return false;
1905
Richard Sandiford24e597b2013-08-23 11:27:19 +00001906 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1907 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001908 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001909 // The only exceptions are when the second operand is a constant and
1910 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001911 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001912 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001913 // The unsigned memory-immediate instructions can handle 16-bit
1914 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001915 if (C.ICmpType != SystemZICMP::SignedOnly &&
1916 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001917 return false;
1918 // The signed memory-immediate instructions can handle 16-bit
1919 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001920 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1921 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001922 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001923 return true;
1924 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001925
1926 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001927 unsigned Opcode0 = C.Op0.getOpcode();
1928 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001929 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001930 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001931 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001932 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001933 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001934 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1935 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001936 return true;
1937
Richard Sandiford24e597b2013-08-23 11:27:19 +00001938 return false;
1939}
1940
Richard Sandiford73170f82013-12-11 11:45:08 +00001941// Return a version of comparison CC mask CCMask in which the LT and GT
1942// actions are swapped.
1943static unsigned reverseCCMask(unsigned CCMask) {
1944 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1945 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1946 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1947 (CCMask & SystemZ::CCMASK_CMP_UO));
1948}
1949
Richard Sandiford0847c452013-12-13 15:50:30 +00001950// Check whether C tests for equality between X and Y and whether X - Y
1951// or Y - X is also computed. In that case it's better to compare the
1952// result of the subtraction against zero.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001953static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
1954 Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001955 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1956 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001957 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001958 SDNode *N = *I;
1959 if (N->getOpcode() == ISD::SUB &&
1960 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1961 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1962 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001963 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001964 return;
1965 }
1966 }
1967 }
1968}
1969
Richard Sandifordd420f732013-12-13 15:28:45 +00001970// Check whether C compares a floating-point value with zero and if that
1971// floating-point value is also negated. In this case we can use the
1972// negation to set CC, so avoiding separate LOAD AND TEST and
1973// LOAD (NEGATIVE/COMPLEMENT) instructions.
1974static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001975 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001976 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001977 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001978 SDNode *N = *I;
1979 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001980 C.Op0 = SDValue(N, 0);
1981 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001982 return;
1983 }
1984 }
1985 }
1986}
1987
Richard Sandifordd420f732013-12-13 15:28:45 +00001988// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001989// also sign-extended. In that case it is better to test the result
1990// of the sign extension using LTGFR.
1991//
1992// This case is important because InstCombine transforms a comparison
1993// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001994static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001995 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001996 if (C.Op0.getOpcode() == ISD::SHL &&
1997 C.Op0.getValueType() == MVT::i64 &&
1998 C.Op1.getOpcode() == ISD::Constant &&
1999 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002000 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002001 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002002 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002003 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00002004 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002005 SDNode *N = *I;
2006 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
2007 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00002008 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00002009 return;
2010 }
2011 }
2012 }
2013 }
2014}
2015
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002016// If C compares the truncation of an extending load, try to compare
2017// the untruncated value instead. This exposes more opportunities to
2018// reuse CC.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002019static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
2020 Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002021 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
2022 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
2023 C.Op1.getOpcode() == ISD::Constant &&
2024 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002025 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002026 if (L->getMemoryVT().getStoreSizeInBits() <= C.Op0.getValueSizeInBits()) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002027 unsigned Type = L->getExtensionType();
2028 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
2029 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
2030 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002031 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002032 }
2033 }
2034 }
2035}
2036
Richard Sandiford030c1652013-09-13 09:09:50 +00002037// Return true if shift operation N has an in-range constant shift value.
2038// Store it in ShiftVal if so.
2039static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00002040 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00002041 if (!Shift)
2042 return false;
2043
2044 uint64_t Amount = Shift->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002045 if (Amount >= N.getValueSizeInBits())
Richard Sandiford030c1652013-09-13 09:09:50 +00002046 return false;
2047
2048 ShiftVal = Amount;
2049 return true;
2050}
2051
2052// Check whether an AND with Mask is suitable for a TEST UNDER MASK
2053// instruction and whether the CC value is descriptive enough to handle
2054// a comparison of type Opcode between the AND result and CmpVal.
2055// CCMask says which comparison result is being tested and BitSize is
2056// the number of bits in the operands. If TEST UNDER MASK can be used,
2057// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002058static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
2059 uint64_t Mask, uint64_t CmpVal,
2060 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002061 assert(Mask != 0 && "ANDs with zero should have been removed by now");
2062
Richard Sandiford030c1652013-09-13 09:09:50 +00002063 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
2064 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
2065 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
2066 return 0;
2067
Richard Sandiford113c8702013-09-03 15:38:35 +00002068 // Work out the masks for the lowest and highest bits.
2069 unsigned HighShift = 63 - countLeadingZeros(Mask);
2070 uint64_t High = uint64_t(1) << HighShift;
2071 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
2072
2073 // Signed ordered comparisons are effectively unsigned if the sign
2074 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002075 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00002076
2077 // Check for equality comparisons with 0, or the equivalent.
2078 if (CmpVal == 0) {
2079 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2080 return SystemZ::CCMASK_TM_ALL_0;
2081 if (CCMask == SystemZ::CCMASK_CMP_NE)
2082 return SystemZ::CCMASK_TM_SOME_1;
2083 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00002084 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002085 if (CCMask == SystemZ::CCMASK_CMP_LT)
2086 return SystemZ::CCMASK_TM_ALL_0;
2087 if (CCMask == SystemZ::CCMASK_CMP_GE)
2088 return SystemZ::CCMASK_TM_SOME_1;
2089 }
2090 if (EffectivelyUnsigned && CmpVal < Low) {
2091 if (CCMask == SystemZ::CCMASK_CMP_LE)
2092 return SystemZ::CCMASK_TM_ALL_0;
2093 if (CCMask == SystemZ::CCMASK_CMP_GT)
2094 return SystemZ::CCMASK_TM_SOME_1;
2095 }
2096
2097 // Check for equality comparisons with the mask, or the equivalent.
2098 if (CmpVal == Mask) {
2099 if (CCMask == SystemZ::CCMASK_CMP_EQ)
2100 return SystemZ::CCMASK_TM_ALL_1;
2101 if (CCMask == SystemZ::CCMASK_CMP_NE)
2102 return SystemZ::CCMASK_TM_SOME_0;
2103 }
2104 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
2105 if (CCMask == SystemZ::CCMASK_CMP_GT)
2106 return SystemZ::CCMASK_TM_ALL_1;
2107 if (CCMask == SystemZ::CCMASK_CMP_LE)
2108 return SystemZ::CCMASK_TM_SOME_0;
2109 }
2110 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
2111 if (CCMask == SystemZ::CCMASK_CMP_GE)
2112 return SystemZ::CCMASK_TM_ALL_1;
2113 if (CCMask == SystemZ::CCMASK_CMP_LT)
2114 return SystemZ::CCMASK_TM_SOME_0;
2115 }
2116
2117 // Check for ordered comparisons with the top bit.
2118 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
2119 if (CCMask == SystemZ::CCMASK_CMP_LE)
2120 return SystemZ::CCMASK_TM_MSB_0;
2121 if (CCMask == SystemZ::CCMASK_CMP_GT)
2122 return SystemZ::CCMASK_TM_MSB_1;
2123 }
2124 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
2125 if (CCMask == SystemZ::CCMASK_CMP_LT)
2126 return SystemZ::CCMASK_TM_MSB_0;
2127 if (CCMask == SystemZ::CCMASK_CMP_GE)
2128 return SystemZ::CCMASK_TM_MSB_1;
2129 }
2130
2131 // If there are just two bits, we can do equality checks for Low and High
2132 // as well.
2133 if (Mask == Low + High) {
2134 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
2135 return SystemZ::CCMASK_TM_MIXED_MSB_0;
2136 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
2137 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
2138 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
2139 return SystemZ::CCMASK_TM_MIXED_MSB_1;
2140 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
2141 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
2142 }
2143
2144 // Looks like we've exhausted our options.
2145 return 0;
2146}
2147
Richard Sandifordd420f732013-12-13 15:28:45 +00002148// See whether C can be implemented as a TEST UNDER MASK instruction.
2149// Update the arguments with the TM version if so.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002150static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
2151 Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00002152 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002153 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00002154 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00002155 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00002156 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00002157
2158 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002159 Comparison NewC(C);
2160 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00002161 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002162 if (C.Op0.getOpcode() == ISD::AND) {
2163 NewC.Op0 = C.Op0.getOperand(0);
2164 NewC.Op1 = C.Op0.getOperand(1);
2165 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
2166 if (!Mask)
2167 return;
2168 MaskVal = Mask->getZExtValue();
2169 } else {
2170 // There is no instruction to compare with a 64-bit immediate
2171 // so use TMHH instead if possible. We need an unsigned ordered
2172 // comparison with an i64 immediate.
2173 if (NewC.Op0.getValueType() != MVT::i64 ||
2174 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
2175 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
2176 NewC.ICmpType == SystemZICMP::SignedOnly)
2177 return;
2178 // Convert LE and GT comparisons into LT and GE.
2179 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
2180 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
2181 if (CmpVal == uint64_t(-1))
2182 return;
2183 CmpVal += 1;
2184 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
2185 }
2186 // If the low N bits of Op1 are zero than the low N bits of Op0 can
2187 // be masked off without changing the result.
2188 MaskVal = -(CmpVal & -CmpVal);
2189 NewC.ICmpType = SystemZICMP::UnsignedOnly;
2190 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00002191 if (!MaskVal)
2192 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002193
Richard Sandiford113c8702013-09-03 15:38:35 +00002194 // Check whether the combination of mask, comparison value and comparison
2195 // type are suitable.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00002196 unsigned BitSize = NewC.Op0.getValueSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002197 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002198 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2199 NewC.Op0.getOpcode() == ISD::SHL &&
2200 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002201 (MaskVal >> ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002202 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2203 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002204 CmpVal >> ShiftVal,
2205 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002206 NewC.Op0 = NewC.Op0.getOperand(0);
2207 MaskVal >>= ShiftVal;
2208 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2209 NewC.Op0.getOpcode() == ISD::SRL &&
2210 isSimpleShift(NewC.Op0, ShiftVal) &&
Jonas Paulsson8c336472017-06-26 13:38:27 +00002211 (MaskVal << ShiftVal != 0) &&
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002212 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002213 MaskVal << ShiftVal,
2214 CmpVal << ShiftVal,
2215 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002216 NewC.Op0 = NewC.Op0.getOperand(0);
2217 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002218 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002219 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2220 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002221 if (!NewCCMask)
2222 return;
2223 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002224
Richard Sandiford35b9be22013-08-28 10:31:43 +00002225 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002226 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002227 C.Op0 = NewC.Op0;
2228 if (Mask && Mask->getZExtValue() == MaskVal)
2229 C.Op1 = SDValue(Mask, 0);
2230 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002231 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002232 C.CCValid = SystemZ::CCMASK_TM;
2233 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002234}
2235
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002236// Return a Comparison that tests the condition-code result of intrinsic
2237// node Call against constant integer CC using comparison code Cond.
2238// Opcode is the opcode of the SystemZISD operation for the intrinsic
2239// and CCValid is the set of possible condition-code results.
2240static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2241 SDValue Call, unsigned CCValid, uint64_t CC,
2242 ISD::CondCode Cond) {
2243 Comparison C(Call, SDValue());
2244 C.Opcode = Opcode;
2245 C.CCValid = CCValid;
2246 if (Cond == ISD::SETEQ)
2247 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2248 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2249 else if (Cond == ISD::SETNE)
2250 // ...and the inverse of that.
2251 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2252 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2253 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2254 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002255 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002256 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2257 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002258 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002259 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2260 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2261 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002262 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002263 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2264 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002265 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002266 else
2267 llvm_unreachable("Unexpected integer comparison type");
2268 C.CCMask &= CCValid;
2269 return C;
2270}
2271
Richard Sandifordd420f732013-12-13 15:28:45 +00002272// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2273static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002274 ISD::CondCode Cond, const SDLoc &DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002275 if (CmpOp1.getOpcode() == ISD::Constant) {
2276 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2277 unsigned Opcode, CCValid;
2278 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2279 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2280 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2281 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002282 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2283 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2284 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2285 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002286 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002287 Comparison C(CmpOp0, CmpOp1);
2288 C.CCMask = CCMaskForCondCode(Cond);
2289 if (C.Op0.getValueType().isFloatingPoint()) {
2290 C.CCValid = SystemZ::CCMASK_FCMP;
2291 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002292 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002293 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002294 C.CCValid = SystemZ::CCMASK_ICMP;
2295 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002296 // Choose the type of comparison. Equality and inequality tests can
2297 // use either signed or unsigned comparisons. The choice also doesn't
2298 // matter if both sign bits are known to be clear. In those cases we
2299 // want to give the main isel code the freedom to choose whichever
2300 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002301 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2302 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2303 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2304 C.ICmpType = SystemZICMP::Any;
2305 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2306 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002307 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002308 C.ICmpType = SystemZICMP::SignedOnly;
2309 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002310 adjustZeroCmp(DAG, DL, C);
2311 adjustSubwordCmp(DAG, DL, C);
2312 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002313 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002314 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002315 }
2316
Richard Sandifordd420f732013-12-13 15:28:45 +00002317 if (shouldSwapCmpOperands(C)) {
2318 std::swap(C.Op0, C.Op1);
2319 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002320 }
2321
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002322 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002323 return C;
2324}
2325
2326// Emit the comparison instruction described by C.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002327static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002328 if (!C.Op1.getNode()) {
2329 SDValue Op;
2330 switch (C.Op0.getOpcode()) {
2331 case ISD::INTRINSIC_W_CHAIN:
2332 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2333 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002334 case ISD::INTRINSIC_WO_CHAIN:
2335 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2336 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002337 default:
2338 llvm_unreachable("Invalid comparison operands");
2339 }
2340 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2341 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002342 if (C.Opcode == SystemZISD::ICMP)
2343 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002344 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002345 if (C.Opcode == SystemZISD::TM) {
2346 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2347 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2348 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002349 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002350 }
2351 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002352}
2353
Richard Sandiford7d86e472013-08-21 09:34:56 +00002354// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2355// 64 bits. Extend is the extension type to use. Store the high part
2356// in Hi and the low part in Lo.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002357static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
2358 SDValue Op0, SDValue Op1, SDValue &Hi,
2359 SDValue &Lo) {
Richard Sandiford7d86e472013-08-21 09:34:56 +00002360 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2361 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2362 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002363 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2364 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002365 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2366 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2367}
2368
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002369// Lower a binary operation that produces two VT results, one in each
2370// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002371// and Opcode performs the GR128 operation. Store the even register result
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002372// in Even and the odd register result in Odd.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002373static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00002374 unsigned Opcode, SDValue Op0, SDValue Op1,
2375 SDValue &Even, SDValue &Odd) {
2376 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002377 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002378 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2379 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002380}
2381
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002382// Return an i32 value that is 1 if the CC value produced by Glue is
2383// in the mask CCMask and 0 otherwise. CC is known to have a value
2384// in CCValid, so other values can be ignored.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002385static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue Glue,
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002386 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002387 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2388 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2389
2390 if (Conversion.XORValue)
2391 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002392 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002393
2394 if (Conversion.AddValue)
2395 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002396 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002397
2398 // The SHR/AND sequence should get optimized to an RISBG.
2399 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002400 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002401 if (Conversion.Bit != 31)
2402 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002403 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002404 return Result;
2405}
2406
Ulrich Weigandcd808232015-05-05 19:26:48 +00002407// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2408// be done directly. IsFP is true if CC is for a floating-point rather than
2409// integer comparison.
2410static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002411 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002412 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002413 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002414 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002415
Ulrich Weigandcd808232015-05-05 19:26:48 +00002416 case ISD::SETOGE:
2417 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002418 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002419
2420 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002421 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002422 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002423
2424 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002425 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002426
2427 default:
2428 return 0;
2429 }
2430}
2431
2432// Return the SystemZISD vector comparison operation for CC or its inverse,
2433// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002434// result is for the inverse of CC. IsFP is true if CC is for a
2435// floating-point rather than integer comparison.
2436static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2437 bool &Invert) {
2438 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002439 Invert = false;
2440 return Opcode;
2441 }
2442
Ulrich Weigandcd808232015-05-05 19:26:48 +00002443 CC = ISD::getSetCCInverse(CC, !IsFP);
2444 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002445 Invert = true;
2446 return Opcode;
2447 }
2448
2449 return 0;
2450}
2451
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002452// Return a v2f64 that contains the extended form of elements Start and Start+1
2453// of v4f32 value Op.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002454static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002455 SDValue Op) {
2456 int Mask[] = { Start, -1, Start + 1, -1 };
2457 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2458 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2459}
2460
2461// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2462// producing a result of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002463SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
2464 const SDLoc &DL, EVT VT,
2465 SDValue CmpOp0,
2466 SDValue CmpOp1) const {
2467 // There is no hardware support for v4f32 (unless we have the vector
2468 // enhancements facility 1), so extend the vector into two v2f64s
2469 // and compare those.
2470 if (CmpOp0.getValueType() == MVT::v4f32 &&
2471 !Subtarget.hasVectorEnhancements1()) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002472 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2473 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2474 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2475 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2476 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2477 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2478 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2479 }
2480 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2481}
2482
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002483// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2484// an integer mask of type VT.
Ulrich Weigand33435c42017-07-17 17:42:48 +00002485SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
2486 const SDLoc &DL, EVT VT,
2487 ISD::CondCode CC,
2488 SDValue CmpOp0,
2489 SDValue CmpOp1) const {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002490 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002491 bool Invert = false;
2492 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002493 switch (CC) {
2494 // Handle tests for order using (or (ogt y x) (oge x y)).
2495 case ISD::SETUO:
2496 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002497 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002498 case ISD::SETO: {
2499 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002500 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2501 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002502 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2503 break;
2504 }
2505
2506 // Handle <> tests using (or (ogt y x) (ogt x y)).
2507 case ISD::SETUEQ:
2508 Invert = true;
Simon Pilgrim8c4069e2017-07-07 10:07:09 +00002509 LLVM_FALLTHROUGH;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002510 case ISD::SETONE: {
2511 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002512 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2513 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002514 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2515 break;
2516 }
2517
2518 // Otherwise a single comparison is enough. It doesn't really
2519 // matter whether we try the inversion or the swap first, since
2520 // there are no cases where both work.
2521 default:
2522 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002523 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002524 else {
2525 CC = ISD::getSetCCSwappedOperands(CC);
2526 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002527 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002528 else
2529 llvm_unreachable("Unhandled comparison");
2530 }
2531 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002532 }
2533 if (Invert) {
2534 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2535 DAG.getConstant(65535, DL, MVT::i32));
2536 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2537 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2538 }
2539 return Cmp;
2540}
2541
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002542SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2543 SelectionDAG &DAG) const {
2544 SDValue CmpOp0 = Op.getOperand(0);
2545 SDValue CmpOp1 = Op.getOperand(1);
2546 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2547 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002548 EVT VT = Op.getValueType();
2549 if (VT.isVector())
2550 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002551
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002552 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002553 SDValue Glue = emitCmp(DAG, DL, C);
2554 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002555}
2556
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002557SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002558 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2559 SDValue CmpOp0 = Op.getOperand(2);
2560 SDValue CmpOp1 = Op.getOperand(3);
2561 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002562 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002563
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002564 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002565 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002566 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002567 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2568 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002569}
2570
Richard Sandiford57485472013-12-13 15:35:00 +00002571// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2572// allowing Pos and Neg to be wider than CmpOp.
2573static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2574 return (Neg.getOpcode() == ISD::SUB &&
2575 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2576 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2577 Neg.getOperand(1) == Pos &&
2578 (Pos == CmpOp ||
2579 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2580 Pos.getOperand(0) == CmpOp)));
2581}
2582
2583// Return the absolute or negative absolute of Op; IsNegative decides which.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002584static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
Richard Sandiford57485472013-12-13 15:35:00 +00002585 bool IsNegative) {
2586 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2587 if (IsNegative)
2588 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002589 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002590 return Op;
2591}
2592
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002593SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2594 SelectionDAG &DAG) const {
2595 SDValue CmpOp0 = Op.getOperand(0);
2596 SDValue CmpOp1 = Op.getOperand(1);
2597 SDValue TrueOp = Op.getOperand(2);
2598 SDValue FalseOp = Op.getOperand(3);
2599 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002600 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002601
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002602 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002603
2604 // Check for absolute and negative-absolute selections, including those
2605 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2606 // This check supplements the one in DAGCombiner.
2607 if (C.Opcode == SystemZISD::ICMP &&
2608 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2609 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2610 C.Op1.getOpcode() == ISD::Constant &&
2611 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2612 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2613 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2614 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2615 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2616 }
2617
Richard Sandifordd420f732013-12-13 15:28:45 +00002618 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002619
2620 // Special case for handling -1/0 results. The shifts we use here
2621 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002622 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2623 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002624 if (TrueC && FalseC) {
2625 int64_t TrueVal = TrueC->getSExtValue();
2626 int64_t FalseVal = FalseC->getSExtValue();
2627 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2628 // Invert the condition if we want -1 on false.
2629 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002630 C.CCMask ^= C.CCValid;
2631 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002632 EVT VT = Op.getValueType();
2633 // Extend the result to VT. Upper bits are ignored.
2634 if (!is32Bit(VT))
2635 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2636 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002637 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002638 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2639 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2640 }
2641 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002642
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002643 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2644 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002645
2646 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002647 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002648}
2649
2650SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2651 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002652 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002653 const GlobalValue *GV = Node->getGlobal();
2654 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002655 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002656 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002657
2658 SDValue Result;
Rafael Espindola3beef8d2016-06-27 23:15:57 +00002659 if (Subtarget.isPC32DBLSymbol(GV, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002660 // Assign anchors at 1<<12 byte boundaries.
2661 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2662 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2663 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2664
2665 // The offset can be folded into the address if it is aligned to a halfword.
2666 Offset -= Anchor;
2667 if (Offset != 0 && (Offset & 1) == 0) {
2668 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2669 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002670 Offset = 0;
2671 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002672 } else {
2673 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2674 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2675 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Justin Lebar9c375812016-07-15 18:27:10 +00002676 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002677 }
2678
2679 // If there was a non-zero offset that we didn't fold, create an explicit
2680 // addition for it.
2681 if (Offset != 0)
2682 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002683 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002684
2685 return Result;
2686}
2687
Ulrich Weigand7db69182015-02-18 09:13:27 +00002688SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2689 SelectionDAG &DAG,
2690 unsigned Opcode,
2691 SDValue GOTOffset) const {
2692 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002693 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002694 SDValue Chain = DAG.getEntryNode();
2695 SDValue Glue;
2696
2697 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2698 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2699 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2700 Glue = Chain.getValue(1);
2701 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2702 Glue = Chain.getValue(1);
2703
2704 // The first call operand is the chain and the second is the TLS symbol.
2705 SmallVector<SDValue, 8> Ops;
2706 Ops.push_back(Chain);
2707 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2708 Node->getValueType(0),
2709 0, 0));
2710
2711 // Add argument registers to the end of the list so that they are
2712 // known live into the call.
2713 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2714 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2715
2716 // Add a register mask operand representing the call-preserved registers.
2717 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002718 const uint32_t *Mask =
2719 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002720 assert(Mask && "Missing call preserved mask for calling convention");
2721 Ops.push_back(DAG.getRegisterMask(Mask));
2722
2723 // Glue the call to the argument copies.
2724 Ops.push_back(Glue);
2725
2726 // Emit the call.
2727 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2728 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2729 Glue = Chain.getValue(1);
2730
2731 // Copy the return value from %r2.
2732 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2733}
2734
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002735SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2736 SelectionDAG &DAG) const {
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002737 SDValue Chain = DAG.getEntryNode();
Mehdi Amini44ede332015-07-09 02:09:04 +00002738 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002739
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002740 // The high part of the thread pointer is in access register 0.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002741 SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002742 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2743
2744 // The low part of the thread pointer is in access register 1.
Ulrich Weigandfffc7112016-11-08 20:15:26 +00002745 SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002746 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2747
2748 // Merge them into a single 64-bit address.
2749 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002750 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002751 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2752}
2753
2754SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2755 SelectionDAG &DAG) const {
2756 if (DAG.getTarget().Options.EmulatedTLS)
2757 return LowerToTLSEmulatedModel(Node, DAG);
2758 SDLoc DL(Node);
2759 const GlobalValue *GV = Node->getGlobal();
2760 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2761 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2762
2763 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002764
Ulrich Weigand7db69182015-02-18 09:13:27 +00002765 // Get the offset of GA from the thread pointer, based on the TLS model.
2766 SDValue Offset;
2767 switch (model) {
2768 case TLSModel::GeneralDynamic: {
2769 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2770 SystemZConstantPoolValue *CPV =
2771 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002772
Ulrich Weigand7db69182015-02-18 09:13:27 +00002773 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002774 Offset = DAG.getLoad(
2775 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002776 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002777
2778 // Call __tls_get_offset to retrieve the offset.
2779 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2780 break;
2781 }
2782
2783 case TLSModel::LocalDynamic: {
2784 // Load the GOT offset of the module ID.
2785 SystemZConstantPoolValue *CPV =
2786 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2787
2788 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002789 Offset = DAG.getLoad(
2790 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002791 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002792
2793 // Call __tls_get_offset to retrieve the module base offset.
2794 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2795
2796 // Note: The SystemZLDCleanupPass will remove redundant computations
2797 // of the module base offset. Count total number of local-dynamic
2798 // accesses to trigger execution of that pass.
2799 SystemZMachineFunctionInfo* MFI =
2800 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2801 MFI->incNumLocalDynamicTLSAccesses();
2802
2803 // Add the per-symbol offset.
2804 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2805
2806 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002807 DTPOffset = DAG.getLoad(
2808 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
Justin Lebar9c375812016-07-15 18:27:10 +00002809 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002810
2811 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2812 break;
2813 }
2814
2815 case TLSModel::InitialExec: {
2816 // Load the offset from the GOT.
2817 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2818 SystemZII::MO_INDNTPOFF);
2819 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +00002820 Offset =
2821 DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2822 MachinePointerInfo::getGOT(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002823 break;
2824 }
2825
2826 case TLSModel::LocalExec: {
2827 // Force the offset into the constant pool and load it from there.
2828 SystemZConstantPoolValue *CPV =
2829 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2830
2831 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002832 Offset = DAG.getLoad(
2833 PtrVT, DL, DAG.getEntryNode(), Offset,
Justin Lebar9c375812016-07-15 18:27:10 +00002834 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
Ulrich Weigand7db69182015-02-18 09:13:27 +00002835 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002836 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002837 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002838
2839 // Add the base and offset together.
2840 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2841}
2842
2843SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2844 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002845 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002846 const BlockAddress *BA = Node->getBlockAddress();
2847 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002848 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002849
2850 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2851 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2852 return Result;
2853}
2854
2855SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2856 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002857 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002858 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002859 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2860
2861 // Use LARL to load the address of the table.
2862 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2863}
2864
2865SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2866 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002867 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002868 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002869
2870 SDValue Result;
2871 if (CP->isMachineConstantPoolEntry())
2872 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002873 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002874 else
2875 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002876 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002877
2878 // Use LARL to load the address of the constant pool entry.
2879 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2880}
2881
Ulrich Weigandf557d082016-04-04 12:44:55 +00002882SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2883 SelectionDAG &DAG) const {
2884 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002885 MachineFrameInfo &MFI = MF.getFrameInfo();
2886 MFI.setFrameAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002887
2888 SDLoc DL(Op);
2889 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2890 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2891
2892 // If the back chain frame index has not been allocated yet, do so.
2893 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2894 int BackChainIdx = FI->getFramePointerSaveIndex();
2895 if (!BackChainIdx) {
2896 // By definition, the frame address is the address of the back chain.
Matthias Braun941a7052016-07-28 18:40:00 +00002897 BackChainIdx = MFI.CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002898 FI->setFramePointerSaveIndex(BackChainIdx);
2899 }
2900 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2901
2902 // FIXME The frontend should detect this case.
2903 if (Depth > 0) {
2904 report_fatal_error("Unsupported stack frame traversal count");
2905 }
2906
2907 return BackChain;
2908}
2909
2910SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2911 SelectionDAG &DAG) const {
2912 MachineFunction &MF = DAG.getMachineFunction();
Matthias Braun941a7052016-07-28 18:40:00 +00002913 MachineFrameInfo &MFI = MF.getFrameInfo();
2914 MFI.setReturnAddressIsTaken(true);
Ulrich Weigandf557d082016-04-04 12:44:55 +00002915
2916 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2917 return SDValue();
2918
2919 SDLoc DL(Op);
2920 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2921 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2922
2923 // FIXME The frontend should detect this case.
2924 if (Depth > 0) {
2925 report_fatal_error("Unsupported stack frame traversal count");
2926 }
2927
2928 // Return R14D, which has the return address. Mark it an implicit live-in.
2929 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2930 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2931}
2932
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002933SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2934 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002935 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002936 SDValue In = Op.getOperand(0);
2937 EVT InVT = In.getValueType();
2938 EVT ResVT = Op.getValueType();
2939
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002940 // Convert loads directly. This is normally done by DAGCombiner,
2941 // but we need this case for bitcasts that are created during lowering
2942 // and which are then lowered themselves.
2943 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
Nirav Daveaa65a2b2017-04-05 15:42:48 +00002944 if (ISD::isNormalLoad(LoadN))
2945 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2946 LoadN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002947
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002948 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002949 SDValue In64;
2950 if (Subtarget.hasHighWord()) {
2951 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2952 MVT::i64);
2953 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2954 MVT::i64, SDValue(U64, 0), In);
2955 } else {
2956 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2957 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002958 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002959 }
2960 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002961 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002962 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002963 }
2964 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2965 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002966 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002967 MVT::f64, SDValue(U64, 0), In);
2968 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002969 if (Subtarget.hasHighWord())
2970 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2971 MVT::i32, Out64);
2972 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002973 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002974 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002975 }
2976 llvm_unreachable("Unexpected bitcast combination");
2977}
2978
2979SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2980 SelectionDAG &DAG) const {
2981 MachineFunction &MF = DAG.getMachineFunction();
2982 SystemZMachineFunctionInfo *FuncInfo =
2983 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002984 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002985
2986 SDValue Chain = Op.getOperand(0);
2987 SDValue Addr = Op.getOperand(1);
2988 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002989 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002990
2991 // The initial values of each field.
2992 const unsigned NumFields = 4;
2993 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002994 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2995 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002996 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2997 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2998 };
2999
3000 // Store each field into its respective slot.
3001 SDValue MemOps[NumFields];
3002 unsigned Offset = 0;
3003 for (unsigned I = 0; I < NumFields; ++I) {
3004 SDValue FieldAddr = Addr;
3005 if (Offset != 0)
3006 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003007 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003008 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
Justin Lebar9c375812016-07-15 18:27:10 +00003009 MachinePointerInfo(SV, Offset));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003010 Offset += 8;
3011 }
Craig Topper48d114b2014-04-26 18:35:24 +00003012 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003013}
3014
3015SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
3016 SelectionDAG &DAG) const {
3017 SDValue Chain = Op.getOperand(0);
3018 SDValue DstPtr = Op.getOperand(1);
3019 SDValue SrcPtr = Op.getOperand(2);
3020 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
3021 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003022 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003023
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003024 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003025 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00003026 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003027 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
3028}
3029
3030SDValue SystemZTargetLowering::
3031lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003032 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003033 MachineFunction &MF = DAG.getMachineFunction();
3034 bool RealignOpt = !MF.getFunction()-> hasFnAttribute("no-realign-stack");
3035 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003036
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003037 SDValue Chain = Op.getOperand(0);
3038 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003039 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003040 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003041
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003042 // If user has set the no alignment function attribute, ignore
3043 // alloca alignments.
3044 uint64_t AlignVal = (RealignOpt ?
3045 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
3046
3047 uint64_t StackAlign = TFI->getStackAlignment();
3048 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
3049 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
3050
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003051 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003052 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003053
3054 // Get a reference to the stack pointer.
3055 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
3056
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003057 // If we need a backchain, save it now.
3058 SDValue Backchain;
3059 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003060 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003061
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003062 // Add extra space for alignment if needed.
3063 if (ExtraAlignSpace)
3064 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
Elliot Colpbc2cfc22016-07-06 18:13:11 +00003065 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003066
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003067 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003068 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003069
3070 // Copy the new stack pointer back.
3071 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
3072
3073 // The allocated data lives above the 160 bytes allocated for the standard
3074 // frame, plus any outgoing stack arguments. We don't know how much that
3075 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
3076 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3077 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
3078
Jonas Paulssonf12b9252015-11-28 11:02:32 +00003079 // Dynamically realign if needed.
3080 if (RequiredAlign > StackAlign) {
3081 Result =
3082 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
3083 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
3084 Result =
3085 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
3086 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
3087 }
3088
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003089 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003090 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003091
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003092 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00003093 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003094}
3095
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00003096SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
3097 SDValue Op, SelectionDAG &DAG) const {
3098 SDLoc DL(Op);
3099
3100 return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
3101}
3102
Richard Sandiford7d86e472013-08-21 09:34:56 +00003103SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
3104 SelectionDAG &DAG) const {
3105 EVT VT = Op.getValueType();
3106 SDLoc DL(Op);
3107 SDValue Ops[2];
3108 if (is32Bit(VT))
3109 // Just do a normal 64-bit multiplication and extract the results.
3110 // We define this so that it can be used for constant division.
3111 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
3112 Op.getOperand(1), Ops[1], Ops[0]);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00003113 else if (Subtarget.hasMiscellaneousExtensions2())
3114 // SystemZISD::SMUL_LOHI returns the low result in the odd register and
3115 // the high result in the even register. ISD::SMUL_LOHI is defined to
3116 // return the low half first, so the results are in reverse order.
3117 lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
3118 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003119 else {
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003120 // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
Richard Sandiford7d86e472013-08-21 09:34:56 +00003121 //
3122 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
3123 //
3124 // but using the fact that the upper halves are either all zeros
3125 // or all ones:
3126 //
3127 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
3128 //
3129 // and grouping the right terms together since they are quicker than the
3130 // multiplication:
3131 //
3132 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003133 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003134 SDValue LL = Op.getOperand(0);
3135 SDValue RL = Op.getOperand(1);
3136 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
3137 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003138 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3139 // the high result in the even register. ISD::SMUL_LOHI is defined to
3140 // return the low half first, so the results are in reverse order.
3141 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003142 LL, RL, Ops[1], Ops[0]);
3143 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
3144 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
3145 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
3146 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
3147 }
Craig Topper64941d92014-04-27 19:20:57 +00003148 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00003149}
3150
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003151SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
3152 SelectionDAG &DAG) const {
3153 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003154 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003155 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00003156 if (is32Bit(VT))
3157 // Just do a normal 64-bit multiplication and extract the results.
3158 // We define this so that it can be used for constant division.
3159 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
3160 Op.getOperand(1), Ops[1], Ops[0]);
3161 else
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003162 // SystemZISD::UMUL_LOHI returns the low result in the odd register and
3163 // the high result in the even register. ISD::UMUL_LOHI is defined to
3164 // return the low half first, so the results are in reverse order.
3165 lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
Richard Sandiford7d86e472013-08-21 09:34:56 +00003166 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003167 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003168}
3169
3170SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
3171 SelectionDAG &DAG) const {
3172 SDValue Op0 = Op.getOperand(0);
3173 SDValue Op1 = Op.getOperand(1);
3174 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003175 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003176
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003177 // We use DSGF for 32-bit division. This means the first operand must
3178 // always be 64-bit, and the second operand should be 32-bit whenever
3179 // that is possible, to improve performance.
3180 if (is32Bit(VT))
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003181 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003182 else if (DAG.ComputeNumSignBits(Op1) > 32)
Richard Sandiforde6e78852013-07-02 15:40:22 +00003183 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003184
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003185 // DSG(F) returns the remainder in the even register and the
3186 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003187 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003188 lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003189 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003190}
3191
3192SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
3193 SelectionDAG &DAG) const {
3194 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003195 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003196
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003197 // DL(G) returns the remainder in the even register and the
3198 // quotient in the odd register.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003199 SDValue Ops[2];
Ulrich Weigand43579cf2017-07-05 13:17:31 +00003200 lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
3201 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00003202 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003203}
3204
3205SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3206 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3207
3208 // Get the known-zero masks for each operand.
3209 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
Craig Topperd0af7e82017-04-28 05:31:46 +00003210 KnownBits Known[2];
3211 DAG.computeKnownBits(Ops[0], Known[0]);
3212 DAG.computeKnownBits(Ops[1], Known[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003213
3214 // See if the upper 32 bits of one operand and the lower 32 bits of the
3215 // other are known zero. They are the low and high operands respectively.
Craig Topperd0af7e82017-04-28 05:31:46 +00003216 uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
3217 Known[1].Zero.getZExtValue() };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003218 unsigned High, Low;
3219 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3220 High = 1, Low = 0;
3221 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3222 High = 0, Low = 1;
3223 else
3224 return Op;
3225
3226 SDValue LowOp = Ops[Low];
3227 SDValue HighOp = Ops[High];
3228
3229 // If the high part is a constant, we're better off using IILH.
3230 if (HighOp.getOpcode() == ISD::Constant)
3231 return Op;
3232
3233 // If the low part is a constant that is outside the range of LHI,
3234 // then we're better off using IILF.
3235 if (LowOp.getOpcode() == ISD::Constant) {
3236 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3237 if (!isInt<16>(Value))
3238 return Op;
3239 }
3240
3241 // Check whether the high part is an AND that doesn't change the
3242 // high 32 bits and just masks out low bits. We can skip it if so.
3243 if (HighOp.getOpcode() == ISD::AND &&
3244 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003245 SDValue HighOp0 = HighOp.getOperand(0);
3246 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3247 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3248 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003249 }
3250
3251 // Take advantage of the fact that all GR32 operations only change the
3252 // low 32 bits by truncating Low to an i32 and inserting it directly
3253 // using a subreg. The interesting cases are those where the truncation
3254 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003255 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003256 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003257 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003258 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003259}
3260
Ulrich Weigandb4012182015-03-31 12:56:33 +00003261SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3262 SelectionDAG &DAG) const {
3263 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003264 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003265 Op = Op.getOperand(0);
3266
3267 // Handle vector types via VPOPCT.
3268 if (VT.isVector()) {
3269 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3270 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00003271 switch (VT.getScalarSizeInBits()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003272 case 8:
3273 break;
3274 case 16: {
3275 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3276 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3277 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3278 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3279 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3280 break;
3281 }
3282 case 32: {
3283 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3284 DAG.getConstant(0, DL, MVT::i32));
3285 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3286 break;
3287 }
3288 case 64: {
3289 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3290 DAG.getConstant(0, DL, MVT::i32));
3291 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3292 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3293 break;
3294 }
3295 default:
3296 llvm_unreachable("Unexpected type");
3297 }
3298 return Op;
3299 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003300
3301 // Get the known-zero mask for the operand.
Craig Topperd0af7e82017-04-28 05:31:46 +00003302 KnownBits Known;
3303 DAG.computeKnownBits(Op, Known);
3304 unsigned NumSignificantBits = (~Known.Zero).getActiveBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003305 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003306 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003307
3308 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003309 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003310 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3311 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003312
3313 // The POPCNT instruction counts the number of bits in each byte.
3314 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3315 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3316 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3317
3318 // Add up per-byte counts in a binary tree. All bits of Op at
3319 // position larger than BitSize remain zero throughout.
3320 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003321 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003322 if (BitSize != OrigBitSize)
3323 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003325 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3326 }
3327
3328 // Extract overall result from high byte.
3329 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003330 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3331 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003332
3333 return Op;
3334}
3335
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003336SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3337 SelectionDAG &DAG) const {
3338 SDLoc DL(Op);
3339 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3340 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003341 SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003342 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3343
3344 // The only fence that needs an instruction is a sequentially-consistent
3345 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003346 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00003347 FenceSSID == SyncScope::System) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003348 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003349 Op.getOperand(0)),
3350 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003351 }
3352
3353 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3354 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3355}
3356
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003357// Op is an atomic load. Lower it into a normal volatile load.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003358SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3359 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003360 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003361 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003362 Node->getChain(), Node->getBasePtr(),
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003363 Node->getMemoryVT(), Node->getMemOperand());
3364}
3365
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003366// Op is an atomic store. Lower it into a normal volatile store.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003367SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3368 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003369 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003370 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3371 Node->getBasePtr(), Node->getMemoryVT(),
3372 Node->getMemOperand());
Ulrich Weigand02f1c022017-08-04 18:53:35 +00003373 // We have to enforce sequential consistency by performing a
3374 // serialization operation after the store.
3375 if (Node->getOrdering() == AtomicOrdering::SequentiallyConsistent)
3376 Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
3377 MVT::Other, Chain), 0);
3378 return Chain;
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003379}
3380
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003381// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3382// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003383SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3384 SelectionDAG &DAG,
3385 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003386 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003387
3388 // 32-bit operations need no code outside the main loop.
3389 EVT NarrowVT = Node->getMemoryVT();
3390 EVT WideVT = MVT::i32;
3391 if (NarrowVT == WideVT)
3392 return Op;
3393
3394 int64_t BitSize = NarrowVT.getSizeInBits();
3395 SDValue ChainIn = Node->getChain();
3396 SDValue Addr = Node->getBasePtr();
3397 SDValue Src2 = Node->getVal();
3398 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003399 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003400 EVT PtrVT = Addr.getValueType();
3401
3402 // Convert atomic subtracts of constants into additions.
3403 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003404 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003405 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003406 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003407 }
3408
3409 // Get the address of the containing word.
3410 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003412
3413 // Get the number of bits that the word must be rotated left in order
3414 // to bring the field to the top bits of a GR32.
3415 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003416 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003417 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3418
3419 // Get the complementing shift amount, for rotating a field in the top
3420 // bits back to its proper position.
3421 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003422 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003423
3424 // Extend the source operand to 32 bits and prepare it for the inner loop.
3425 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3426 // operations require the source to be shifted in advance. (This shift
3427 // can be folded if the source is constant.) For AND and NAND, the lower
3428 // bits must be set, while for other opcodes they should be left clear.
3429 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3430 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003431 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003432 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3433 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3434 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003435 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003436
3437 // Construct the ATOMIC_LOADW_* node.
3438 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3439 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003440 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003441 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003442 NarrowVT, MMO);
3443
3444 // Rotate the result of the final CS so that the field is in the lower
3445 // bits of a GR32, then truncate it.
3446 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003447 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003448 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3449
3450 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003451 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003452}
3453
Richard Sandiford41350a52013-12-24 15:18:04 +00003454// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003455// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003456// operations into additions.
3457SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3458 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003459 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003460 EVT MemVT = Node->getMemoryVT();
3461 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3462 // A full-width operation.
3463 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3464 SDValue Src2 = Node->getVal();
3465 SDValue NegSrc2;
3466 SDLoc DL(Src2);
3467
Richard Sandiford21f5d682014-03-06 11:22:58 +00003468 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003469 // Use an addition if the operand is constant and either LAA(G) is
3470 // available or the negative value is in the range of A(G)FHI.
3471 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003472 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003473 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003474 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003475 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003476 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003477 Src2);
3478
3479 if (NegSrc2.getNode())
3480 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3481 Node->getChain(), Node->getBasePtr(), NegSrc2,
Konstantin Zhuravlyov8ea02462016-10-15 22:01:18 +00003482 Node->getMemOperand());
Richard Sandiford41350a52013-12-24 15:18:04 +00003483
3484 // Use the node as-is.
3485 return Op;
3486 }
3487
3488 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3489}
3490
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003491// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3492// into a fullword ATOMIC_CMP_SWAPW operation.
3493SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3494 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003495 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003496
3497 // We have native support for 32-bit compare and swap.
3498 EVT NarrowVT = Node->getMemoryVT();
3499 EVT WideVT = MVT::i32;
3500 if (NarrowVT == WideVT)
3501 return Op;
3502
3503 int64_t BitSize = NarrowVT.getSizeInBits();
3504 SDValue ChainIn = Node->getOperand(0);
3505 SDValue Addr = Node->getOperand(1);
3506 SDValue CmpVal = Node->getOperand(2);
3507 SDValue SwapVal = Node->getOperand(3);
3508 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003509 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003510 EVT PtrVT = Addr.getValueType();
3511
3512 // Get the address of the containing word.
3513 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003514 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003515
3516 // Get the number of bits that the word must be rotated left in order
3517 // to bring the field to the top bits of a GR32.
3518 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003519 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003520 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3521
3522 // Get the complementing shift amount, for rotating a field in the top
3523 // bits back to its proper position.
3524 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003525 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003526
3527 // Construct the ATOMIC_CMP_SWAPW node.
3528 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3529 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003530 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003531 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003532 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003533 return AtomicOp;
3534}
3535
3536SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3537 SelectionDAG &DAG) const {
3538 MachineFunction &MF = DAG.getMachineFunction();
3539 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003540 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003541 SystemZ::R15D, Op.getValueType());
3542}
3543
3544SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3545 SelectionDAG &DAG) const {
3546 MachineFunction &MF = DAG.getMachineFunction();
3547 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003548 bool StoreBackchain = MF.getFunction()->hasFnAttribute("backchain");
3549
3550 SDValue Chain = Op.getOperand(0);
3551 SDValue NewSP = Op.getOperand(1);
3552 SDValue Backchain;
3553 SDLoc DL(Op);
3554
3555 if (StoreBackchain) {
3556 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, MVT::i64);
Justin Lebar9c375812016-07-15 18:27:10 +00003557 Backchain = DAG.getLoad(MVT::i64, DL, Chain, OldSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003558 }
3559
3560 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R15D, NewSP);
3561
3562 if (StoreBackchain)
Justin Lebar9c375812016-07-15 18:27:10 +00003563 Chain = DAG.getStore(Chain, DL, Backchain, NewSP, MachinePointerInfo());
Marcin Koscielnickiad1482c2016-05-05 00:37:30 +00003564
3565 return Chain;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003566}
3567
Richard Sandiford03481332013-08-23 11:36:42 +00003568SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3569 SelectionDAG &DAG) const {
3570 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3571 if (!IsData)
3572 // Just preserve the chain.
3573 return Op.getOperand(0);
3574
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003575 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003576 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3577 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003578 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003579 SDValue Ops[] = {
3580 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003581 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003582 Op.getOperand(1)
3583 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003584 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003585 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003586 Node->getMemoryVT(), Node->getMemOperand());
3587}
3588
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003589// Return an i32 that contains the value of CC immediately after After,
3590// whose final operand must be MVT::Glue.
3591static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003592 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003593 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003594 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3595 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3596 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003597}
3598
3599SDValue
3600SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3601 SelectionDAG &DAG) const {
3602 unsigned Opcode, CCValid;
3603 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3604 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3605 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3606 SDValue CC = getCCResult(DAG, Glued.getNode());
3607 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3608 return SDValue();
3609 }
3610
3611 return SDValue();
3612}
3613
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003614SDValue
3615SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3616 SelectionDAG &DAG) const {
3617 unsigned Opcode, CCValid;
3618 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3619 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3620 SDValue CC = getCCResult(DAG, Glued.getNode());
3621 if (Op->getNumValues() == 1)
3622 return CC;
3623 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003624 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3625 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003626 }
3627
3628 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3629 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003630 case Intrinsic::thread_pointer:
3631 return lowerThreadPointer(SDLoc(Op), DAG);
3632
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003633 case Intrinsic::s390_vpdi:
3634 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3635 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3636
3637 case Intrinsic::s390_vperm:
3638 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3639 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3640
3641 case Intrinsic::s390_vuphb:
3642 case Intrinsic::s390_vuphh:
3643 case Intrinsic::s390_vuphf:
3644 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3645 Op.getOperand(1));
3646
3647 case Intrinsic::s390_vuplhb:
3648 case Intrinsic::s390_vuplhh:
3649 case Intrinsic::s390_vuplhf:
3650 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3651 Op.getOperand(1));
3652
3653 case Intrinsic::s390_vuplb:
3654 case Intrinsic::s390_vuplhw:
3655 case Intrinsic::s390_vuplf:
3656 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3657 Op.getOperand(1));
3658
3659 case Intrinsic::s390_vupllb:
3660 case Intrinsic::s390_vupllh:
3661 case Intrinsic::s390_vupllf:
3662 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3663 Op.getOperand(1));
3664
3665 case Intrinsic::s390_vsumb:
3666 case Intrinsic::s390_vsumh:
3667 case Intrinsic::s390_vsumgh:
3668 case Intrinsic::s390_vsumgf:
3669 case Intrinsic::s390_vsumqf:
3670 case Intrinsic::s390_vsumqg:
3671 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3672 Op.getOperand(1), Op.getOperand(2));
3673 }
3674
3675 return SDValue();
3676}
3677
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003678namespace {
3679// Says that SystemZISD operation Opcode can be used to perform the equivalent
3680// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3681// Operand is the constant third operand, otherwise it is the number of
3682// bytes in each element of the result.
3683struct Permute {
3684 unsigned Opcode;
3685 unsigned Operand;
3686 unsigned char Bytes[SystemZ::VectorBytes];
3687};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003688}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003689
3690static const Permute PermuteForms[] = {
3691 // VMRHG
3692 { SystemZISD::MERGE_HIGH, 8,
3693 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3694 // VMRHF
3695 { SystemZISD::MERGE_HIGH, 4,
3696 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3697 // VMRHH
3698 { SystemZISD::MERGE_HIGH, 2,
3699 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3700 // VMRHB
3701 { SystemZISD::MERGE_HIGH, 1,
3702 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3703 // VMRLG
3704 { SystemZISD::MERGE_LOW, 8,
3705 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3706 // VMRLF
3707 { SystemZISD::MERGE_LOW, 4,
3708 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3709 // VMRLH
3710 { SystemZISD::MERGE_LOW, 2,
3711 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3712 // VMRLB
3713 { SystemZISD::MERGE_LOW, 1,
3714 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3715 // VPKG
3716 { SystemZISD::PACK, 4,
3717 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3718 // VPKF
3719 { SystemZISD::PACK, 2,
3720 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3721 // VPKH
3722 { SystemZISD::PACK, 1,
3723 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3724 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3725 { SystemZISD::PERMUTE_DWORDS, 4,
3726 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3727 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3728 { SystemZISD::PERMUTE_DWORDS, 1,
3729 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3730};
3731
3732// Called after matching a vector shuffle against a particular pattern.
3733// Both the original shuffle and the pattern have two vector operands.
3734// OpNos[0] is the operand of the original shuffle that should be used for
3735// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3736// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3737// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3738// for operands 0 and 1 of the pattern.
3739static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3740 if (OpNos[0] < 0) {
3741 if (OpNos[1] < 0)
3742 return false;
3743 OpNo0 = OpNo1 = OpNos[1];
3744 } else if (OpNos[1] < 0) {
3745 OpNo0 = OpNo1 = OpNos[0];
3746 } else {
3747 OpNo0 = OpNos[0];
3748 OpNo1 = OpNos[1];
3749 }
3750 return true;
3751}
3752
3753// Bytes is a VPERM-like permute vector, except that -1 is used for
3754// undefined bytes. Return true if the VPERM can be implemented using P.
3755// When returning true set OpNo0 to the VPERM operand that should be
3756// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3757//
3758// For example, if swapping the VPERM operands allows P to match, OpNo0
3759// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3760// operand, but rewriting it to use two duplicated operands allows it to
3761// match P, then OpNo0 and OpNo1 will be the same.
3762static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3763 unsigned &OpNo0, unsigned &OpNo1) {
3764 int OpNos[] = { -1, -1 };
3765 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3766 int Elt = Bytes[I];
3767 if (Elt >= 0) {
3768 // Make sure that the two permute vectors use the same suboperand
3769 // byte number. Only the operand numbers (the high bits) are
3770 // allowed to differ.
3771 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3772 return false;
3773 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3774 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3775 // Make sure that the operand mappings are consistent with previous
3776 // elements.
3777 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3778 return false;
3779 OpNos[ModelOpNo] = RealOpNo;
3780 }
3781 }
3782 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3783}
3784
3785// As above, but search for a matching permute.
3786static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3787 unsigned &OpNo0, unsigned &OpNo1) {
3788 for (auto &P : PermuteForms)
3789 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3790 return &P;
3791 return nullptr;
3792}
3793
3794// Bytes is a VPERM-like permute vector, except that -1 is used for
3795// undefined bytes. This permute is an operand of an outer permute.
3796// See whether redistributing the -1 bytes gives a shuffle that can be
3797// implemented using P. If so, set Transform to a VPERM-like permute vector
3798// that, when applied to the result of P, gives the original permute in Bytes.
3799static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3800 const Permute &P,
3801 SmallVectorImpl<int> &Transform) {
3802 unsigned To = 0;
3803 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3804 int Elt = Bytes[From];
3805 if (Elt < 0)
3806 // Byte number From of the result is undefined.
3807 Transform[From] = -1;
3808 else {
3809 while (P.Bytes[To] != Elt) {
3810 To += 1;
3811 if (To == SystemZ::VectorBytes)
3812 return false;
3813 }
3814 Transform[From] = To;
3815 }
3816 }
3817 return true;
3818}
3819
3820// As above, but search for a matching permute.
3821static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3822 SmallVectorImpl<int> &Transform) {
3823 for (auto &P : PermuteForms)
3824 if (matchDoublePermute(Bytes, P, Transform))
3825 return &P;
3826 return nullptr;
3827}
3828
3829// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3830// as if it had type vNi8.
3831static void getVPermMask(ShuffleVectorSDNode *VSN,
3832 SmallVectorImpl<int> &Bytes) {
3833 EVT VT = VSN->getValueType(0);
3834 unsigned NumElements = VT.getVectorNumElements();
3835 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3836 Bytes.resize(NumElements * BytesPerElement, -1);
3837 for (unsigned I = 0; I < NumElements; ++I) {
3838 int Index = VSN->getMaskElt(I);
3839 if (Index >= 0)
3840 for (unsigned J = 0; J < BytesPerElement; ++J)
3841 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3842 }
3843}
3844
3845// Bytes is a VPERM-like permute vector, except that -1 is used for
3846// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3847// the result come from a contiguous sequence of bytes from one input.
3848// Set Base to the selector for the first byte if so.
3849static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3850 unsigned BytesPerElement, int &Base) {
3851 Base = -1;
3852 for (unsigned I = 0; I < BytesPerElement; ++I) {
3853 if (Bytes[Start + I] >= 0) {
3854 unsigned Elem = Bytes[Start + I];
3855 if (Base < 0) {
3856 Base = Elem - I;
3857 // Make sure the bytes would come from one input operand.
3858 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3859 return false;
3860 } else if (unsigned(Base) != Elem - I)
3861 return false;
3862 }
3863 }
3864 return true;
3865}
3866
3867// Bytes is a VPERM-like permute vector, except that -1 is used for
3868// undefined bytes. Return true if it can be performed using VSLDI.
3869// When returning true, set StartIndex to the shift amount and OpNo0
3870// and OpNo1 to the VPERM operands that should be used as the first
3871// and second shift operand respectively.
3872static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3873 unsigned &StartIndex, unsigned &OpNo0,
3874 unsigned &OpNo1) {
3875 int OpNos[] = { -1, -1 };
3876 int Shift = -1;
3877 for (unsigned I = 0; I < 16; ++I) {
3878 int Index = Bytes[I];
3879 if (Index >= 0) {
3880 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3881 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3882 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3883 if (Shift < 0)
3884 Shift = ExpectedShift;
3885 else if (Shift != ExpectedShift)
3886 return false;
3887 // Make sure that the operand mappings are consistent with previous
3888 // elements.
3889 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3890 return false;
3891 OpNos[ModelOpNo] = RealOpNo;
3892 }
3893 }
3894 StartIndex = Shift;
3895 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3896}
3897
3898// Create a node that performs P on operands Op0 and Op1, casting the
3899// operands to the appropriate type. The type of the result is determined by P.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003900static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003901 const Permute &P, SDValue Op0, SDValue Op1) {
3902 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3903 // elements of a PACK are twice as wide as the outputs.
3904 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3905 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3906 P.Operand);
3907 // Cast both operands to the appropriate type.
3908 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3909 SystemZ::VectorBytes / InBytes);
3910 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3911 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3912 SDValue Op;
3913 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3914 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3915 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3916 } else if (P.Opcode == SystemZISD::PACK) {
3917 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3918 SystemZ::VectorBytes / P.Operand);
3919 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3920 } else {
3921 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3922 }
3923 return Op;
3924}
3925
3926// Bytes is a VPERM-like permute vector, except that -1 is used for
3927// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3928// VSLDI or VPERM.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003929static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
3930 SDValue *Ops,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003931 const SmallVectorImpl<int> &Bytes) {
3932 for (unsigned I = 0; I < 2; ++I)
3933 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3934
3935 // First see whether VSLDI can be used.
3936 unsigned StartIndex, OpNo0, OpNo1;
3937 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3938 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3939 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3940
3941 // Fall back on VPERM. Construct an SDNode for the permute vector.
3942 SDValue IndexNodes[SystemZ::VectorBytes];
3943 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3944 if (Bytes[I] >= 0)
3945 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3946 else
3947 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003948 SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003949 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3950}
3951
3952namespace {
3953// Describes a general N-operand vector shuffle.
3954struct GeneralShuffle {
3955 GeneralShuffle(EVT vt) : VT(vt) {}
3956 void addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003957 bool add(SDValue, unsigned);
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003958 SDValue getNode(SelectionDAG &, const SDLoc &);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003959
3960 // The operands of the shuffle.
3961 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3962
3963 // Index I is -1 if byte I of the result is undefined. Otherwise the
3964 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3965 // Bytes[I] / SystemZ::VectorBytes.
3966 SmallVector<int, SystemZ::VectorBytes> Bytes;
3967
3968 // The type of the shuffle result.
3969 EVT VT;
3970};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003971}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003972
3973// Add an extra undefined element to the shuffle.
3974void GeneralShuffle::addUndef() {
3975 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3976 for (unsigned I = 0; I < BytesPerElement; ++I)
3977 Bytes.push_back(-1);
3978}
3979
3980// Add an extra element to the shuffle, taking it from element Elem of Op.
3981// A null Op indicates a vector input whose value will be calculated later;
3982// there is at most one such input per shuffle and it always has the same
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003983// type as the result. Aborts and returns false if the source vector elements
3984// of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
3985// LLVM they become implicitly extended, but this is rare and not optimized.
3986bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003987 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3988
3989 // The source vector can have wider elements than the result,
3990 // either through an explicit TRUNCATE or because of type legalization.
3991 // We want the least significant part.
3992 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3993 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00003994
3995 // Return false if the source elements are smaller than their destination
3996 // elements.
3997 if (FromBytesPerElement < BytesPerElement)
3998 return false;
3999
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004000 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
4001 (FromBytesPerElement - BytesPerElement));
4002
4003 // Look through things like shuffles and bitcasts.
4004 while (Op.getNode()) {
4005 if (Op.getOpcode() == ISD::BITCAST)
4006 Op = Op.getOperand(0);
4007 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
4008 // See whether the bytes we need come from a contiguous part of one
4009 // operand.
4010 SmallVector<int, SystemZ::VectorBytes> OpBytes;
4011 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
4012 int NewByte;
4013 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
4014 break;
4015 if (NewByte < 0) {
4016 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004017 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004018 }
4019 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
4020 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00004021 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004022 addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004023 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004024 } else
4025 break;
4026 }
4027
4028 // Make sure that the source of the extraction is in Ops.
4029 unsigned OpNo = 0;
4030 for (; OpNo < Ops.size(); ++OpNo)
4031 if (Ops[OpNo] == Op)
4032 break;
4033 if (OpNo == Ops.size())
4034 Ops.push_back(Op);
4035
4036 // Add the element to Bytes.
4037 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
4038 for (unsigned I = 0; I < BytesPerElement; ++I)
4039 Bytes.push_back(Base + I);
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004040
4041 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004042}
4043
4044// Return SDNodes for the completed shuffle.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004045SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004046 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
4047
4048 if (Ops.size() == 0)
4049 return DAG.getUNDEF(VT);
4050
4051 // Make sure that there are at least two shuffle operands.
4052 if (Ops.size() == 1)
4053 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
4054
4055 // Create a tree of shuffles, deferring root node until after the loop.
4056 // Try to redistribute the undefined elements of non-root nodes so that
4057 // the non-root shuffles match something like a pack or merge, then adjust
4058 // the parent node's permute vector to compensate for the new order.
4059 // Among other things, this copes with vectors like <2 x i16> that were
4060 // padded with undefined elements during type legalization.
4061 //
4062 // In the best case this redistribution will lead to the whole tree
4063 // using packs and merges. It should rarely be a loss in other cases.
4064 unsigned Stride = 1;
4065 for (; Stride * 2 < Ops.size(); Stride *= 2) {
4066 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
4067 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
4068
4069 // Create a mask for just these two operands.
4070 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
4071 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4072 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
4073 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
4074 if (OpNo == I)
4075 NewBytes[J] = Byte;
4076 else if (OpNo == I + Stride)
4077 NewBytes[J] = SystemZ::VectorBytes + Byte;
4078 else
4079 NewBytes[J] = -1;
4080 }
4081 // See if it would be better to reorganize NewMask to avoid using VPERM.
4082 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
4083 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
4084 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
4085 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
4086 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
4087 if (NewBytes[J] >= 0) {
4088 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
4089 "Invalid double permute");
4090 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
4091 } else
4092 assert(NewBytesMap[J] < 0 && "Invalid double permute");
4093 }
4094 } else {
4095 // Just use NewBytes on the operands.
4096 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
4097 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
4098 if (NewBytes[J] >= 0)
4099 Bytes[J] = I * SystemZ::VectorBytes + J;
4100 }
4101 }
4102 }
4103
4104 // Now we just have 2 inputs. Put the second operand in Ops[1].
4105 if (Stride > 1) {
4106 Ops[1] = Ops[Stride];
4107 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
4108 if (Bytes[I] >= int(SystemZ::VectorBytes))
4109 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
4110 }
4111
4112 // Look for an instruction that can do the permute without resorting
4113 // to VPERM.
4114 unsigned OpNo0, OpNo1;
4115 SDValue Op;
4116 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
4117 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
4118 else
4119 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
4120 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4121}
4122
Ulrich Weigandcd808232015-05-05 19:26:48 +00004123// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
4124static bool isScalarToVector(SDValue Op) {
4125 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004126 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004127 return false;
4128 return true;
4129}
4130
4131// Return a vector of type VT that contains Value in the first element.
4132// The other elements don't matter.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004133static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004134 SDValue Value) {
4135 // If we have a constant, replicate it to all elements and let the
4136 // BUILD_VECTOR lowering take care of it.
4137 if (Value.getOpcode() == ISD::Constant ||
4138 Value.getOpcode() == ISD::ConstantFP) {
4139 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004140 return DAG.getBuildVector(VT, DL, Ops);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004141 }
Sanjay Patel57195842016-03-14 17:28:46 +00004142 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004143 return DAG.getUNDEF(VT);
4144 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
4145}
4146
4147// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
4148// element 1. Used for cases in which replication is cheap.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004149static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandcd808232015-05-05 19:26:48 +00004150 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004151 if (Op0.isUndef()) {
4152 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004153 return DAG.getUNDEF(VT);
4154 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
4155 }
Sanjay Patel57195842016-03-14 17:28:46 +00004156 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00004157 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
4158 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
4159 buildScalarToVector(DAG, DL, VT, Op0),
4160 buildScalarToVector(DAG, DL, VT, Op1));
4161}
4162
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004163// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
4164// vector for them.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004165static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004166 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00004167 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004168 return DAG.getUNDEF(MVT::v2i64);
4169 // If one of the two inputs is undefined then replicate the other one,
4170 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00004171 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004172 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00004173 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004174 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4175 else {
4176 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
4177 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
4178 }
4179 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
4180}
4181
4182// Try to represent constant BUILD_VECTOR node BVN using a
4183// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
4184// on success.
4185static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
4186 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
4187 unsigned BytesPerElement = ElemVT.getStoreSize();
4188 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
4189 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00004190 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004191 uint64_t Value;
4192 if (Op.getOpcode() == ISD::Constant)
4193 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
4194 else if (Op.getOpcode() == ISD::ConstantFP)
4195 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
4196 .getZExtValue());
4197 else
4198 return false;
4199 for (unsigned J = 0; J < BytesPerElement; ++J) {
4200 uint64_t Byte = (Value >> (J * 8)) & 0xff;
4201 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00004202 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004203 else if (Byte != 0)
4204 return false;
4205 }
4206 }
4207 }
4208 return true;
4209}
4210
4211// Try to load a vector constant in which BitsPerElement-bit value Value
4212// is replicated to fill the vector. VT is the type of the resulting
4213// constant, which may have elements of a different size from BitsPerElement.
4214// Return the SDValue of the constant on success, otherwise return
4215// an empty value.
4216static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
4217 const SystemZInstrInfo *TII,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004218 const SDLoc &DL, EVT VT, uint64_t Value,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004219 unsigned BitsPerElement) {
4220 // Signed 16-bit values can be replicated using VREPI.
4221 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
4222 if (isInt<16>(SignedValue)) {
4223 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4224 SystemZ::VectorBits / BitsPerElement);
4225 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
4226 DAG.getConstant(SignedValue, DL, MVT::i32));
4227 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4228 }
4229 // See whether rotating the constant left some N places gives a value that
4230 // is one less than a power of 2 (i.e. all zeros followed by all ones).
4231 // If so we can use VGM.
4232 unsigned Start, End;
4233 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4234 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4235 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4236 // bit numbers for an BitsPerElement value, so that 0 denotes
4237 // 1 << (BitsPerElement-1).
4238 Start -= 64 - BitsPerElement;
4239 End -= 64 - BitsPerElement;
4240 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4241 SystemZ::VectorBits / BitsPerElement);
4242 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4243 DAG.getConstant(Start, DL, MVT::i32),
4244 DAG.getConstant(End, DL, MVT::i32));
4245 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4246 }
4247 return SDValue();
4248}
4249
4250// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4251// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4252// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4253// would benefit from this representation and return it if so.
4254static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4255 BuildVectorSDNode *BVN) {
4256 EVT VT = BVN->getValueType(0);
4257 unsigned NumElements = VT.getVectorNumElements();
4258
4259 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4260 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4261 // need a BUILD_VECTOR, add an additional placeholder operand for that
4262 // BUILD_VECTOR and store its operands in ResidueOps.
4263 GeneralShuffle GS(VT);
4264 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4265 bool FoundOne = false;
4266 for (unsigned I = 0; I < NumElements; ++I) {
4267 SDValue Op = BVN->getOperand(I);
4268 if (Op.getOpcode() == ISD::TRUNCATE)
4269 Op = Op.getOperand(0);
4270 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4271 Op.getOperand(1).getOpcode() == ISD::Constant) {
4272 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004273 if (!GS.add(Op.getOperand(0), Elem))
4274 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004275 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004276 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004277 GS.addUndef();
4278 } else {
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004279 if (!GS.add(SDValue(), ResidueOps.size()))
4280 return SDValue();
Ulrich Weigande861e642015-09-15 14:27:46 +00004281 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004282 }
4283 }
4284
4285 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4286 if (!FoundOne)
4287 return SDValue();
4288
4289 // Create the BUILD_VECTOR for the remaining elements, if any.
4290 if (!ResidueOps.empty()) {
4291 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004292 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004293 for (auto &Op : GS.Ops) {
4294 if (!Op.getNode()) {
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004295 Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004296 break;
4297 }
4298 }
4299 }
4300 return GS.getNode(DAG, SDLoc(BVN));
4301}
4302
4303// Combine GPR scalar values Elems into a vector of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004304static SDValue buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004305 SmallVectorImpl<SDValue> &Elems) {
4306 // See whether there is a single replicated value.
4307 SDValue Single;
4308 unsigned int NumElements = Elems.size();
4309 unsigned int Count = 0;
4310 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004311 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004312 if (!Single.getNode())
4313 Single = Elem;
4314 else if (Elem != Single) {
4315 Single = SDValue();
4316 break;
4317 }
4318 Count += 1;
4319 }
4320 }
4321 // There are three cases here:
4322 //
4323 // - if the only defined element is a loaded one, the best sequence
4324 // is a replicating load.
4325 //
4326 // - otherwise, if the only defined element is an i64 value, we will
4327 // end up with the same VLVGP sequence regardless of whether we short-cut
4328 // for replication or fall through to the later code.
4329 //
4330 // - otherwise, if the only defined element is an i32 or smaller value,
4331 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4332 // This is only a win if the single defined element is used more than once.
4333 // In other cases we're better off using a single VLVGx.
4334 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4335 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4336
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004337 // If all elements are loads, use VLREP/VLEs (below).
4338 bool AllLoads = true;
4339 for (auto Elem : Elems)
4340 if (Elem.getOpcode() != ISD::LOAD || cast<LoadSDNode>(Elem)->isIndexed()) {
4341 AllLoads = false;
4342 break;
4343 }
4344
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004345 // The best way of building a v2i64 from two i64s is to use VLVGP.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004346 if (VT == MVT::v2i64 && !AllLoads)
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004347 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4348
Ulrich Weigandcd808232015-05-05 19:26:48 +00004349 // Use a 64-bit merge high to combine two doubles.
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004350 if (VT == MVT::v2f64 && !AllLoads)
Ulrich Weigandcd808232015-05-05 19:26:48 +00004351 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4352
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004353 // Build v4f32 values directly from the FPRs:
4354 //
4355 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4356 // V V VMRHF
4357 // <ABxx> <CDxx>
4358 // V VMRHG
4359 // <ABCD>
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004360 if (VT == MVT::v4f32 && !AllLoads) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004361 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4362 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4363 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004364 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004365 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004366 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004367 Op23 = Op01;
4368 // Merging identical replications is a no-op.
4369 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4370 return Op01;
4371 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4372 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4373 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4374 DL, MVT::v2i64, Op01, Op23);
4375 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4376 }
4377
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004378 // Collect the constant terms.
4379 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4380 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4381
4382 unsigned NumConstants = 0;
4383 for (unsigned I = 0; I < NumElements; ++I) {
4384 SDValue Elem = Elems[I];
4385 if (Elem.getOpcode() == ISD::Constant ||
4386 Elem.getOpcode() == ISD::ConstantFP) {
4387 NumConstants += 1;
4388 Constants[I] = Elem;
4389 Done[I] = true;
4390 }
4391 }
4392 // If there was at least one constant, fill in the other elements of
4393 // Constants with undefs to get a full vector constant and use that
4394 // as the starting point.
4395 SDValue Result;
4396 if (NumConstants > 0) {
4397 for (unsigned I = 0; I < NumElements; ++I)
4398 if (!Constants[I].getNode())
4399 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
Ahmed Bougacha128f8732016-04-26 21:15:30 +00004400 Result = DAG.getBuildVector(VT, DL, Constants);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004401 } else {
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004402 // Otherwise try to use VLREP or VLVGP to start the sequence in order to
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004403 // avoid a false dependency on any previous contents of the vector
Jonas Paulssonfe0c0932017-05-29 13:22:23 +00004404 // register.
4405
4406 // Use a VLREP if at least one element is a load.
4407 unsigned LoadElIdx = UINT_MAX;
4408 for (unsigned I = 0; I < NumElements; ++I)
4409 if (Elems[I].getOpcode() == ISD::LOAD &&
4410 cast<LoadSDNode>(Elems[I])->isUnindexed()) {
4411 LoadElIdx = I;
4412 break;
4413 }
4414 if (LoadElIdx != UINT_MAX) {
4415 Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, Elems[LoadElIdx]);
4416 Done[LoadElIdx] = true;
4417 } else {
4418 // Try to use VLVGP.
4419 unsigned I1 = NumElements / 2 - 1;
4420 unsigned I2 = NumElements - 1;
4421 bool Def1 = !Elems[I1].isUndef();
4422 bool Def2 = !Elems[I2].isUndef();
4423 if (Def1 || Def2) {
4424 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4425 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4426 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4427 joinDwords(DAG, DL, Elem1, Elem2));
4428 Done[I1] = true;
4429 Done[I2] = true;
4430 } else
4431 Result = DAG.getUNDEF(VT);
4432 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004433 }
4434
4435 // Use VLVGx to insert the other elements.
4436 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004437 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004438 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4439 DAG.getConstant(I, DL, MVT::i32));
4440 return Result;
4441}
4442
4443SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4444 SelectionDAG &DAG) const {
4445 const SystemZInstrInfo *TII =
4446 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4447 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4448 SDLoc DL(Op);
4449 EVT VT = Op.getValueType();
4450
4451 if (BVN->isConstant()) {
4452 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4453 // preferred way of creating all-zero and all-one vectors so give it
4454 // priority over other methods below.
4455 uint64_t Mask = 0;
4456 if (tryBuildVectorByteMask(BVN, Mask)) {
4457 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4458 DAG.getConstant(Mask, DL, MVT::i32));
4459 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4460 }
4461
4462 // Try using some form of replication.
4463 APInt SplatBits, SplatUndef;
4464 unsigned SplatBitSize;
4465 bool HasAnyUndefs;
4466 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4467 8, true) &&
4468 SplatBitSize <= 64) {
4469 // First try assuming that any undefined bits above the highest set bit
4470 // and below the lowest set bit are 1s. This increases the likelihood of
4471 // being able to use a sign-extended element value in VECTOR REPLICATE
4472 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4473 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4474 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4475 uint64_t Lower = (SplatUndefZ
4476 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4477 uint64_t Upper = (SplatUndefZ
4478 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4479 uint64_t Value = SplatBitsZ | Upper | Lower;
4480 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4481 SplatBitSize);
4482 if (Op.getNode())
4483 return Op;
4484
4485 // Now try assuming that any undefined bits between the first and
4486 // last defined set bits are set. This increases the chances of
4487 // using a non-wraparound mask.
4488 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4489 Value = SplatBitsZ | Middle;
4490 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4491 if (Op.getNode())
4492 return Op;
4493 }
4494
4495 // Fall back to loading it from memory.
4496 return SDValue();
4497 }
4498
4499 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004500 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004501 return Res;
4502
Ulrich Weigandcd808232015-05-05 19:26:48 +00004503 // Detect SCALAR_TO_VECTOR conversions.
4504 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4505 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4506
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004507 // Otherwise use buildVector to build the vector up from GPRs.
4508 unsigned NumElements = Op.getNumOperands();
4509 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4510 for (unsigned I = 0; I < NumElements; ++I)
4511 Ops[I] = Op.getOperand(I);
4512 return buildVector(DAG, DL, VT, Ops);
4513}
4514
4515SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4516 SelectionDAG &DAG) const {
4517 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4518 SDLoc DL(Op);
4519 EVT VT = Op.getValueType();
4520 unsigned NumElements = VT.getVectorNumElements();
4521
4522 if (VSN->isSplat()) {
4523 SDValue Op0 = Op.getOperand(0);
4524 unsigned Index = VSN->getSplatIndex();
4525 assert(Index < VT.getVectorNumElements() &&
4526 "Splat index should be defined and in first operand");
4527 // See whether the value we're splatting is directly available as a scalar.
4528 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4529 Op0.getOpcode() == ISD::BUILD_VECTOR)
4530 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4531 // Otherwise keep it as a vector-to-vector operation.
4532 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4533 DAG.getConstant(Index, DL, MVT::i32));
4534 }
4535
4536 GeneralShuffle GS(VT);
4537 for (unsigned I = 0; I < NumElements; ++I) {
4538 int Elt = VSN->getMaskElt(I);
4539 if (Elt < 0)
4540 GS.addUndef();
Jonas Paulsson463e2a62017-01-24 05:43:03 +00004541 else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4542 unsigned(Elt) % NumElements))
4543 return SDValue();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004544 }
4545 return GS.getNode(DAG, SDLoc(VSN));
4546}
4547
4548SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4549 SelectionDAG &DAG) const {
4550 SDLoc DL(Op);
4551 // Just insert the scalar into element 0 of an undefined vector.
4552 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4553 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4554 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4555}
4556
Ulrich Weigandcd808232015-05-05 19:26:48 +00004557SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4558 SelectionDAG &DAG) const {
4559 // Handle insertions of floating-point values.
4560 SDLoc DL(Op);
4561 SDValue Op0 = Op.getOperand(0);
4562 SDValue Op1 = Op.getOperand(1);
4563 SDValue Op2 = Op.getOperand(2);
4564 EVT VT = Op.getValueType();
4565
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004566 // Insertions into constant indices of a v2f64 can be done using VPDI.
4567 // However, if the inserted value is a bitcast or a constant then it's
4568 // better to use GPRs, as below.
4569 if (VT == MVT::v2f64 &&
4570 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004571 Op1.getOpcode() != ISD::ConstantFP &&
4572 Op2.getOpcode() == ISD::Constant) {
4573 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4574 unsigned Mask = VT.getVectorNumElements() - 1;
4575 if (Index <= Mask)
4576 return Op;
4577 }
4578
4579 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004580 MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
Ulrich Weigandcd808232015-05-05 19:26:48 +00004581 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4582 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4583 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4584 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4585 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4586}
4587
4588SDValue
4589SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4590 SelectionDAG &DAG) const {
4591 // Handle extractions of floating-point values.
4592 SDLoc DL(Op);
4593 SDValue Op0 = Op.getOperand(0);
4594 SDValue Op1 = Op.getOperand(1);
4595 EVT VT = Op.getValueType();
4596 EVT VecVT = Op0.getValueType();
4597
4598 // Extractions of constant indices can be done directly.
4599 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4600 uint64_t Index = CIndexN->getZExtValue();
4601 unsigned Mask = VecVT.getVectorNumElements() - 1;
4602 if (Index <= Mask)
4603 return Op;
4604 }
4605
4606 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4607 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4608 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4609 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4610 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4611 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4612}
4613
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004614SDValue
4615SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004616 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004617 SDValue PackedOp = Op.getOperand(0);
4618 EVT OutVT = Op.getValueType();
4619 EVT InVT = PackedOp.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004620 unsigned ToBits = OutVT.getScalarSizeInBits();
4621 unsigned FromBits = InVT.getScalarSizeInBits();
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004622 do {
4623 FromBits *= 2;
4624 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4625 SystemZ::VectorBits / FromBits);
4626 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4627 } while (FromBits != ToBits);
4628 return PackedOp;
4629}
4630
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004631SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4632 unsigned ByScalar) const {
4633 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4634 SDValue Op0 = Op.getOperand(0);
4635 SDValue Op1 = Op.getOperand(1);
4636 SDLoc DL(Op);
4637 EVT VT = Op.getValueType();
Sanjay Patel1ed771f2016-09-14 16:37:15 +00004638 unsigned ElemBitSize = VT.getScalarSizeInBits();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004639
4640 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4641 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4642 APInt SplatBits, SplatUndef;
4643 unsigned SplatBitSize;
4644 bool HasAnyUndefs;
4645 // Check for constant splats. Use ElemBitSize as the minimum element
4646 // width and reject splats that need wider elements.
4647 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4648 ElemBitSize, true) &&
4649 SplatBitSize == ElemBitSize) {
4650 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4651 DL, MVT::i32);
4652 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4653 }
4654 // Check for variable splats.
4655 BitVector UndefElements;
4656 SDValue Splat = BVN->getSplatValue(&UndefElements);
4657 if (Splat) {
4658 // Since i32 is the smallest legal type, we either need a no-op
4659 // or a truncation.
4660 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4661 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4662 }
4663 }
4664
4665 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4666 // and the shift amount is directly available in a GPR.
4667 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4668 if (VSN->isSplat()) {
4669 SDValue VSNOp0 = VSN->getOperand(0);
4670 unsigned Index = VSN->getSplatIndex();
4671 assert(Index < VT.getVectorNumElements() &&
4672 "Splat index should be defined and in first operand");
4673 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4674 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4675 // Since i32 is the smallest legal type, we either need a no-op
4676 // or a truncation.
4677 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4678 VSNOp0.getOperand(Index));
4679 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4680 }
4681 }
4682 }
4683
4684 // Otherwise just treat the current form as legal.
4685 return Op;
4686}
4687
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004688SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4689 SelectionDAG &DAG) const {
4690 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004691 case ISD::FRAMEADDR:
4692 return lowerFRAMEADDR(Op, DAG);
4693 case ISD::RETURNADDR:
4694 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004695 case ISD::BR_CC:
4696 return lowerBR_CC(Op, DAG);
4697 case ISD::SELECT_CC:
4698 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004699 case ISD::SETCC:
4700 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004701 case ISD::GlobalAddress:
4702 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4703 case ISD::GlobalTLSAddress:
4704 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4705 case ISD::BlockAddress:
4706 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4707 case ISD::JumpTable:
4708 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4709 case ISD::ConstantPool:
4710 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4711 case ISD::BITCAST:
4712 return lowerBITCAST(Op, DAG);
4713 case ISD::VASTART:
4714 return lowerVASTART(Op, DAG);
4715 case ISD::VACOPY:
4716 return lowerVACOPY(Op, DAG);
4717 case ISD::DYNAMIC_STACKALLOC:
4718 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +00004719 case ISD::GET_DYNAMIC_AREA_OFFSET:
4720 return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004721 case ISD::SMUL_LOHI:
4722 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004723 case ISD::UMUL_LOHI:
4724 return lowerUMUL_LOHI(Op, DAG);
4725 case ISD::SDIVREM:
4726 return lowerSDIVREM(Op, DAG);
4727 case ISD::UDIVREM:
4728 return lowerUDIVREM(Op, DAG);
4729 case ISD::OR:
4730 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004731 case ISD::CTPOP:
4732 return lowerCTPOP(Op, DAG);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004733 case ISD::ATOMIC_FENCE:
4734 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004735 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004736 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4737 case ISD::ATOMIC_STORE:
4738 return lowerATOMIC_STORE(Op, DAG);
4739 case ISD::ATOMIC_LOAD:
4740 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004741 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004742 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004743 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004744 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004745 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004746 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004747 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004748 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004749 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004750 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004751 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004752 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004753 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004754 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004755 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004756 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004757 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004758 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004759 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004760 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004761 case ISD::ATOMIC_CMP_SWAP:
4762 return lowerATOMIC_CMP_SWAP(Op, DAG);
4763 case ISD::STACKSAVE:
4764 return lowerSTACKSAVE(Op, DAG);
4765 case ISD::STACKRESTORE:
4766 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004767 case ISD::PREFETCH:
4768 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004769 case ISD::INTRINSIC_W_CHAIN:
4770 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004771 case ISD::INTRINSIC_WO_CHAIN:
4772 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004773 case ISD::BUILD_VECTOR:
4774 return lowerBUILD_VECTOR(Op, DAG);
4775 case ISD::VECTOR_SHUFFLE:
4776 return lowerVECTOR_SHUFFLE(Op, DAG);
4777 case ISD::SCALAR_TO_VECTOR:
4778 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004779 case ISD::INSERT_VECTOR_ELT:
4780 return lowerINSERT_VECTOR_ELT(Op, DAG);
4781 case ISD::EXTRACT_VECTOR_ELT:
4782 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004783 case ISD::SIGN_EXTEND_VECTOR_INREG:
4784 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4785 case ISD::ZERO_EXTEND_VECTOR_INREG:
4786 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004787 case ISD::SHL:
4788 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4789 case ISD::SRL:
4790 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4791 case ISD::SRA:
4792 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004793 default:
4794 llvm_unreachable("Unexpected node to lower");
4795 }
4796}
4797
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004798// Lower operations with invalid operand or result types (currently used
4799// only for 128-bit integer types).
4800
4801static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
4802 SDLoc DL(In);
4803 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4804 DAG.getIntPtrConstant(0, DL));
4805 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
4806 DAG.getIntPtrConstant(1, DL));
4807 SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
4808 MVT::Untyped, Hi, Lo);
4809 return SDValue(Pair, 0);
4810}
4811
4812static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
4813 SDLoc DL(In);
4814 SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
4815 DL, MVT::i64, In);
4816 SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
4817 DL, MVT::i64, In);
4818 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
4819}
4820
4821void
4822SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
4823 SmallVectorImpl<SDValue> &Results,
4824 SelectionDAG &DAG) const {
4825 switch (N->getOpcode()) {
4826 case ISD::ATOMIC_LOAD: {
4827 SDLoc DL(N);
4828 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4829 SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
4830 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4831 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
4832 DL, Tys, Ops, MVT::i128, MMO);
4833 Results.push_back(lowerGR128ToI128(DAG, Res));
4834 Results.push_back(Res.getValue(1));
4835 break;
4836 }
4837 case ISD::ATOMIC_STORE: {
4838 SDLoc DL(N);
4839 SDVTList Tys = DAG.getVTList(MVT::Other);
4840 SDValue Ops[] = { N->getOperand(0),
4841 lowerI128ToGR128(DAG, N->getOperand(2)),
4842 N->getOperand(1) };
4843 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4844 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
4845 DL, Tys, Ops, MVT::i128, MMO);
4846 // We have to enforce sequential consistency by performing a
4847 // serialization operation after the store.
4848 if (cast<AtomicSDNode>(N)->getOrdering() ==
4849 AtomicOrdering::SequentiallyConsistent)
4850 Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
4851 MVT::Other, Res), 0);
4852 Results.push_back(Res);
4853 break;
4854 }
4855 case ISD::ATOMIC_CMP_SWAP: {
4856 SDLoc DL(N);
4857 SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
4858 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
4859 lowerI128ToGR128(DAG, N->getOperand(2)),
4860 lowerI128ToGR128(DAG, N->getOperand(3)) };
4861 MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
4862 SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
4863 DL, Tys, Ops, MVT::i128, MMO);
4864 Results.push_back(lowerGR128ToI128(DAG, Res));
4865 Results.push_back(Res.getValue(1));
4866 break;
4867 }
4868 default:
4869 llvm_unreachable("Unexpected node to lower");
4870 }
4871}
4872
4873void
4874SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
4875 SmallVectorImpl<SDValue> &Results,
4876 SelectionDAG &DAG) const {
4877 return LowerOperationWrapper(N, Results, DAG);
4878}
4879
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004880const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4881#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004882 switch ((SystemZISD::NodeType)Opcode) {
4883 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004884 OPCODE(RET_FLAG);
4885 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004886 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004887 OPCODE(TLS_GDCALL);
4888 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004889 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004890 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004891 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004892 OPCODE(ICMP);
4893 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004894 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004895 OPCODE(BR_CCMASK);
4896 OPCODE(SELECT_CCMASK);
4897 OPCODE(ADJDYNALLOC);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004898 OPCODE(POPCNT);
Ulrich Weigand2b3482f2017-07-17 17:41:11 +00004899 OPCODE(SMUL_LOHI);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00004900 OPCODE(UMUL_LOHI);
4901 OPCODE(SDIVREM);
4902 OPCODE(UDIVREM);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004903 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004904 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004905 OPCODE(NC);
4906 OPCODE(NC_LOOP);
4907 OPCODE(OC);
4908 OPCODE(OC_LOOP);
4909 OPCODE(XC);
4910 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004911 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004912 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004913 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004914 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004915 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004916 OPCODE(IPM);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004917 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004918 OPCODE(TBEGIN);
4919 OPCODE(TBEGIN_NOFLOAT);
4920 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004921 OPCODE(BYTE_MASK);
4922 OPCODE(ROTATE_MASK);
4923 OPCODE(REPLICATE);
4924 OPCODE(JOIN_DWORDS);
4925 OPCODE(SPLAT);
4926 OPCODE(MERGE_HIGH);
4927 OPCODE(MERGE_LOW);
4928 OPCODE(SHL_DOUBLE);
4929 OPCODE(PERMUTE_DWORDS);
4930 OPCODE(PERMUTE);
4931 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004932 OPCODE(PACKS_CC);
4933 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004934 OPCODE(UNPACK_HIGH);
4935 OPCODE(UNPACKL_HIGH);
4936 OPCODE(UNPACK_LOW);
4937 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004938 OPCODE(VSHL_BY_SCALAR);
4939 OPCODE(VSRL_BY_SCALAR);
4940 OPCODE(VSRA_BY_SCALAR);
4941 OPCODE(VSUM);
4942 OPCODE(VICMPE);
4943 OPCODE(VICMPH);
4944 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004945 OPCODE(VICMPES);
4946 OPCODE(VICMPHS);
4947 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004948 OPCODE(VFCMPE);
4949 OPCODE(VFCMPH);
4950 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004951 OPCODE(VFCMPES);
4952 OPCODE(VFCMPHS);
4953 OPCODE(VFCMPHES);
4954 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004955 OPCODE(VEXTEND);
4956 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004957 OPCODE(VTM);
4958 OPCODE(VFAE_CC);
4959 OPCODE(VFAEZ_CC);
4960 OPCODE(VFEE_CC);
4961 OPCODE(VFEEZ_CC);
4962 OPCODE(VFENE_CC);
4963 OPCODE(VFENEZ_CC);
4964 OPCODE(VISTR_CC);
4965 OPCODE(VSTRC_CC);
4966 OPCODE(VSTRCZ_CC);
Marcin Koscielnicki32e87342016-07-02 02:20:40 +00004967 OPCODE(TDC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004968 OPCODE(ATOMIC_SWAPW);
4969 OPCODE(ATOMIC_LOADW_ADD);
4970 OPCODE(ATOMIC_LOADW_SUB);
4971 OPCODE(ATOMIC_LOADW_AND);
4972 OPCODE(ATOMIC_LOADW_OR);
4973 OPCODE(ATOMIC_LOADW_XOR);
4974 OPCODE(ATOMIC_LOADW_NAND);
4975 OPCODE(ATOMIC_LOADW_MIN);
4976 OPCODE(ATOMIC_LOADW_MAX);
4977 OPCODE(ATOMIC_LOADW_UMIN);
4978 OPCODE(ATOMIC_LOADW_UMAX);
4979 OPCODE(ATOMIC_CMP_SWAPW);
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00004980 OPCODE(ATOMIC_LOAD_128);
4981 OPCODE(ATOMIC_STORE_128);
4982 OPCODE(ATOMIC_CMP_SWAP_128);
Bryan Chan28b759c2016-05-16 20:32:22 +00004983 OPCODE(LRV);
4984 OPCODE(STRV);
Richard Sandiford03481332013-08-23 11:36:42 +00004985 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004986 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004987 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004988#undef OPCODE
4989}
4990
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004991// Return true if VT is a vector whose elements are a whole number of bytes
Jonas Paulssoncad72ef2017-04-07 12:35:11 +00004992// in width. Also check for presence of vector support.
4993bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
4994 if (!Subtarget.hasVector())
4995 return false;
4996
Jonas Paulsson1d33cd32017-03-07 09:49:31 +00004997 return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004998}
4999
5000// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
5001// producing a result of type ResVT. Op is a possibly bitcast version
5002// of the input vector and Index is the index (based on type VecVT) that
5003// should be extracted. Return the new extraction if a simplification
5004// was possible or if Force is true.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005005SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
5006 EVT VecVT, SDValue Op,
5007 unsigned Index,
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005008 DAGCombinerInfo &DCI,
5009 bool Force) const {
5010 SelectionDAG &DAG = DCI.DAG;
5011
5012 // The number of bytes being extracted.
5013 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5014
5015 for (;;) {
5016 unsigned Opcode = Op.getOpcode();
5017 if (Opcode == ISD::BITCAST)
5018 // Look through bitcasts.
5019 Op = Op.getOperand(0);
5020 else if (Opcode == ISD::VECTOR_SHUFFLE &&
5021 canTreatAsByteVector(Op.getValueType())) {
5022 // Get a VPERM-like permute mask and see whether the bytes covered
5023 // by the extracted element are a contiguous sequence from one
5024 // source operand.
5025 SmallVector<int, SystemZ::VectorBytes> Bytes;
5026 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
5027 int First;
5028 if (!getShuffleInput(Bytes, Index * BytesPerElement,
5029 BytesPerElement, First))
5030 break;
5031 if (First < 0)
5032 return DAG.getUNDEF(ResVT);
5033 // Make sure the contiguous sequence starts at a multiple of the
5034 // original element size.
5035 unsigned Byte = unsigned(First) % Bytes.size();
5036 if (Byte % BytesPerElement != 0)
5037 break;
5038 // We can get the extracted value directly from an input.
5039 Index = Byte / BytesPerElement;
5040 Op = Op.getOperand(unsigned(First) / Bytes.size());
5041 Force = true;
5042 } else if (Opcode == ISD::BUILD_VECTOR &&
5043 canTreatAsByteVector(Op.getValueType())) {
5044 // We can only optimize this case if the BUILD_VECTOR elements are
5045 // at least as wide as the extracted value.
5046 EVT OpVT = Op.getValueType();
5047 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5048 if (OpBytesPerElement < BytesPerElement)
5049 break;
5050 // Make sure that the least-significant bit of the extracted value
5051 // is the least significant bit of an input.
5052 unsigned End = (Index + 1) * BytesPerElement;
5053 if (End % OpBytesPerElement != 0)
5054 break;
5055 // We're extracting the low part of one operand of the BUILD_VECTOR.
5056 Op = Op.getOperand(End / OpBytesPerElement - 1);
5057 if (!Op.getValueType().isInteger()) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005058 EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005059 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
5060 DCI.AddToWorklist(Op.getNode());
5061 }
5062 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
5063 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
5064 if (VT != ResVT) {
5065 DCI.AddToWorklist(Op.getNode());
5066 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
5067 }
5068 return Op;
5069 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005070 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
5071 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
5072 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005073 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
5074 // Make sure that only the unextended bits are significant.
5075 EVT ExtVT = Op.getValueType();
5076 EVT OpVT = Op.getOperand(0).getValueType();
5077 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
5078 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
5079 unsigned Byte = Index * BytesPerElement;
5080 unsigned SubByte = Byte % ExtBytesPerElement;
5081 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
5082 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005083 SubByte + BytesPerElement > ExtBytesPerElement)
5084 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005085 // Get the byte offset of the unextended element
5086 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
5087 // ...then add the byte offset relative to that element.
5088 Byte += SubByte - MinSubByte;
5089 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00005090 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005091 Op = Op.getOperand(0);
5092 Index = Byte / BytesPerElement;
5093 Force = true;
5094 } else
5095 break;
5096 }
5097 if (Force) {
5098 if (Op.getValueType() != VecVT) {
5099 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
5100 DCI.AddToWorklist(Op.getNode());
5101 }
5102 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
5103 DAG.getConstant(Index, DL, MVT::i32));
5104 }
5105 return SDValue();
5106}
5107
5108// Optimize vector operations in scalar value Op on the basis that Op
5109// is truncated to TruncVT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00005110SDValue SystemZTargetLowering::combineTruncateExtract(
5111 const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005112 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
5113 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
5114 // of type TruncVT.
5115 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5116 TruncVT.getSizeInBits() % 8 == 0) {
5117 SDValue Vec = Op.getOperand(0);
5118 EVT VecVT = Vec.getValueType();
5119 if (canTreatAsByteVector(VecVT)) {
5120 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
5121 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
5122 unsigned TruncBytes = TruncVT.getStoreSize();
5123 if (BytesPerElement % TruncBytes == 0) {
5124 // Calculate the value of Y' in the above description. We are
5125 // splitting the original elements into Scale equal-sized pieces
5126 // and for truncation purposes want the last (least-significant)
5127 // of these pieces for IndexN. This is easiest to do by calculating
5128 // the start index of the following element and then subtracting 1.
5129 unsigned Scale = BytesPerElement / TruncBytes;
5130 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
5131
5132 // Defer the creation of the bitcast from X to combineExtract,
5133 // which might be able to optimize the extraction.
5134 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
5135 VecVT.getStoreSize() / TruncBytes);
5136 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
5137 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
5138 }
5139 }
5140 }
5141 }
5142 return SDValue();
5143}
5144
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005145SDValue SystemZTargetLowering::combineSIGN_EXTEND(
5146 SDNode *N, DAGCombinerInfo &DCI) const {
5147 // Convert (sext (ashr (shl X, C1), C2)) to
5148 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
5149 // cheap as narrower ones.
5150 SelectionDAG &DAG = DCI.DAG;
5151 SDValue N0 = N->getOperand(0);
5152 EVT VT = N->getValueType(0);
5153 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
5154 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
5155 SDValue Inner = N0.getOperand(0);
5156 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
5157 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00005158 unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005159 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
5160 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
5161 EVT ShiftVT = N0.getOperand(1).getValueType();
5162 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
5163 Inner.getOperand(0));
5164 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
5165 DAG.getConstant(NewShlAmt, SDLoc(Inner),
5166 ShiftVT));
5167 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
5168 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
5169 }
5170 }
5171 }
5172 return SDValue();
5173}
5174
5175SDValue SystemZTargetLowering::combineMERGE(
5176 SDNode *N, DAGCombinerInfo &DCI) const {
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005177 SelectionDAG &DAG = DCI.DAG;
5178 unsigned Opcode = N->getOpcode();
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005179 SDValue Op0 = N->getOperand(0);
5180 SDValue Op1 = N->getOperand(1);
5181 if (Op0.getOpcode() == ISD::BITCAST)
5182 Op0 = Op0.getOperand(0);
5183 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
5184 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
5185 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
5186 // for v4f32.
5187 if (Op1 == N->getOperand(0))
5188 return Op1;
5189 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
5190 EVT VT = Op1.getValueType();
5191 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
5192 if (ElemBytes <= 4) {
5193 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
5194 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
5195 EVT InVT = VT.changeVectorElementTypeToInteger();
5196 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
5197 SystemZ::VectorBytes / ElemBytes / 2);
5198 if (VT != InVT) {
5199 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
5200 DCI.AddToWorklist(Op1.getNode());
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005201 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005202 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
5203 DCI.AddToWorklist(Op.getNode());
5204 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005205 }
5206 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005207 return SDValue();
5208}
5209
5210SDValue SystemZTargetLowering::combineSTORE(
5211 SDNode *N, DAGCombinerInfo &DCI) const {
5212 SelectionDAG &DAG = DCI.DAG;
5213 auto *SN = cast<StoreSDNode>(N);
5214 auto &Op1 = N->getOperand(1);
5215 EVT MemVT = SN->getMemoryVT();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005216 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
5217 // for the extraction to be done on a vMiN value, so that we can use VSTE.
5218 // If X has wider elements then convert it to:
5219 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005220 if (MemVT.isInteger()) {
5221 if (SDValue Value =
5222 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
5223 DCI.AddToWorklist(Value.getNode());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005224
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005225 // Rewrite the store with the new form of stored value.
5226 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
5227 SN->getBasePtr(), SN->getMemoryVT(),
5228 SN->getMemOperand());
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005229 }
5230 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005231 // Combine STORE (BSWAP) into STRVH/STRV/STRVG
5232 // See comment in combineBSWAP about volatile accesses.
5233 if (!SN->isVolatile() &&
5234 Op1.getOpcode() == ISD::BSWAP &&
5235 Op1.getNode()->hasOneUse() &&
5236 (Op1.getValueType() == MVT::i16 ||
5237 Op1.getValueType() == MVT::i32 ||
5238 Op1.getValueType() == MVT::i64)) {
5239
5240 SDValue BSwapOp = Op1.getOperand(0);
5241
5242 if (BSwapOp.getValueType() == MVT::i16)
5243 BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
5244
5245 SDValue Ops[] = {
5246 N->getOperand(0), BSwapOp, N->getOperand(2),
5247 DAG.getValueType(Op1.getValueType())
5248 };
5249
5250 return
5251 DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
5252 Ops, MemVT, SN->getMemOperand());
5253 }
5254 return SDValue();
5255}
5256
5257SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
5258 SDNode *N, DAGCombinerInfo &DCI) const {
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005259
Jonas Paulsson56bb0852017-03-31 13:22:59 +00005260 if (!Subtarget.hasVector())
5261 return SDValue();
5262
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005263 // Try to simplify a vector extraction.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005264 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
5265 SDValue Op0 = N->getOperand(0);
5266 EVT VecVT = Op0.getValueType();
5267 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
5268 IndexN->getZExtValue(), DCI, false);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005269 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005270 return SDValue();
5271}
5272
5273SDValue SystemZTargetLowering::combineJOIN_DWORDS(
5274 SDNode *N, DAGCombinerInfo &DCI) const {
5275 SelectionDAG &DAG = DCI.DAG;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005276 // (join_dwords X, X) == (replicate X)
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005277 if (N->getOperand(0) == N->getOperand(1))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005278 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
5279 N->getOperand(0));
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005280 return SDValue();
5281}
5282
5283SDValue SystemZTargetLowering::combineFP_ROUND(
5284 SDNode *N, DAGCombinerInfo &DCI) const {
Michael Kuperstein2bc3d4d2016-08-18 20:08:15 +00005285 // (fpround (extract_vector_elt X 0))
5286 // (fpround (extract_vector_elt X 1)) ->
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005287 // (extract_vector_elt (VROUND X) 0)
5288 // (extract_vector_elt (VROUND X) 1)
5289 //
5290 // This is a special case since the target doesn't really support v2f32s.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005291 SelectionDAG &DAG = DCI.DAG;
5292 SDValue Op0 = N->getOperand(0);
5293 if (N->getValueType(0) == MVT::f32 &&
5294 Op0.hasOneUse() &&
5295 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5296 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
5297 Op0.getOperand(1).getOpcode() == ISD::Constant &&
5298 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
5299 SDValue Vec = Op0.getOperand(0);
5300 for (auto *U : Vec->uses()) {
5301 if (U != Op0.getNode() &&
5302 U->hasOneUse() &&
5303 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5304 U->getOperand(0) == Vec &&
5305 U->getOperand(1).getOpcode() == ISD::Constant &&
5306 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
5307 SDValue OtherRound = SDValue(*U->use_begin(), 0);
5308 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
5309 OtherRound.getOperand(0) == SDValue(U, 0) &&
5310 OtherRound.getValueType() == MVT::f32) {
5311 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
5312 MVT::v4f32, Vec);
5313 DCI.AddToWorklist(VRound.getNode());
5314 SDValue Extract1 =
5315 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
5316 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
5317 DCI.AddToWorklist(Extract1.getNode());
5318 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
5319 SDValue Extract0 =
5320 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
5321 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
5322 return Extract0;
Ulrich Weigand80b3af72015-05-05 19:27:45 +00005323 }
5324 }
5325 }
5326 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005327 return SDValue();
5328}
Bryan Chan28b759c2016-05-16 20:32:22 +00005329
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005330SDValue SystemZTargetLowering::combineBSWAP(
5331 SDNode *N, DAGCombinerInfo &DCI) const {
5332 SelectionDAG &DAG = DCI.DAG;
Bryan Chan28b759c2016-05-16 20:32:22 +00005333 // Combine BSWAP (LOAD) into LRVH/LRV/LRVG
5334 // These loads are allowed to access memory multiple times, and so we must check
5335 // that the loads are not volatile before performing the combine.
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005336 if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
5337 N->getOperand(0).hasOneUse() &&
5338 (N->getValueType(0) == MVT::i16 || N->getValueType(0) == MVT::i32 ||
5339 N->getValueType(0) == MVT::i64) &&
5340 !cast<LoadSDNode>(N->getOperand(0))->isVolatile()) {
Bryan Chan28b759c2016-05-16 20:32:22 +00005341 SDValue Load = N->getOperand(0);
5342 LoadSDNode *LD = cast<LoadSDNode>(Load);
5343
5344 // Create the byte-swapping load.
5345 SDValue Ops[] = {
5346 LD->getChain(), // Chain
5347 LD->getBasePtr(), // Ptr
5348 DAG.getValueType(N->getValueType(0)) // VT
5349 };
5350 SDValue BSLoad =
5351 DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
5352 DAG.getVTList(N->getValueType(0) == MVT::i64 ?
5353 MVT::i64 : MVT::i32, MVT::Other),
5354 Ops, LD->getMemoryVT(), LD->getMemOperand());
5355
5356 // If this is an i16 load, insert the truncate.
5357 SDValue ResVal = BSLoad;
5358 if (N->getValueType(0) == MVT::i16)
5359 ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
5360
5361 // First, combine the bswap away. This makes the value produced by the
5362 // load dead.
5363 DCI.CombineTo(N, ResVal);
5364
5365 // Next, combine the load away, we give it a bogus result value but a real
5366 // chain result. The result value is dead because the bswap is dead.
5367 DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
5368
5369 // Return N so it doesn't get rechecked!
5370 return SDValue(N, 0);
5371 }
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005372 return SDValue();
5373}
Bryan Chan28b759c2016-05-16 20:32:22 +00005374
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005375SDValue SystemZTargetLowering::combineSHIFTROT(
5376 SDNode *N, DAGCombinerInfo &DCI) const {
5377
5378 SelectionDAG &DAG = DCI.DAG;
5379
5380 // Shift/rotate instructions only use the last 6 bits of the second operand
5381 // register. If the second operand is the result of an AND with an immediate
5382 // value that has its last 6 bits set, we can safely remove the AND operation.
Elliot Colp687691a2016-08-18 18:04:26 +00005383 //
5384 // If the AND operation doesn't have the last 6 bits set, we can't remove it
Elliot Colpa4092102016-08-23 14:03:02 +00005385 // entirely, but we can still truncate it to a 16-bit value. This prevents
5386 // us from ending up with a NILL with a signed operand, which will cause the
5387 // instruction printer to abort.
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005388 SDValue N1 = N->getOperand(1);
5389 if (N1.getOpcode() == ISD::AND) {
Elliot Colp687691a2016-08-18 18:04:26 +00005390 SDValue AndMaskOp = N1->getOperand(1);
5391 auto *AndMask = dyn_cast<ConstantSDNode>(AndMaskOp);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005392
5393 // The AND mask is constant
5394 if (AndMask) {
Elliot Colpa4092102016-08-23 14:03:02 +00005395 auto AmtVal = AndMask->getZExtValue();
5396
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005397 // Bottom 6 bits are set
5398 if ((AmtVal & 0x3f) == 0x3f) {
Elliot Colpa4092102016-08-23 14:03:02 +00005399 SDValue AndOp = N1->getOperand(0);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005400
5401 // This is the only use, so remove the node
5402 if (N1.hasOneUse()) {
5403 // Combine the AND away
5404 DCI.CombineTo(N1.getNode(), AndOp);
5405
5406 // Return N so it isn't rechecked
5407 return SDValue(N, 0);
5408
5409 // The node will be reused, so create a new node for this one use
5410 } else {
5411 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5412 N->getValueType(0), N->getOperand(0),
5413 AndOp);
5414 DCI.AddToWorklist(Replace.getNode());
5415
5416 return Replace;
5417 }
Elliot Colp687691a2016-08-18 18:04:26 +00005418
Elliot Colpa4092102016-08-23 14:03:02 +00005419 // We can't remove the AND, but we can use NILL here (normally we would
5420 // use NILF). Only keep the last 16 bits of the mask. The actual
5421 // transformation will be handled by .td definitions.
5422 } else if (AmtVal >> 16 != 0) {
5423 SDValue AndOp = N1->getOperand(0);
Elliot Colp687691a2016-08-18 18:04:26 +00005424
Elliot Colpa4092102016-08-23 14:03:02 +00005425 auto NewMask = DAG.getConstant(AndMask->getZExtValue() & 0x0000ffff,
5426 SDLoc(AndMaskOp),
5427 AndMaskOp.getValueType());
Elliot Colp687691a2016-08-18 18:04:26 +00005428
Elliot Colpa4092102016-08-23 14:03:02 +00005429 auto NewAnd = DAG.getNode(N1.getOpcode(), SDLoc(N1), N1.getValueType(),
5430 AndOp, NewMask);
Elliot Colp687691a2016-08-18 18:04:26 +00005431
Elliot Colpa4092102016-08-23 14:03:02 +00005432 SDValue Replace = DAG.getNode(N->getOpcode(), SDLoc(N),
5433 N->getValueType(0), N->getOperand(0),
5434 NewAnd);
5435 DCI.AddToWorklist(Replace.getNode());
Elliot Colp687691a2016-08-18 18:04:26 +00005436
Elliot Colpa4092102016-08-23 14:03:02 +00005437 return Replace;
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005438 }
5439 }
5440 }
5441
5442 return SDValue();
5443}
5444
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005445SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
5446 DAGCombinerInfo &DCI) const {
5447 switch(N->getOpcode()) {
5448 default: break;
5449 case ISD::SIGN_EXTEND: return combineSIGN_EXTEND(N, DCI);
5450 case SystemZISD::MERGE_HIGH:
5451 case SystemZISD::MERGE_LOW: return combineMERGE(N, DCI);
5452 case ISD::STORE: return combineSTORE(N, DCI);
5453 case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
5454 case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
5455 case ISD::FP_ROUND: return combineFP_ROUND(N, DCI);
5456 case ISD::BSWAP: return combineBSWAP(N, DCI);
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005457 case ISD::SHL:
5458 case ISD::SRA:
5459 case ISD::SRL:
5460 case ISD::ROTL: return combineSHIFTROT(N, DCI);
Marcin Koscielnicki68747ac2016-06-30 00:08:54 +00005461 }
Elliot Colpbc2cfc22016-07-06 18:13:11 +00005462
Richard Sandiford95bc5f92014-03-07 11:34:35 +00005463 return SDValue();
5464}
5465
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005466//===----------------------------------------------------------------------===//
5467// Custom insertion
5468//===----------------------------------------------------------------------===//
5469
5470// Create a new basic block after MBB.
5471static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
5472 MachineFunction &MF = *MBB->getParent();
5473 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005474 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005475 return NewMBB;
5476}
5477
Richard Sandifordbe133a82013-08-28 09:01:51 +00005478// Split MBB after MI and return the new block (the one that contains
5479// instructions after MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005480static MachineBasicBlock *splitBlockAfter(MachineBasicBlock::iterator MI,
Richard Sandifordbe133a82013-08-28 09:01:51 +00005481 MachineBasicBlock *MBB) {
5482 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
5483 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00005484 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00005485 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5486 return NewMBB;
5487}
5488
Richard Sandiford5e318f02013-08-27 09:54:29 +00005489// Split MBB before MI and return the new block (the one that contains MI).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005490static MachineBasicBlock *splitBlockBefore(MachineBasicBlock::iterator MI,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005491 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005492 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005493 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005494 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
5495 return NewMBB;
5496}
5497
Richard Sandiford5e318f02013-08-27 09:54:29 +00005498// Force base value Base into a register before MI. Return the register.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005499static unsigned forceReg(MachineInstr &MI, MachineOperand &Base,
Richard Sandiford5e318f02013-08-27 09:54:29 +00005500 const SystemZInstrInfo *TII) {
5501 if (Base.isReg())
5502 return Base.getReg();
5503
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005504 MachineBasicBlock *MBB = MI.getParent();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005505 MachineFunction &MF = *MBB->getParent();
5506 MachineRegisterInfo &MRI = MF.getRegInfo();
5507
5508 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005509 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00005510 .add(Base)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005511 .addImm(0)
5512 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005513 return Reg;
5514}
5515
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005516// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
5517MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005518SystemZTargetLowering::emitSelect(MachineInstr &MI,
Ulrich Weigand524f2762016-11-28 13:34:08 +00005519 MachineBasicBlock *MBB,
5520 unsigned LOCROpcode) const {
Eric Christophera6734172015-01-31 00:06:45 +00005521 const SystemZInstrInfo *TII =
5522 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005523
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005524 unsigned DestReg = MI.getOperand(0).getReg();
5525 unsigned TrueReg = MI.getOperand(1).getReg();
5526 unsigned FalseReg = MI.getOperand(2).getReg();
5527 unsigned CCValid = MI.getOperand(3).getImm();
5528 unsigned CCMask = MI.getOperand(4).getImm();
5529 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005530
Ulrich Weigand524f2762016-11-28 13:34:08 +00005531 // Use LOCROpcode if possible.
5532 if (LOCROpcode && Subtarget.hasLoadStoreOnCond()) {
5533 BuildMI(*MBB, MI, DL, TII->get(LOCROpcode), DestReg)
5534 .addReg(FalseReg).addReg(TrueReg)
5535 .addImm(CCValid).addImm(CCMask);
5536 MI.eraseFromParent();
5537 return MBB;
5538 }
5539
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005540 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005541 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005542 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5543
5544 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005545 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005546 // # fallthrough to FalseMBB
5547 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005548 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5549 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005550 MBB->addSuccessor(JoinMBB);
5551 MBB->addSuccessor(FalseMBB);
5552
5553 // FalseMBB:
5554 // # fallthrough to JoinMBB
5555 MBB = FalseMBB;
5556 MBB->addSuccessor(JoinMBB);
5557
5558 // JoinMBB:
5559 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5560 // ...
5561 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005562 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005563 .addReg(TrueReg).addMBB(StartMBB)
5564 .addReg(FalseReg).addMBB(FalseMBB);
5565
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005566 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005567 return JoinMBB;
5568}
5569
Richard Sandifordb86a8342013-06-27 09:27:40 +00005570// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5571// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005572// happen when the condition is false rather than true. If a STORE ON
5573// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005574MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
5575 MachineBasicBlock *MBB,
5576 unsigned StoreOpcode,
5577 unsigned STOCOpcode,
5578 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005579 const SystemZInstrInfo *TII =
5580 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005581
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005582 unsigned SrcReg = MI.getOperand(0).getReg();
5583 MachineOperand Base = MI.getOperand(1);
5584 int64_t Disp = MI.getOperand(2).getImm();
5585 unsigned IndexReg = MI.getOperand(3).getReg();
5586 unsigned CCValid = MI.getOperand(4).getImm();
5587 unsigned CCMask = MI.getOperand(5).getImm();
5588 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005589
5590 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5591
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005592 // Use STOCOpcode if possible. We could use different store patterns in
5593 // order to avoid matching the index register, but the performance trade-offs
5594 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005595 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005596 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005597 CCMask ^= CCValid;
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005598
5599 // ISel pattern matching also adds a load memory operand of the same
5600 // address, so take special care to find the storing memory operand.
5601 MachineMemOperand *MMO = nullptr;
5602 for (auto *I : MI.memoperands())
5603 if (I->isStore()) {
5604 MMO = I;
5605 break;
5606 }
5607
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005608 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00005609 .addReg(SrcReg)
5610 .add(Base)
5611 .addImm(Disp)
5612 .addImm(CCValid)
5613 .addImm(CCMask)
5614 .addMemOperand(MMO);
5615
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005616 MI.eraseFromParent();
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005617 return MBB;
5618 }
5619
Richard Sandifordb86a8342013-06-27 09:27:40 +00005620 // Get the condition needed to branch around the store.
5621 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005622 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005623
5624 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005625 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005626 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5627
5628 // StartMBB:
5629 // BRC CCMask, JoinMBB
5630 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005631 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005632 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5633 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005634 MBB->addSuccessor(JoinMBB);
5635 MBB->addSuccessor(FalseMBB);
5636
5637 // FalseMBB:
5638 // store %SrcReg, %Disp(%Index,%Base)
5639 // # fallthrough to JoinMBB
5640 MBB = FalseMBB;
5641 BuildMI(MBB, DL, TII->get(StoreOpcode))
Diana Picus116bbab2017-01-13 09:58:52 +00005642 .addReg(SrcReg)
5643 .add(Base)
5644 .addImm(Disp)
5645 .addReg(IndexReg);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005646 MBB->addSuccessor(JoinMBB);
5647
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005648 MI.eraseFromParent();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005649 return JoinMBB;
5650}
5651
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005652// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5653// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5654// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5655// BitSize is the width of the field in bits, or 0 if this is a partword
5656// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5657// is one of the operands. Invert says whether the field should be
5658// inverted after performing BinOpcode (e.g. for NAND).
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005659MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
5660 MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
5661 unsigned BitSize, bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005662 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005663 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005664 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005665 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005666 bool IsSubWord = (BitSize < 32);
5667
5668 // Extract the operands. Base can be a register or a frame index.
5669 // Src2 can be a register or immediate.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005670 unsigned Dest = MI.getOperand(0).getReg();
5671 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5672 int64_t Disp = MI.getOperand(2).getImm();
5673 MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
5674 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5675 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5676 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005677 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005678 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005679
5680 // Subword operations use 32-bit registers.
5681 const TargetRegisterClass *RC = (BitSize <= 32 ?
5682 &SystemZ::GR32BitRegClass :
5683 &SystemZ::GR64BitRegClass);
5684 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5685 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5686
5687 // Get the right opcodes for the displacement.
5688 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5689 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5690 assert(LOpcode && CSOpcode && "Displacement out of range");
5691
5692 // Create virtual registers for temporary results.
5693 unsigned OrigVal = MRI.createVirtualRegister(RC);
5694 unsigned OldVal = MRI.createVirtualRegister(RC);
5695 unsigned NewVal = (BinOpcode || IsSubWord ?
5696 MRI.createVirtualRegister(RC) : Src2.getReg());
5697 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5698 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5699
5700 // Insert a basic block for the main loop.
5701 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005702 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005703 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5704
5705 // StartMBB:
5706 // ...
5707 // %OrigVal = L Disp(%Base)
5708 // # fall through to LoopMMB
5709 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005710 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005711 MBB->addSuccessor(LoopMBB);
5712
5713 // LoopMBB:
5714 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5715 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5716 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5717 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5718 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5719 // JNE LoopMBB
5720 // # fall through to DoneMMB
5721 MBB = LoopMBB;
5722 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5723 .addReg(OrigVal).addMBB(StartMBB)
5724 .addReg(Dest).addMBB(LoopMBB);
5725 if (IsSubWord)
5726 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5727 .addReg(OldVal).addReg(BitShift).addImm(0);
5728 if (Invert) {
5729 // Perform the operation normally and then invert every bit of the field.
5730 unsigned Tmp = MRI.createVirtualRegister(RC);
Diana Picus116bbab2017-01-13 09:58:52 +00005731 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005732 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005733 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005734 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005735 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005736 else {
5737 // Use LCGR and add -1 to the result, which is more compact than
5738 // an XILF, XILH pair.
5739 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5740 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5741 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5742 .addReg(Tmp2).addImm(-1);
5743 }
5744 } else if (BinOpcode)
5745 // A simply binary operation.
5746 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005747 .addReg(RotatedOldVal)
5748 .add(Src2);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005749 else if (IsSubWord)
5750 // Use RISBG to rotate Src2 into position and use it to replace the
5751 // field in RotatedOldVal.
5752 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5753 .addReg(RotatedOldVal).addReg(Src2.getReg())
5754 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5755 if (IsSubWord)
5756 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5757 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5758 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005759 .addReg(OldVal)
5760 .addReg(NewVal)
5761 .add(Base)
5762 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005763 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5764 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005765 MBB->addSuccessor(LoopMBB);
5766 MBB->addSuccessor(DoneMBB);
5767
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005768 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005769 return DoneMBB;
5770}
5771
5772// Implement EmitInstrWithCustomInserter for pseudo
5773// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5774// instruction that should be used to compare the current field with the
5775// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5776// for when the current field should be kept. BitSize is the width of
5777// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005778MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
5779 MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
5780 unsigned KeepOldMask, unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005781 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005782 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005783 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005784 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005785 bool IsSubWord = (BitSize < 32);
5786
5787 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005788 unsigned Dest = MI.getOperand(0).getReg();
5789 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5790 int64_t Disp = MI.getOperand(2).getImm();
5791 unsigned Src2 = MI.getOperand(3).getReg();
5792 unsigned BitShift = (IsSubWord ? MI.getOperand(4).getReg() : 0);
5793 unsigned NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : 0);
5794 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005795 if (IsSubWord)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005796 BitSize = MI.getOperand(6).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005797
5798 // Subword operations use 32-bit registers.
5799 const TargetRegisterClass *RC = (BitSize <= 32 ?
5800 &SystemZ::GR32BitRegClass :
5801 &SystemZ::GR64BitRegClass);
5802 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5803 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5804
5805 // Get the right opcodes for the displacement.
5806 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5807 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5808 assert(LOpcode && CSOpcode && "Displacement out of range");
5809
5810 // Create virtual registers for temporary results.
5811 unsigned OrigVal = MRI.createVirtualRegister(RC);
5812 unsigned OldVal = MRI.createVirtualRegister(RC);
5813 unsigned NewVal = MRI.createVirtualRegister(RC);
5814 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5815 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5816 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5817
5818 // Insert 3 basic blocks for the loop.
5819 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005820 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005821 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5822 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5823 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5824
5825 // StartMBB:
5826 // ...
5827 // %OrigVal = L Disp(%Base)
5828 // # fall through to LoopMMB
5829 MBB = StartMBB;
Diana Picus116bbab2017-01-13 09:58:52 +00005830 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005831 MBB->addSuccessor(LoopMBB);
5832
5833 // LoopMBB:
5834 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5835 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5836 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005837 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005838 MBB = LoopMBB;
5839 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5840 .addReg(OrigVal).addMBB(StartMBB)
5841 .addReg(Dest).addMBB(UpdateMBB);
5842 if (IsSubWord)
5843 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5844 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005845 BuildMI(MBB, DL, TII->get(CompareOpcode))
5846 .addReg(RotatedOldVal).addReg(Src2);
5847 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005848 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005849 MBB->addSuccessor(UpdateMBB);
5850 MBB->addSuccessor(UseAltMBB);
5851
5852 // UseAltMBB:
5853 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5854 // # fall through to UpdateMMB
5855 MBB = UseAltMBB;
5856 if (IsSubWord)
5857 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5858 .addReg(RotatedOldVal).addReg(Src2)
5859 .addImm(32).addImm(31 + BitSize).addImm(0);
5860 MBB->addSuccessor(UpdateMBB);
5861
5862 // UpdateMBB:
5863 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5864 // [ %RotatedAltVal, UseAltMBB ]
5865 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5866 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5867 // JNE LoopMBB
5868 // # fall through to DoneMMB
5869 MBB = UpdateMBB;
5870 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5871 .addReg(RotatedOldVal).addMBB(LoopMBB)
5872 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5873 if (IsSubWord)
5874 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5875 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5876 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
Diana Picus116bbab2017-01-13 09:58:52 +00005877 .addReg(OldVal)
5878 .addReg(NewVal)
5879 .add(Base)
5880 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005881 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5882 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005883 MBB->addSuccessor(LoopMBB);
5884 MBB->addSuccessor(DoneMBB);
5885
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005886 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005887 return DoneMBB;
5888}
5889
5890// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5891// instruction MI.
5892MachineBasicBlock *
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005893SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005894 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005895
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005896 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005897 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005898 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005899 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005900
5901 // Extract the operands. Base can be a register or a frame index.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00005902 unsigned Dest = MI.getOperand(0).getReg();
5903 MachineOperand Base = earlyUseOperand(MI.getOperand(1));
5904 int64_t Disp = MI.getOperand(2).getImm();
5905 unsigned OrigCmpVal = MI.getOperand(3).getReg();
5906 unsigned OrigSwapVal = MI.getOperand(4).getReg();
5907 unsigned BitShift = MI.getOperand(5).getReg();
5908 unsigned NegBitShift = MI.getOperand(6).getReg();
5909 int64_t BitSize = MI.getOperand(7).getImm();
5910 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005911
5912 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5913
5914 // Get the right opcodes for the displacement.
5915 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5916 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5917 assert(LOpcode && CSOpcode && "Displacement out of range");
5918
5919 // Create virtual registers for temporary results.
5920 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5921 unsigned OldVal = MRI.createVirtualRegister(RC);
5922 unsigned CmpVal = MRI.createVirtualRegister(RC);
5923 unsigned SwapVal = MRI.createVirtualRegister(RC);
5924 unsigned StoreVal = MRI.createVirtualRegister(RC);
5925 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5926 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5927 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5928
5929 // Insert 2 basic blocks for the loop.
5930 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005931 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005932 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5933 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5934
5935 // StartMBB:
5936 // ...
5937 // %OrigOldVal = L Disp(%Base)
5938 // # fall through to LoopMMB
5939 MBB = StartMBB;
5940 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005941 .add(Base)
5942 .addImm(Disp)
5943 .addReg(0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005944 MBB->addSuccessor(LoopMBB);
5945
5946 // LoopMBB:
5947 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5948 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5949 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5950 // %Dest = RLL %OldVal, BitSize(%BitShift)
5951 // ^^ The low BitSize bits contain the field
5952 // of interest.
5953 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5954 // ^^ Replace the upper 32-BitSize bits of the
5955 // comparison value with those that we loaded,
5956 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005957 // CR %Dest, %RetryCmpVal
5958 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005959 // # Fall through to SetMBB
5960 MBB = LoopMBB;
5961 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5962 .addReg(OrigOldVal).addMBB(StartMBB)
5963 .addReg(RetryOldVal).addMBB(SetMBB);
5964 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5965 .addReg(OrigCmpVal).addMBB(StartMBB)
5966 .addReg(RetryCmpVal).addMBB(SetMBB);
5967 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5968 .addReg(OrigSwapVal).addMBB(StartMBB)
5969 .addReg(RetrySwapVal).addMBB(SetMBB);
5970 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5971 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5972 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5973 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005974 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5975 .addReg(Dest).addReg(RetryCmpVal);
5976 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005977 .addImm(SystemZ::CCMASK_ICMP)
5978 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005979 MBB->addSuccessor(DoneMBB);
5980 MBB->addSuccessor(SetMBB);
5981
5982 // SetMBB:
5983 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5984 // ^^ Replace the upper 32-BitSize bits of the new
5985 // value with those that we loaded.
5986 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5987 // ^^ Rotate the new field to its proper position.
5988 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5989 // JNE LoopMBB
5990 // # fall through to ExitMMB
5991 MBB = SetMBB;
5992 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5993 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5994 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5995 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5996 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
Diana Picus116bbab2017-01-13 09:58:52 +00005997 .addReg(OldVal)
5998 .addReg(StoreVal)
5999 .add(Base)
6000 .addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00006001 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6002 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006003 MBB->addSuccessor(LoopMBB);
6004 MBB->addSuccessor(DoneMBB);
6005
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006006 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006007 return DoneMBB;
6008}
6009
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006010// Emit a move from two GR64s to a GR128.
6011MachineBasicBlock *
6012SystemZTargetLowering::emitPair128(MachineInstr &MI,
6013 MachineBasicBlock *MBB) const {
6014 MachineFunction &MF = *MBB->getParent();
6015 const SystemZInstrInfo *TII =
6016 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
6017 MachineRegisterInfo &MRI = MF.getRegInfo();
6018 DebugLoc DL = MI.getDebugLoc();
6019
6020 unsigned Dest = MI.getOperand(0).getReg();
6021 unsigned Hi = MI.getOperand(1).getReg();
6022 unsigned Lo = MI.getOperand(2).getReg();
6023 unsigned Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6024 unsigned Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6025
6026 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
6027 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
6028 .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
6029 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
6030 .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
6031
6032 MI.eraseFromParent();
6033 return MBB;
6034}
6035
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006036// Emit an extension from a GR64 to a GR128. ClearEven is true
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006037// if the high register of the GR128 value must be cleared or false if
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006038// it's "don't care".
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006039MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
6040 MachineBasicBlock *MBB,
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006041 bool ClearEven) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006042 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006043 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006044 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006045 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006046 DebugLoc DL = MI.getDebugLoc();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006047
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006048 unsigned Dest = MI.getOperand(0).getReg();
6049 unsigned Src = MI.getOperand(1).getReg();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006050 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6051
6052 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
6053 if (ClearEven) {
6054 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
6055 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
6056
6057 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
6058 .addImm(0);
6059 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00006060 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006061 In128 = NewIn128;
6062 }
6063 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006064 .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006065
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006066 MI.eraseFromParent();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006067 return MBB;
6068}
6069
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006070MachineBasicBlock *SystemZTargetLowering::emitMemMemWrapper(
6071 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006072 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006073 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006074 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006075 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006076 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006077
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006078 MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
6079 uint64_t DestDisp = MI.getOperand(1).getImm();
6080 MachineOperand SrcBase = earlyUseOperand(MI.getOperand(2));
6081 uint64_t SrcDisp = MI.getOperand(3).getImm();
6082 uint64_t Length = MI.getOperand(4).getImm();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006083
Richard Sandifordbe133a82013-08-28 09:01:51 +00006084 // When generating more than one CLC, all but the last will need to
6085 // branch to the end when a difference is found.
6086 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00006087 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006088
Richard Sandiford5e318f02013-08-27 09:54:29 +00006089 // Check for the loop form, in which operand 5 is the trip count.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006090 if (MI.getNumExplicitOperands() > 5) {
Richard Sandiford5e318f02013-08-27 09:54:29 +00006091 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
6092
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006093 uint64_t StartCountReg = MI.getOperand(5).getReg();
Richard Sandiford5e318f02013-08-27 09:54:29 +00006094 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
6095 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
6096 forceReg(MI, DestBase, TII));
6097
6098 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
6099 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
6100 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
6101 MRI.createVirtualRegister(RC));
6102 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
6103 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
6104 MRI.createVirtualRegister(RC));
6105
6106 RC = &SystemZ::GR64BitRegClass;
6107 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
6108 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
6109
6110 MachineBasicBlock *StartMBB = MBB;
6111 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
6112 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00006113 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006114
6115 // StartMBB:
6116 // # fall through to LoopMMB
6117 MBB->addSuccessor(LoopMBB);
6118
6119 // LoopMBB:
6120 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006121 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006122 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006123 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00006124 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00006125 // [ %NextCountReg, NextMBB ]
6126 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00006127 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00006128 // ( JLH EndMBB )
6129 //
6130 // The prefetch is used only for MVC. The JLH is used only for CLC.
6131 MBB = LoopMBB;
6132
6133 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
6134 .addReg(StartDestReg).addMBB(StartMBB)
6135 .addReg(NextDestReg).addMBB(NextMBB);
6136 if (!HaveSingleBase)
6137 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
6138 .addReg(StartSrcReg).addMBB(StartMBB)
6139 .addReg(NextSrcReg).addMBB(NextMBB);
6140 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
6141 .addReg(StartCountReg).addMBB(StartMBB)
6142 .addReg(NextCountReg).addMBB(NextMBB);
6143 if (Opcode == SystemZ::MVC)
6144 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
6145 .addImm(SystemZ::PFD_WRITE)
6146 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
6147 BuildMI(MBB, DL, TII->get(Opcode))
6148 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
6149 .addReg(ThisSrcReg).addImm(SrcDisp);
6150 if (EndMBB) {
6151 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6152 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6153 .addMBB(EndMBB);
6154 MBB->addSuccessor(EndMBB);
6155 MBB->addSuccessor(NextMBB);
6156 }
6157
6158 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00006159 // %NextDestReg = LA 256(%ThisDestReg)
6160 // %NextSrcReg = LA 256(%ThisSrcReg)
6161 // %NextCountReg = AGHI %ThisCountReg, -1
6162 // CGHI %NextCountReg, 0
6163 // JLH LoopMBB
6164 // # fall through to DoneMMB
6165 //
6166 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00006167 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006168
Richard Sandiford5e318f02013-08-27 09:54:29 +00006169 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
6170 .addReg(ThisDestReg).addImm(256).addReg(0);
6171 if (!HaveSingleBase)
6172 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
6173 .addReg(ThisSrcReg).addImm(256).addReg(0);
6174 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
6175 .addReg(ThisCountReg).addImm(-1);
6176 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
6177 .addReg(NextCountReg).addImm(0);
6178 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6179 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6180 .addMBB(LoopMBB);
6181 MBB->addSuccessor(LoopMBB);
6182 MBB->addSuccessor(DoneMBB);
6183
6184 DestBase = MachineOperand::CreateReg(NextDestReg, false);
6185 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
6186 Length &= 255;
6187 MBB = DoneMBB;
6188 }
6189 // Handle any remaining bytes with straight-line code.
6190 while (Length > 0) {
6191 uint64_t ThisLength = std::min(Length, uint64_t(256));
6192 // The previous iteration might have created out-of-range displacements.
6193 // Apply them using LAY if so.
6194 if (!isUInt<12>(DestDisp)) {
6195 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006196 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006197 .add(DestBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006198 .addImm(DestDisp)
6199 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006200 DestBase = MachineOperand::CreateReg(Reg, false);
6201 DestDisp = 0;
6202 }
6203 if (!isUInt<12>(SrcDisp)) {
6204 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006205 BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LAY), Reg)
Diana Picus116bbab2017-01-13 09:58:52 +00006206 .add(SrcBase)
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006207 .addImm(SrcDisp)
6208 .addReg(0);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006209 SrcBase = MachineOperand::CreateReg(Reg, false);
6210 SrcDisp = 0;
6211 }
6212 BuildMI(*MBB, MI, DL, TII->get(Opcode))
Diana Picus116bbab2017-01-13 09:58:52 +00006213 .add(DestBase)
6214 .addImm(DestDisp)
6215 .addImm(ThisLength)
6216 .add(SrcBase)
Jonas Paulssonae8d22c2017-06-07 14:08:34 +00006217 .addImm(SrcDisp)
6218 ->setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
Richard Sandiford5e318f02013-08-27 09:54:29 +00006219 DestDisp += ThisLength;
6220 SrcDisp += ThisLength;
6221 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00006222 // If there's another CLC to go, branch to the end if a difference
6223 // was found.
6224 if (EndMBB && Length > 0) {
6225 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
6226 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6227 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
6228 .addMBB(EndMBB);
6229 MBB->addSuccessor(EndMBB);
6230 MBB->addSuccessor(NextMBB);
6231 MBB = NextMBB;
6232 }
6233 }
6234 if (EndMBB) {
6235 MBB->addSuccessor(EndMBB);
6236 MBB = EndMBB;
6237 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006238 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00006239
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006240 MI.eraseFromParent();
Richard Sandifordd131ff82013-07-08 09:35:23 +00006241 return MBB;
6242}
6243
Richard Sandifordca232712013-08-16 11:21:54 +00006244// Decompose string pseudo-instruction MI into a loop that continually performs
6245// Opcode until CC != 3.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006246MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
6247 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00006248 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00006249 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00006250 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00006251 MachineRegisterInfo &MRI = MF.getRegInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006252 DebugLoc DL = MI.getDebugLoc();
Richard Sandifordca232712013-08-16 11:21:54 +00006253
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006254 uint64_t End1Reg = MI.getOperand(0).getReg();
6255 uint64_t Start1Reg = MI.getOperand(1).getReg();
6256 uint64_t Start2Reg = MI.getOperand(2).getReg();
6257 uint64_t CharReg = MI.getOperand(3).getReg();
Richard Sandifordca232712013-08-16 11:21:54 +00006258
6259 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
6260 uint64_t This1Reg = MRI.createVirtualRegister(RC);
6261 uint64_t This2Reg = MRI.createVirtualRegister(RC);
6262 uint64_t End2Reg = MRI.createVirtualRegister(RC);
6263
6264 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00006265 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00006266 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
6267
6268 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00006269 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00006270 MBB->addSuccessor(LoopMBB);
6271
6272 // LoopMBB:
6273 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
6274 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00006275 // R0L = %CharReg
6276 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00006277 // JO LoopMBB
6278 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00006279 //
Richard Sandiford7789b082013-09-30 08:48:38 +00006280 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00006281 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00006282
6283 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
6284 .addReg(Start1Reg).addMBB(StartMBB)
6285 .addReg(End1Reg).addMBB(LoopMBB);
6286 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
6287 .addReg(Start2Reg).addMBB(StartMBB)
6288 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00006289 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00006290 BuildMI(MBB, DL, TII->get(Opcode))
6291 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
6292 .addReg(This1Reg).addReg(This2Reg);
6293 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
6294 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
6295 MBB->addSuccessor(LoopMBB);
6296 MBB->addSuccessor(DoneMBB);
6297
6298 DoneMBB->addLiveIn(SystemZ::CC);
6299
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006300 MI.eraseFromParent();
Richard Sandifordca232712013-08-16 11:21:54 +00006301 return DoneMBB;
6302}
6303
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006304// Update TBEGIN instruction with final opcode and register clobbers.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006305MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
6306 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
6307 bool NoFloat) const {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006308 MachineFunction &MF = *MBB->getParent();
6309 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
6310 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
6311
6312 // Update opcode.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006313 MI.setDesc(TII->get(Opcode));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006314
6315 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
6316 // Make sure to add the corresponding GRSM bits if they are missing.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006317 uint64_t Control = MI.getOperand(2).getImm();
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006318 static const unsigned GPRControlBit[16] = {
6319 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
6320 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
6321 };
6322 Control |= GPRControlBit[15];
6323 if (TFI->hasFP(MF))
6324 Control |= GPRControlBit[11];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006325 MI.getOperand(2).setImm(Control);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006326
6327 // Add GPR clobbers.
6328 for (int I = 0; I < 16; I++) {
6329 if ((Control & GPRControlBit[I]) == 0) {
6330 unsigned Reg = SystemZMC::GR64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006331 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006332 }
6333 }
6334
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006335 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006336 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006337 if (Subtarget.hasVector()) {
6338 for (int I = 0; I < 32; I++) {
6339 unsigned Reg = SystemZMC::VR128Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006340 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006341 }
6342 } else {
6343 for (int I = 0; I < 16; I++) {
6344 unsigned Reg = SystemZMC::FP64Regs[I];
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006345 MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00006346 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006347 }
6348 }
6349
6350 return MBB;
6351}
6352
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006353MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
6354 MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006355 MachineFunction &MF = *MBB->getParent();
6356 MachineRegisterInfo *MRI = &MF.getRegInfo();
6357 const SystemZInstrInfo *TII =
6358 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006359 DebugLoc DL = MI.getDebugLoc();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006360
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006361 unsigned SrcReg = MI.getOperand(0).getReg();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006362
6363 // Create new virtual register of the same class as source.
6364 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
6365 unsigned DstReg = MRI->createVirtualRegister(RC);
6366
6367 // Replace pseudo with a normal load-and-test that models the def as
6368 // well.
6369 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
6370 .addReg(SrcReg);
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006371 MI.eraseFromParent();
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006372
6373 return MBB;
6374}
6375
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00006376MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
6377 MachineInstr &MI, MachineBasicBlock *MBB) const {
6378 switch (MI.getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00006379 case SystemZ::Select32Mux:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006380 return emitSelect(MI, MBB,
6381 Subtarget.hasLoadStoreOnCond2()? SystemZ::LOCRMux : 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006382 case SystemZ::Select32:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006383 return emitSelect(MI, MBB, SystemZ::LOCR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006384 case SystemZ::Select64:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006385 return emitSelect(MI, MBB, SystemZ::LOCGR);
6386 case SystemZ::SelectF32:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006387 case SystemZ::SelectF64:
6388 case SystemZ::SelectF128:
Ulrich Weigandf2968d52017-07-17 17:44:20 +00006389 case SystemZ::SelectVR128:
Ulrich Weigand524f2762016-11-28 13:34:08 +00006390 return emitSelect(MI, MBB, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006391
Richard Sandiford2896d042013-10-01 14:33:55 +00006392 case SystemZ::CondStore8Mux:
6393 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
6394 case SystemZ::CondStore8MuxInv:
6395 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
6396 case SystemZ::CondStore16Mux:
6397 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
6398 case SystemZ::CondStore16MuxInv:
6399 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Ulrich Weigand524f2762016-11-28 13:34:08 +00006400 case SystemZ::CondStore32Mux:
6401 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
6402 case SystemZ::CondStore32MuxInv:
6403 return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006404 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006405 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006406 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006407 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006408 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006409 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006410 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006411 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006412 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006413 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006414 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006415 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006416 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006417 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006418 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006419 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006420 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006421 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006422 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006423 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006424 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006425 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006426 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00006427 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00006428
Ulrich Weiganda11f63a2017-08-04 18:57:58 +00006429 case SystemZ::PAIR128:
6430 return emitPair128(MI, MBB);
Ulrich Weigand43579cf2017-07-05 13:17:31 +00006431 case SystemZ::AEXT128:
6432 return emitExt128(MI, MBB, false);
6433 case SystemZ::ZEXT128:
6434 return emitExt128(MI, MBB, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006435
6436 case SystemZ::ATOMIC_SWAPW:
6437 return emitAtomicLoadBinary(MI, MBB, 0, 0);
6438 case SystemZ::ATOMIC_SWAP_32:
6439 return emitAtomicLoadBinary(MI, MBB, 0, 32);
6440 case SystemZ::ATOMIC_SWAP_64:
6441 return emitAtomicLoadBinary(MI, MBB, 0, 64);
6442
6443 case SystemZ::ATOMIC_LOADW_AR:
6444 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
6445 case SystemZ::ATOMIC_LOADW_AFI:
6446 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
6447 case SystemZ::ATOMIC_LOAD_AR:
6448 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
6449 case SystemZ::ATOMIC_LOAD_AHI:
6450 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
6451 case SystemZ::ATOMIC_LOAD_AFI:
6452 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
6453 case SystemZ::ATOMIC_LOAD_AGR:
6454 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
6455 case SystemZ::ATOMIC_LOAD_AGHI:
6456 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
6457 case SystemZ::ATOMIC_LOAD_AGFI:
6458 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
6459
6460 case SystemZ::ATOMIC_LOADW_SR:
6461 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
6462 case SystemZ::ATOMIC_LOAD_SR:
6463 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
6464 case SystemZ::ATOMIC_LOAD_SGR:
6465 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
6466
6467 case SystemZ::ATOMIC_LOADW_NR:
6468 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
6469 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006470 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006471 case SystemZ::ATOMIC_LOAD_NR:
6472 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006473 case SystemZ::ATOMIC_LOAD_NILL:
6474 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
6475 case SystemZ::ATOMIC_LOAD_NILH:
6476 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
6477 case SystemZ::ATOMIC_LOAD_NILF:
6478 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006479 case SystemZ::ATOMIC_LOAD_NGR:
6480 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006481 case SystemZ::ATOMIC_LOAD_NILL64:
6482 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
6483 case SystemZ::ATOMIC_LOAD_NILH64:
6484 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006485 case SystemZ::ATOMIC_LOAD_NIHL64:
6486 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
6487 case SystemZ::ATOMIC_LOAD_NIHH64:
6488 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006489 case SystemZ::ATOMIC_LOAD_NILF64:
6490 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00006491 case SystemZ::ATOMIC_LOAD_NIHF64:
6492 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006493
6494 case SystemZ::ATOMIC_LOADW_OR:
6495 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
6496 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00006497 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006498 case SystemZ::ATOMIC_LOAD_OR:
6499 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006500 case SystemZ::ATOMIC_LOAD_OILL:
6501 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
6502 case SystemZ::ATOMIC_LOAD_OILH:
6503 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
6504 case SystemZ::ATOMIC_LOAD_OILF:
6505 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006506 case SystemZ::ATOMIC_LOAD_OGR:
6507 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006508 case SystemZ::ATOMIC_LOAD_OILL64:
6509 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
6510 case SystemZ::ATOMIC_LOAD_OILH64:
6511 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006512 case SystemZ::ATOMIC_LOAD_OIHL64:
6513 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
6514 case SystemZ::ATOMIC_LOAD_OIHH64:
6515 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006516 case SystemZ::ATOMIC_LOAD_OILF64:
6517 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00006518 case SystemZ::ATOMIC_LOAD_OIHF64:
6519 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006520
6521 case SystemZ::ATOMIC_LOADW_XR:
6522 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
6523 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00006524 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006525 case SystemZ::ATOMIC_LOAD_XR:
6526 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00006527 case SystemZ::ATOMIC_LOAD_XILF:
6528 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006529 case SystemZ::ATOMIC_LOAD_XGR:
6530 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00006531 case SystemZ::ATOMIC_LOAD_XILF64:
6532 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00006533 case SystemZ::ATOMIC_LOAD_XIHF64:
6534 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006535
6536 case SystemZ::ATOMIC_LOADW_NRi:
6537 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
6538 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00006539 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006540 case SystemZ::ATOMIC_LOAD_NRi:
6541 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006542 case SystemZ::ATOMIC_LOAD_NILLi:
6543 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
6544 case SystemZ::ATOMIC_LOAD_NILHi:
6545 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
6546 case SystemZ::ATOMIC_LOAD_NILFi:
6547 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006548 case SystemZ::ATOMIC_LOAD_NGRi:
6549 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006550 case SystemZ::ATOMIC_LOAD_NILL64i:
6551 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
6552 case SystemZ::ATOMIC_LOAD_NILH64i:
6553 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006554 case SystemZ::ATOMIC_LOAD_NIHL64i:
6555 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
6556 case SystemZ::ATOMIC_LOAD_NIHH64i:
6557 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00006558 case SystemZ::ATOMIC_LOAD_NILF64i:
6559 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00006560 case SystemZ::ATOMIC_LOAD_NIHF64i:
6561 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006562
6563 case SystemZ::ATOMIC_LOADW_MIN:
6564 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6565 SystemZ::CCMASK_CMP_LE, 0);
6566 case SystemZ::ATOMIC_LOAD_MIN_32:
6567 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6568 SystemZ::CCMASK_CMP_LE, 32);
6569 case SystemZ::ATOMIC_LOAD_MIN_64:
6570 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6571 SystemZ::CCMASK_CMP_LE, 64);
6572
6573 case SystemZ::ATOMIC_LOADW_MAX:
6574 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6575 SystemZ::CCMASK_CMP_GE, 0);
6576 case SystemZ::ATOMIC_LOAD_MAX_32:
6577 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
6578 SystemZ::CCMASK_CMP_GE, 32);
6579 case SystemZ::ATOMIC_LOAD_MAX_64:
6580 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
6581 SystemZ::CCMASK_CMP_GE, 64);
6582
6583 case SystemZ::ATOMIC_LOADW_UMIN:
6584 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6585 SystemZ::CCMASK_CMP_LE, 0);
6586 case SystemZ::ATOMIC_LOAD_UMIN_32:
6587 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6588 SystemZ::CCMASK_CMP_LE, 32);
6589 case SystemZ::ATOMIC_LOAD_UMIN_64:
6590 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6591 SystemZ::CCMASK_CMP_LE, 64);
6592
6593 case SystemZ::ATOMIC_LOADW_UMAX:
6594 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6595 SystemZ::CCMASK_CMP_GE, 0);
6596 case SystemZ::ATOMIC_LOAD_UMAX_32:
6597 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6598 SystemZ::CCMASK_CMP_GE, 32);
6599 case SystemZ::ATOMIC_LOAD_UMAX_64:
6600 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6601 SystemZ::CCMASK_CMP_GE, 64);
6602
6603 case SystemZ::ATOMIC_CMP_SWAPW:
6604 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006605 case SystemZ::MVCSequence:
6606 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006607 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006608 case SystemZ::NCSequence:
6609 case SystemZ::NCLoop:
6610 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6611 case SystemZ::OCSequence:
6612 case SystemZ::OCLoop:
6613 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6614 case SystemZ::XCSequence:
6615 case SystemZ::XCLoop:
6616 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006617 case SystemZ::CLCSequence:
6618 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006619 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006620 case SystemZ::CLSTLoop:
6621 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006622 case SystemZ::MVSTLoop:
6623 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006624 case SystemZ::SRSTLoop:
6625 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006626 case SystemZ::TBEGIN:
6627 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6628 case SystemZ::TBEGIN_nofloat:
6629 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6630 case SystemZ::TBEGINC:
6631 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006632 case SystemZ::LTEBRCompare_VecPseudo:
6633 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6634 case SystemZ::LTDBRCompare_VecPseudo:
6635 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6636 case SystemZ::LTXBRCompare_VecPseudo:
6637 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6638
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006639 default:
6640 llvm_unreachable("Unexpected instr type to insert");
6641 }
6642}
Jonas Paulsson11d251c2017-05-10 13:03:25 +00006643
6644// This is only used by the isel schedulers, and is needed only to prevent
6645// compiler from crashing when list-ilp is used.
6646const TargetRegisterClass *
6647SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
6648 if (VT == MVT::Untyped)
6649 return &SystemZ::ADDR128BitRegClass;
6650 return TargetLowering::getRepRegClassFor(VT);
6651}