blob: 9848c2a571a816c31a2dafd109380a6d001d3c92 [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"
Will Dietz981af002013-10-12 00:55:57 +000024#include <cctype>
25
Ulrich Weigand5f613df2013-05-06 16:15:19 +000026using namespace llvm;
27
Chandler Carruth84e68b22014-04-22 02:41:26 +000028#define DEBUG_TYPE "systemz-lower"
29
Richard Sandifordf722a8e302013-10-16 11:10:55 +000030namespace {
31// Represents a sequence for extracting a 0/1 value from an IPM result:
32// (((X ^ XORValue) + AddValue) >> Bit)
33struct IPMConversion {
34 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
35 : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
36
37 int64_t XORValue;
38 int64_t AddValue;
39 unsigned Bit;
40};
Richard Sandifordd420f732013-12-13 15:28:45 +000041
42// Represents information about a comparison.
43struct Comparison {
44 Comparison(SDValue Op0In, SDValue Op1In)
45 : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
46
47 // The operands to the comparison.
48 SDValue Op0, Op1;
49
50 // The opcode that should be used to compare Op0 and Op1.
51 unsigned Opcode;
52
53 // A SystemZICMP value. Only used for integer comparisons.
54 unsigned ICmpType;
55
56 // The mask of CC values that Opcode can produce.
57 unsigned CCValid;
58
59 // The mask of CC values for which the original condition is true.
60 unsigned CCMask;
61};
Richard Sandifordc2312692014-03-06 10:38:30 +000062} // end anonymous namespace
Richard Sandifordf722a8e302013-10-16 11:10:55 +000063
Ulrich Weigand5f613df2013-05-06 16:15:19 +000064// Classify VT as either 32 or 64 bit.
65static bool is32Bit(EVT VT) {
66 switch (VT.getSimpleVT().SimpleTy) {
67 case MVT::i32:
68 return true;
69 case MVT::i64:
70 return false;
71 default:
72 llvm_unreachable("Unsupported type");
73 }
74}
75
76// Return a version of MachineOperand that can be safely used before the
77// final use.
78static MachineOperand earlyUseOperand(MachineOperand Op) {
79 if (Op.isReg())
80 Op.setIsKill(false);
81 return Op;
82}
83
Mehdi Amini44ede332015-07-09 02:09:04 +000084SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
Eric Christophera6734172015-01-31 00:06:45 +000085 const SystemZSubtarget &STI)
Mehdi Amini44ede332015-07-09 02:09:04 +000086 : TargetLowering(TM), Subtarget(STI) {
Mehdi Amini26d48132015-07-24 16:04:22 +000087 MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
Ulrich Weigand5f613df2013-05-06 16:15:19 +000088
89 // Set up the register classes.
Richard Sandiford0755c932013-10-01 11:26:28 +000090 if (Subtarget.hasHighWord())
91 addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
92 else
93 addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
Ulrich Weigand49506d72015-05-05 19:28:34 +000094 addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
95 if (Subtarget.hasVector()) {
96 addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
97 addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
98 } else {
99 addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
100 addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
101 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000102 addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
103
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000104 if (Subtarget.hasVector()) {
105 addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
106 addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
107 addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
108 addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000109 addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000110 addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000111 }
112
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000113 // Compute derived properties from the register classes
Eric Christopher23a3a7c2015-02-26 00:00:24 +0000114 computeRegisterProperties(Subtarget.getRegisterInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000115
116 // Set up special registers.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000117 setStackPointerRegisterToSaveRestore(SystemZ::R15D);
118
119 // TODO: It may be better to default to latency-oriented scheduling, however
120 // LLVM's current latency-oriented scheduler can't handle physreg definitions
Richard Sandiford14a44492013-05-22 13:38:45 +0000121 // such as SystemZ has with CC, so set this to the register-pressure
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000122 // scheduler, because it can.
123 setSchedulingPreference(Sched::RegPressure);
124
125 setBooleanContents(ZeroOrOneBooleanContent);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000126 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127
128 // Instructions are strings of 2-byte aligned 2-byte values.
129 setMinFunctionAlignment(2);
130
131 // Handle operations that are handled in a similar way for all types.
132 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
133 I <= MVT::LAST_FP_VALUETYPE;
134 ++I) {
135 MVT VT = MVT::SimpleValueType(I);
136 if (isTypeLegal(VT)) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +0000137 // Lower SET_CC into an IPM-based sequence.
138 setOperationAction(ISD::SETCC, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000139
140 // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
141 setOperationAction(ISD::SELECT, VT, Expand);
142
143 // Lower SELECT_CC and BR_CC into separate comparisons and branches.
144 setOperationAction(ISD::SELECT_CC, VT, Custom);
145 setOperationAction(ISD::BR_CC, VT, Custom);
146 }
147 }
148
149 // Expand jump table branches as address arithmetic followed by an
150 // indirect jump.
151 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152
153 // Expand BRCOND into a BR_CC (see above).
154 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
155
156 // Handle integer types.
157 for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
158 I <= MVT::LAST_INTEGER_VALUETYPE;
159 ++I) {
160 MVT VT = MVT::SimpleValueType(I);
161 if (isTypeLegal(VT)) {
162 // Expand individual DIV and REMs into DIVREMs.
163 setOperationAction(ISD::SDIV, VT, Expand);
164 setOperationAction(ISD::UDIV, VT, Expand);
165 setOperationAction(ISD::SREM, VT, Expand);
166 setOperationAction(ISD::UREM, VT, Expand);
167 setOperationAction(ISD::SDIVREM, VT, Custom);
168 setOperationAction(ISD::UDIVREM, VT, Custom);
169
Richard Sandifordbef3d7a2013-12-10 10:49:34 +0000170 // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
171 // stores, putting a serialization instruction after the stores.
172 setOperationAction(ISD::ATOMIC_LOAD, VT, Custom);
173 setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000174
Richard Sandiford41350a52013-12-24 15:18:04 +0000175 // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
176 // available, or if the operand is constant.
177 setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
178
Ulrich Weigandb4012182015-03-31 12:56:33 +0000179 // Use POPCNT on z196 and above.
180 if (Subtarget.hasPopulationCount())
181 setOperationAction(ISD::CTPOP, VT, Custom);
182 else
183 setOperationAction(ISD::CTPOP, VT, Expand);
184
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 // No special instructions for these.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000186 setOperationAction(ISD::CTTZ, VT, Expand);
187 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
188 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
189 setOperationAction(ISD::ROTR, VT, Expand);
190
Richard Sandiford7d86e472013-08-21 09:34:56 +0000191 // Use *MUL_LOHI where possible instead of MULH*.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000192 setOperationAction(ISD::MULHS, VT, Expand);
193 setOperationAction(ISD::MULHU, VT, Expand);
Richard Sandiford7d86e472013-08-21 09:34:56 +0000194 setOperationAction(ISD::SMUL_LOHI, VT, Custom);
195 setOperationAction(ISD::UMUL_LOHI, VT, Custom);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000197 // Only z196 and above have native support for conversions to unsigned.
198 if (!Subtarget.hasFPExtension())
199 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200 }
201 }
202
203 // Type legalization will convert 8- and 16-bit atomic operations into
204 // forms that operate on i32s (but still keeping the original memory VT).
205 // Lower them into full i32 operations.
206 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Custom);
207 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
209 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i32, Custom);
210 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i32, Custom);
211 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i32, Custom);
212 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
213 setOperationAction(ISD::ATOMIC_LOAD_MIN, MVT::i32, Custom);
214 setOperationAction(ISD::ATOMIC_LOAD_MAX, MVT::i32, Custom);
215 setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
216 setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
217 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
218
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +0000219 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
220
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000221 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000223 if (!Subtarget.hasFPExtension()) {
224 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
225 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
226 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000227
228 // We have native support for a 64-bit CTLZ, via FLOGR.
229 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
230 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
231
232 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
233 setOperationAction(ISD::OR, MVT::i64, Custom);
234
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000235 // FIXME: Can we support these natively?
236 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
237 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
238 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
239
240 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000241 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000242 for (MVT VT : MVT::integer_valuetypes()) {
243 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
244 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
245 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
246 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247
248 // Handle the various types of symbolic address.
249 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
250 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
251 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
252 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
253 setOperationAction(ISD::JumpTable, PtrVT, Custom);
254
255 // We need to handle dynamic allocations specially because of the
256 // 160-byte area at the bottom of the stack.
257 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
258
259 // Use custom expanders so that we can force the function to use
260 // a frame pointer.
261 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
262 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
263
Richard Sandiford03481332013-08-23 11:36:42 +0000264 // Handle prefetches with PFD or PFDRL.
265 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
266
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000267 for (MVT VT : MVT::vector_valuetypes()) {
268 // Assume by default that all vector operations need to be expanded.
269 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
270 if (getOperationAction(Opcode, VT) == Legal)
271 setOperationAction(Opcode, VT, Expand);
272
273 // Likewise all truncating stores and extending loads.
274 for (MVT InnerVT : MVT::vector_valuetypes()) {
275 setTruncStoreAction(VT, InnerVT, Expand);
276 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
277 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
278 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
279 }
280
281 if (isTypeLegal(VT)) {
282 // These operations are legal for anything that can be stored in a
283 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000284 // as such. In particular, we can do these for v4f32 even though there
285 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000286 setOperationAction(ISD::LOAD, VT, Legal);
287 setOperationAction(ISD::STORE, VT, Legal);
288 setOperationAction(ISD::VSELECT, VT, Legal);
289 setOperationAction(ISD::BITCAST, VT, Legal);
290 setOperationAction(ISD::UNDEF, VT, Legal);
291
292 // Likewise, except that we need to replace the nodes with something
293 // more specific.
294 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
295 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
296 }
297 }
298
299 // Handle integer vector types.
300 for (MVT VT : MVT::integer_vector_valuetypes()) {
301 if (isTypeLegal(VT)) {
302 // These operations have direct equivalents.
303 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
304 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
305 setOperationAction(ISD::ADD, VT, Legal);
306 setOperationAction(ISD::SUB, VT, Legal);
307 if (VT != MVT::v2i64)
308 setOperationAction(ISD::MUL, VT, Legal);
309 setOperationAction(ISD::AND, VT, Legal);
310 setOperationAction(ISD::OR, VT, Legal);
311 setOperationAction(ISD::XOR, VT, Legal);
312 setOperationAction(ISD::CTPOP, VT, Custom);
313 setOperationAction(ISD::CTTZ, VT, Legal);
314 setOperationAction(ISD::CTLZ, VT, Legal);
315 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
316 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
317
318 // Convert a GPR scalar to a vector by inserting it into element 0.
319 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
320
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000321 // Use a series of unpacks for extensions.
322 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
323 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
324
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000325 // Detect shifts by a scalar amount and convert them into
326 // V*_BY_SCALAR.
327 setOperationAction(ISD::SHL, VT, Custom);
328 setOperationAction(ISD::SRA, VT, Custom);
329 setOperationAction(ISD::SRL, VT, Custom);
330
331 // At present ROTL isn't matched by DAGCombiner. ROTR should be
332 // converted into ROTL.
333 setOperationAction(ISD::ROTL, VT, Expand);
334 setOperationAction(ISD::ROTR, VT, Expand);
335
336 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
337 // and inverting the result as necessary.
338 setOperationAction(ISD::SETCC, VT, Custom);
339 }
340 }
341
Ulrich Weigandcd808232015-05-05 19:26:48 +0000342 if (Subtarget.hasVector()) {
343 // There should be no need to check for float types other than v2f64
344 // since <2 x f32> isn't a legal type.
345 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
346 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
347 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
348 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
349 }
350
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351 // Handle floating-point types.
352 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
353 I <= MVT::LAST_FP_VALUETYPE;
354 ++I) {
355 MVT VT = MVT::SimpleValueType(I);
356 if (isTypeLegal(VT)) {
357 // We can use FI for FRINT.
358 setOperationAction(ISD::FRINT, VT, Legal);
359
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000360 // We can use the extended form of FI for other rounding operations.
361 if (Subtarget.hasFPExtension()) {
362 setOperationAction(ISD::FNEARBYINT, VT, Legal);
363 setOperationAction(ISD::FFLOOR, VT, Legal);
364 setOperationAction(ISD::FCEIL, VT, Legal);
365 setOperationAction(ISD::FTRUNC, VT, Legal);
366 setOperationAction(ISD::FROUND, VT, Legal);
367 }
368
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000369 // No special instructions for these.
370 setOperationAction(ISD::FSIN, VT, Expand);
371 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000372 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000374 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000375 }
376 }
377
Ulrich Weigandcd808232015-05-05 19:26:48 +0000378 // Handle floating-point vector types.
379 if (Subtarget.hasVector()) {
380 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000381 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000382 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
383
384 // Some insertions and extractions can be done directly but others
385 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000386 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000387 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000388 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000389 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
390
391 // These operations have direct equivalents.
392 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
393 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
394 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
395 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
396 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
397 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
398 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
399 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
400 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
401 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
402 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
403 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
404 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
405 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
406 }
407
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000408 // We have fused multiply-addition for f32 and f64 but not f128.
409 setOperationAction(ISD::FMA, MVT::f32, Legal);
410 setOperationAction(ISD::FMA, MVT::f64, Legal);
411 setOperationAction(ISD::FMA, MVT::f128, Expand);
412
413 // Needed so that we don't try to implement f128 constant loads using
414 // a load-and-extend of a f80 constant (in cases where the constant
415 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000416 for (MVT VT : MVT::fp_valuetypes())
417 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000418
419 // Floating-point truncation and stores need to be done separately.
420 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
421 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
422 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
423
424 // We have 64-bit FPR<->GPR moves, but need special handling for
425 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000426 if (!Subtarget.hasVector()) {
427 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
428 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
429 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000430
431 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
432 // structure, but VAEND is a no-op.
433 setOperationAction(ISD::VASTART, MVT::Other, Custom);
434 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
435 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000436
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000437 // Codes for which we want to perform some z-specific combinations.
438 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000439 setTargetDAGCombine(ISD::STORE);
440 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000441 setTargetDAGCombine(ISD::FP_ROUND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000442
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000443 // Handle intrinsics.
444 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000445 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000446
Richard Sandifordd131ff82013-07-08 09:35:23 +0000447 // We want to use MVC in preference to even a single load/store pair.
448 MaxStoresPerMemcpy = 0;
449 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000450
451 // The main memset sequence is a byte store followed by an MVC.
452 // Two STC or MV..I stores win over that, but the kind of fused stores
453 // generated by target-independent code don't when the byte value is
454 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
455 // than "STC;MVC". Handle the choice in target-specific code instead.
456 MaxStoresPerMemset = 0;
457 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000458}
459
Mehdi Amini44ede332015-07-09 02:09:04 +0000460EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
461 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000462 if (!VT.isVector())
463 return MVT::i32;
464 return VT.changeVectorElementTypeToInteger();
465}
466
467bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000468 VT = VT.getScalarType();
469
470 if (!VT.isSimple())
471 return false;
472
473 switch (VT.getSimpleVT().SimpleTy) {
474 case MVT::f32:
475 case MVT::f64:
476 return true;
477 case MVT::f128:
478 return false;
479 default:
480 break;
481 }
482
483 return false;
484}
485
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000486bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
487 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
488 return Imm.isZero() || Imm.isNegZero();
489}
490
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000491bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
492 // We can use CGFI or CLGFI.
493 return isInt<32>(Imm) || isUInt<32>(Imm);
494}
495
496bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
497 // We can use ALGFI or SLGFI.
498 return isUInt<32>(Imm) || isUInt<32>(-Imm);
499}
500
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000501bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
502 unsigned,
503 unsigned,
504 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000505 // Unaligned accesses should never be slower than the expanded version.
506 // We check specifically for aligned accesses in the few cases where
507 // they are required.
508 if (Fast)
509 *Fast = true;
510 return true;
511}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000512
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000513bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
514 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000515 unsigned AS) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000516 // Punt on globals for now, although they can be used in limited
517 // RELATIVE LONG cases.
518 if (AM.BaseGV)
519 return false;
520
521 // Require a 20-bit signed offset.
522 if (!isInt<20>(AM.BaseOffs))
523 return false;
524
525 // Indexing is OK but no scale factor can be applied.
526 return AM.Scale == 0 || AM.Scale == 1;
527}
528
Richard Sandiford709bda62013-08-19 12:42:31 +0000529bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
530 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
531 return false;
532 unsigned FromBits = FromType->getPrimitiveSizeInBits();
533 unsigned ToBits = ToType->getPrimitiveSizeInBits();
534 return FromBits > ToBits;
535}
536
537bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
538 if (!FromVT.isInteger() || !ToVT.isInteger())
539 return false;
540 unsigned FromBits = FromVT.getSizeInBits();
541 unsigned ToBits = ToVT.getSizeInBits();
542 return FromBits > ToBits;
543}
544
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000545//===----------------------------------------------------------------------===//
546// Inline asm support
547//===----------------------------------------------------------------------===//
548
549TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000550SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000551 if (Constraint.size() == 1) {
552 switch (Constraint[0]) {
553 case 'a': // Address register
554 case 'd': // Data register (equivalent to 'r')
555 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000556 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000557 case 'r': // General-purpose register
558 return C_RegisterClass;
559
560 case 'Q': // Memory with base and unsigned 12-bit displacement
561 case 'R': // Likewise, plus an index
562 case 'S': // Memory with base and signed 20-bit displacement
563 case 'T': // Likewise, plus an index
564 case 'm': // Equivalent to 'T'.
565 return C_Memory;
566
567 case 'I': // Unsigned 8-bit constant
568 case 'J': // Unsigned 12-bit constant
569 case 'K': // Signed 16-bit constant
570 case 'L': // Signed 20-bit displacement (on all targets we support)
571 case 'M': // 0x7fffffff
572 return C_Other;
573
574 default:
575 break;
576 }
577 }
578 return TargetLowering::getConstraintType(Constraint);
579}
580
581TargetLowering::ConstraintWeight SystemZTargetLowering::
582getSingleConstraintMatchWeight(AsmOperandInfo &info,
583 const char *constraint) const {
584 ConstraintWeight weight = CW_Invalid;
585 Value *CallOperandVal = info.CallOperandVal;
586 // If we don't have a value, we can't do a match,
587 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000588 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000589 return CW_Default;
590 Type *type = CallOperandVal->getType();
591 // Look at the constraint type.
592 switch (*constraint) {
593 default:
594 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
595 break;
596
597 case 'a': // Address register
598 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000599 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000600 case 'r': // General-purpose register
601 if (CallOperandVal->getType()->isIntegerTy())
602 weight = CW_Register;
603 break;
604
605 case 'f': // Floating-point register
606 if (type->isFloatingPointTy())
607 weight = CW_Register;
608 break;
609
610 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000611 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000612 if (isUInt<8>(C->getZExtValue()))
613 weight = CW_Constant;
614 break;
615
616 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000617 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000618 if (isUInt<12>(C->getZExtValue()))
619 weight = CW_Constant;
620 break;
621
622 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000623 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000624 if (isInt<16>(C->getSExtValue()))
625 weight = CW_Constant;
626 break;
627
628 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000629 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000630 if (isInt<20>(C->getSExtValue()))
631 weight = CW_Constant;
632 break;
633
634 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000635 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000636 if (C->getZExtValue() == 0x7fffffff)
637 weight = CW_Constant;
638 break;
639 }
640 return weight;
641}
642
Richard Sandifordb8204052013-07-12 09:08:12 +0000643// Parse a "{tNNN}" register constraint for which the register type "t"
644// has already been verified. MC is the class associated with "t" and
645// Map maps 0-based register numbers to LLVM register numbers.
646static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000647parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
648 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000649 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
650 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000651 unsigned Index;
652 bool Failed =
653 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
654 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000655 return std::make_pair(Map[Index], RC);
656 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000657 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000658}
659
Eric Christopher11e4df72015-02-26 22:38:43 +0000660std::pair<unsigned, const TargetRegisterClass *>
661SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000662 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000663 if (Constraint.size() == 1) {
664 // GCC Constraint Letters
665 switch (Constraint[0]) {
666 default: break;
667 case 'd': // Data register (equivalent to 'r')
668 case 'r': // General-purpose register
669 if (VT == MVT::i64)
670 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
671 else if (VT == MVT::i128)
672 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
673 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
674
675 case 'a': // Address register
676 if (VT == MVT::i64)
677 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
678 else if (VT == MVT::i128)
679 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
680 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
681
Richard Sandiford0755c932013-10-01 11:26:28 +0000682 case 'h': // High-part register (an LLVM extension)
683 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
684
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000685 case 'f': // Floating-point register
686 if (VT == MVT::f64)
687 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
688 else if (VT == MVT::f128)
689 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
690 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
691 }
692 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000693 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000694 // We need to override the default register parsing for GPRs and FPRs
695 // because the interpretation depends on VT. The internal names of
696 // the registers are also different from the external names
697 // (F0D and F0S instead of F0, etc.).
698 if (Constraint[1] == 'r') {
699 if (VT == MVT::i32)
700 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
701 SystemZMC::GR32Regs);
702 if (VT == MVT::i128)
703 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
704 SystemZMC::GR128Regs);
705 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
706 SystemZMC::GR64Regs);
707 }
708 if (Constraint[1] == 'f') {
709 if (VT == MVT::f32)
710 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
711 SystemZMC::FP32Regs);
712 if (VT == MVT::f128)
713 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
714 SystemZMC::FP128Regs);
715 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
716 SystemZMC::FP64Regs);
717 }
718 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000719 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000720}
721
722void SystemZTargetLowering::
723LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
724 std::vector<SDValue> &Ops,
725 SelectionDAG &DAG) const {
726 // Only support length 1 constraints for now.
727 if (Constraint.length() == 1) {
728 switch (Constraint[0]) {
729 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000730 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000731 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000732 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000733 Op.getValueType()));
734 return;
735
736 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000737 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000739 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000740 Op.getValueType()));
741 return;
742
743 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000744 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000746 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000747 Op.getValueType()));
748 return;
749
750 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000751 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000752 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000753 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000754 Op.getValueType()));
755 return;
756
757 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000758 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000760 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000761 Op.getValueType()));
762 return;
763 }
764 }
765 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
766}
767
768//===----------------------------------------------------------------------===//
769// Calling conventions
770//===----------------------------------------------------------------------===//
771
772#include "SystemZGenCallingConv.inc"
773
Richard Sandiford709bda62013-08-19 12:42:31 +0000774bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
775 Type *ToType) const {
776 return isTruncateFree(FromType, ToType);
777}
778
779bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000780 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000781}
782
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000783// We do not yet support 128-bit single-element vector types. If the user
784// attempts to use such types as function argument or return type, prefer
785// to error out instead of emitting code violating the ABI.
786static void VerifyVectorType(MVT VT, EVT ArgVT) {
787 if (ArgVT.isVector() && !VT.isVector())
788 report_fatal_error("Unsupported vector argument or return type");
789}
790
791static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
792 for (unsigned i = 0; i < Ins.size(); ++i)
793 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
794}
795
796static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
797 for (unsigned i = 0; i < Outs.size(); ++i)
798 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
799}
800
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000801// Value is a value that has been passed to us in the location described by VA
802// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
803// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000804static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000805 CCValAssign &VA, SDValue Chain,
806 SDValue Value) {
807 // If the argument has been promoted from a smaller type, insert an
808 // assertion to capture this.
809 if (VA.getLocInfo() == CCValAssign::SExt)
810 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
811 DAG.getValueType(VA.getValVT()));
812 else if (VA.getLocInfo() == CCValAssign::ZExt)
813 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
814 DAG.getValueType(VA.getValVT()));
815
816 if (VA.isExtInLoc())
817 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000818 else if (VA.getLocInfo() == CCValAssign::BCvt) {
819 // If this is a short vector argument loaded from the stack,
820 // extend from i64 to full vector size and then bitcast.
821 assert(VA.getLocVT() == MVT::i64);
822 assert(VA.getValVT().isVector());
823 Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64,
824 Value, DAG.getUNDEF(MVT::i64));
825 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
826 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000827 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
828 return Value;
829}
830
831// Value is a value of type VA.getValVT() that we need to copy into
832// the location described by VA. Return a copy of Value converted to
833// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000834static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000835 CCValAssign &VA, SDValue Value) {
836 switch (VA.getLocInfo()) {
837 case CCValAssign::SExt:
838 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
839 case CCValAssign::ZExt:
840 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
841 case CCValAssign::AExt:
842 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000843 case CCValAssign::BCvt:
844 // If this is a short vector argument to be stored to the stack,
845 // bitcast to v2i64 and then extract first element.
846 assert(VA.getLocVT() == MVT::i64);
847 assert(VA.getValVT().isVector());
848 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
849 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
850 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000851 case CCValAssign::Full:
852 return Value;
853 default:
854 llvm_unreachable("Unhandled getLocInfo()");
855 }
856}
857
858SDValue SystemZTargetLowering::
859LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
860 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000861 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000862 SmallVectorImpl<SDValue> &InVals) const {
863 MachineFunction &MF = DAG.getMachineFunction();
864 MachineFrameInfo *MFI = MF.getFrameInfo();
865 MachineRegisterInfo &MRI = MF.getRegInfo();
866 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000867 MF.getInfo<SystemZMachineFunctionInfo>();
868 auto *TFL =
869 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000870 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000871
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000872 // Detect unsupported vector argument types.
873 if (Subtarget.hasVector())
874 VerifyVectorTypes(Ins);
875
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000876 // Assign locations to all of the incoming arguments.
877 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000878 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000879 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
880
881 unsigned NumFixedGPRs = 0;
882 unsigned NumFixedFPRs = 0;
883 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
884 SDValue ArgValue;
885 CCValAssign &VA = ArgLocs[I];
886 EVT LocVT = VA.getLocVT();
887 if (VA.isRegLoc()) {
888 // Arguments passed in registers
889 const TargetRegisterClass *RC;
890 switch (LocVT.getSimpleVT().SimpleTy) {
891 default:
892 // Integers smaller than i64 should be promoted to i64.
893 llvm_unreachable("Unexpected argument type");
894 case MVT::i32:
895 NumFixedGPRs += 1;
896 RC = &SystemZ::GR32BitRegClass;
897 break;
898 case MVT::i64:
899 NumFixedGPRs += 1;
900 RC = &SystemZ::GR64BitRegClass;
901 break;
902 case MVT::f32:
903 NumFixedFPRs += 1;
904 RC = &SystemZ::FP32BitRegClass;
905 break;
906 case MVT::f64:
907 NumFixedFPRs += 1;
908 RC = &SystemZ::FP64BitRegClass;
909 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000910 case MVT::v16i8:
911 case MVT::v8i16:
912 case MVT::v4i32:
913 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000914 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000915 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000916 RC = &SystemZ::VR128BitRegClass;
917 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000918 }
919
920 unsigned VReg = MRI.createVirtualRegister(RC);
921 MRI.addLiveIn(VA.getLocReg(), VReg);
922 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
923 } else {
924 assert(VA.isMemLoc() && "Argument not register or memory");
925
926 // Create the frame index object for this incoming parameter.
927 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
928 VA.getLocMemOffset(), true);
929
930 // Create the SelectionDAG nodes corresponding to a load
931 // from this parameter. Unpromoted ints and floats are
932 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000933 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
934 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000935 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
936 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000937 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000938 MachinePointerInfo::getFixedStack(MF, FI), false,
939 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000940 }
941
942 // Convert the value of the argument register into the value that's
943 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000944 if (VA.getLocInfo() == CCValAssign::Indirect) {
945 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
946 ArgValue, MachinePointerInfo(),
947 false, false, false, 0));
948 // If the original argument was split (e.g. i128), we need
949 // to load all parts of it here (using the same address).
950 unsigned ArgIndex = Ins[I].OrigArgIndex;
951 assert (Ins[I].PartOffset == 0);
952 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
953 CCValAssign &PartVA = ArgLocs[I + 1];
954 unsigned PartOffset = Ins[I + 1].PartOffset;
955 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
956 DAG.getIntPtrConstant(PartOffset, DL));
957 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain,
958 Address, MachinePointerInfo(),
959 false, false, false, 0));
960 ++I;
961 }
962 } else
963 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000964 }
965
966 if (IsVarArg) {
967 // Save the number of non-varargs registers for later use by va_start, etc.
968 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
969 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
970
971 // Likewise the address (in the form of a frame index) of where the
972 // first stack vararg would be. The 1-byte size here is arbitrary.
973 int64_t StackSize = CCInfo.getNextStackOffset();
974 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
975
976 // ...and a similar frame index for the caller-allocated save area
977 // that will be used to store the incoming registers.
978 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
979 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
980 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
981
982 // Store the FPR varargs in the reserved frame slots. (We store the
983 // GPRs as part of the prologue.)
984 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
985 SDValue MemOps[SystemZ::NumArgFPRs];
986 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
987 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
988 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000989 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000990 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
991 &SystemZ::FP64BitRegClass);
992 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
993 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000994 MachinePointerInfo::getFixedStack(MF, FI),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000995 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000996 }
997 // Join the stores, which are independent of one another.
998 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000999 makeArrayRef(&MemOps[NumFixedFPRs],
1000 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001001 }
1002 }
1003
1004 return Chain;
1005}
1006
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001007static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +00001008 SmallVectorImpl<CCValAssign> &ArgLocs) {
1009 // Punt if there are any indirect or stack arguments, or if the call
1010 // needs the call-saved argument register R6.
1011 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1012 CCValAssign &VA = ArgLocs[I];
1013 if (VA.getLocInfo() == CCValAssign::Indirect)
1014 return false;
1015 if (!VA.isRegLoc())
1016 return false;
1017 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001018 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001019 return false;
1020 }
1021 return true;
1022}
1023
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001024SDValue
1025SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1026 SmallVectorImpl<SDValue> &InVals) const {
1027 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001028 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001029 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1030 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1031 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001032 SDValue Chain = CLI.Chain;
1033 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001034 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001035 CallingConv::ID CallConv = CLI.CallConv;
1036 bool IsVarArg = CLI.IsVarArg;
1037 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001038 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001039
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001040 // Detect unsupported vector argument and return types.
1041 if (Subtarget.hasVector()) {
1042 VerifyVectorTypes(Outs);
1043 VerifyVectorTypes(Ins);
1044 }
1045
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001046 // Analyze the operands of the call, assigning locations to each operand.
1047 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001048 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001049 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1050
Richard Sandiford709bda62013-08-19 12:42:31 +00001051 // We don't support GuaranteedTailCallOpt, only automatically-detected
1052 // sibling calls.
1053 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
1054 IsTailCall = false;
1055
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001056 // Get a count of how many bytes are to be pushed on the stack.
1057 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1058
1059 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001060 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001061 Chain = DAG.getCALLSEQ_START(Chain,
1062 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001063 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001064
1065 // Copy argument values to their designated locations.
1066 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1067 SmallVector<SDValue, 8> MemOpChains;
1068 SDValue StackPtr;
1069 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1070 CCValAssign &VA = ArgLocs[I];
1071 SDValue ArgValue = OutVals[I];
1072
1073 if (VA.getLocInfo() == CCValAssign::Indirect) {
1074 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001075 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001076 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001077 MemOpChains.push_back(DAG.getStore(
1078 Chain, DL, ArgValue, SpillSlot,
1079 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001080 // If the original argument was split (e.g. i128), we need
1081 // to store all parts of it here (and pass just one address).
1082 unsigned ArgIndex = Outs[I].OrigArgIndex;
1083 assert (Outs[I].PartOffset == 0);
1084 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1085 SDValue PartValue = OutVals[I + 1];
1086 unsigned PartOffset = Outs[I + 1].PartOffset;
1087 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1088 DAG.getIntPtrConstant(PartOffset, DL));
1089 MemOpChains.push_back(DAG.getStore(
1090 Chain, DL, PartValue, Address,
1091 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
1092 ++I;
1093 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001094 ArgValue = SpillSlot;
1095 } else
1096 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1097
1098 if (VA.isRegLoc())
1099 // Queue up the argument copies and emit them at the end.
1100 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1101 else {
1102 assert(VA.isMemLoc() && "Argument not register or memory");
1103
1104 // Work out the address of the stack slot. Unpromoted ints and
1105 // floats are passed as right-justified 8-byte values.
1106 if (!StackPtr.getNode())
1107 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1108 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1109 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1110 Offset += 4;
1111 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001112 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001113
1114 // Emit the store.
1115 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1116 MachinePointerInfo(),
1117 false, false, 0));
1118 }
1119 }
1120
1121 // Join the stores, which are independent of one another.
1122 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001123 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001124
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001125 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001126 // associated Target* opcodes. Force %r1 to be used for indirect
1127 // tail calls.
1128 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001129 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001130 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1131 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001132 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001133 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1134 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001135 } else if (IsTailCall) {
1136 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1137 Glue = Chain.getValue(1);
1138 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1139 }
1140
1141 // Build a sequence of copy-to-reg nodes, chained and glued together.
1142 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1143 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1144 RegsToPass[I].second, Glue);
1145 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001146 }
1147
1148 // The first call operand is the chain and the second is the target address.
1149 SmallVector<SDValue, 8> Ops;
1150 Ops.push_back(Chain);
1151 Ops.push_back(Callee);
1152
1153 // Add argument registers to the end of the list so that they are
1154 // known live into the call.
1155 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1156 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1157 RegsToPass[I].second.getValueType()));
1158
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001159 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001160 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001161 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001162 assert(Mask && "Missing call preserved mask for calling convention");
1163 Ops.push_back(DAG.getRegisterMask(Mask));
1164
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001165 // Glue the call to the argument copies, if any.
1166 if (Glue.getNode())
1167 Ops.push_back(Glue);
1168
1169 // Emit the call.
1170 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001171 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001172 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1173 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001174 Glue = Chain.getValue(1);
1175
1176 // Mark the end of the call, which is glued to the call itself.
1177 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001178 DAG.getConstant(NumBytes, DL, PtrVT, true),
1179 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001180 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001181 Glue = Chain.getValue(1);
1182
1183 // Assign locations to each value returned by this call.
1184 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001185 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001186 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1187
1188 // Copy all of the result registers out of their specified physreg.
1189 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1190 CCValAssign &VA = RetLocs[I];
1191
1192 // Copy the value out, gluing the copy to the end of the call sequence.
1193 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1194 VA.getLocVT(), Glue);
1195 Chain = RetValue.getValue(1);
1196 Glue = RetValue.getValue(2);
1197
1198 // Convert the value of the return register into the value that's
1199 // being returned.
1200 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1201 }
1202
1203 return Chain;
1204}
1205
Ulrich Weiganda887f062015-08-13 13:37:06 +00001206bool SystemZTargetLowering::
1207CanLowerReturn(CallingConv::ID CallConv,
1208 MachineFunction &MF, bool isVarArg,
1209 const SmallVectorImpl<ISD::OutputArg> &Outs,
1210 LLVMContext &Context) const {
1211 // Detect unsupported vector return types.
1212 if (Subtarget.hasVector())
1213 VerifyVectorTypes(Outs);
1214
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001215 // Special case that we cannot easily detect in RetCC_SystemZ since
1216 // i128 is not a legal type.
1217 for (auto &Out : Outs)
1218 if (Out.ArgVT == MVT::i128)
1219 return false;
1220
Ulrich Weiganda887f062015-08-13 13:37:06 +00001221 SmallVector<CCValAssign, 16> RetLocs;
1222 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1223 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1224}
1225
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001226SDValue
1227SystemZTargetLowering::LowerReturn(SDValue Chain,
1228 CallingConv::ID CallConv, bool IsVarArg,
1229 const SmallVectorImpl<ISD::OutputArg> &Outs,
1230 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001231 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001232 MachineFunction &MF = DAG.getMachineFunction();
1233
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001234 // Detect unsupported vector return types.
1235 if (Subtarget.hasVector())
1236 VerifyVectorTypes(Outs);
1237
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001238 // Assign locations to each returned value.
1239 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001240 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001241 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1242
1243 // Quick exit for void returns
1244 if (RetLocs.empty())
1245 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1246
1247 // Copy the result values into the output registers.
1248 SDValue Glue;
1249 SmallVector<SDValue, 4> RetOps;
1250 RetOps.push_back(Chain);
1251 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1252 CCValAssign &VA = RetLocs[I];
1253 SDValue RetValue = OutVals[I];
1254
1255 // Make the return register live on exit.
1256 assert(VA.isRegLoc() && "Can only return in registers!");
1257
1258 // Promote the value as required.
1259 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1260
1261 // Chain and glue the copies together.
1262 unsigned Reg = VA.getLocReg();
1263 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1264 Glue = Chain.getValue(1);
1265 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1266 }
1267
1268 // Update chain and glue.
1269 RetOps[0] = Chain;
1270 if (Glue.getNode())
1271 RetOps.push_back(Glue);
1272
Craig Topper48d114b2014-04-26 18:35:24 +00001273 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001274}
1275
Richard Sandiford9afe6132013-12-10 10:36:34 +00001276SDValue SystemZTargetLowering::
1277prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1278 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1279}
1280
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001281// Return true if Op is an intrinsic node with chain that returns the CC value
1282// as its only (other) argument. Provide the associated SystemZISD opcode and
1283// the mask of valid CC values if so.
1284static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1285 unsigned &CCValid) {
1286 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1287 switch (Id) {
1288 case Intrinsic::s390_tbegin:
1289 Opcode = SystemZISD::TBEGIN;
1290 CCValid = SystemZ::CCMASK_TBEGIN;
1291 return true;
1292
1293 case Intrinsic::s390_tbegin_nofloat:
1294 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1295 CCValid = SystemZ::CCMASK_TBEGIN;
1296 return true;
1297
1298 case Intrinsic::s390_tend:
1299 Opcode = SystemZISD::TEND;
1300 CCValid = SystemZ::CCMASK_TEND;
1301 return true;
1302
1303 default:
1304 return false;
1305 }
1306}
1307
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001308// Return true if Op is an intrinsic node without chain that returns the
1309// CC value as its final argument. Provide the associated SystemZISD
1310// opcode and the mask of valid CC values if so.
1311static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1312 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1313 switch (Id) {
1314 case Intrinsic::s390_vpkshs:
1315 case Intrinsic::s390_vpksfs:
1316 case Intrinsic::s390_vpksgs:
1317 Opcode = SystemZISD::PACKS_CC;
1318 CCValid = SystemZ::CCMASK_VCMP;
1319 return true;
1320
1321 case Intrinsic::s390_vpklshs:
1322 case Intrinsic::s390_vpklsfs:
1323 case Intrinsic::s390_vpklsgs:
1324 Opcode = SystemZISD::PACKLS_CC;
1325 CCValid = SystemZ::CCMASK_VCMP;
1326 return true;
1327
1328 case Intrinsic::s390_vceqbs:
1329 case Intrinsic::s390_vceqhs:
1330 case Intrinsic::s390_vceqfs:
1331 case Intrinsic::s390_vceqgs:
1332 Opcode = SystemZISD::VICMPES;
1333 CCValid = SystemZ::CCMASK_VCMP;
1334 return true;
1335
1336 case Intrinsic::s390_vchbs:
1337 case Intrinsic::s390_vchhs:
1338 case Intrinsic::s390_vchfs:
1339 case Intrinsic::s390_vchgs:
1340 Opcode = SystemZISD::VICMPHS;
1341 CCValid = SystemZ::CCMASK_VCMP;
1342 return true;
1343
1344 case Intrinsic::s390_vchlbs:
1345 case Intrinsic::s390_vchlhs:
1346 case Intrinsic::s390_vchlfs:
1347 case Intrinsic::s390_vchlgs:
1348 Opcode = SystemZISD::VICMPHLS;
1349 CCValid = SystemZ::CCMASK_VCMP;
1350 return true;
1351
1352 case Intrinsic::s390_vtm:
1353 Opcode = SystemZISD::VTM;
1354 CCValid = SystemZ::CCMASK_VCMP;
1355 return true;
1356
1357 case Intrinsic::s390_vfaebs:
1358 case Intrinsic::s390_vfaehs:
1359 case Intrinsic::s390_vfaefs:
1360 Opcode = SystemZISD::VFAE_CC;
1361 CCValid = SystemZ::CCMASK_ANY;
1362 return true;
1363
1364 case Intrinsic::s390_vfaezbs:
1365 case Intrinsic::s390_vfaezhs:
1366 case Intrinsic::s390_vfaezfs:
1367 Opcode = SystemZISD::VFAEZ_CC;
1368 CCValid = SystemZ::CCMASK_ANY;
1369 return true;
1370
1371 case Intrinsic::s390_vfeebs:
1372 case Intrinsic::s390_vfeehs:
1373 case Intrinsic::s390_vfeefs:
1374 Opcode = SystemZISD::VFEE_CC;
1375 CCValid = SystemZ::CCMASK_ANY;
1376 return true;
1377
1378 case Intrinsic::s390_vfeezbs:
1379 case Intrinsic::s390_vfeezhs:
1380 case Intrinsic::s390_vfeezfs:
1381 Opcode = SystemZISD::VFEEZ_CC;
1382 CCValid = SystemZ::CCMASK_ANY;
1383 return true;
1384
1385 case Intrinsic::s390_vfenebs:
1386 case Intrinsic::s390_vfenehs:
1387 case Intrinsic::s390_vfenefs:
1388 Opcode = SystemZISD::VFENE_CC;
1389 CCValid = SystemZ::CCMASK_ANY;
1390 return true;
1391
1392 case Intrinsic::s390_vfenezbs:
1393 case Intrinsic::s390_vfenezhs:
1394 case Intrinsic::s390_vfenezfs:
1395 Opcode = SystemZISD::VFENEZ_CC;
1396 CCValid = SystemZ::CCMASK_ANY;
1397 return true;
1398
1399 case Intrinsic::s390_vistrbs:
1400 case Intrinsic::s390_vistrhs:
1401 case Intrinsic::s390_vistrfs:
1402 Opcode = SystemZISD::VISTR_CC;
1403 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1404 return true;
1405
1406 case Intrinsic::s390_vstrcbs:
1407 case Intrinsic::s390_vstrchs:
1408 case Intrinsic::s390_vstrcfs:
1409 Opcode = SystemZISD::VSTRC_CC;
1410 CCValid = SystemZ::CCMASK_ANY;
1411 return true;
1412
1413 case Intrinsic::s390_vstrczbs:
1414 case Intrinsic::s390_vstrczhs:
1415 case Intrinsic::s390_vstrczfs:
1416 Opcode = SystemZISD::VSTRCZ_CC;
1417 CCValid = SystemZ::CCMASK_ANY;
1418 return true;
1419
1420 case Intrinsic::s390_vfcedbs:
1421 Opcode = SystemZISD::VFCMPES;
1422 CCValid = SystemZ::CCMASK_VCMP;
1423 return true;
1424
1425 case Intrinsic::s390_vfchdbs:
1426 Opcode = SystemZISD::VFCMPHS;
1427 CCValid = SystemZ::CCMASK_VCMP;
1428 return true;
1429
1430 case Intrinsic::s390_vfchedbs:
1431 Opcode = SystemZISD::VFCMPHES;
1432 CCValid = SystemZ::CCMASK_VCMP;
1433 return true;
1434
1435 case Intrinsic::s390_vftcidb:
1436 Opcode = SystemZISD::VFTCI;
1437 CCValid = SystemZ::CCMASK_VCMP;
1438 return true;
1439
1440 default:
1441 return false;
1442 }
1443}
1444
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001445// Emit an intrinsic with chain with a glued value instead of its CC result.
1446static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1447 unsigned Opcode) {
1448 // Copy all operands except the intrinsic ID.
1449 unsigned NumOps = Op.getNumOperands();
1450 SmallVector<SDValue, 6> Ops;
1451 Ops.reserve(NumOps - 1);
1452 Ops.push_back(Op.getOperand(0));
1453 for (unsigned I = 2; I < NumOps; ++I)
1454 Ops.push_back(Op.getOperand(I));
1455
1456 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1457 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1458 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1459 SDValue OldChain = SDValue(Op.getNode(), 1);
1460 SDValue NewChain = SDValue(Intr.getNode(), 0);
1461 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1462 return Intr;
1463}
1464
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001465// Emit an intrinsic with a glued value instead of its CC result.
1466static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1467 unsigned Opcode) {
1468 // Copy all operands except the intrinsic ID.
1469 unsigned NumOps = Op.getNumOperands();
1470 SmallVector<SDValue, 6> Ops;
1471 Ops.reserve(NumOps - 1);
1472 for (unsigned I = 1; I < NumOps; ++I)
1473 Ops.push_back(Op.getOperand(I));
1474
1475 if (Op->getNumValues() == 1)
1476 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1477 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1478 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1479 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1480}
1481
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001482// CC is a comparison that will be implemented using an integer or
1483// floating-point comparison. Return the condition code mask for
1484// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1485// unsigned comparisons and clear for signed ones. In the floating-point
1486// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1487static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1488#define CONV(X) \
1489 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1490 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1491 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1492
1493 switch (CC) {
1494 default:
1495 llvm_unreachable("Invalid integer condition!");
1496
1497 CONV(EQ);
1498 CONV(NE);
1499 CONV(GT);
1500 CONV(GE);
1501 CONV(LT);
1502 CONV(LE);
1503
1504 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1505 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1506 }
1507#undef CONV
1508}
1509
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001510// Return a sequence for getting a 1 from an IPM result when CC has a
1511// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1512// The handling of CC values outside CCValid doesn't matter.
1513static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1514 // Deal with cases where the result can be taken directly from a bit
1515 // of the IPM result.
1516 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1517 return IPMConversion(0, 0, SystemZ::IPM_CC);
1518 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1519 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1520
1521 // Deal with cases where we can add a value to force the sign bit
1522 // to contain the right value. Putting the bit in 31 means we can
1523 // use SRL rather than RISBG(L), and also makes it easier to get a
1524 // 0/-1 value, so it has priority over the other tests below.
1525 //
1526 // These sequences rely on the fact that the upper two bits of the
1527 // IPM result are zero.
1528 uint64_t TopBit = uint64_t(1) << 31;
1529 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1530 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1531 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1532 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1533 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1534 | SystemZ::CCMASK_1
1535 | SystemZ::CCMASK_2)))
1536 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1537 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1538 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1539 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1540 | SystemZ::CCMASK_2
1541 | SystemZ::CCMASK_3)))
1542 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1543
1544 // Next try inverting the value and testing a bit. 0/1 could be
1545 // handled this way too, but we dealt with that case above.
1546 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1547 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1548
1549 // Handle cases where adding a value forces a non-sign bit to contain
1550 // the right value.
1551 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1552 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1553 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1554 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1555
Alp Tokercb402912014-01-24 17:20:08 +00001556 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001557 // can be done by inverting the low CC bit and applying one of the
1558 // sign-based extractions above.
1559 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1560 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1561 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1562 return IPMConversion(1 << SystemZ::IPM_CC,
1563 TopBit - (3 << SystemZ::IPM_CC), 31);
1564 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1565 | SystemZ::CCMASK_1
1566 | SystemZ::CCMASK_3)))
1567 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1568 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1569 | SystemZ::CCMASK_2
1570 | SystemZ::CCMASK_3)))
1571 return IPMConversion(1 << SystemZ::IPM_CC,
1572 TopBit - (1 << SystemZ::IPM_CC), 31);
1573
1574 llvm_unreachable("Unexpected CC combination");
1575}
1576
Richard Sandifordd420f732013-12-13 15:28:45 +00001577// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001578// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001579static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001580 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001581 return;
1582
Richard Sandiford21f5d682014-03-06 11:22:58 +00001583 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001584 if (!ConstOp1)
1585 return;
1586
1587 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001588 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1589 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1590 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1591 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1592 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001593 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001594 }
1595}
1596
Richard Sandifordd420f732013-12-13 15:28:45 +00001597// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1598// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001599static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001600 // For us to make any changes, it must a comparison between a single-use
1601 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001602 if (!C.Op0.hasOneUse() ||
1603 C.Op0.getOpcode() != ISD::LOAD ||
1604 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001605 return;
1606
1607 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001608 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001609 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1610 if (NumBits != 8 && NumBits != 16)
1611 return;
1612
1613 // The load must be an extending one and the constant must be within the
1614 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001615 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001616 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001617 uint64_t Mask = (1 << NumBits) - 1;
1618 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001619 // Make sure that ConstOp1 is in range of C.Op0.
1620 int64_t SignedValue = ConstOp1->getSExtValue();
1621 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001622 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001623 if (C.ICmpType != SystemZICMP::SignedOnly) {
1624 // Unsigned comparison between two sign-extended values is equivalent
1625 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001626 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001627 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001628 // Try to treat the comparison as unsigned, so that we can use CLI.
1629 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001630 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001631 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001632 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1633 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001634 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001635 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001636 else
1637 // No instruction exists for this combination.
1638 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001639 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001640 }
1641 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1642 if (Value > Mask)
1643 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001644 // If the constant is in range, we can use any comparison.
1645 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001646 } else
1647 return;
1648
1649 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001650 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1651 ISD::SEXTLOAD :
1652 ISD::ZEXTLOAD);
1653 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001654 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001655 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1656 Load->getChain(), Load->getBasePtr(),
1657 Load->getPointerInfo(), Load->getMemoryVT(),
1658 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001659 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001660
1661 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001662 if (C.Op1.getValueType() != MVT::i32 ||
1663 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001664 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001665}
1666
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001667// Return true if Op is either an unextended load, or a load suitable
1668// for integer register-memory comparisons of type ICmpType.
1669static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001670 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001671 if (Load) {
1672 // There are no instructions to compare a register with a memory byte.
1673 if (Load->getMemoryVT() == MVT::i8)
1674 return false;
1675 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001676 switch (Load->getExtensionType()) {
1677 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001678 return true;
1679 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001680 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001681 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001682 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001683 default:
1684 break;
1685 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001686 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001687 return false;
1688}
1689
Richard Sandifordd420f732013-12-13 15:28:45 +00001690// Return true if it is better to swap the operands of C.
1691static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001692 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001693 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001694 return false;
1695
1696 // Always keep a floating-point constant second, since comparisons with
1697 // zero can use LOAD TEST and comparisons with other constants make a
1698 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001699 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001700 return false;
1701
1702 // Never swap comparisons with zero since there are many ways to optimize
1703 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001704 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001705 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001706 return false;
1707
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001708 // Also keep natural memory operands second if the loaded value is
1709 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001710 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001711 return false;
1712
Richard Sandiford24e597b2013-08-23 11:27:19 +00001713 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1714 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001715 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001716 // The only exceptions are when the second operand is a constant and
1717 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001718 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001719 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001720 // The unsigned memory-immediate instructions can handle 16-bit
1721 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001722 if (C.ICmpType != SystemZICMP::SignedOnly &&
1723 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001724 return false;
1725 // The signed memory-immediate instructions can handle 16-bit
1726 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001727 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1728 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001729 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001730 return true;
1731 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001732
1733 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001734 unsigned Opcode0 = C.Op0.getOpcode();
1735 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001736 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001737 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001738 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001739 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001740 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001741 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1742 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001743 return true;
1744
Richard Sandiford24e597b2013-08-23 11:27:19 +00001745 return false;
1746}
1747
Richard Sandiford73170f82013-12-11 11:45:08 +00001748// Return a version of comparison CC mask CCMask in which the LT and GT
1749// actions are swapped.
1750static unsigned reverseCCMask(unsigned CCMask) {
1751 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1752 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1753 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1754 (CCMask & SystemZ::CCMASK_CMP_UO));
1755}
1756
Richard Sandiford0847c452013-12-13 15:50:30 +00001757// Check whether C tests for equality between X and Y and whether X - Y
1758// or Y - X is also computed. In that case it's better to compare the
1759// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001760static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001761 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1762 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001763 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001764 SDNode *N = *I;
1765 if (N->getOpcode() == ISD::SUB &&
1766 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1767 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1768 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001769 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001770 return;
1771 }
1772 }
1773 }
1774}
1775
Richard Sandifordd420f732013-12-13 15:28:45 +00001776// Check whether C compares a floating-point value with zero and if that
1777// floating-point value is also negated. In this case we can use the
1778// negation to set CC, so avoiding separate LOAD AND TEST and
1779// LOAD (NEGATIVE/COMPLEMENT) instructions.
1780static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001781 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001782 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001783 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001784 SDNode *N = *I;
1785 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001786 C.Op0 = SDValue(N, 0);
1787 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001788 return;
1789 }
1790 }
1791 }
1792}
1793
Richard Sandifordd420f732013-12-13 15:28:45 +00001794// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001795// also sign-extended. In that case it is better to test the result
1796// of the sign extension using LTGFR.
1797//
1798// This case is important because InstCombine transforms a comparison
1799// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001800static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001801 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001802 if (C.Op0.getOpcode() == ISD::SHL &&
1803 C.Op0.getValueType() == MVT::i64 &&
1804 C.Op1.getOpcode() == ISD::Constant &&
1805 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001806 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001807 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001808 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001809 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001810 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001811 SDNode *N = *I;
1812 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1813 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001814 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001815 return;
1816 }
1817 }
1818 }
1819 }
1820}
1821
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001822// If C compares the truncation of an extending load, try to compare
1823// the untruncated value instead. This exposes more opportunities to
1824// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001825static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001826 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1827 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1828 C.Op1.getOpcode() == ISD::Constant &&
1829 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001830 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001831 if (L->getMemoryVT().getStoreSizeInBits()
1832 <= C.Op0.getValueType().getSizeInBits()) {
1833 unsigned Type = L->getExtensionType();
1834 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1835 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1836 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001837 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001838 }
1839 }
1840 }
1841}
1842
Richard Sandiford030c1652013-09-13 09:09:50 +00001843// Return true if shift operation N has an in-range constant shift value.
1844// Store it in ShiftVal if so.
1845static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001846 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001847 if (!Shift)
1848 return false;
1849
1850 uint64_t Amount = Shift->getZExtValue();
1851 if (Amount >= N.getValueType().getSizeInBits())
1852 return false;
1853
1854 ShiftVal = Amount;
1855 return true;
1856}
1857
1858// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1859// instruction and whether the CC value is descriptive enough to handle
1860// a comparison of type Opcode between the AND result and CmpVal.
1861// CCMask says which comparison result is being tested and BitSize is
1862// the number of bits in the operands. If TEST UNDER MASK can be used,
1863// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001864static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1865 uint64_t Mask, uint64_t CmpVal,
1866 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001867 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1868
Richard Sandiford030c1652013-09-13 09:09:50 +00001869 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1870 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1871 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1872 return 0;
1873
Richard Sandiford113c8702013-09-03 15:38:35 +00001874 // Work out the masks for the lowest and highest bits.
1875 unsigned HighShift = 63 - countLeadingZeros(Mask);
1876 uint64_t High = uint64_t(1) << HighShift;
1877 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1878
1879 // Signed ordered comparisons are effectively unsigned if the sign
1880 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001881 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001882
1883 // Check for equality comparisons with 0, or the equivalent.
1884 if (CmpVal == 0) {
1885 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1886 return SystemZ::CCMASK_TM_ALL_0;
1887 if (CCMask == SystemZ::CCMASK_CMP_NE)
1888 return SystemZ::CCMASK_TM_SOME_1;
1889 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001890 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001891 if (CCMask == SystemZ::CCMASK_CMP_LT)
1892 return SystemZ::CCMASK_TM_ALL_0;
1893 if (CCMask == SystemZ::CCMASK_CMP_GE)
1894 return SystemZ::CCMASK_TM_SOME_1;
1895 }
1896 if (EffectivelyUnsigned && CmpVal < Low) {
1897 if (CCMask == SystemZ::CCMASK_CMP_LE)
1898 return SystemZ::CCMASK_TM_ALL_0;
1899 if (CCMask == SystemZ::CCMASK_CMP_GT)
1900 return SystemZ::CCMASK_TM_SOME_1;
1901 }
1902
1903 // Check for equality comparisons with the mask, or the equivalent.
1904 if (CmpVal == Mask) {
1905 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1906 return SystemZ::CCMASK_TM_ALL_1;
1907 if (CCMask == SystemZ::CCMASK_CMP_NE)
1908 return SystemZ::CCMASK_TM_SOME_0;
1909 }
1910 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1911 if (CCMask == SystemZ::CCMASK_CMP_GT)
1912 return SystemZ::CCMASK_TM_ALL_1;
1913 if (CCMask == SystemZ::CCMASK_CMP_LE)
1914 return SystemZ::CCMASK_TM_SOME_0;
1915 }
1916 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1917 if (CCMask == SystemZ::CCMASK_CMP_GE)
1918 return SystemZ::CCMASK_TM_ALL_1;
1919 if (CCMask == SystemZ::CCMASK_CMP_LT)
1920 return SystemZ::CCMASK_TM_SOME_0;
1921 }
1922
1923 // Check for ordered comparisons with the top bit.
1924 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1925 if (CCMask == SystemZ::CCMASK_CMP_LE)
1926 return SystemZ::CCMASK_TM_MSB_0;
1927 if (CCMask == SystemZ::CCMASK_CMP_GT)
1928 return SystemZ::CCMASK_TM_MSB_1;
1929 }
1930 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1931 if (CCMask == SystemZ::CCMASK_CMP_LT)
1932 return SystemZ::CCMASK_TM_MSB_0;
1933 if (CCMask == SystemZ::CCMASK_CMP_GE)
1934 return SystemZ::CCMASK_TM_MSB_1;
1935 }
1936
1937 // If there are just two bits, we can do equality checks for Low and High
1938 // as well.
1939 if (Mask == Low + High) {
1940 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1941 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1942 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1943 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1944 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1945 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1946 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1947 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1948 }
1949
1950 // Looks like we've exhausted our options.
1951 return 0;
1952}
1953
Richard Sandifordd420f732013-12-13 15:28:45 +00001954// See whether C can be implemented as a TEST UNDER MASK instruction.
1955// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001956static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001957 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001958 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001959 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001960 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001961 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001962
1963 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001964 Comparison NewC(C);
1965 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001966 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001967 if (C.Op0.getOpcode() == ISD::AND) {
1968 NewC.Op0 = C.Op0.getOperand(0);
1969 NewC.Op1 = C.Op0.getOperand(1);
1970 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1971 if (!Mask)
1972 return;
1973 MaskVal = Mask->getZExtValue();
1974 } else {
1975 // There is no instruction to compare with a 64-bit immediate
1976 // so use TMHH instead if possible. We need an unsigned ordered
1977 // comparison with an i64 immediate.
1978 if (NewC.Op0.getValueType() != MVT::i64 ||
1979 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1980 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1981 NewC.ICmpType == SystemZICMP::SignedOnly)
1982 return;
1983 // Convert LE and GT comparisons into LT and GE.
1984 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1985 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1986 if (CmpVal == uint64_t(-1))
1987 return;
1988 CmpVal += 1;
1989 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1990 }
1991 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1992 // be masked off without changing the result.
1993 MaskVal = -(CmpVal & -CmpVal);
1994 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1995 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001996 if (!MaskVal)
1997 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001998
Richard Sandiford113c8702013-09-03 15:38:35 +00001999 // Check whether the combination of mask, comparison value and comparison
2000 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002001 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002002 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002003 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2004 NewC.Op0.getOpcode() == ISD::SHL &&
2005 isSimpleShift(NewC.Op0, ShiftVal) &&
2006 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2007 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002008 CmpVal >> ShiftVal,
2009 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002010 NewC.Op0 = NewC.Op0.getOperand(0);
2011 MaskVal >>= ShiftVal;
2012 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2013 NewC.Op0.getOpcode() == ISD::SRL &&
2014 isSimpleShift(NewC.Op0, ShiftVal) &&
2015 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002016 MaskVal << ShiftVal,
2017 CmpVal << ShiftVal,
2018 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002019 NewC.Op0 = NewC.Op0.getOperand(0);
2020 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002021 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002022 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2023 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002024 if (!NewCCMask)
2025 return;
2026 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002027
Richard Sandiford35b9be22013-08-28 10:31:43 +00002028 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002029 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002030 C.Op0 = NewC.Op0;
2031 if (Mask && Mask->getZExtValue() == MaskVal)
2032 C.Op1 = SDValue(Mask, 0);
2033 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002034 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002035 C.CCValid = SystemZ::CCMASK_TM;
2036 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002037}
2038
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002039// Return a Comparison that tests the condition-code result of intrinsic
2040// node Call against constant integer CC using comparison code Cond.
2041// Opcode is the opcode of the SystemZISD operation for the intrinsic
2042// and CCValid is the set of possible condition-code results.
2043static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2044 SDValue Call, unsigned CCValid, uint64_t CC,
2045 ISD::CondCode Cond) {
2046 Comparison C(Call, SDValue());
2047 C.Opcode = Opcode;
2048 C.CCValid = CCValid;
2049 if (Cond == ISD::SETEQ)
2050 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2051 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2052 else if (Cond == ISD::SETNE)
2053 // ...and the inverse of that.
2054 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2055 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2056 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2057 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002058 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002059 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2060 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002061 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002062 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2063 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2064 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002065 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002066 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2067 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002068 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002069 else
2070 llvm_unreachable("Unexpected integer comparison type");
2071 C.CCMask &= CCValid;
2072 return C;
2073}
2074
Richard Sandifordd420f732013-12-13 15:28:45 +00002075// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2076static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002077 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002078 if (CmpOp1.getOpcode() == ISD::Constant) {
2079 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2080 unsigned Opcode, CCValid;
2081 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2082 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2083 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2084 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002085 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2086 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2087 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2088 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002089 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002090 Comparison C(CmpOp0, CmpOp1);
2091 C.CCMask = CCMaskForCondCode(Cond);
2092 if (C.Op0.getValueType().isFloatingPoint()) {
2093 C.CCValid = SystemZ::CCMASK_FCMP;
2094 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002095 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002096 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002097 C.CCValid = SystemZ::CCMASK_ICMP;
2098 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002099 // Choose the type of comparison. Equality and inequality tests can
2100 // use either signed or unsigned comparisons. The choice also doesn't
2101 // matter if both sign bits are known to be clear. In those cases we
2102 // want to give the main isel code the freedom to choose whichever
2103 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002104 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2105 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2106 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2107 C.ICmpType = SystemZICMP::Any;
2108 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2109 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002110 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002111 C.ICmpType = SystemZICMP::SignedOnly;
2112 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002113 adjustZeroCmp(DAG, DL, C);
2114 adjustSubwordCmp(DAG, DL, C);
2115 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002116 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002117 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002118 }
2119
Richard Sandifordd420f732013-12-13 15:28:45 +00002120 if (shouldSwapCmpOperands(C)) {
2121 std::swap(C.Op0, C.Op1);
2122 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002123 }
2124
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002125 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002126 return C;
2127}
2128
2129// Emit the comparison instruction described by C.
2130static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002131 if (!C.Op1.getNode()) {
2132 SDValue Op;
2133 switch (C.Op0.getOpcode()) {
2134 case ISD::INTRINSIC_W_CHAIN:
2135 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2136 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002137 case ISD::INTRINSIC_WO_CHAIN:
2138 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2139 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002140 default:
2141 llvm_unreachable("Invalid comparison operands");
2142 }
2143 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2144 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002145 if (C.Opcode == SystemZISD::ICMP)
2146 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002147 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002148 if (C.Opcode == SystemZISD::TM) {
2149 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2150 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2151 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002152 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002153 }
2154 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002155}
2156
Richard Sandiford7d86e472013-08-21 09:34:56 +00002157// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2158// 64 bits. Extend is the extension type to use. Store the high part
2159// in Hi and the low part in Lo.
2160static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
2161 unsigned Extend, SDValue Op0, SDValue Op1,
2162 SDValue &Hi, SDValue &Lo) {
2163 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2164 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2165 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002166 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2167 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002168 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2169 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2170}
2171
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002172// Lower a binary operation that produces two VT results, one in each
2173// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2174// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2175// on the extended Op0 and (unextended) Op1. Store the even register result
2176// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002177static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002178 unsigned Extend, unsigned Opcode,
2179 SDValue Op0, SDValue Op1,
2180 SDValue &Even, SDValue &Odd) {
2181 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2182 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2183 SDValue(In128, 0), Op1);
2184 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002185 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2186 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002187}
2188
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002189// Return an i32 value that is 1 if the CC value produced by Glue is
2190// in the mask CCMask and 0 otherwise. CC is known to have a value
2191// in CCValid, so other values can be ignored.
2192static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
2193 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002194 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2195 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2196
2197 if (Conversion.XORValue)
2198 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002199 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002200
2201 if (Conversion.AddValue)
2202 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002203 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002204
2205 // The SHR/AND sequence should get optimized to an RISBG.
2206 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002207 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002208 if (Conversion.Bit != 31)
2209 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002210 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002211 return Result;
2212}
2213
Ulrich Weigandcd808232015-05-05 19:26:48 +00002214// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2215// be done directly. IsFP is true if CC is for a floating-point rather than
2216// integer comparison.
2217static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002218 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002219 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002220 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002221 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002222
Ulrich Weigandcd808232015-05-05 19:26:48 +00002223 case ISD::SETOGE:
2224 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002225 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002226
2227 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002228 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002229 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002230
2231 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002232 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002233
2234 default:
2235 return 0;
2236 }
2237}
2238
2239// Return the SystemZISD vector comparison operation for CC or its inverse,
2240// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002241// result is for the inverse of CC. IsFP is true if CC is for a
2242// floating-point rather than integer comparison.
2243static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2244 bool &Invert) {
2245 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002246 Invert = false;
2247 return Opcode;
2248 }
2249
Ulrich Weigandcd808232015-05-05 19:26:48 +00002250 CC = ISD::getSetCCInverse(CC, !IsFP);
2251 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002252 Invert = true;
2253 return Opcode;
2254 }
2255
2256 return 0;
2257}
2258
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002259// Return a v2f64 that contains the extended form of elements Start and Start+1
2260// of v4f32 value Op.
2261static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
2262 SDValue Op) {
2263 int Mask[] = { Start, -1, Start + 1, -1 };
2264 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2265 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2266}
2267
2268// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2269// producing a result of type VT.
2270static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2271 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2272 // There is no hardware support for v4f32, so extend the vector into
2273 // two v2f64s and compare those.
2274 if (CmpOp0.getValueType() == MVT::v4f32) {
2275 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2276 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2277 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2278 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2279 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2280 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2281 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2282 }
2283 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2284}
2285
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002286// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2287// an integer mask of type VT.
2288static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2289 ISD::CondCode CC, SDValue CmpOp0,
2290 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002291 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002292 bool Invert = false;
2293 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002294 switch (CC) {
2295 // Handle tests for order using (or (ogt y x) (oge x y)).
2296 case ISD::SETUO:
2297 Invert = true;
2298 case ISD::SETO: {
2299 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002300 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2301 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002302 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2303 break;
2304 }
2305
2306 // Handle <> tests using (or (ogt y x) (ogt x y)).
2307 case ISD::SETUEQ:
2308 Invert = true;
2309 case ISD::SETONE: {
2310 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002311 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2312 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002313 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2314 break;
2315 }
2316
2317 // Otherwise a single comparison is enough. It doesn't really
2318 // matter whether we try the inversion or the swap first, since
2319 // there are no cases where both work.
2320 default:
2321 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002322 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002323 else {
2324 CC = ISD::getSetCCSwappedOperands(CC);
2325 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002326 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002327 else
2328 llvm_unreachable("Unhandled comparison");
2329 }
2330 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002331 }
2332 if (Invert) {
2333 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2334 DAG.getConstant(65535, DL, MVT::i32));
2335 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2336 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2337 }
2338 return Cmp;
2339}
2340
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002341SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2342 SelectionDAG &DAG) const {
2343 SDValue CmpOp0 = Op.getOperand(0);
2344 SDValue CmpOp1 = Op.getOperand(1);
2345 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2346 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002347 EVT VT = Op.getValueType();
2348 if (VT.isVector())
2349 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002350
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002351 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002352 SDValue Glue = emitCmp(DAG, DL, C);
2353 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002354}
2355
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002356SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002357 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2358 SDValue CmpOp0 = Op.getOperand(2);
2359 SDValue CmpOp1 = Op.getOperand(3);
2360 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002361 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002362
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002363 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002364 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002365 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002366 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2367 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002368}
2369
Richard Sandiford57485472013-12-13 15:35:00 +00002370// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2371// allowing Pos and Neg to be wider than CmpOp.
2372static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2373 return (Neg.getOpcode() == ISD::SUB &&
2374 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2375 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2376 Neg.getOperand(1) == Pos &&
2377 (Pos == CmpOp ||
2378 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2379 Pos.getOperand(0) == CmpOp)));
2380}
2381
2382// Return the absolute or negative absolute of Op; IsNegative decides which.
2383static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2384 bool IsNegative) {
2385 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2386 if (IsNegative)
2387 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002388 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002389 return Op;
2390}
2391
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002392SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2393 SelectionDAG &DAG) const {
2394 SDValue CmpOp0 = Op.getOperand(0);
2395 SDValue CmpOp1 = Op.getOperand(1);
2396 SDValue TrueOp = Op.getOperand(2);
2397 SDValue FalseOp = Op.getOperand(3);
2398 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002399 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002400
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002401 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002402
2403 // Check for absolute and negative-absolute selections, including those
2404 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2405 // This check supplements the one in DAGCombiner.
2406 if (C.Opcode == SystemZISD::ICMP &&
2407 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2408 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2409 C.Op1.getOpcode() == ISD::Constant &&
2410 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2411 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2412 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2413 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2414 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2415 }
2416
Richard Sandifordd420f732013-12-13 15:28:45 +00002417 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002418
2419 // Special case for handling -1/0 results. The shifts we use here
2420 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002421 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2422 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002423 if (TrueC && FalseC) {
2424 int64_t TrueVal = TrueC->getSExtValue();
2425 int64_t FalseVal = FalseC->getSExtValue();
2426 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2427 // Invert the condition if we want -1 on false.
2428 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002429 C.CCMask ^= C.CCValid;
2430 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002431 EVT VT = Op.getValueType();
2432 // Extend the result to VT. Upper bits are ignored.
2433 if (!is32Bit(VT))
2434 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2435 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002436 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002437 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2438 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2439 }
2440 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002441
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002442 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2443 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002444
2445 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002446 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002447}
2448
2449SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2450 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002451 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002452 const GlobalValue *GV = Node->getGlobal();
2453 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002454 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002455 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2456 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002457
2458 SDValue Result;
2459 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002460 // Assign anchors at 1<<12 byte boundaries.
2461 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2462 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2463 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2464
2465 // The offset can be folded into the address if it is aligned to a halfword.
2466 Offset -= Anchor;
2467 if (Offset != 0 && (Offset & 1) == 0) {
2468 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2469 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002470 Offset = 0;
2471 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002472 } else {
2473 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2474 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2475 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002476 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2477 false, false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002478 }
2479
2480 // If there was a non-zero offset that we didn't fold, create an explicit
2481 // addition for it.
2482 if (Offset != 0)
2483 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002484 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002485
2486 return Result;
2487}
2488
Ulrich Weigand7db69182015-02-18 09:13:27 +00002489SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2490 SelectionDAG &DAG,
2491 unsigned Opcode,
2492 SDValue GOTOffset) const {
2493 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002494 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002495 SDValue Chain = DAG.getEntryNode();
2496 SDValue Glue;
2497
2498 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2499 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2500 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2501 Glue = Chain.getValue(1);
2502 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2503 Glue = Chain.getValue(1);
2504
2505 // The first call operand is the chain and the second is the TLS symbol.
2506 SmallVector<SDValue, 8> Ops;
2507 Ops.push_back(Chain);
2508 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2509 Node->getValueType(0),
2510 0, 0));
2511
2512 // Add argument registers to the end of the list so that they are
2513 // known live into the call.
2514 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2515 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2516
2517 // Add a register mask operand representing the call-preserved registers.
2518 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002519 const uint32_t *Mask =
2520 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002521 assert(Mask && "Missing call preserved mask for calling convention");
2522 Ops.push_back(DAG.getRegisterMask(Mask));
2523
2524 // Glue the call to the argument copies.
2525 Ops.push_back(Glue);
2526
2527 // Emit the call.
2528 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2529 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2530 Glue = Chain.getValue(1);
2531
2532 // Copy the return value from %r2.
2533 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2534}
2535
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002536SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002537 SelectionDAG &DAG) const {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002538 if (DAG.getTarget().Options.EmulatedTLS)
2539 return LowerToTLSEmulatedModel(Node, DAG);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002540 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002541 const GlobalValue *GV = Node->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002542 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002543 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002544
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002545 // The high part of the thread pointer is in access register 0.
2546 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002548 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2549
2550 // The low part of the thread pointer is in access register 1.
2551 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002552 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002553 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2554
2555 // Merge them into a single 64-bit address.
2556 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002557 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002558 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2559
Ulrich Weigand7db69182015-02-18 09:13:27 +00002560 // Get the offset of GA from the thread pointer, based on the TLS model.
2561 SDValue Offset;
2562 switch (model) {
2563 case TLSModel::GeneralDynamic: {
2564 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2565 SystemZConstantPoolValue *CPV =
2566 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002567
Ulrich Weigand7db69182015-02-18 09:13:27 +00002568 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002569 Offset = DAG.getLoad(
2570 PtrVT, DL, DAG.getEntryNode(), Offset,
2571 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2572 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002573
2574 // Call __tls_get_offset to retrieve the offset.
2575 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2576 break;
2577 }
2578
2579 case TLSModel::LocalDynamic: {
2580 // Load the GOT offset of the module ID.
2581 SystemZConstantPoolValue *CPV =
2582 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2583
2584 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002585 Offset = DAG.getLoad(
2586 PtrVT, DL, DAG.getEntryNode(), Offset,
2587 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2588 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002589
2590 // Call __tls_get_offset to retrieve the module base offset.
2591 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2592
2593 // Note: The SystemZLDCleanupPass will remove redundant computations
2594 // of the module base offset. Count total number of local-dynamic
2595 // accesses to trigger execution of that pass.
2596 SystemZMachineFunctionInfo* MFI =
2597 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2598 MFI->incNumLocalDynamicTLSAccesses();
2599
2600 // Add the per-symbol offset.
2601 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2602
2603 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002604 DTPOffset = DAG.getLoad(
2605 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2606 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2607 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002608
2609 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2610 break;
2611 }
2612
2613 case TLSModel::InitialExec: {
2614 // Load the offset from the GOT.
2615 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2616 SystemZII::MO_INDNTPOFF);
2617 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002618 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2619 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Ulrich Weigand7db69182015-02-18 09:13:27 +00002620 false, false, false, 0);
2621 break;
2622 }
2623
2624 case TLSModel::LocalExec: {
2625 // Force the offset into the constant pool and load it from there.
2626 SystemZConstantPoolValue *CPV =
2627 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2628
2629 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002630 Offset = DAG.getLoad(
2631 PtrVT, DL, DAG.getEntryNode(), Offset,
2632 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2633 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002634 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002635 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002636 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002637
2638 // Add the base and offset together.
2639 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2640}
2641
2642SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2643 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002644 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002645 const BlockAddress *BA = Node->getBlockAddress();
2646 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002647 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002648
2649 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2650 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2651 return Result;
2652}
2653
2654SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2655 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002656 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002657 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002658 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2659
2660 // Use LARL to load the address of the table.
2661 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2662}
2663
2664SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2665 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002666 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002667 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002668
2669 SDValue Result;
2670 if (CP->isMachineConstantPoolEntry())
2671 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002672 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002673 else
2674 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002675 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002676
2677 // Use LARL to load the address of the constant pool entry.
2678 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2679}
2680
Ulrich Weigandf557d082016-04-04 12:44:55 +00002681SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2682 SelectionDAG &DAG) const {
2683 MachineFunction &MF = DAG.getMachineFunction();
2684 MachineFrameInfo *MFI = MF.getFrameInfo();
2685 MFI->setFrameAddressIsTaken(true);
2686
2687 SDLoc DL(Op);
2688 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2689 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2690
2691 // If the back chain frame index has not been allocated yet, do so.
2692 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2693 int BackChainIdx = FI->getFramePointerSaveIndex();
2694 if (!BackChainIdx) {
2695 // By definition, the frame address is the address of the back chain.
2696 BackChainIdx = MFI->CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
2697 FI->setFramePointerSaveIndex(BackChainIdx);
2698 }
2699 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2700
2701 // FIXME The frontend should detect this case.
2702 if (Depth > 0) {
2703 report_fatal_error("Unsupported stack frame traversal count");
2704 }
2705
2706 return BackChain;
2707}
2708
2709SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2710 SelectionDAG &DAG) const {
2711 MachineFunction &MF = DAG.getMachineFunction();
2712 MachineFrameInfo *MFI = MF.getFrameInfo();
2713 MFI->setReturnAddressIsTaken(true);
2714
2715 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2716 return SDValue();
2717
2718 SDLoc DL(Op);
2719 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2720 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2721
2722 // FIXME The frontend should detect this case.
2723 if (Depth > 0) {
2724 report_fatal_error("Unsupported stack frame traversal count");
2725 }
2726
2727 // Return R14D, which has the return address. Mark it an implicit live-in.
2728 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2729 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2730}
2731
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002732SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2733 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002734 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002735 SDValue In = Op.getOperand(0);
2736 EVT InVT = In.getValueType();
2737 EVT ResVT = Op.getValueType();
2738
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002739 // Convert loads directly. This is normally done by DAGCombiner,
2740 // but we need this case for bitcasts that are created during lowering
2741 // and which are then lowered themselves.
2742 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2743 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2744 LoadN->getMemOperand());
2745
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002746 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002747 SDValue In64;
2748 if (Subtarget.hasHighWord()) {
2749 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2750 MVT::i64);
2751 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2752 MVT::i64, SDValue(U64, 0), In);
2753 } else {
2754 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2755 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002756 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002757 }
2758 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002759 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002760 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002761 }
2762 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2763 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002764 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002765 MVT::f64, SDValue(U64, 0), In);
2766 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002767 if (Subtarget.hasHighWord())
2768 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2769 MVT::i32, Out64);
2770 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002771 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002772 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002773 }
2774 llvm_unreachable("Unexpected bitcast combination");
2775}
2776
2777SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2778 SelectionDAG &DAG) const {
2779 MachineFunction &MF = DAG.getMachineFunction();
2780 SystemZMachineFunctionInfo *FuncInfo =
2781 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002782 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002783
2784 SDValue Chain = Op.getOperand(0);
2785 SDValue Addr = Op.getOperand(1);
2786 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002787 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002788
2789 // The initial values of each field.
2790 const unsigned NumFields = 4;
2791 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002792 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2793 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002794 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2795 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2796 };
2797
2798 // Store each field into its respective slot.
2799 SDValue MemOps[NumFields];
2800 unsigned Offset = 0;
2801 for (unsigned I = 0; I < NumFields; ++I) {
2802 SDValue FieldAddr = Addr;
2803 if (Offset != 0)
2804 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002805 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002806 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2807 MachinePointerInfo(SV, Offset),
2808 false, false, 0);
2809 Offset += 8;
2810 }
Craig Topper48d114b2014-04-26 18:35:24 +00002811 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002812}
2813
2814SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2815 SelectionDAG &DAG) const {
2816 SDValue Chain = Op.getOperand(0);
2817 SDValue DstPtr = Op.getOperand(1);
2818 SDValue SrcPtr = Op.getOperand(2);
2819 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2820 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002821 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002822
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002823 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002824 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002825 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002826 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2827}
2828
2829SDValue SystemZTargetLowering::
2830lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002831 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2832 bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2833 hasFnAttribute("no-realign-stack");
2834
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002835 SDValue Chain = Op.getOperand(0);
2836 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002837 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002838 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002839
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002840 // If user has set the no alignment function attribute, ignore
2841 // alloca alignments.
2842 uint64_t AlignVal = (RealignOpt ?
2843 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2844
2845 uint64_t StackAlign = TFI->getStackAlignment();
2846 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2847 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2848
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002849 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002850 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002851
2852 // Get a reference to the stack pointer.
2853 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2854
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002855 // Add extra space for alignment if needed.
2856 if (ExtraAlignSpace)
2857 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2858 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2859
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002860 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002861 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002862
2863 // Copy the new stack pointer back.
2864 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2865
2866 // The allocated data lives above the 160 bytes allocated for the standard
2867 // frame, plus any outgoing stack arguments. We don't know how much that
2868 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2869 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2870 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2871
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002872 // Dynamically realign if needed.
2873 if (RequiredAlign > StackAlign) {
2874 Result =
2875 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2876 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2877 Result =
2878 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2879 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2880 }
2881
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002882 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002883 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002884}
2885
Richard Sandiford7d86e472013-08-21 09:34:56 +00002886SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2887 SelectionDAG &DAG) const {
2888 EVT VT = Op.getValueType();
2889 SDLoc DL(Op);
2890 SDValue Ops[2];
2891 if (is32Bit(VT))
2892 // Just do a normal 64-bit multiplication and extract the results.
2893 // We define this so that it can be used for constant division.
2894 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2895 Op.getOperand(1), Ops[1], Ops[0]);
2896 else {
2897 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2898 //
2899 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2900 //
2901 // but using the fact that the upper halves are either all zeros
2902 // or all ones:
2903 //
2904 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2905 //
2906 // and grouping the right terms together since they are quicker than the
2907 // multiplication:
2908 //
2909 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002910 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002911 SDValue LL = Op.getOperand(0);
2912 SDValue RL = Op.getOperand(1);
2913 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2914 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2915 // UMUL_LOHI64 returns the low result in the odd register and the high
2916 // result in the even register. SMUL_LOHI is defined to return the
2917 // low half first, so the results are in reverse order.
2918 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2919 LL, RL, Ops[1], Ops[0]);
2920 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2921 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2922 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2923 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2924 }
Craig Topper64941d92014-04-27 19:20:57 +00002925 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002926}
2927
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002928SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2929 SelectionDAG &DAG) const {
2930 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002931 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002932 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002933 if (is32Bit(VT))
2934 // Just do a normal 64-bit multiplication and extract the results.
2935 // We define this so that it can be used for constant division.
2936 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2937 Op.getOperand(1), Ops[1], Ops[0]);
2938 else
2939 // UMUL_LOHI64 returns the low result in the odd register and the high
2940 // result in the even register. UMUL_LOHI is defined to return the
2941 // low half first, so the results are in reverse order.
2942 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2943 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002944 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002945}
2946
2947SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2948 SelectionDAG &DAG) const {
2949 SDValue Op0 = Op.getOperand(0);
2950 SDValue Op1 = Op.getOperand(1);
2951 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002952 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002953 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002954
2955 // We use DSGF for 32-bit division.
2956 if (is32Bit(VT)) {
2957 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002958 Opcode = SystemZISD::SDIVREM32;
2959 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2960 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2961 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002962 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00002963 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002964
2965 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2966 // input is "don't care". The instruction returns the remainder in
2967 // the even register and the quotient in the odd register.
2968 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002969 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002970 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002971 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002972}
2973
2974SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2975 SelectionDAG &DAG) const {
2976 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002977 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002978
2979 // DL(G) uses a double-width dividend, so we need to clear the even
2980 // register in the GR128 input. The instruction returns the remainder
2981 // in the even register and the quotient in the odd register.
2982 SDValue Ops[2];
2983 if (is32Bit(VT))
2984 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2985 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2986 else
2987 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2988 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002989 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002990}
2991
2992SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2993 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2994
2995 // Get the known-zero masks for each operand.
2996 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2997 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002998 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2999 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003000
3001 // See if the upper 32 bits of one operand and the lower 32 bits of the
3002 // other are known zero. They are the low and high operands respectively.
3003 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
3004 KnownZero[1].getZExtValue() };
3005 unsigned High, Low;
3006 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3007 High = 1, Low = 0;
3008 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3009 High = 0, Low = 1;
3010 else
3011 return Op;
3012
3013 SDValue LowOp = Ops[Low];
3014 SDValue HighOp = Ops[High];
3015
3016 // If the high part is a constant, we're better off using IILH.
3017 if (HighOp.getOpcode() == ISD::Constant)
3018 return Op;
3019
3020 // If the low part is a constant that is outside the range of LHI,
3021 // then we're better off using IILF.
3022 if (LowOp.getOpcode() == ISD::Constant) {
3023 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3024 if (!isInt<16>(Value))
3025 return Op;
3026 }
3027
3028 // Check whether the high part is an AND that doesn't change the
3029 // high 32 bits and just masks out low bits. We can skip it if so.
3030 if (HighOp.getOpcode() == ISD::AND &&
3031 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003032 SDValue HighOp0 = HighOp.getOperand(0);
3033 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3034 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3035 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003036 }
3037
3038 // Take advantage of the fact that all GR32 operations only change the
3039 // low 32 bits by truncating Low to an i32 and inserting it directly
3040 // using a subreg. The interesting cases are those where the truncation
3041 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003042 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003043 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003044 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003045 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003046}
3047
Ulrich Weigandb4012182015-03-31 12:56:33 +00003048SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3049 SelectionDAG &DAG) const {
3050 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003051 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003052 Op = Op.getOperand(0);
3053
3054 // Handle vector types via VPOPCT.
3055 if (VT.isVector()) {
3056 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3057 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
3058 switch (VT.getVectorElementType().getSizeInBits()) {
3059 case 8:
3060 break;
3061 case 16: {
3062 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3063 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3064 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3065 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3066 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3067 break;
3068 }
3069 case 32: {
3070 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3071 DAG.getConstant(0, DL, MVT::i32));
3072 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3073 break;
3074 }
3075 case 64: {
3076 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3077 DAG.getConstant(0, DL, MVT::i32));
3078 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3079 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3080 break;
3081 }
3082 default:
3083 llvm_unreachable("Unexpected type");
3084 }
3085 return Op;
3086 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003087
3088 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003089 APInt KnownZero, KnownOne;
3090 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003091 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3092 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003093 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003094
3095 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003096 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003097 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3098 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003099
3100 // The POPCNT instruction counts the number of bits in each byte.
3101 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3102 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3103 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3104
3105 // Add up per-byte counts in a binary tree. All bits of Op at
3106 // position larger than BitSize remain zero throughout.
3107 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003108 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003109 if (BitSize != OrigBitSize)
3110 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003111 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003112 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3113 }
3114
3115 // Extract overall result from high byte.
3116 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003117 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3118 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003119
3120 return Op;
3121}
3122
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003123SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3124 SelectionDAG &DAG) const {
3125 SDLoc DL(Op);
3126 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3127 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
3128 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
3129 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3130
3131 // The only fence that needs an instruction is a sequentially-consistent
3132 // cross-thread fence.
3133 if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
3134 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
3135 Op.getOperand(0)), 0);
3136 }
3137
3138 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3139 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3140}
3141
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003142// Op is an atomic load. Lower it into a normal volatile load.
3143SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3144 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003145 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003146 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3147 Node->getChain(), Node->getBasePtr(),
3148 Node->getMemoryVT(), Node->getMemOperand());
3149}
3150
3151// Op is an atomic store. Lower it into a normal volatile store followed
3152// by a serialization.
3153SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3154 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003155 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003156 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3157 Node->getBasePtr(), Node->getMemoryVT(),
3158 Node->getMemOperand());
3159 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3160 Chain), 0);
3161}
3162
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003163// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3164// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003165SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3166 SelectionDAG &DAG,
3167 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003168 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003169
3170 // 32-bit operations need no code outside the main loop.
3171 EVT NarrowVT = Node->getMemoryVT();
3172 EVT WideVT = MVT::i32;
3173 if (NarrowVT == WideVT)
3174 return Op;
3175
3176 int64_t BitSize = NarrowVT.getSizeInBits();
3177 SDValue ChainIn = Node->getChain();
3178 SDValue Addr = Node->getBasePtr();
3179 SDValue Src2 = Node->getVal();
3180 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003181 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003182 EVT PtrVT = Addr.getValueType();
3183
3184 // Convert atomic subtracts of constants into additions.
3185 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003186 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003187 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003188 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003189 }
3190
3191 // Get the address of the containing word.
3192 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003193 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003194
3195 // Get the number of bits that the word must be rotated left in order
3196 // to bring the field to the top bits of a GR32.
3197 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003198 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003199 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3200
3201 // Get the complementing shift amount, for rotating a field in the top
3202 // bits back to its proper position.
3203 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003204 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003205
3206 // Extend the source operand to 32 bits and prepare it for the inner loop.
3207 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3208 // operations require the source to be shifted in advance. (This shift
3209 // can be folded if the source is constant.) For AND and NAND, the lower
3210 // bits must be set, while for other opcodes they should be left clear.
3211 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3212 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003213 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003214 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3215 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3216 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003217 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003218
3219 // Construct the ATOMIC_LOADW_* node.
3220 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3221 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003222 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003223 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003224 NarrowVT, MMO);
3225
3226 // Rotate the result of the final CS so that the field is in the lower
3227 // bits of a GR32, then truncate it.
3228 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003229 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003230 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3231
3232 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003233 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003234}
3235
Richard Sandiford41350a52013-12-24 15:18:04 +00003236// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003237// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003238// operations into additions.
3239SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3240 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003241 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003242 EVT MemVT = Node->getMemoryVT();
3243 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3244 // A full-width operation.
3245 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3246 SDValue Src2 = Node->getVal();
3247 SDValue NegSrc2;
3248 SDLoc DL(Src2);
3249
Richard Sandiford21f5d682014-03-06 11:22:58 +00003250 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003251 // Use an addition if the operand is constant and either LAA(G) is
3252 // available or the negative value is in the range of A(G)FHI.
3253 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003254 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003255 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003256 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003257 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003258 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003259 Src2);
3260
3261 if (NegSrc2.getNode())
3262 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3263 Node->getChain(), Node->getBasePtr(), NegSrc2,
3264 Node->getMemOperand(), Node->getOrdering(),
3265 Node->getSynchScope());
3266
3267 // Use the node as-is.
3268 return Op;
3269 }
3270
3271 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3272}
3273
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003274// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3275// into a fullword ATOMIC_CMP_SWAPW operation.
3276SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3277 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003278 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003279
3280 // We have native support for 32-bit compare and swap.
3281 EVT NarrowVT = Node->getMemoryVT();
3282 EVT WideVT = MVT::i32;
3283 if (NarrowVT == WideVT)
3284 return Op;
3285
3286 int64_t BitSize = NarrowVT.getSizeInBits();
3287 SDValue ChainIn = Node->getOperand(0);
3288 SDValue Addr = Node->getOperand(1);
3289 SDValue CmpVal = Node->getOperand(2);
3290 SDValue SwapVal = Node->getOperand(3);
3291 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003292 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003293 EVT PtrVT = Addr.getValueType();
3294
3295 // Get the address of the containing word.
3296 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003297 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003298
3299 // Get the number of bits that the word must be rotated left in order
3300 // to bring the field to the top bits of a GR32.
3301 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003302 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003303 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3304
3305 // Get the complementing shift amount, for rotating a field in the top
3306 // bits back to its proper position.
3307 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003308 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003309
3310 // Construct the ATOMIC_CMP_SWAPW node.
3311 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3312 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003313 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003314 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003315 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003316 return AtomicOp;
3317}
3318
3319SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3320 SelectionDAG &DAG) const {
3321 MachineFunction &MF = DAG.getMachineFunction();
3322 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003323 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003324 SystemZ::R15D, Op.getValueType());
3325}
3326
3327SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3328 SelectionDAG &DAG) const {
3329 MachineFunction &MF = DAG.getMachineFunction();
3330 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003331 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003332 SystemZ::R15D, Op.getOperand(1));
3333}
3334
Richard Sandiford03481332013-08-23 11:36:42 +00003335SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3336 SelectionDAG &DAG) const {
3337 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3338 if (!IsData)
3339 // Just preserve the chain.
3340 return Op.getOperand(0);
3341
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003342 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003343 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3344 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003345 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003346 SDValue Ops[] = {
3347 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003348 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003349 Op.getOperand(1)
3350 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003351 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003352 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003353 Node->getMemoryVT(), Node->getMemOperand());
3354}
3355
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003356// Return an i32 that contains the value of CC immediately after After,
3357// whose final operand must be MVT::Glue.
3358static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003359 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003360 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003361 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3362 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3363 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003364}
3365
3366SDValue
3367SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3368 SelectionDAG &DAG) const {
3369 unsigned Opcode, CCValid;
3370 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3371 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3372 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3373 SDValue CC = getCCResult(DAG, Glued.getNode());
3374 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3375 return SDValue();
3376 }
3377
3378 return SDValue();
3379}
3380
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003381SDValue
3382SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3383 SelectionDAG &DAG) const {
3384 unsigned Opcode, CCValid;
3385 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3386 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3387 SDValue CC = getCCResult(DAG, Glued.getNode());
3388 if (Op->getNumValues() == 1)
3389 return CC;
3390 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003391 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3392 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003393 }
3394
3395 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3396 switch (Id) {
3397 case Intrinsic::s390_vpdi:
3398 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3399 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3400
3401 case Intrinsic::s390_vperm:
3402 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3403 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3404
3405 case Intrinsic::s390_vuphb:
3406 case Intrinsic::s390_vuphh:
3407 case Intrinsic::s390_vuphf:
3408 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3409 Op.getOperand(1));
3410
3411 case Intrinsic::s390_vuplhb:
3412 case Intrinsic::s390_vuplhh:
3413 case Intrinsic::s390_vuplhf:
3414 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3415 Op.getOperand(1));
3416
3417 case Intrinsic::s390_vuplb:
3418 case Intrinsic::s390_vuplhw:
3419 case Intrinsic::s390_vuplf:
3420 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3421 Op.getOperand(1));
3422
3423 case Intrinsic::s390_vupllb:
3424 case Intrinsic::s390_vupllh:
3425 case Intrinsic::s390_vupllf:
3426 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3427 Op.getOperand(1));
3428
3429 case Intrinsic::s390_vsumb:
3430 case Intrinsic::s390_vsumh:
3431 case Intrinsic::s390_vsumgh:
3432 case Intrinsic::s390_vsumgf:
3433 case Intrinsic::s390_vsumqf:
3434 case Intrinsic::s390_vsumqg:
3435 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3436 Op.getOperand(1), Op.getOperand(2));
3437 }
3438
3439 return SDValue();
3440}
3441
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003442namespace {
3443// Says that SystemZISD operation Opcode can be used to perform the equivalent
3444// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3445// Operand is the constant third operand, otherwise it is the number of
3446// bytes in each element of the result.
3447struct Permute {
3448 unsigned Opcode;
3449 unsigned Operand;
3450 unsigned char Bytes[SystemZ::VectorBytes];
3451};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003452}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003453
3454static const Permute PermuteForms[] = {
3455 // VMRHG
3456 { SystemZISD::MERGE_HIGH, 8,
3457 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3458 // VMRHF
3459 { SystemZISD::MERGE_HIGH, 4,
3460 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3461 // VMRHH
3462 { SystemZISD::MERGE_HIGH, 2,
3463 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3464 // VMRHB
3465 { SystemZISD::MERGE_HIGH, 1,
3466 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3467 // VMRLG
3468 { SystemZISD::MERGE_LOW, 8,
3469 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3470 // VMRLF
3471 { SystemZISD::MERGE_LOW, 4,
3472 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3473 // VMRLH
3474 { SystemZISD::MERGE_LOW, 2,
3475 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3476 // VMRLB
3477 { SystemZISD::MERGE_LOW, 1,
3478 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3479 // VPKG
3480 { SystemZISD::PACK, 4,
3481 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3482 // VPKF
3483 { SystemZISD::PACK, 2,
3484 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3485 // VPKH
3486 { SystemZISD::PACK, 1,
3487 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3488 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3489 { SystemZISD::PERMUTE_DWORDS, 4,
3490 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3491 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3492 { SystemZISD::PERMUTE_DWORDS, 1,
3493 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3494};
3495
3496// Called after matching a vector shuffle against a particular pattern.
3497// Both the original shuffle and the pattern have two vector operands.
3498// OpNos[0] is the operand of the original shuffle that should be used for
3499// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3500// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3501// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3502// for operands 0 and 1 of the pattern.
3503static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3504 if (OpNos[0] < 0) {
3505 if (OpNos[1] < 0)
3506 return false;
3507 OpNo0 = OpNo1 = OpNos[1];
3508 } else if (OpNos[1] < 0) {
3509 OpNo0 = OpNo1 = OpNos[0];
3510 } else {
3511 OpNo0 = OpNos[0];
3512 OpNo1 = OpNos[1];
3513 }
3514 return true;
3515}
3516
3517// Bytes is a VPERM-like permute vector, except that -1 is used for
3518// undefined bytes. Return true if the VPERM can be implemented using P.
3519// When returning true set OpNo0 to the VPERM operand that should be
3520// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3521//
3522// For example, if swapping the VPERM operands allows P to match, OpNo0
3523// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3524// operand, but rewriting it to use two duplicated operands allows it to
3525// match P, then OpNo0 and OpNo1 will be the same.
3526static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3527 unsigned &OpNo0, unsigned &OpNo1) {
3528 int OpNos[] = { -1, -1 };
3529 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3530 int Elt = Bytes[I];
3531 if (Elt >= 0) {
3532 // Make sure that the two permute vectors use the same suboperand
3533 // byte number. Only the operand numbers (the high bits) are
3534 // allowed to differ.
3535 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3536 return false;
3537 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3538 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3539 // Make sure that the operand mappings are consistent with previous
3540 // elements.
3541 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3542 return false;
3543 OpNos[ModelOpNo] = RealOpNo;
3544 }
3545 }
3546 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3547}
3548
3549// As above, but search for a matching permute.
3550static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3551 unsigned &OpNo0, unsigned &OpNo1) {
3552 for (auto &P : PermuteForms)
3553 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3554 return &P;
3555 return nullptr;
3556}
3557
3558// Bytes is a VPERM-like permute vector, except that -1 is used for
3559// undefined bytes. This permute is an operand of an outer permute.
3560// See whether redistributing the -1 bytes gives a shuffle that can be
3561// implemented using P. If so, set Transform to a VPERM-like permute vector
3562// that, when applied to the result of P, gives the original permute in Bytes.
3563static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3564 const Permute &P,
3565 SmallVectorImpl<int> &Transform) {
3566 unsigned To = 0;
3567 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3568 int Elt = Bytes[From];
3569 if (Elt < 0)
3570 // Byte number From of the result is undefined.
3571 Transform[From] = -1;
3572 else {
3573 while (P.Bytes[To] != Elt) {
3574 To += 1;
3575 if (To == SystemZ::VectorBytes)
3576 return false;
3577 }
3578 Transform[From] = To;
3579 }
3580 }
3581 return true;
3582}
3583
3584// As above, but search for a matching permute.
3585static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3586 SmallVectorImpl<int> &Transform) {
3587 for (auto &P : PermuteForms)
3588 if (matchDoublePermute(Bytes, P, Transform))
3589 return &P;
3590 return nullptr;
3591}
3592
3593// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3594// as if it had type vNi8.
3595static void getVPermMask(ShuffleVectorSDNode *VSN,
3596 SmallVectorImpl<int> &Bytes) {
3597 EVT VT = VSN->getValueType(0);
3598 unsigned NumElements = VT.getVectorNumElements();
3599 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3600 Bytes.resize(NumElements * BytesPerElement, -1);
3601 for (unsigned I = 0; I < NumElements; ++I) {
3602 int Index = VSN->getMaskElt(I);
3603 if (Index >= 0)
3604 for (unsigned J = 0; J < BytesPerElement; ++J)
3605 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3606 }
3607}
3608
3609// Bytes is a VPERM-like permute vector, except that -1 is used for
3610// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3611// the result come from a contiguous sequence of bytes from one input.
3612// Set Base to the selector for the first byte if so.
3613static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3614 unsigned BytesPerElement, int &Base) {
3615 Base = -1;
3616 for (unsigned I = 0; I < BytesPerElement; ++I) {
3617 if (Bytes[Start + I] >= 0) {
3618 unsigned Elem = Bytes[Start + I];
3619 if (Base < 0) {
3620 Base = Elem - I;
3621 // Make sure the bytes would come from one input operand.
3622 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3623 return false;
3624 } else if (unsigned(Base) != Elem - I)
3625 return false;
3626 }
3627 }
3628 return true;
3629}
3630
3631// Bytes is a VPERM-like permute vector, except that -1 is used for
3632// undefined bytes. Return true if it can be performed using VSLDI.
3633// When returning true, set StartIndex to the shift amount and OpNo0
3634// and OpNo1 to the VPERM operands that should be used as the first
3635// and second shift operand respectively.
3636static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3637 unsigned &StartIndex, unsigned &OpNo0,
3638 unsigned &OpNo1) {
3639 int OpNos[] = { -1, -1 };
3640 int Shift = -1;
3641 for (unsigned I = 0; I < 16; ++I) {
3642 int Index = Bytes[I];
3643 if (Index >= 0) {
3644 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3645 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3646 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3647 if (Shift < 0)
3648 Shift = ExpectedShift;
3649 else if (Shift != ExpectedShift)
3650 return false;
3651 // Make sure that the operand mappings are consistent with previous
3652 // elements.
3653 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3654 return false;
3655 OpNos[ModelOpNo] = RealOpNo;
3656 }
3657 }
3658 StartIndex = Shift;
3659 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3660}
3661
3662// Create a node that performs P on operands Op0 and Op1, casting the
3663// operands to the appropriate type. The type of the result is determined by P.
3664static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3665 const Permute &P, SDValue Op0, SDValue Op1) {
3666 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3667 // elements of a PACK are twice as wide as the outputs.
3668 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3669 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3670 P.Operand);
3671 // Cast both operands to the appropriate type.
3672 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3673 SystemZ::VectorBytes / InBytes);
3674 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3675 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3676 SDValue Op;
3677 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3678 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3679 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3680 } else if (P.Opcode == SystemZISD::PACK) {
3681 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3682 SystemZ::VectorBytes / P.Operand);
3683 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3684 } else {
3685 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3686 }
3687 return Op;
3688}
3689
3690// Bytes is a VPERM-like permute vector, except that -1 is used for
3691// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3692// VSLDI or VPERM.
3693static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3694 const SmallVectorImpl<int> &Bytes) {
3695 for (unsigned I = 0; I < 2; ++I)
3696 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3697
3698 // First see whether VSLDI can be used.
3699 unsigned StartIndex, OpNo0, OpNo1;
3700 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3701 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3702 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3703
3704 // Fall back on VPERM. Construct an SDNode for the permute vector.
3705 SDValue IndexNodes[SystemZ::VectorBytes];
3706 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3707 if (Bytes[I] >= 0)
3708 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3709 else
3710 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3711 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3712 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3713}
3714
3715namespace {
3716// Describes a general N-operand vector shuffle.
3717struct GeneralShuffle {
3718 GeneralShuffle(EVT vt) : VT(vt) {}
3719 void addUndef();
3720 void add(SDValue, unsigned);
3721 SDValue getNode(SelectionDAG &, SDLoc);
3722
3723 // The operands of the shuffle.
3724 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3725
3726 // Index I is -1 if byte I of the result is undefined. Otherwise the
3727 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3728 // Bytes[I] / SystemZ::VectorBytes.
3729 SmallVector<int, SystemZ::VectorBytes> Bytes;
3730
3731 // The type of the shuffle result.
3732 EVT VT;
3733};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003734}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003735
3736// Add an extra undefined element to the shuffle.
3737void GeneralShuffle::addUndef() {
3738 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3739 for (unsigned I = 0; I < BytesPerElement; ++I)
3740 Bytes.push_back(-1);
3741}
3742
3743// Add an extra element to the shuffle, taking it from element Elem of Op.
3744// A null Op indicates a vector input whose value will be calculated later;
3745// there is at most one such input per shuffle and it always has the same
3746// type as the result.
3747void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3748 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3749
3750 // The source vector can have wider elements than the result,
3751 // either through an explicit TRUNCATE or because of type legalization.
3752 // We want the least significant part.
3753 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3754 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3755 assert(FromBytesPerElement >= BytesPerElement &&
3756 "Invalid EXTRACT_VECTOR_ELT");
3757 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3758 (FromBytesPerElement - BytesPerElement));
3759
3760 // Look through things like shuffles and bitcasts.
3761 while (Op.getNode()) {
3762 if (Op.getOpcode() == ISD::BITCAST)
3763 Op = Op.getOperand(0);
3764 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3765 // See whether the bytes we need come from a contiguous part of one
3766 // operand.
3767 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3768 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3769 int NewByte;
3770 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3771 break;
3772 if (NewByte < 0) {
3773 addUndef();
3774 return;
3775 }
3776 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3777 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003778 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003779 addUndef();
3780 return;
3781 } else
3782 break;
3783 }
3784
3785 // Make sure that the source of the extraction is in Ops.
3786 unsigned OpNo = 0;
3787 for (; OpNo < Ops.size(); ++OpNo)
3788 if (Ops[OpNo] == Op)
3789 break;
3790 if (OpNo == Ops.size())
3791 Ops.push_back(Op);
3792
3793 // Add the element to Bytes.
3794 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3795 for (unsigned I = 0; I < BytesPerElement; ++I)
3796 Bytes.push_back(Base + I);
3797}
3798
3799// Return SDNodes for the completed shuffle.
3800SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3801 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3802
3803 if (Ops.size() == 0)
3804 return DAG.getUNDEF(VT);
3805
3806 // Make sure that there are at least two shuffle operands.
3807 if (Ops.size() == 1)
3808 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3809
3810 // Create a tree of shuffles, deferring root node until after the loop.
3811 // Try to redistribute the undefined elements of non-root nodes so that
3812 // the non-root shuffles match something like a pack or merge, then adjust
3813 // the parent node's permute vector to compensate for the new order.
3814 // Among other things, this copes with vectors like <2 x i16> that were
3815 // padded with undefined elements during type legalization.
3816 //
3817 // In the best case this redistribution will lead to the whole tree
3818 // using packs and merges. It should rarely be a loss in other cases.
3819 unsigned Stride = 1;
3820 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3821 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3822 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3823
3824 // Create a mask for just these two operands.
3825 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3826 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3827 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3828 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3829 if (OpNo == I)
3830 NewBytes[J] = Byte;
3831 else if (OpNo == I + Stride)
3832 NewBytes[J] = SystemZ::VectorBytes + Byte;
3833 else
3834 NewBytes[J] = -1;
3835 }
3836 // See if it would be better to reorganize NewMask to avoid using VPERM.
3837 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3838 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3839 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3840 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3841 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3842 if (NewBytes[J] >= 0) {
3843 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3844 "Invalid double permute");
3845 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3846 } else
3847 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3848 }
3849 } else {
3850 // Just use NewBytes on the operands.
3851 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3852 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3853 if (NewBytes[J] >= 0)
3854 Bytes[J] = I * SystemZ::VectorBytes + J;
3855 }
3856 }
3857 }
3858
3859 // Now we just have 2 inputs. Put the second operand in Ops[1].
3860 if (Stride > 1) {
3861 Ops[1] = Ops[Stride];
3862 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3863 if (Bytes[I] >= int(SystemZ::VectorBytes))
3864 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3865 }
3866
3867 // Look for an instruction that can do the permute without resorting
3868 // to VPERM.
3869 unsigned OpNo0, OpNo1;
3870 SDValue Op;
3871 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3872 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3873 else
3874 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3875 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3876}
3877
Ulrich Weigandcd808232015-05-05 19:26:48 +00003878// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3879static bool isScalarToVector(SDValue Op) {
3880 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003881 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003882 return false;
3883 return true;
3884}
3885
3886// Return a vector of type VT that contains Value in the first element.
3887// The other elements don't matter.
3888static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3889 SDValue Value) {
3890 // If we have a constant, replicate it to all elements and let the
3891 // BUILD_VECTOR lowering take care of it.
3892 if (Value.getOpcode() == ISD::Constant ||
3893 Value.getOpcode() == ISD::ConstantFP) {
3894 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3895 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3896 }
Sanjay Patel57195842016-03-14 17:28:46 +00003897 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003898 return DAG.getUNDEF(VT);
3899 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3900}
3901
3902// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3903// element 1. Used for cases in which replication is cheap.
3904static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3905 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003906 if (Op0.isUndef()) {
3907 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003908 return DAG.getUNDEF(VT);
3909 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3910 }
Sanjay Patel57195842016-03-14 17:28:46 +00003911 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003912 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3913 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3914 buildScalarToVector(DAG, DL, VT, Op0),
3915 buildScalarToVector(DAG, DL, VT, Op1));
3916}
3917
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003918// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3919// vector for them.
3920static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3921 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003922 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003923 return DAG.getUNDEF(MVT::v2i64);
3924 // If one of the two inputs is undefined then replicate the other one,
3925 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00003926 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003927 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00003928 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003929 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3930 else {
3931 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3932 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3933 }
3934 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3935}
3936
3937// Try to represent constant BUILD_VECTOR node BVN using a
3938// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3939// on success.
3940static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3941 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3942 unsigned BytesPerElement = ElemVT.getStoreSize();
3943 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3944 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00003945 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003946 uint64_t Value;
3947 if (Op.getOpcode() == ISD::Constant)
3948 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3949 else if (Op.getOpcode() == ISD::ConstantFP)
3950 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3951 .getZExtValue());
3952 else
3953 return false;
3954 for (unsigned J = 0; J < BytesPerElement; ++J) {
3955 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3956 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00003957 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003958 else if (Byte != 0)
3959 return false;
3960 }
3961 }
3962 }
3963 return true;
3964}
3965
3966// Try to load a vector constant in which BitsPerElement-bit value Value
3967// is replicated to fill the vector. VT is the type of the resulting
3968// constant, which may have elements of a different size from BitsPerElement.
3969// Return the SDValue of the constant on success, otherwise return
3970// an empty value.
3971static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3972 const SystemZInstrInfo *TII,
3973 SDLoc DL, EVT VT, uint64_t Value,
3974 unsigned BitsPerElement) {
3975 // Signed 16-bit values can be replicated using VREPI.
3976 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3977 if (isInt<16>(SignedValue)) {
3978 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3979 SystemZ::VectorBits / BitsPerElement);
3980 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3981 DAG.getConstant(SignedValue, DL, MVT::i32));
3982 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3983 }
3984 // See whether rotating the constant left some N places gives a value that
3985 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3986 // If so we can use VGM.
3987 unsigned Start, End;
3988 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3989 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3990 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3991 // bit numbers for an BitsPerElement value, so that 0 denotes
3992 // 1 << (BitsPerElement-1).
3993 Start -= 64 - BitsPerElement;
3994 End -= 64 - BitsPerElement;
3995 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3996 SystemZ::VectorBits / BitsPerElement);
3997 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3998 DAG.getConstant(Start, DL, MVT::i32),
3999 DAG.getConstant(End, DL, MVT::i32));
4000 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4001 }
4002 return SDValue();
4003}
4004
4005// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4006// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4007// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4008// would benefit from this representation and return it if so.
4009static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4010 BuildVectorSDNode *BVN) {
4011 EVT VT = BVN->getValueType(0);
4012 unsigned NumElements = VT.getVectorNumElements();
4013
4014 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4015 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4016 // need a BUILD_VECTOR, add an additional placeholder operand for that
4017 // BUILD_VECTOR and store its operands in ResidueOps.
4018 GeneralShuffle GS(VT);
4019 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4020 bool FoundOne = false;
4021 for (unsigned I = 0; I < NumElements; ++I) {
4022 SDValue Op = BVN->getOperand(I);
4023 if (Op.getOpcode() == ISD::TRUNCATE)
4024 Op = Op.getOperand(0);
4025 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4026 Op.getOperand(1).getOpcode() == ISD::Constant) {
4027 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4028 GS.add(Op.getOperand(0), Elem);
4029 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004030 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004031 GS.addUndef();
4032 } else {
4033 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00004034 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004035 }
4036 }
4037
4038 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4039 if (!FoundOne)
4040 return SDValue();
4041
4042 // Create the BUILD_VECTOR for the remaining elements, if any.
4043 if (!ResidueOps.empty()) {
4044 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004045 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004046 for (auto &Op : GS.Ops) {
4047 if (!Op.getNode()) {
4048 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
4049 break;
4050 }
4051 }
4052 }
4053 return GS.getNode(DAG, SDLoc(BVN));
4054}
4055
4056// Combine GPR scalar values Elems into a vector of type VT.
4057static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
4058 SmallVectorImpl<SDValue> &Elems) {
4059 // See whether there is a single replicated value.
4060 SDValue Single;
4061 unsigned int NumElements = Elems.size();
4062 unsigned int Count = 0;
4063 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004064 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004065 if (!Single.getNode())
4066 Single = Elem;
4067 else if (Elem != Single) {
4068 Single = SDValue();
4069 break;
4070 }
4071 Count += 1;
4072 }
4073 }
4074 // There are three cases here:
4075 //
4076 // - if the only defined element is a loaded one, the best sequence
4077 // is a replicating load.
4078 //
4079 // - otherwise, if the only defined element is an i64 value, we will
4080 // end up with the same VLVGP sequence regardless of whether we short-cut
4081 // for replication or fall through to the later code.
4082 //
4083 // - otherwise, if the only defined element is an i32 or smaller value,
4084 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4085 // This is only a win if the single defined element is used more than once.
4086 // In other cases we're better off using a single VLVGx.
4087 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4088 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4089
4090 // The best way of building a v2i64 from two i64s is to use VLVGP.
4091 if (VT == MVT::v2i64)
4092 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4093
Ulrich Weigandcd808232015-05-05 19:26:48 +00004094 // Use a 64-bit merge high to combine two doubles.
4095 if (VT == MVT::v2f64)
4096 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4097
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004098 // Build v4f32 values directly from the FPRs:
4099 //
4100 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4101 // V V VMRHF
4102 // <ABxx> <CDxx>
4103 // V VMRHG
4104 // <ABCD>
4105 if (VT == MVT::v4f32) {
4106 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4107 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4108 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004109 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004110 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004111 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004112 Op23 = Op01;
4113 // Merging identical replications is a no-op.
4114 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4115 return Op01;
4116 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4117 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4118 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4119 DL, MVT::v2i64, Op01, Op23);
4120 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4121 }
4122
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004123 // Collect the constant terms.
4124 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4125 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4126
4127 unsigned NumConstants = 0;
4128 for (unsigned I = 0; I < NumElements; ++I) {
4129 SDValue Elem = Elems[I];
4130 if (Elem.getOpcode() == ISD::Constant ||
4131 Elem.getOpcode() == ISD::ConstantFP) {
4132 NumConstants += 1;
4133 Constants[I] = Elem;
4134 Done[I] = true;
4135 }
4136 }
4137 // If there was at least one constant, fill in the other elements of
4138 // Constants with undefs to get a full vector constant and use that
4139 // as the starting point.
4140 SDValue Result;
4141 if (NumConstants > 0) {
4142 for (unsigned I = 0; I < NumElements; ++I)
4143 if (!Constants[I].getNode())
4144 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4145 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4146 } else {
4147 // Otherwise try to use VLVGP to start the sequence in order to
4148 // avoid a false dependency on any previous contents of the vector
4149 // register. This only makes sense if one of the associated elements
4150 // is defined.
4151 unsigned I1 = NumElements / 2 - 1;
4152 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004153 bool Def1 = !Elems[I1].isUndef();
4154 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004155 if (Def1 || Def2) {
4156 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4157 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4158 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4159 joinDwords(DAG, DL, Elem1, Elem2));
4160 Done[I1] = true;
4161 Done[I2] = true;
4162 } else
4163 Result = DAG.getUNDEF(VT);
4164 }
4165
4166 // Use VLVGx to insert the other elements.
4167 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004168 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004169 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4170 DAG.getConstant(I, DL, MVT::i32));
4171 return Result;
4172}
4173
4174SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4175 SelectionDAG &DAG) const {
4176 const SystemZInstrInfo *TII =
4177 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4178 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4179 SDLoc DL(Op);
4180 EVT VT = Op.getValueType();
4181
4182 if (BVN->isConstant()) {
4183 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4184 // preferred way of creating all-zero and all-one vectors so give it
4185 // priority over other methods below.
4186 uint64_t Mask = 0;
4187 if (tryBuildVectorByteMask(BVN, Mask)) {
4188 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4189 DAG.getConstant(Mask, DL, MVT::i32));
4190 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4191 }
4192
4193 // Try using some form of replication.
4194 APInt SplatBits, SplatUndef;
4195 unsigned SplatBitSize;
4196 bool HasAnyUndefs;
4197 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4198 8, true) &&
4199 SplatBitSize <= 64) {
4200 // First try assuming that any undefined bits above the highest set bit
4201 // and below the lowest set bit are 1s. This increases the likelihood of
4202 // being able to use a sign-extended element value in VECTOR REPLICATE
4203 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4204 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4205 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4206 uint64_t Lower = (SplatUndefZ
4207 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4208 uint64_t Upper = (SplatUndefZ
4209 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4210 uint64_t Value = SplatBitsZ | Upper | Lower;
4211 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4212 SplatBitSize);
4213 if (Op.getNode())
4214 return Op;
4215
4216 // Now try assuming that any undefined bits between the first and
4217 // last defined set bits are set. This increases the chances of
4218 // using a non-wraparound mask.
4219 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4220 Value = SplatBitsZ | Middle;
4221 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4222 if (Op.getNode())
4223 return Op;
4224 }
4225
4226 // Fall back to loading it from memory.
4227 return SDValue();
4228 }
4229
4230 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004231 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004232 return Res;
4233
Ulrich Weigandcd808232015-05-05 19:26:48 +00004234 // Detect SCALAR_TO_VECTOR conversions.
4235 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4236 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4237
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004238 // Otherwise use buildVector to build the vector up from GPRs.
4239 unsigned NumElements = Op.getNumOperands();
4240 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4241 for (unsigned I = 0; I < NumElements; ++I)
4242 Ops[I] = Op.getOperand(I);
4243 return buildVector(DAG, DL, VT, Ops);
4244}
4245
4246SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4247 SelectionDAG &DAG) const {
4248 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4249 SDLoc DL(Op);
4250 EVT VT = Op.getValueType();
4251 unsigned NumElements = VT.getVectorNumElements();
4252
4253 if (VSN->isSplat()) {
4254 SDValue Op0 = Op.getOperand(0);
4255 unsigned Index = VSN->getSplatIndex();
4256 assert(Index < VT.getVectorNumElements() &&
4257 "Splat index should be defined and in first operand");
4258 // See whether the value we're splatting is directly available as a scalar.
4259 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4260 Op0.getOpcode() == ISD::BUILD_VECTOR)
4261 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4262 // Otherwise keep it as a vector-to-vector operation.
4263 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4264 DAG.getConstant(Index, DL, MVT::i32));
4265 }
4266
4267 GeneralShuffle GS(VT);
4268 for (unsigned I = 0; I < NumElements; ++I) {
4269 int Elt = VSN->getMaskElt(I);
4270 if (Elt < 0)
4271 GS.addUndef();
4272 else
4273 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4274 unsigned(Elt) % NumElements);
4275 }
4276 return GS.getNode(DAG, SDLoc(VSN));
4277}
4278
4279SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4280 SelectionDAG &DAG) const {
4281 SDLoc DL(Op);
4282 // Just insert the scalar into element 0 of an undefined vector.
4283 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4284 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4285 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4286}
4287
Ulrich Weigandcd808232015-05-05 19:26:48 +00004288SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4289 SelectionDAG &DAG) const {
4290 // Handle insertions of floating-point values.
4291 SDLoc DL(Op);
4292 SDValue Op0 = Op.getOperand(0);
4293 SDValue Op1 = Op.getOperand(1);
4294 SDValue Op2 = Op.getOperand(2);
4295 EVT VT = Op.getValueType();
4296
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004297 // Insertions into constant indices of a v2f64 can be done using VPDI.
4298 // However, if the inserted value is a bitcast or a constant then it's
4299 // better to use GPRs, as below.
4300 if (VT == MVT::v2f64 &&
4301 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004302 Op1.getOpcode() != ISD::ConstantFP &&
4303 Op2.getOpcode() == ISD::Constant) {
4304 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4305 unsigned Mask = VT.getVectorNumElements() - 1;
4306 if (Index <= Mask)
4307 return Op;
4308 }
4309
4310 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4311 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4312 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4313 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4314 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4315 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4316 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4317}
4318
4319SDValue
4320SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4321 SelectionDAG &DAG) const {
4322 // Handle extractions of floating-point values.
4323 SDLoc DL(Op);
4324 SDValue Op0 = Op.getOperand(0);
4325 SDValue Op1 = Op.getOperand(1);
4326 EVT VT = Op.getValueType();
4327 EVT VecVT = Op0.getValueType();
4328
4329 // Extractions of constant indices can be done directly.
4330 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4331 uint64_t Index = CIndexN->getZExtValue();
4332 unsigned Mask = VecVT.getVectorNumElements() - 1;
4333 if (Index <= Mask)
4334 return Op;
4335 }
4336
4337 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4338 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4339 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4340 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4341 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4342 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4343}
4344
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004345SDValue
4346SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004347 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004348 SDValue PackedOp = Op.getOperand(0);
4349 EVT OutVT = Op.getValueType();
4350 EVT InVT = PackedOp.getValueType();
4351 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4352 unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4353 do {
4354 FromBits *= 2;
4355 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4356 SystemZ::VectorBits / FromBits);
4357 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4358 } while (FromBits != ToBits);
4359 return PackedOp;
4360}
4361
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004362SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4363 unsigned ByScalar) const {
4364 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4365 SDValue Op0 = Op.getOperand(0);
4366 SDValue Op1 = Op.getOperand(1);
4367 SDLoc DL(Op);
4368 EVT VT = Op.getValueType();
4369 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4370
4371 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4372 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4373 APInt SplatBits, SplatUndef;
4374 unsigned SplatBitSize;
4375 bool HasAnyUndefs;
4376 // Check for constant splats. Use ElemBitSize as the minimum element
4377 // width and reject splats that need wider elements.
4378 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4379 ElemBitSize, true) &&
4380 SplatBitSize == ElemBitSize) {
4381 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4382 DL, MVT::i32);
4383 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4384 }
4385 // Check for variable splats.
4386 BitVector UndefElements;
4387 SDValue Splat = BVN->getSplatValue(&UndefElements);
4388 if (Splat) {
4389 // Since i32 is the smallest legal type, we either need a no-op
4390 // or a truncation.
4391 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4392 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4393 }
4394 }
4395
4396 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4397 // and the shift amount is directly available in a GPR.
4398 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4399 if (VSN->isSplat()) {
4400 SDValue VSNOp0 = VSN->getOperand(0);
4401 unsigned Index = VSN->getSplatIndex();
4402 assert(Index < VT.getVectorNumElements() &&
4403 "Splat index should be defined and in first operand");
4404 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4405 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4406 // Since i32 is the smallest legal type, we either need a no-op
4407 // or a truncation.
4408 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4409 VSNOp0.getOperand(Index));
4410 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4411 }
4412 }
4413 }
4414
4415 // Otherwise just treat the current form as legal.
4416 return Op;
4417}
4418
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004419SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4420 SelectionDAG &DAG) const {
4421 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004422 case ISD::FRAMEADDR:
4423 return lowerFRAMEADDR(Op, DAG);
4424 case ISD::RETURNADDR:
4425 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004426 case ISD::BR_CC:
4427 return lowerBR_CC(Op, DAG);
4428 case ISD::SELECT_CC:
4429 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004430 case ISD::SETCC:
4431 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004432 case ISD::GlobalAddress:
4433 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4434 case ISD::GlobalTLSAddress:
4435 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4436 case ISD::BlockAddress:
4437 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4438 case ISD::JumpTable:
4439 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4440 case ISD::ConstantPool:
4441 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4442 case ISD::BITCAST:
4443 return lowerBITCAST(Op, DAG);
4444 case ISD::VASTART:
4445 return lowerVASTART(Op, DAG);
4446 case ISD::VACOPY:
4447 return lowerVACOPY(Op, DAG);
4448 case ISD::DYNAMIC_STACKALLOC:
4449 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004450 case ISD::SMUL_LOHI:
4451 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004452 case ISD::UMUL_LOHI:
4453 return lowerUMUL_LOHI(Op, DAG);
4454 case ISD::SDIVREM:
4455 return lowerSDIVREM(Op, DAG);
4456 case ISD::UDIVREM:
4457 return lowerUDIVREM(Op, DAG);
4458 case ISD::OR:
4459 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004460 case ISD::CTPOP:
4461 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004462 case ISD::CTLZ_ZERO_UNDEF:
4463 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4464 Op.getValueType(), Op.getOperand(0));
4465 case ISD::CTTZ_ZERO_UNDEF:
4466 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4467 Op.getValueType(), Op.getOperand(0));
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004468 case ISD::ATOMIC_FENCE:
4469 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004470 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004471 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4472 case ISD::ATOMIC_STORE:
4473 return lowerATOMIC_STORE(Op, DAG);
4474 case ISD::ATOMIC_LOAD:
4475 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004476 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004477 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004478 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004479 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004480 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004481 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004482 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004483 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004484 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004485 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004486 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004487 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004488 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004489 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004490 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004491 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004492 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004493 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004494 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004495 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004496 case ISD::ATOMIC_CMP_SWAP:
4497 return lowerATOMIC_CMP_SWAP(Op, DAG);
4498 case ISD::STACKSAVE:
4499 return lowerSTACKSAVE(Op, DAG);
4500 case ISD::STACKRESTORE:
4501 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004502 case ISD::PREFETCH:
4503 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004504 case ISD::INTRINSIC_W_CHAIN:
4505 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004506 case ISD::INTRINSIC_WO_CHAIN:
4507 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004508 case ISD::BUILD_VECTOR:
4509 return lowerBUILD_VECTOR(Op, DAG);
4510 case ISD::VECTOR_SHUFFLE:
4511 return lowerVECTOR_SHUFFLE(Op, DAG);
4512 case ISD::SCALAR_TO_VECTOR:
4513 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004514 case ISD::INSERT_VECTOR_ELT:
4515 return lowerINSERT_VECTOR_ELT(Op, DAG);
4516 case ISD::EXTRACT_VECTOR_ELT:
4517 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004518 case ISD::SIGN_EXTEND_VECTOR_INREG:
4519 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4520 case ISD::ZERO_EXTEND_VECTOR_INREG:
4521 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004522 case ISD::SHL:
4523 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4524 case ISD::SRL:
4525 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4526 case ISD::SRA:
4527 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004528 default:
4529 llvm_unreachable("Unexpected node to lower");
4530 }
4531}
4532
4533const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4534#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004535 switch ((SystemZISD::NodeType)Opcode) {
4536 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004537 OPCODE(RET_FLAG);
4538 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004539 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004540 OPCODE(TLS_GDCALL);
4541 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004542 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004543 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004544 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004545 OPCODE(ICMP);
4546 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004547 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004548 OPCODE(BR_CCMASK);
4549 OPCODE(SELECT_CCMASK);
4550 OPCODE(ADJDYNALLOC);
4551 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004552 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004553 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004554 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004555 OPCODE(SDIVREM64);
4556 OPCODE(UDIVREM32);
4557 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004558 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004559 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004560 OPCODE(NC);
4561 OPCODE(NC_LOOP);
4562 OPCODE(OC);
4563 OPCODE(OC_LOOP);
4564 OPCODE(XC);
4565 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004566 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004567 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004568 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004569 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004570 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004571 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004572 OPCODE(SERIALIZE);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004573 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004574 OPCODE(TBEGIN);
4575 OPCODE(TBEGIN_NOFLOAT);
4576 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004577 OPCODE(BYTE_MASK);
4578 OPCODE(ROTATE_MASK);
4579 OPCODE(REPLICATE);
4580 OPCODE(JOIN_DWORDS);
4581 OPCODE(SPLAT);
4582 OPCODE(MERGE_HIGH);
4583 OPCODE(MERGE_LOW);
4584 OPCODE(SHL_DOUBLE);
4585 OPCODE(PERMUTE_DWORDS);
4586 OPCODE(PERMUTE);
4587 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004588 OPCODE(PACKS_CC);
4589 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004590 OPCODE(UNPACK_HIGH);
4591 OPCODE(UNPACKL_HIGH);
4592 OPCODE(UNPACK_LOW);
4593 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004594 OPCODE(VSHL_BY_SCALAR);
4595 OPCODE(VSRL_BY_SCALAR);
4596 OPCODE(VSRA_BY_SCALAR);
4597 OPCODE(VSUM);
4598 OPCODE(VICMPE);
4599 OPCODE(VICMPH);
4600 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004601 OPCODE(VICMPES);
4602 OPCODE(VICMPHS);
4603 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004604 OPCODE(VFCMPE);
4605 OPCODE(VFCMPH);
4606 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004607 OPCODE(VFCMPES);
4608 OPCODE(VFCMPHS);
4609 OPCODE(VFCMPHES);
4610 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004611 OPCODE(VEXTEND);
4612 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004613 OPCODE(VTM);
4614 OPCODE(VFAE_CC);
4615 OPCODE(VFAEZ_CC);
4616 OPCODE(VFEE_CC);
4617 OPCODE(VFEEZ_CC);
4618 OPCODE(VFENE_CC);
4619 OPCODE(VFENEZ_CC);
4620 OPCODE(VISTR_CC);
4621 OPCODE(VSTRC_CC);
4622 OPCODE(VSTRCZ_CC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004623 OPCODE(ATOMIC_SWAPW);
4624 OPCODE(ATOMIC_LOADW_ADD);
4625 OPCODE(ATOMIC_LOADW_SUB);
4626 OPCODE(ATOMIC_LOADW_AND);
4627 OPCODE(ATOMIC_LOADW_OR);
4628 OPCODE(ATOMIC_LOADW_XOR);
4629 OPCODE(ATOMIC_LOADW_NAND);
4630 OPCODE(ATOMIC_LOADW_MIN);
4631 OPCODE(ATOMIC_LOADW_MAX);
4632 OPCODE(ATOMIC_LOADW_UMIN);
4633 OPCODE(ATOMIC_LOADW_UMAX);
4634 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004635 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004636 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004637 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004638#undef OPCODE
4639}
4640
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004641// Return true if VT is a vector whose elements are a whole number of bytes
4642// in width.
4643static bool canTreatAsByteVector(EVT VT) {
4644 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4645}
4646
4647// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4648// producing a result of type ResVT. Op is a possibly bitcast version
4649// of the input vector and Index is the index (based on type VecVT) that
4650// should be extracted. Return the new extraction if a simplification
4651// was possible or if Force is true.
4652SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4653 SDValue Op, unsigned Index,
4654 DAGCombinerInfo &DCI,
4655 bool Force) const {
4656 SelectionDAG &DAG = DCI.DAG;
4657
4658 // The number of bytes being extracted.
4659 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4660
4661 for (;;) {
4662 unsigned Opcode = Op.getOpcode();
4663 if (Opcode == ISD::BITCAST)
4664 // Look through bitcasts.
4665 Op = Op.getOperand(0);
4666 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4667 canTreatAsByteVector(Op.getValueType())) {
4668 // Get a VPERM-like permute mask and see whether the bytes covered
4669 // by the extracted element are a contiguous sequence from one
4670 // source operand.
4671 SmallVector<int, SystemZ::VectorBytes> Bytes;
4672 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4673 int First;
4674 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4675 BytesPerElement, First))
4676 break;
4677 if (First < 0)
4678 return DAG.getUNDEF(ResVT);
4679 // Make sure the contiguous sequence starts at a multiple of the
4680 // original element size.
4681 unsigned Byte = unsigned(First) % Bytes.size();
4682 if (Byte % BytesPerElement != 0)
4683 break;
4684 // We can get the extracted value directly from an input.
4685 Index = Byte / BytesPerElement;
4686 Op = Op.getOperand(unsigned(First) / Bytes.size());
4687 Force = true;
4688 } else if (Opcode == ISD::BUILD_VECTOR &&
4689 canTreatAsByteVector(Op.getValueType())) {
4690 // We can only optimize this case if the BUILD_VECTOR elements are
4691 // at least as wide as the extracted value.
4692 EVT OpVT = Op.getValueType();
4693 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4694 if (OpBytesPerElement < BytesPerElement)
4695 break;
4696 // Make sure that the least-significant bit of the extracted value
4697 // is the least significant bit of an input.
4698 unsigned End = (Index + 1) * BytesPerElement;
4699 if (End % OpBytesPerElement != 0)
4700 break;
4701 // We're extracting the low part of one operand of the BUILD_VECTOR.
4702 Op = Op.getOperand(End / OpBytesPerElement - 1);
4703 if (!Op.getValueType().isInteger()) {
4704 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4705 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4706 DCI.AddToWorklist(Op.getNode());
4707 }
4708 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4709 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4710 if (VT != ResVT) {
4711 DCI.AddToWorklist(Op.getNode());
4712 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4713 }
4714 return Op;
4715 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004716 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4717 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4718 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004719 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4720 // Make sure that only the unextended bits are significant.
4721 EVT ExtVT = Op.getValueType();
4722 EVT OpVT = Op.getOperand(0).getValueType();
4723 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4724 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4725 unsigned Byte = Index * BytesPerElement;
4726 unsigned SubByte = Byte % ExtBytesPerElement;
4727 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4728 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004729 SubByte + BytesPerElement > ExtBytesPerElement)
4730 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004731 // Get the byte offset of the unextended element
4732 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4733 // ...then add the byte offset relative to that element.
4734 Byte += SubByte - MinSubByte;
4735 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004736 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004737 Op = Op.getOperand(0);
4738 Index = Byte / BytesPerElement;
4739 Force = true;
4740 } else
4741 break;
4742 }
4743 if (Force) {
4744 if (Op.getValueType() != VecVT) {
4745 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4746 DCI.AddToWorklist(Op.getNode());
4747 }
4748 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4749 DAG.getConstant(Index, DL, MVT::i32));
4750 }
4751 return SDValue();
4752}
4753
4754// Optimize vector operations in scalar value Op on the basis that Op
4755// is truncated to TruncVT.
4756SDValue
4757SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4758 DAGCombinerInfo &DCI) const {
4759 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4760 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4761 // of type TruncVT.
4762 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4763 TruncVT.getSizeInBits() % 8 == 0) {
4764 SDValue Vec = Op.getOperand(0);
4765 EVT VecVT = Vec.getValueType();
4766 if (canTreatAsByteVector(VecVT)) {
4767 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4768 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4769 unsigned TruncBytes = TruncVT.getStoreSize();
4770 if (BytesPerElement % TruncBytes == 0) {
4771 // Calculate the value of Y' in the above description. We are
4772 // splitting the original elements into Scale equal-sized pieces
4773 // and for truncation purposes want the last (least-significant)
4774 // of these pieces for IndexN. This is easiest to do by calculating
4775 // the start index of the following element and then subtracting 1.
4776 unsigned Scale = BytesPerElement / TruncBytes;
4777 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4778
4779 // Defer the creation of the bitcast from X to combineExtract,
4780 // which might be able to optimize the extraction.
4781 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4782 VecVT.getStoreSize() / TruncBytes);
4783 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4784 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4785 }
4786 }
4787 }
4788 }
4789 return SDValue();
4790}
4791
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004792SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4793 DAGCombinerInfo &DCI) const {
4794 SelectionDAG &DAG = DCI.DAG;
4795 unsigned Opcode = N->getOpcode();
4796 if (Opcode == ISD::SIGN_EXTEND) {
4797 // Convert (sext (ashr (shl X, C1), C2)) to
4798 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4799 // cheap as narrower ones.
4800 SDValue N0 = N->getOperand(0);
4801 EVT VT = N->getValueType(0);
4802 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4803 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4804 SDValue Inner = N0.getOperand(0);
4805 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4806 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4807 unsigned Extra = (VT.getSizeInBits() -
4808 N0.getValueType().getSizeInBits());
4809 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4810 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4811 EVT ShiftVT = N0.getOperand(1).getValueType();
4812 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4813 Inner.getOperand(0));
4814 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004815 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4816 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004817 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004818 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004819 }
4820 }
4821 }
4822 }
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004823 if (Opcode == SystemZISD::MERGE_HIGH ||
4824 Opcode == SystemZISD::MERGE_LOW) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004825 SDValue Op0 = N->getOperand(0);
4826 SDValue Op1 = N->getOperand(1);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004827 if (Op0.getOpcode() == ISD::BITCAST)
4828 Op0 = Op0.getOperand(0);
4829 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4830 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4831 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4832 // for v4f32.
4833 if (Op1 == N->getOperand(0))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004834 return Op1;
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004835 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4836 EVT VT = Op1.getValueType();
4837 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4838 if (ElemBytes <= 4) {
4839 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4840 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4841 EVT InVT = VT.changeVectorElementTypeToInteger();
4842 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4843 SystemZ::VectorBytes / ElemBytes / 2);
4844 if (VT != InVT) {
4845 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4846 DCI.AddToWorklist(Op1.getNode());
4847 }
4848 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4849 DCI.AddToWorklist(Op.getNode());
4850 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4851 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004852 }
4853 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004854 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4855 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4856 // If X has wider elements then convert it to:
4857 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4858 if (Opcode == ISD::STORE) {
4859 auto *SN = cast<StoreSDNode>(N);
4860 EVT MemVT = SN->getMemoryVT();
4861 if (MemVT.isInteger()) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004862 if (SDValue Value =
4863 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004864 DCI.AddToWorklist(Value.getNode());
4865
4866 // Rewrite the store with the new form of stored value.
4867 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4868 SN->getBasePtr(), SN->getMemoryVT(),
4869 SN->getMemOperand());
4870 }
4871 }
4872 }
4873 // Try to simplify a vector extraction.
4874 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4875 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4876 SDValue Op0 = N->getOperand(0);
4877 EVT VecVT = Op0.getValueType();
4878 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4879 IndexN->getZExtValue(), DCI, false);
4880 }
4881 }
4882 // (join_dwords X, X) == (replicate X)
4883 if (Opcode == SystemZISD::JOIN_DWORDS &&
4884 N->getOperand(0) == N->getOperand(1))
4885 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4886 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004887 // (fround (extract_vector_elt X 0))
4888 // (fround (extract_vector_elt X 1)) ->
4889 // (extract_vector_elt (VROUND X) 0)
4890 // (extract_vector_elt (VROUND X) 1)
4891 //
4892 // This is a special case since the target doesn't really support v2f32s.
4893 if (Opcode == ISD::FP_ROUND) {
4894 SDValue Op0 = N->getOperand(0);
4895 if (N->getValueType(0) == MVT::f32 &&
4896 Op0.hasOneUse() &&
4897 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4898 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4899 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4900 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4901 SDValue Vec = Op0.getOperand(0);
4902 for (auto *U : Vec->uses()) {
4903 if (U != Op0.getNode() &&
4904 U->hasOneUse() &&
4905 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4906 U->getOperand(0) == Vec &&
4907 U->getOperand(1).getOpcode() == ISD::Constant &&
4908 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4909 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4910 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4911 OtherRound.getOperand(0) == SDValue(U, 0) &&
4912 OtherRound.getValueType() == MVT::f32) {
4913 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4914 MVT::v4f32, Vec);
4915 DCI.AddToWorklist(VRound.getNode());
4916 SDValue Extract1 =
4917 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4918 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4919 DCI.AddToWorklist(Extract1.getNode());
4920 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4921 SDValue Extract0 =
4922 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4923 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4924 return Extract0;
4925 }
4926 }
4927 }
4928 }
4929 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004930 return SDValue();
4931}
4932
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004933//===----------------------------------------------------------------------===//
4934// Custom insertion
4935//===----------------------------------------------------------------------===//
4936
4937// Create a new basic block after MBB.
4938static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4939 MachineFunction &MF = *MBB->getParent();
4940 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004941 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004942 return NewMBB;
4943}
4944
Richard Sandifordbe133a82013-08-28 09:01:51 +00004945// Split MBB after MI and return the new block (the one that contains
4946// instructions after MI).
4947static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4948 MachineBasicBlock *MBB) {
4949 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4950 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004951 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004952 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4953 return NewMBB;
4954}
4955
Richard Sandiford5e318f02013-08-27 09:54:29 +00004956// Split MBB before MI and return the new block (the one that contains MI).
4957static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4958 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004959 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004960 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004961 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4962 return NewMBB;
4963}
4964
Richard Sandiford5e318f02013-08-27 09:54:29 +00004965// Force base value Base into a register before MI. Return the register.
4966static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4967 const SystemZInstrInfo *TII) {
4968 if (Base.isReg())
4969 return Base.getReg();
4970
4971 MachineBasicBlock *MBB = MI->getParent();
4972 MachineFunction &MF = *MBB->getParent();
4973 MachineRegisterInfo &MRI = MF.getRegInfo();
4974
4975 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4976 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4977 .addOperand(Base).addImm(0).addReg(0);
4978 return Reg;
4979}
4980
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004981// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4982MachineBasicBlock *
4983SystemZTargetLowering::emitSelect(MachineInstr *MI,
4984 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004985 const SystemZInstrInfo *TII =
4986 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004987
4988 unsigned DestReg = MI->getOperand(0).getReg();
4989 unsigned TrueReg = MI->getOperand(1).getReg();
4990 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004991 unsigned CCValid = MI->getOperand(3).getImm();
4992 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004993 DebugLoc DL = MI->getDebugLoc();
4994
4995 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004996 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004997 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4998
4999 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005000 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005001 // # fallthrough to FalseMBB
5002 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005003 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5004 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005005 MBB->addSuccessor(JoinMBB);
5006 MBB->addSuccessor(FalseMBB);
5007
5008 // FalseMBB:
5009 // # fallthrough to JoinMBB
5010 MBB = FalseMBB;
5011 MBB->addSuccessor(JoinMBB);
5012
5013 // JoinMBB:
5014 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5015 // ...
5016 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005017 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005018 .addReg(TrueReg).addMBB(StartMBB)
5019 .addReg(FalseReg).addMBB(FalseMBB);
5020
5021 MI->eraseFromParent();
5022 return JoinMBB;
5023}
5024
Richard Sandifordb86a8342013-06-27 09:27:40 +00005025// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5026// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005027// happen when the condition is false rather than true. If a STORE ON
5028// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00005029MachineBasicBlock *
5030SystemZTargetLowering::emitCondStore(MachineInstr *MI,
5031 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005032 unsigned StoreOpcode, unsigned STOCOpcode,
5033 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005034 const SystemZInstrInfo *TII =
5035 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005036
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005037 unsigned SrcReg = MI->getOperand(0).getReg();
5038 MachineOperand Base = MI->getOperand(1);
5039 int64_t Disp = MI->getOperand(2).getImm();
5040 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00005041 unsigned CCValid = MI->getOperand(4).getImm();
5042 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005043 DebugLoc DL = MI->getDebugLoc();
5044
5045 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5046
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005047 // Use STOCOpcode if possible. We could use different store patterns in
5048 // order to avoid matching the index register, but the performance trade-offs
5049 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005050 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005051 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005052 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005053 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00005054 .addReg(SrcReg).addOperand(Base).addImm(Disp)
5055 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005056 MI->eraseFromParent();
5057 return MBB;
5058 }
5059
Richard Sandifordb86a8342013-06-27 09:27:40 +00005060 // Get the condition needed to branch around the store.
5061 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005062 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005063
5064 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005065 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005066 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5067
5068 // StartMBB:
5069 // BRC CCMask, JoinMBB
5070 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005071 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005072 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5073 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005074 MBB->addSuccessor(JoinMBB);
5075 MBB->addSuccessor(FalseMBB);
5076
5077 // FalseMBB:
5078 // store %SrcReg, %Disp(%Index,%Base)
5079 // # fallthrough to JoinMBB
5080 MBB = FalseMBB;
5081 BuildMI(MBB, DL, TII->get(StoreOpcode))
5082 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5083 MBB->addSuccessor(JoinMBB);
5084
5085 MI->eraseFromParent();
5086 return JoinMBB;
5087}
5088
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005089// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5090// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5091// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5092// BitSize is the width of the field in bits, or 0 if this is a partword
5093// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5094// is one of the operands. Invert says whether the field should be
5095// inverted after performing BinOpcode (e.g. for NAND).
5096MachineBasicBlock *
5097SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
5098 MachineBasicBlock *MBB,
5099 unsigned BinOpcode,
5100 unsigned BitSize,
5101 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005102 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005103 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005104 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005105 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005106 bool IsSubWord = (BitSize < 32);
5107
5108 // Extract the operands. Base can be a register or a frame index.
5109 // Src2 can be a register or immediate.
5110 unsigned Dest = MI->getOperand(0).getReg();
5111 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5112 int64_t Disp = MI->getOperand(2).getImm();
5113 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
5114 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5115 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5116 DebugLoc DL = MI->getDebugLoc();
5117 if (IsSubWord)
5118 BitSize = MI->getOperand(6).getImm();
5119
5120 // Subword operations use 32-bit registers.
5121 const TargetRegisterClass *RC = (BitSize <= 32 ?
5122 &SystemZ::GR32BitRegClass :
5123 &SystemZ::GR64BitRegClass);
5124 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5125 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5126
5127 // Get the right opcodes for the displacement.
5128 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5129 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5130 assert(LOpcode && CSOpcode && "Displacement out of range");
5131
5132 // Create virtual registers for temporary results.
5133 unsigned OrigVal = MRI.createVirtualRegister(RC);
5134 unsigned OldVal = MRI.createVirtualRegister(RC);
5135 unsigned NewVal = (BinOpcode || IsSubWord ?
5136 MRI.createVirtualRegister(RC) : Src2.getReg());
5137 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5138 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5139
5140 // Insert a basic block for the main loop.
5141 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005142 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005143 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5144
5145 // StartMBB:
5146 // ...
5147 // %OrigVal = L Disp(%Base)
5148 // # fall through to LoopMMB
5149 MBB = StartMBB;
5150 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5151 .addOperand(Base).addImm(Disp).addReg(0);
5152 MBB->addSuccessor(LoopMBB);
5153
5154 // LoopMBB:
5155 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5156 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5157 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5158 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5159 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5160 // JNE LoopMBB
5161 // # fall through to DoneMMB
5162 MBB = LoopMBB;
5163 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5164 .addReg(OrigVal).addMBB(StartMBB)
5165 .addReg(Dest).addMBB(LoopMBB);
5166 if (IsSubWord)
5167 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5168 .addReg(OldVal).addReg(BitShift).addImm(0);
5169 if (Invert) {
5170 // Perform the operation normally and then invert every bit of the field.
5171 unsigned Tmp = MRI.createVirtualRegister(RC);
5172 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5173 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005174 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005175 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005176 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005177 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005178 else {
5179 // Use LCGR and add -1 to the result, which is more compact than
5180 // an XILF, XILH pair.
5181 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5182 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5183 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5184 .addReg(Tmp2).addImm(-1);
5185 }
5186 } else if (BinOpcode)
5187 // A simply binary operation.
5188 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5189 .addReg(RotatedOldVal).addOperand(Src2);
5190 else if (IsSubWord)
5191 // Use RISBG to rotate Src2 into position and use it to replace the
5192 // field in RotatedOldVal.
5193 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5194 .addReg(RotatedOldVal).addReg(Src2.getReg())
5195 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5196 if (IsSubWord)
5197 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5198 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5199 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5200 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005201 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5202 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005203 MBB->addSuccessor(LoopMBB);
5204 MBB->addSuccessor(DoneMBB);
5205
5206 MI->eraseFromParent();
5207 return DoneMBB;
5208}
5209
5210// Implement EmitInstrWithCustomInserter for pseudo
5211// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5212// instruction that should be used to compare the current field with the
5213// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5214// for when the current field should be kept. BitSize is the width of
5215// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5216MachineBasicBlock *
5217SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5218 MachineBasicBlock *MBB,
5219 unsigned CompareOpcode,
5220 unsigned KeepOldMask,
5221 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005222 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005223 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005224 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005225 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005226 bool IsSubWord = (BitSize < 32);
5227
5228 // Extract the operands. Base can be a register or a frame index.
5229 unsigned Dest = MI->getOperand(0).getReg();
5230 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5231 int64_t Disp = MI->getOperand(2).getImm();
5232 unsigned Src2 = MI->getOperand(3).getReg();
5233 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5234 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5235 DebugLoc DL = MI->getDebugLoc();
5236 if (IsSubWord)
5237 BitSize = MI->getOperand(6).getImm();
5238
5239 // Subword operations use 32-bit registers.
5240 const TargetRegisterClass *RC = (BitSize <= 32 ?
5241 &SystemZ::GR32BitRegClass :
5242 &SystemZ::GR64BitRegClass);
5243 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5244 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5245
5246 // Get the right opcodes for the displacement.
5247 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5248 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5249 assert(LOpcode && CSOpcode && "Displacement out of range");
5250
5251 // Create virtual registers for temporary results.
5252 unsigned OrigVal = MRI.createVirtualRegister(RC);
5253 unsigned OldVal = MRI.createVirtualRegister(RC);
5254 unsigned NewVal = MRI.createVirtualRegister(RC);
5255 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5256 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5257 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5258
5259 // Insert 3 basic blocks for the loop.
5260 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005261 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005262 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5263 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5264 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5265
5266 // StartMBB:
5267 // ...
5268 // %OrigVal = L Disp(%Base)
5269 // # fall through to LoopMMB
5270 MBB = StartMBB;
5271 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5272 .addOperand(Base).addImm(Disp).addReg(0);
5273 MBB->addSuccessor(LoopMBB);
5274
5275 // LoopMBB:
5276 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5277 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5278 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005279 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005280 MBB = LoopMBB;
5281 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5282 .addReg(OrigVal).addMBB(StartMBB)
5283 .addReg(Dest).addMBB(UpdateMBB);
5284 if (IsSubWord)
5285 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5286 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005287 BuildMI(MBB, DL, TII->get(CompareOpcode))
5288 .addReg(RotatedOldVal).addReg(Src2);
5289 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005290 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005291 MBB->addSuccessor(UpdateMBB);
5292 MBB->addSuccessor(UseAltMBB);
5293
5294 // UseAltMBB:
5295 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5296 // # fall through to UpdateMMB
5297 MBB = UseAltMBB;
5298 if (IsSubWord)
5299 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5300 .addReg(RotatedOldVal).addReg(Src2)
5301 .addImm(32).addImm(31 + BitSize).addImm(0);
5302 MBB->addSuccessor(UpdateMBB);
5303
5304 // UpdateMBB:
5305 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5306 // [ %RotatedAltVal, UseAltMBB ]
5307 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5308 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5309 // JNE LoopMBB
5310 // # fall through to DoneMMB
5311 MBB = UpdateMBB;
5312 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5313 .addReg(RotatedOldVal).addMBB(LoopMBB)
5314 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5315 if (IsSubWord)
5316 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5317 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5318 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5319 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005320 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5321 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005322 MBB->addSuccessor(LoopMBB);
5323 MBB->addSuccessor(DoneMBB);
5324
5325 MI->eraseFromParent();
5326 return DoneMBB;
5327}
5328
5329// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5330// instruction MI.
5331MachineBasicBlock *
5332SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5333 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005334
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005335 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005336 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005337 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005338 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005339
5340 // Extract the operands. Base can be a register or a frame index.
5341 unsigned Dest = MI->getOperand(0).getReg();
5342 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5343 int64_t Disp = MI->getOperand(2).getImm();
5344 unsigned OrigCmpVal = MI->getOperand(3).getReg();
5345 unsigned OrigSwapVal = MI->getOperand(4).getReg();
5346 unsigned BitShift = MI->getOperand(5).getReg();
5347 unsigned NegBitShift = MI->getOperand(6).getReg();
5348 int64_t BitSize = MI->getOperand(7).getImm();
5349 DebugLoc DL = MI->getDebugLoc();
5350
5351 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5352
5353 // Get the right opcodes for the displacement.
5354 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5355 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5356 assert(LOpcode && CSOpcode && "Displacement out of range");
5357
5358 // Create virtual registers for temporary results.
5359 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5360 unsigned OldVal = MRI.createVirtualRegister(RC);
5361 unsigned CmpVal = MRI.createVirtualRegister(RC);
5362 unsigned SwapVal = MRI.createVirtualRegister(RC);
5363 unsigned StoreVal = MRI.createVirtualRegister(RC);
5364 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5365 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5366 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5367
5368 // Insert 2 basic blocks for the loop.
5369 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005370 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005371 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5372 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5373
5374 // StartMBB:
5375 // ...
5376 // %OrigOldVal = L Disp(%Base)
5377 // # fall through to LoopMMB
5378 MBB = StartMBB;
5379 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5380 .addOperand(Base).addImm(Disp).addReg(0);
5381 MBB->addSuccessor(LoopMBB);
5382
5383 // LoopMBB:
5384 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5385 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5386 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5387 // %Dest = RLL %OldVal, BitSize(%BitShift)
5388 // ^^ The low BitSize bits contain the field
5389 // of interest.
5390 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5391 // ^^ Replace the upper 32-BitSize bits of the
5392 // comparison value with those that we loaded,
5393 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005394 // CR %Dest, %RetryCmpVal
5395 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005396 // # Fall through to SetMBB
5397 MBB = LoopMBB;
5398 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5399 .addReg(OrigOldVal).addMBB(StartMBB)
5400 .addReg(RetryOldVal).addMBB(SetMBB);
5401 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5402 .addReg(OrigCmpVal).addMBB(StartMBB)
5403 .addReg(RetryCmpVal).addMBB(SetMBB);
5404 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5405 .addReg(OrigSwapVal).addMBB(StartMBB)
5406 .addReg(RetrySwapVal).addMBB(SetMBB);
5407 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5408 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5409 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5410 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005411 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5412 .addReg(Dest).addReg(RetryCmpVal);
5413 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005414 .addImm(SystemZ::CCMASK_ICMP)
5415 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005416 MBB->addSuccessor(DoneMBB);
5417 MBB->addSuccessor(SetMBB);
5418
5419 // SetMBB:
5420 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5421 // ^^ Replace the upper 32-BitSize bits of the new
5422 // value with those that we loaded.
5423 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5424 // ^^ Rotate the new field to its proper position.
5425 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5426 // JNE LoopMBB
5427 // # fall through to ExitMMB
5428 MBB = SetMBB;
5429 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5430 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5431 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5432 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5433 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5434 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005435 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5436 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005437 MBB->addSuccessor(LoopMBB);
5438 MBB->addSuccessor(DoneMBB);
5439
5440 MI->eraseFromParent();
5441 return DoneMBB;
5442}
5443
5444// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5445// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005446// it's "don't care". SubReg is subreg_l32 when extending a GR32
5447// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005448MachineBasicBlock *
5449SystemZTargetLowering::emitExt128(MachineInstr *MI,
5450 MachineBasicBlock *MBB,
5451 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005452 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005453 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005454 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005455 MachineRegisterInfo &MRI = MF.getRegInfo();
5456 DebugLoc DL = MI->getDebugLoc();
5457
5458 unsigned Dest = MI->getOperand(0).getReg();
5459 unsigned Src = MI->getOperand(1).getReg();
5460 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5461
5462 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5463 if (ClearEven) {
5464 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5465 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5466
5467 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5468 .addImm(0);
5469 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005470 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005471 In128 = NewIn128;
5472 }
5473 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5474 .addReg(In128).addReg(Src).addImm(SubReg);
5475
5476 MI->eraseFromParent();
5477 return MBB;
5478}
5479
Richard Sandifordd131ff82013-07-08 09:35:23 +00005480MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00005481SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5482 MachineBasicBlock *MBB,
5483 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005484 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005485 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005486 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005487 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005488 DebugLoc DL = MI->getDebugLoc();
5489
Richard Sandiford5e318f02013-08-27 09:54:29 +00005490 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005491 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005492 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005493 uint64_t SrcDisp = MI->getOperand(3).getImm();
5494 uint64_t Length = MI->getOperand(4).getImm();
5495
Richard Sandifordbe133a82013-08-28 09:01:51 +00005496 // When generating more than one CLC, all but the last will need to
5497 // branch to the end when a difference is found.
5498 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005499 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005500
Richard Sandiford5e318f02013-08-27 09:54:29 +00005501 // Check for the loop form, in which operand 5 is the trip count.
5502 if (MI->getNumExplicitOperands() > 5) {
5503 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5504
5505 uint64_t StartCountReg = MI->getOperand(5).getReg();
5506 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5507 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5508 forceReg(MI, DestBase, TII));
5509
5510 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5511 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5512 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5513 MRI.createVirtualRegister(RC));
5514 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5515 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5516 MRI.createVirtualRegister(RC));
5517
5518 RC = &SystemZ::GR64BitRegClass;
5519 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5520 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5521
5522 MachineBasicBlock *StartMBB = MBB;
5523 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5524 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005525 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005526
5527 // StartMBB:
5528 // # fall through to LoopMMB
5529 MBB->addSuccessor(LoopMBB);
5530
5531 // LoopMBB:
5532 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005533 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005534 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005535 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005536 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005537 // [ %NextCountReg, NextMBB ]
5538 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005539 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005540 // ( JLH EndMBB )
5541 //
5542 // The prefetch is used only for MVC. The JLH is used only for CLC.
5543 MBB = LoopMBB;
5544
5545 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5546 .addReg(StartDestReg).addMBB(StartMBB)
5547 .addReg(NextDestReg).addMBB(NextMBB);
5548 if (!HaveSingleBase)
5549 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5550 .addReg(StartSrcReg).addMBB(StartMBB)
5551 .addReg(NextSrcReg).addMBB(NextMBB);
5552 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5553 .addReg(StartCountReg).addMBB(StartMBB)
5554 .addReg(NextCountReg).addMBB(NextMBB);
5555 if (Opcode == SystemZ::MVC)
5556 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5557 .addImm(SystemZ::PFD_WRITE)
5558 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5559 BuildMI(MBB, DL, TII->get(Opcode))
5560 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5561 .addReg(ThisSrcReg).addImm(SrcDisp);
5562 if (EndMBB) {
5563 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5564 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5565 .addMBB(EndMBB);
5566 MBB->addSuccessor(EndMBB);
5567 MBB->addSuccessor(NextMBB);
5568 }
5569
5570 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005571 // %NextDestReg = LA 256(%ThisDestReg)
5572 // %NextSrcReg = LA 256(%ThisSrcReg)
5573 // %NextCountReg = AGHI %ThisCountReg, -1
5574 // CGHI %NextCountReg, 0
5575 // JLH LoopMBB
5576 // # fall through to DoneMMB
5577 //
5578 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005579 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005580
Richard Sandiford5e318f02013-08-27 09:54:29 +00005581 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5582 .addReg(ThisDestReg).addImm(256).addReg(0);
5583 if (!HaveSingleBase)
5584 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5585 .addReg(ThisSrcReg).addImm(256).addReg(0);
5586 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5587 .addReg(ThisCountReg).addImm(-1);
5588 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5589 .addReg(NextCountReg).addImm(0);
5590 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5591 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5592 .addMBB(LoopMBB);
5593 MBB->addSuccessor(LoopMBB);
5594 MBB->addSuccessor(DoneMBB);
5595
5596 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5597 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5598 Length &= 255;
5599 MBB = DoneMBB;
5600 }
5601 // Handle any remaining bytes with straight-line code.
5602 while (Length > 0) {
5603 uint64_t ThisLength = std::min(Length, uint64_t(256));
5604 // The previous iteration might have created out-of-range displacements.
5605 // Apply them using LAY if so.
5606 if (!isUInt<12>(DestDisp)) {
5607 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5608 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5609 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5610 DestBase = MachineOperand::CreateReg(Reg, false);
5611 DestDisp = 0;
5612 }
5613 if (!isUInt<12>(SrcDisp)) {
5614 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5615 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5616 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5617 SrcBase = MachineOperand::CreateReg(Reg, false);
5618 SrcDisp = 0;
5619 }
5620 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5621 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5622 .addOperand(SrcBase).addImm(SrcDisp);
5623 DestDisp += ThisLength;
5624 SrcDisp += ThisLength;
5625 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005626 // If there's another CLC to go, branch to the end if a difference
5627 // was found.
5628 if (EndMBB && Length > 0) {
5629 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5630 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5631 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5632 .addMBB(EndMBB);
5633 MBB->addSuccessor(EndMBB);
5634 MBB->addSuccessor(NextMBB);
5635 MBB = NextMBB;
5636 }
5637 }
5638 if (EndMBB) {
5639 MBB->addSuccessor(EndMBB);
5640 MBB = EndMBB;
5641 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005642 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005643
5644 MI->eraseFromParent();
5645 return MBB;
5646}
5647
Richard Sandifordca232712013-08-16 11:21:54 +00005648// Decompose string pseudo-instruction MI into a loop that continually performs
5649// Opcode until CC != 3.
5650MachineBasicBlock *
5651SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5652 MachineBasicBlock *MBB,
5653 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005654 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005655 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005656 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005657 MachineRegisterInfo &MRI = MF.getRegInfo();
5658 DebugLoc DL = MI->getDebugLoc();
5659
5660 uint64_t End1Reg = MI->getOperand(0).getReg();
5661 uint64_t Start1Reg = MI->getOperand(1).getReg();
5662 uint64_t Start2Reg = MI->getOperand(2).getReg();
5663 uint64_t CharReg = MI->getOperand(3).getReg();
5664
5665 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5666 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5667 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5668 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5669
5670 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005671 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005672 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5673
5674 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005675 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005676 MBB->addSuccessor(LoopMBB);
5677
5678 // LoopMBB:
5679 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5680 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005681 // R0L = %CharReg
5682 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005683 // JO LoopMBB
5684 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005685 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005686 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005687 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005688
5689 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5690 .addReg(Start1Reg).addMBB(StartMBB)
5691 .addReg(End1Reg).addMBB(LoopMBB);
5692 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5693 .addReg(Start2Reg).addMBB(StartMBB)
5694 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005695 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005696 BuildMI(MBB, DL, TII->get(Opcode))
5697 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5698 .addReg(This1Reg).addReg(This2Reg);
5699 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5700 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5701 MBB->addSuccessor(LoopMBB);
5702 MBB->addSuccessor(DoneMBB);
5703
5704 DoneMBB->addLiveIn(SystemZ::CC);
5705
5706 MI->eraseFromParent();
5707 return DoneMBB;
5708}
5709
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005710// Update TBEGIN instruction with final opcode and register clobbers.
5711MachineBasicBlock *
5712SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5713 MachineBasicBlock *MBB,
5714 unsigned Opcode,
5715 bool NoFloat) const {
5716 MachineFunction &MF = *MBB->getParent();
5717 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5718 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5719
5720 // Update opcode.
5721 MI->setDesc(TII->get(Opcode));
5722
5723 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5724 // Make sure to add the corresponding GRSM bits if they are missing.
5725 uint64_t Control = MI->getOperand(2).getImm();
5726 static const unsigned GPRControlBit[16] = {
5727 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5728 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5729 };
5730 Control |= GPRControlBit[15];
5731 if (TFI->hasFP(MF))
5732 Control |= GPRControlBit[11];
5733 MI->getOperand(2).setImm(Control);
5734
5735 // Add GPR clobbers.
5736 for (int I = 0; I < 16; I++) {
5737 if ((Control & GPRControlBit[I]) == 0) {
5738 unsigned Reg = SystemZMC::GR64Regs[I];
5739 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5740 }
5741 }
5742
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005743 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005744 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005745 if (Subtarget.hasVector()) {
5746 for (int I = 0; I < 32; I++) {
5747 unsigned Reg = SystemZMC::VR128Regs[I];
5748 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5749 }
5750 } else {
5751 for (int I = 0; I < 16; I++) {
5752 unsigned Reg = SystemZMC::FP64Regs[I];
5753 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5754 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005755 }
5756 }
5757
5758 return MBB;
5759}
5760
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005761MachineBasicBlock *
5762SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
NAKAMURA Takumi50df0c22015-11-02 01:38:12 +00005763 MachineBasicBlock *MBB,
5764 unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005765 MachineFunction &MF = *MBB->getParent();
5766 MachineRegisterInfo *MRI = &MF.getRegInfo();
5767 const SystemZInstrInfo *TII =
5768 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5769 DebugLoc DL = MI->getDebugLoc();
5770
5771 unsigned SrcReg = MI->getOperand(0).getReg();
5772
5773 // Create new virtual register of the same class as source.
5774 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5775 unsigned DstReg = MRI->createVirtualRegister(RC);
5776
5777 // Replace pseudo with a normal load-and-test that models the def as
5778 // well.
5779 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5780 .addReg(SrcReg);
5781 MI->eraseFromParent();
5782
5783 return MBB;
5784}
5785
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005786MachineBasicBlock *SystemZTargetLowering::
5787EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5788 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005789 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005790 case SystemZ::Select32:
5791 case SystemZ::SelectF32:
5792 case SystemZ::Select64:
5793 case SystemZ::SelectF64:
5794 case SystemZ::SelectF128:
5795 return emitSelect(MI, MBB);
5796
Richard Sandiford2896d042013-10-01 14:33:55 +00005797 case SystemZ::CondStore8Mux:
5798 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5799 case SystemZ::CondStore8MuxInv:
5800 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5801 case SystemZ::CondStore16Mux:
5802 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5803 case SystemZ::CondStore16MuxInv:
5804 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005805 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005806 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005807 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005808 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005809 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005810 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005811 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005812 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005813 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005814 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005815 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005816 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005817 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005818 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005819 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005820 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005821 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005822 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005823 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005824 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005825 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005826 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005827 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005828 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005829
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005830 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005831 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005832 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005833 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005834 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005835 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005836
5837 case SystemZ::ATOMIC_SWAPW:
5838 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5839 case SystemZ::ATOMIC_SWAP_32:
5840 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5841 case SystemZ::ATOMIC_SWAP_64:
5842 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5843
5844 case SystemZ::ATOMIC_LOADW_AR:
5845 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5846 case SystemZ::ATOMIC_LOADW_AFI:
5847 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5848 case SystemZ::ATOMIC_LOAD_AR:
5849 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5850 case SystemZ::ATOMIC_LOAD_AHI:
5851 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5852 case SystemZ::ATOMIC_LOAD_AFI:
5853 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5854 case SystemZ::ATOMIC_LOAD_AGR:
5855 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5856 case SystemZ::ATOMIC_LOAD_AGHI:
5857 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5858 case SystemZ::ATOMIC_LOAD_AGFI:
5859 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5860
5861 case SystemZ::ATOMIC_LOADW_SR:
5862 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5863 case SystemZ::ATOMIC_LOAD_SR:
5864 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5865 case SystemZ::ATOMIC_LOAD_SGR:
5866 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5867
5868 case SystemZ::ATOMIC_LOADW_NR:
5869 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5870 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005871 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005872 case SystemZ::ATOMIC_LOAD_NR:
5873 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005874 case SystemZ::ATOMIC_LOAD_NILL:
5875 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5876 case SystemZ::ATOMIC_LOAD_NILH:
5877 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5878 case SystemZ::ATOMIC_LOAD_NILF:
5879 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005880 case SystemZ::ATOMIC_LOAD_NGR:
5881 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005882 case SystemZ::ATOMIC_LOAD_NILL64:
5883 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5884 case SystemZ::ATOMIC_LOAD_NILH64:
5885 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005886 case SystemZ::ATOMIC_LOAD_NIHL64:
5887 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5888 case SystemZ::ATOMIC_LOAD_NIHH64:
5889 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005890 case SystemZ::ATOMIC_LOAD_NILF64:
5891 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005892 case SystemZ::ATOMIC_LOAD_NIHF64:
5893 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005894
5895 case SystemZ::ATOMIC_LOADW_OR:
5896 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5897 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005898 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005899 case SystemZ::ATOMIC_LOAD_OR:
5900 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005901 case SystemZ::ATOMIC_LOAD_OILL:
5902 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5903 case SystemZ::ATOMIC_LOAD_OILH:
5904 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5905 case SystemZ::ATOMIC_LOAD_OILF:
5906 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005907 case SystemZ::ATOMIC_LOAD_OGR:
5908 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005909 case SystemZ::ATOMIC_LOAD_OILL64:
5910 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5911 case SystemZ::ATOMIC_LOAD_OILH64:
5912 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005913 case SystemZ::ATOMIC_LOAD_OIHL64:
5914 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5915 case SystemZ::ATOMIC_LOAD_OIHH64:
5916 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005917 case SystemZ::ATOMIC_LOAD_OILF64:
5918 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005919 case SystemZ::ATOMIC_LOAD_OIHF64:
5920 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005921
5922 case SystemZ::ATOMIC_LOADW_XR:
5923 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5924 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005925 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005926 case SystemZ::ATOMIC_LOAD_XR:
5927 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005928 case SystemZ::ATOMIC_LOAD_XILF:
5929 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005930 case SystemZ::ATOMIC_LOAD_XGR:
5931 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005932 case SystemZ::ATOMIC_LOAD_XILF64:
5933 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005934 case SystemZ::ATOMIC_LOAD_XIHF64:
5935 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005936
5937 case SystemZ::ATOMIC_LOADW_NRi:
5938 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5939 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005940 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005941 case SystemZ::ATOMIC_LOAD_NRi:
5942 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005943 case SystemZ::ATOMIC_LOAD_NILLi:
5944 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5945 case SystemZ::ATOMIC_LOAD_NILHi:
5946 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5947 case SystemZ::ATOMIC_LOAD_NILFi:
5948 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005949 case SystemZ::ATOMIC_LOAD_NGRi:
5950 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005951 case SystemZ::ATOMIC_LOAD_NILL64i:
5952 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5953 case SystemZ::ATOMIC_LOAD_NILH64i:
5954 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005955 case SystemZ::ATOMIC_LOAD_NIHL64i:
5956 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5957 case SystemZ::ATOMIC_LOAD_NIHH64i:
5958 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005959 case SystemZ::ATOMIC_LOAD_NILF64i:
5960 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005961 case SystemZ::ATOMIC_LOAD_NIHF64i:
5962 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005963
5964 case SystemZ::ATOMIC_LOADW_MIN:
5965 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5966 SystemZ::CCMASK_CMP_LE, 0);
5967 case SystemZ::ATOMIC_LOAD_MIN_32:
5968 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5969 SystemZ::CCMASK_CMP_LE, 32);
5970 case SystemZ::ATOMIC_LOAD_MIN_64:
5971 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5972 SystemZ::CCMASK_CMP_LE, 64);
5973
5974 case SystemZ::ATOMIC_LOADW_MAX:
5975 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5976 SystemZ::CCMASK_CMP_GE, 0);
5977 case SystemZ::ATOMIC_LOAD_MAX_32:
5978 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5979 SystemZ::CCMASK_CMP_GE, 32);
5980 case SystemZ::ATOMIC_LOAD_MAX_64:
5981 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5982 SystemZ::CCMASK_CMP_GE, 64);
5983
5984 case SystemZ::ATOMIC_LOADW_UMIN:
5985 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5986 SystemZ::CCMASK_CMP_LE, 0);
5987 case SystemZ::ATOMIC_LOAD_UMIN_32:
5988 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5989 SystemZ::CCMASK_CMP_LE, 32);
5990 case SystemZ::ATOMIC_LOAD_UMIN_64:
5991 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5992 SystemZ::CCMASK_CMP_LE, 64);
5993
5994 case SystemZ::ATOMIC_LOADW_UMAX:
5995 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5996 SystemZ::CCMASK_CMP_GE, 0);
5997 case SystemZ::ATOMIC_LOAD_UMAX_32:
5998 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5999 SystemZ::CCMASK_CMP_GE, 32);
6000 case SystemZ::ATOMIC_LOAD_UMAX_64:
6001 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6002 SystemZ::CCMASK_CMP_GE, 64);
6003
6004 case SystemZ::ATOMIC_CMP_SWAPW:
6005 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006006 case SystemZ::MVCSequence:
6007 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006008 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006009 case SystemZ::NCSequence:
6010 case SystemZ::NCLoop:
6011 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6012 case SystemZ::OCSequence:
6013 case SystemZ::OCLoop:
6014 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6015 case SystemZ::XCSequence:
6016 case SystemZ::XCLoop:
6017 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006018 case SystemZ::CLCSequence:
6019 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006020 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006021 case SystemZ::CLSTLoop:
6022 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006023 case SystemZ::MVSTLoop:
6024 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006025 case SystemZ::SRSTLoop:
6026 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006027 case SystemZ::TBEGIN:
6028 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6029 case SystemZ::TBEGIN_nofloat:
6030 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6031 case SystemZ::TBEGINC:
6032 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006033 case SystemZ::LTEBRCompare_VecPseudo:
6034 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6035 case SystemZ::LTDBRCompare_VecPseudo:
6036 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6037 case SystemZ::LTXBRCompare_VecPseudo:
6038 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6039
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006040 default:
6041 llvm_unreachable("Unexpected instr type to insert");
6042 }
6043}