blob: 68c5e55b569af667c937288b29fbda619670259d [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
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002536SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
2537 SelectionDAG &DAG) const {
Mehdi Amini44ede332015-07-09 02:09:04 +00002538 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002539
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002540 // The high part of the thread pointer is in access register 0.
2541 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002542 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002543 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2544
2545 // The low part of the thread pointer is in access register 1.
2546 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002548 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2549
2550 // Merge them into a single 64-bit address.
2551 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002552 DAG.getConstant(32, DL, PtrVT));
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00002553 return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2554}
2555
2556SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2557 SelectionDAG &DAG) const {
2558 if (DAG.getTarget().Options.EmulatedTLS)
2559 return LowerToTLSEmulatedModel(Node, DAG);
2560 SDLoc DL(Node);
2561 const GlobalValue *GV = Node->getGlobal();
2562 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2563 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2564
2565 SDValue TP = lowerThreadPointer(DL, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002566
Ulrich Weigand7db69182015-02-18 09:13:27 +00002567 // Get the offset of GA from the thread pointer, based on the TLS model.
2568 SDValue Offset;
2569 switch (model) {
2570 case TLSModel::GeneralDynamic: {
2571 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2572 SystemZConstantPoolValue *CPV =
2573 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002574
Ulrich Weigand7db69182015-02-18 09:13:27 +00002575 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002576 Offset = DAG.getLoad(
2577 PtrVT, DL, DAG.getEntryNode(), Offset,
2578 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2579 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002580
2581 // Call __tls_get_offset to retrieve the offset.
2582 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2583 break;
2584 }
2585
2586 case TLSModel::LocalDynamic: {
2587 // Load the GOT offset of the module ID.
2588 SystemZConstantPoolValue *CPV =
2589 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2590
2591 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002592 Offset = DAG.getLoad(
2593 PtrVT, DL, DAG.getEntryNode(), Offset,
2594 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2595 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002596
2597 // Call __tls_get_offset to retrieve the module base offset.
2598 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2599
2600 // Note: The SystemZLDCleanupPass will remove redundant computations
2601 // of the module base offset. Count total number of local-dynamic
2602 // accesses to trigger execution of that pass.
2603 SystemZMachineFunctionInfo* MFI =
2604 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2605 MFI->incNumLocalDynamicTLSAccesses();
2606
2607 // Add the per-symbol offset.
2608 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2609
2610 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002611 DTPOffset = DAG.getLoad(
2612 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2613 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2614 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002615
2616 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2617 break;
2618 }
2619
2620 case TLSModel::InitialExec: {
2621 // Load the offset from the GOT.
2622 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2623 SystemZII::MO_INDNTPOFF);
2624 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002625 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2626 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Ulrich Weigand7db69182015-02-18 09:13:27 +00002627 false, false, false, 0);
2628 break;
2629 }
2630
2631 case TLSModel::LocalExec: {
2632 // Force the offset into the constant pool and load it from there.
2633 SystemZConstantPoolValue *CPV =
2634 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2635
2636 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002637 Offset = DAG.getLoad(
2638 PtrVT, DL, DAG.getEntryNode(), Offset,
2639 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2640 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002641 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002642 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002643 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002644
2645 // Add the base and offset together.
2646 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2647}
2648
2649SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2650 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002651 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002652 const BlockAddress *BA = Node->getBlockAddress();
2653 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002654 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002655
2656 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2657 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2658 return Result;
2659}
2660
2661SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2662 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002663 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002664 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002665 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2666
2667 // Use LARL to load the address of the table.
2668 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2669}
2670
2671SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2672 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002673 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002674 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002675
2676 SDValue Result;
2677 if (CP->isMachineConstantPoolEntry())
2678 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002679 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002680 else
2681 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002682 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002683
2684 // Use LARL to load the address of the constant pool entry.
2685 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2686}
2687
Ulrich Weigandf557d082016-04-04 12:44:55 +00002688SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2689 SelectionDAG &DAG) const {
2690 MachineFunction &MF = DAG.getMachineFunction();
2691 MachineFrameInfo *MFI = MF.getFrameInfo();
2692 MFI->setFrameAddressIsTaken(true);
2693
2694 SDLoc DL(Op);
2695 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2696 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2697
2698 // If the back chain frame index has not been allocated yet, do so.
2699 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2700 int BackChainIdx = FI->getFramePointerSaveIndex();
2701 if (!BackChainIdx) {
2702 // By definition, the frame address is the address of the back chain.
2703 BackChainIdx = MFI->CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
2704 FI->setFramePointerSaveIndex(BackChainIdx);
2705 }
2706 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2707
2708 // FIXME The frontend should detect this case.
2709 if (Depth > 0) {
2710 report_fatal_error("Unsupported stack frame traversal count");
2711 }
2712
2713 return BackChain;
2714}
2715
2716SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2717 SelectionDAG &DAG) const {
2718 MachineFunction &MF = DAG.getMachineFunction();
2719 MachineFrameInfo *MFI = MF.getFrameInfo();
2720 MFI->setReturnAddressIsTaken(true);
2721
2722 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2723 return SDValue();
2724
2725 SDLoc DL(Op);
2726 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2727 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2728
2729 // FIXME The frontend should detect this case.
2730 if (Depth > 0) {
2731 report_fatal_error("Unsupported stack frame traversal count");
2732 }
2733
2734 // Return R14D, which has the return address. Mark it an implicit live-in.
2735 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2736 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2737}
2738
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002739SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2740 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002741 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002742 SDValue In = Op.getOperand(0);
2743 EVT InVT = In.getValueType();
2744 EVT ResVT = Op.getValueType();
2745
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002746 // Convert loads directly. This is normally done by DAGCombiner,
2747 // but we need this case for bitcasts that are created during lowering
2748 // and which are then lowered themselves.
2749 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2750 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2751 LoadN->getMemOperand());
2752
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002753 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002754 SDValue In64;
2755 if (Subtarget.hasHighWord()) {
2756 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2757 MVT::i64);
2758 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2759 MVT::i64, SDValue(U64, 0), In);
2760 } else {
2761 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2762 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002763 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002764 }
2765 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002766 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002767 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002768 }
2769 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2770 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002771 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002772 MVT::f64, SDValue(U64, 0), In);
2773 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002774 if (Subtarget.hasHighWord())
2775 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2776 MVT::i32, Out64);
2777 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002778 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002779 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002780 }
2781 llvm_unreachable("Unexpected bitcast combination");
2782}
2783
2784SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2785 SelectionDAG &DAG) const {
2786 MachineFunction &MF = DAG.getMachineFunction();
2787 SystemZMachineFunctionInfo *FuncInfo =
2788 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002789 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002790
2791 SDValue Chain = Op.getOperand(0);
2792 SDValue Addr = Op.getOperand(1);
2793 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002794 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002795
2796 // The initial values of each field.
2797 const unsigned NumFields = 4;
2798 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002799 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2800 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002801 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2802 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2803 };
2804
2805 // Store each field into its respective slot.
2806 SDValue MemOps[NumFields];
2807 unsigned Offset = 0;
2808 for (unsigned I = 0; I < NumFields; ++I) {
2809 SDValue FieldAddr = Addr;
2810 if (Offset != 0)
2811 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002812 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002813 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2814 MachinePointerInfo(SV, Offset),
2815 false, false, 0);
2816 Offset += 8;
2817 }
Craig Topper48d114b2014-04-26 18:35:24 +00002818 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002819}
2820
2821SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2822 SelectionDAG &DAG) const {
2823 SDValue Chain = Op.getOperand(0);
2824 SDValue DstPtr = Op.getOperand(1);
2825 SDValue SrcPtr = Op.getOperand(2);
2826 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2827 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002828 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002829
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002830 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002831 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002832 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002833 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2834}
2835
2836SDValue SystemZTargetLowering::
2837lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002838 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2839 bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2840 hasFnAttribute("no-realign-stack");
2841
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002842 SDValue Chain = Op.getOperand(0);
2843 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002844 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002845 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002846
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002847 // If user has set the no alignment function attribute, ignore
2848 // alloca alignments.
2849 uint64_t AlignVal = (RealignOpt ?
2850 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2851
2852 uint64_t StackAlign = TFI->getStackAlignment();
2853 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2854 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2855
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002856 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002857 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002858
2859 // Get a reference to the stack pointer.
2860 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2861
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002862 // Add extra space for alignment if needed.
2863 if (ExtraAlignSpace)
2864 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2865 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2866
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002867 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002868 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002869
2870 // Copy the new stack pointer back.
2871 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2872
2873 // The allocated data lives above the 160 bytes allocated for the standard
2874 // frame, plus any outgoing stack arguments. We don't know how much that
2875 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2876 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2877 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2878
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002879 // Dynamically realign if needed.
2880 if (RequiredAlign > StackAlign) {
2881 Result =
2882 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2883 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2884 Result =
2885 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2886 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2887 }
2888
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002889 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002890 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002891}
2892
Richard Sandiford7d86e472013-08-21 09:34:56 +00002893SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2894 SelectionDAG &DAG) const {
2895 EVT VT = Op.getValueType();
2896 SDLoc DL(Op);
2897 SDValue Ops[2];
2898 if (is32Bit(VT))
2899 // Just do a normal 64-bit multiplication and extract the results.
2900 // We define this so that it can be used for constant division.
2901 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2902 Op.getOperand(1), Ops[1], Ops[0]);
2903 else {
2904 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2905 //
2906 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2907 //
2908 // but using the fact that the upper halves are either all zeros
2909 // or all ones:
2910 //
2911 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2912 //
2913 // and grouping the right terms together since they are quicker than the
2914 // multiplication:
2915 //
2916 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002917 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002918 SDValue LL = Op.getOperand(0);
2919 SDValue RL = Op.getOperand(1);
2920 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2921 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2922 // UMUL_LOHI64 returns the low result in the odd register and the high
2923 // result in the even register. SMUL_LOHI is defined to return the
2924 // low half first, so the results are in reverse order.
2925 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2926 LL, RL, Ops[1], Ops[0]);
2927 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2928 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2929 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2930 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2931 }
Craig Topper64941d92014-04-27 19:20:57 +00002932 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002933}
2934
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002935SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2936 SelectionDAG &DAG) const {
2937 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002938 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002939 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002940 if (is32Bit(VT))
2941 // Just do a normal 64-bit multiplication and extract the results.
2942 // We define this so that it can be used for constant division.
2943 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2944 Op.getOperand(1), Ops[1], Ops[0]);
2945 else
2946 // UMUL_LOHI64 returns the low result in the odd register and the high
2947 // result in the even register. UMUL_LOHI is defined to return the
2948 // low half first, so the results are in reverse order.
2949 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2950 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002951 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002952}
2953
2954SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2955 SelectionDAG &DAG) const {
2956 SDValue Op0 = Op.getOperand(0);
2957 SDValue Op1 = Op.getOperand(1);
2958 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002959 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002960 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002961
2962 // We use DSGF for 32-bit division.
2963 if (is32Bit(VT)) {
2964 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002965 Opcode = SystemZISD::SDIVREM32;
2966 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2967 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2968 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002969 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00002970 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002971
2972 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2973 // input is "don't care". The instruction returns the remainder in
2974 // the even register and the quotient in the odd register.
2975 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002976 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002977 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002978 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002979}
2980
2981SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2982 SelectionDAG &DAG) const {
2983 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002984 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002985
2986 // DL(G) uses a double-width dividend, so we need to clear the even
2987 // register in the GR128 input. The instruction returns the remainder
2988 // in the even register and the quotient in the odd register.
2989 SDValue Ops[2];
2990 if (is32Bit(VT))
2991 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2992 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2993 else
2994 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2995 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002996 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002997}
2998
2999SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
3000 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
3001
3002 // Get the known-zero masks for each operand.
3003 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
3004 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00003005 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
3006 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003007
3008 // See if the upper 32 bits of one operand and the lower 32 bits of the
3009 // other are known zero. They are the low and high operands respectively.
3010 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
3011 KnownZero[1].getZExtValue() };
3012 unsigned High, Low;
3013 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3014 High = 1, Low = 0;
3015 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3016 High = 0, Low = 1;
3017 else
3018 return Op;
3019
3020 SDValue LowOp = Ops[Low];
3021 SDValue HighOp = Ops[High];
3022
3023 // If the high part is a constant, we're better off using IILH.
3024 if (HighOp.getOpcode() == ISD::Constant)
3025 return Op;
3026
3027 // If the low part is a constant that is outside the range of LHI,
3028 // then we're better off using IILF.
3029 if (LowOp.getOpcode() == ISD::Constant) {
3030 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3031 if (!isInt<16>(Value))
3032 return Op;
3033 }
3034
3035 // Check whether the high part is an AND that doesn't change the
3036 // high 32 bits and just masks out low bits. We can skip it if so.
3037 if (HighOp.getOpcode() == ISD::AND &&
3038 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003039 SDValue HighOp0 = HighOp.getOperand(0);
3040 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3041 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3042 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003043 }
3044
3045 // Take advantage of the fact that all GR32 operations only change the
3046 // low 32 bits by truncating Low to an i32 and inserting it directly
3047 // using a subreg. The interesting cases are those where the truncation
3048 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003049 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003050 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003051 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003052 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003053}
3054
Ulrich Weigandb4012182015-03-31 12:56:33 +00003055SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3056 SelectionDAG &DAG) const {
3057 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003058 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003059 Op = Op.getOperand(0);
3060
3061 // Handle vector types via VPOPCT.
3062 if (VT.isVector()) {
3063 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3064 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
3065 switch (VT.getVectorElementType().getSizeInBits()) {
3066 case 8:
3067 break;
3068 case 16: {
3069 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3070 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3071 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3072 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3073 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3074 break;
3075 }
3076 case 32: {
3077 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3078 DAG.getConstant(0, DL, MVT::i32));
3079 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3080 break;
3081 }
3082 case 64: {
3083 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3084 DAG.getConstant(0, DL, MVT::i32));
3085 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3086 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3087 break;
3088 }
3089 default:
3090 llvm_unreachable("Unexpected type");
3091 }
3092 return Op;
3093 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003094
3095 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003096 APInt KnownZero, KnownOne;
3097 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003098 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3099 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003100 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003101
3102 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003103 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003104 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3105 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003106
3107 // The POPCNT instruction counts the number of bits in each byte.
3108 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3109 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3110 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3111
3112 // Add up per-byte counts in a binary tree. All bits of Op at
3113 // position larger than BitSize remain zero throughout.
3114 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003115 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003116 if (BitSize != OrigBitSize)
3117 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003118 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003119 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3120 }
3121
3122 // Extract overall result from high byte.
3123 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003124 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3125 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003126
3127 return Op;
3128}
3129
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003130SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
3131 SelectionDAG &DAG) const {
3132 SDLoc DL(Op);
3133 AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
3134 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
3135 SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
3136 cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
3137
3138 // The only fence that needs an instruction is a sequentially-consistent
3139 // cross-thread fence.
JF Bastien800f87a2016-04-06 21:19:33 +00003140 if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
3141 FenceScope == CrossThread) {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003142 return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
JF Bastien800f87a2016-04-06 21:19:33 +00003143 Op.getOperand(0)),
3144 0);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00003145 }
3146
3147 // MEMBARRIER is a compiler barrier; it codegens to a no-op.
3148 return DAG.getNode(SystemZISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
3149}
3150
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003151// Op is an atomic load. Lower it into a normal volatile load.
3152SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3153 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003154 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003155 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3156 Node->getChain(), Node->getBasePtr(),
3157 Node->getMemoryVT(), Node->getMemOperand());
3158}
3159
3160// Op is an atomic store. Lower it into a normal volatile store followed
3161// by a serialization.
3162SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3163 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003164 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003165 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3166 Node->getBasePtr(), Node->getMemoryVT(),
3167 Node->getMemOperand());
3168 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3169 Chain), 0);
3170}
3171
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003172// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3173// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003174SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3175 SelectionDAG &DAG,
3176 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003177 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003178
3179 // 32-bit operations need no code outside the main loop.
3180 EVT NarrowVT = Node->getMemoryVT();
3181 EVT WideVT = MVT::i32;
3182 if (NarrowVT == WideVT)
3183 return Op;
3184
3185 int64_t BitSize = NarrowVT.getSizeInBits();
3186 SDValue ChainIn = Node->getChain();
3187 SDValue Addr = Node->getBasePtr();
3188 SDValue Src2 = Node->getVal();
3189 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003190 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003191 EVT PtrVT = Addr.getValueType();
3192
3193 // Convert atomic subtracts of constants into additions.
3194 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003195 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003196 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003197 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003198 }
3199
3200 // Get the address of the containing word.
3201 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003202 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003203
3204 // Get the number of bits that the word must be rotated left in order
3205 // to bring the field to the top bits of a GR32.
3206 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003207 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003208 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3209
3210 // Get the complementing shift amount, for rotating a field in the top
3211 // bits back to its proper position.
3212 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003213 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003214
3215 // Extend the source operand to 32 bits and prepare it for the inner loop.
3216 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3217 // operations require the source to be shifted in advance. (This shift
3218 // can be folded if the source is constant.) For AND and NAND, the lower
3219 // bits must be set, while for other opcodes they should be left clear.
3220 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3221 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003222 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003223 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3224 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3225 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003226 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003227
3228 // Construct the ATOMIC_LOADW_* node.
3229 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3230 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003231 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003232 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003233 NarrowVT, MMO);
3234
3235 // Rotate the result of the final CS so that the field is in the lower
3236 // bits of a GR32, then truncate it.
3237 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003238 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003239 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3240
3241 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003242 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003243}
3244
Richard Sandiford41350a52013-12-24 15:18:04 +00003245// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003246// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003247// operations into additions.
3248SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3249 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003250 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003251 EVT MemVT = Node->getMemoryVT();
3252 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3253 // A full-width operation.
3254 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3255 SDValue Src2 = Node->getVal();
3256 SDValue NegSrc2;
3257 SDLoc DL(Src2);
3258
Richard Sandiford21f5d682014-03-06 11:22:58 +00003259 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003260 // Use an addition if the operand is constant and either LAA(G) is
3261 // available or the negative value is in the range of A(G)FHI.
3262 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003263 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003264 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003265 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003266 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003267 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003268 Src2);
3269
3270 if (NegSrc2.getNode())
3271 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3272 Node->getChain(), Node->getBasePtr(), NegSrc2,
3273 Node->getMemOperand(), Node->getOrdering(),
3274 Node->getSynchScope());
3275
3276 // Use the node as-is.
3277 return Op;
3278 }
3279
3280 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3281}
3282
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003283// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3284// into a fullword ATOMIC_CMP_SWAPW operation.
3285SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3286 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003287 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003288
3289 // We have native support for 32-bit compare and swap.
3290 EVT NarrowVT = Node->getMemoryVT();
3291 EVT WideVT = MVT::i32;
3292 if (NarrowVT == WideVT)
3293 return Op;
3294
3295 int64_t BitSize = NarrowVT.getSizeInBits();
3296 SDValue ChainIn = Node->getOperand(0);
3297 SDValue Addr = Node->getOperand(1);
3298 SDValue CmpVal = Node->getOperand(2);
3299 SDValue SwapVal = Node->getOperand(3);
3300 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003301 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003302 EVT PtrVT = Addr.getValueType();
3303
3304 // Get the address of the containing word.
3305 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003306 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003307
3308 // Get the number of bits that the word must be rotated left in order
3309 // to bring the field to the top bits of a GR32.
3310 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003311 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003312 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3313
3314 // Get the complementing shift amount, for rotating a field in the top
3315 // bits back to its proper position.
3316 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003317 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003318
3319 // Construct the ATOMIC_CMP_SWAPW node.
3320 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3321 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003322 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003323 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003324 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003325 return AtomicOp;
3326}
3327
3328SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3329 SelectionDAG &DAG) const {
3330 MachineFunction &MF = DAG.getMachineFunction();
3331 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003332 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003333 SystemZ::R15D, Op.getValueType());
3334}
3335
3336SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3337 SelectionDAG &DAG) const {
3338 MachineFunction &MF = DAG.getMachineFunction();
3339 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003340 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003341 SystemZ::R15D, Op.getOperand(1));
3342}
3343
Richard Sandiford03481332013-08-23 11:36:42 +00003344SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3345 SelectionDAG &DAG) const {
3346 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3347 if (!IsData)
3348 // Just preserve the chain.
3349 return Op.getOperand(0);
3350
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003351 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003352 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3353 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003354 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003355 SDValue Ops[] = {
3356 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003357 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003358 Op.getOperand(1)
3359 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003360 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003361 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003362 Node->getMemoryVT(), Node->getMemOperand());
3363}
3364
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003365// Return an i32 that contains the value of CC immediately after After,
3366// whose final operand must be MVT::Glue.
3367static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003368 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003369 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003370 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3371 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3372 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003373}
3374
3375SDValue
3376SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3377 SelectionDAG &DAG) const {
3378 unsigned Opcode, CCValid;
3379 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3380 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3381 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3382 SDValue CC = getCCResult(DAG, Glued.getNode());
3383 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3384 return SDValue();
3385 }
3386
3387 return SDValue();
3388}
3389
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003390SDValue
3391SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3392 SelectionDAG &DAG) const {
3393 unsigned Opcode, CCValid;
3394 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3395 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3396 SDValue CC = getCCResult(DAG, Glued.getNode());
3397 if (Op->getNumValues() == 1)
3398 return CC;
3399 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003400 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3401 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003402 }
3403
3404 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3405 switch (Id) {
Marcin Koscielnickif12609c2016-04-20 01:03:48 +00003406 case Intrinsic::thread_pointer:
3407 return lowerThreadPointer(SDLoc(Op), DAG);
3408
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003409 case Intrinsic::s390_vpdi:
3410 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3411 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3412
3413 case Intrinsic::s390_vperm:
3414 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3415 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3416
3417 case Intrinsic::s390_vuphb:
3418 case Intrinsic::s390_vuphh:
3419 case Intrinsic::s390_vuphf:
3420 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3421 Op.getOperand(1));
3422
3423 case Intrinsic::s390_vuplhb:
3424 case Intrinsic::s390_vuplhh:
3425 case Intrinsic::s390_vuplhf:
3426 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3427 Op.getOperand(1));
3428
3429 case Intrinsic::s390_vuplb:
3430 case Intrinsic::s390_vuplhw:
3431 case Intrinsic::s390_vuplf:
3432 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3433 Op.getOperand(1));
3434
3435 case Intrinsic::s390_vupllb:
3436 case Intrinsic::s390_vupllh:
3437 case Intrinsic::s390_vupllf:
3438 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3439 Op.getOperand(1));
3440
3441 case Intrinsic::s390_vsumb:
3442 case Intrinsic::s390_vsumh:
3443 case Intrinsic::s390_vsumgh:
3444 case Intrinsic::s390_vsumgf:
3445 case Intrinsic::s390_vsumqf:
3446 case Intrinsic::s390_vsumqg:
3447 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3448 Op.getOperand(1), Op.getOperand(2));
3449 }
3450
3451 return SDValue();
3452}
3453
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003454namespace {
3455// Says that SystemZISD operation Opcode can be used to perform the equivalent
3456// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3457// Operand is the constant third operand, otherwise it is the number of
3458// bytes in each element of the result.
3459struct Permute {
3460 unsigned Opcode;
3461 unsigned Operand;
3462 unsigned char Bytes[SystemZ::VectorBytes];
3463};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003464}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003465
3466static const Permute PermuteForms[] = {
3467 // VMRHG
3468 { SystemZISD::MERGE_HIGH, 8,
3469 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3470 // VMRHF
3471 { SystemZISD::MERGE_HIGH, 4,
3472 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3473 // VMRHH
3474 { SystemZISD::MERGE_HIGH, 2,
3475 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3476 // VMRHB
3477 { SystemZISD::MERGE_HIGH, 1,
3478 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3479 // VMRLG
3480 { SystemZISD::MERGE_LOW, 8,
3481 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3482 // VMRLF
3483 { SystemZISD::MERGE_LOW, 4,
3484 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3485 // VMRLH
3486 { SystemZISD::MERGE_LOW, 2,
3487 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3488 // VMRLB
3489 { SystemZISD::MERGE_LOW, 1,
3490 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3491 // VPKG
3492 { SystemZISD::PACK, 4,
3493 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3494 // VPKF
3495 { SystemZISD::PACK, 2,
3496 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3497 // VPKH
3498 { SystemZISD::PACK, 1,
3499 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3500 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3501 { SystemZISD::PERMUTE_DWORDS, 4,
3502 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3503 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3504 { SystemZISD::PERMUTE_DWORDS, 1,
3505 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3506};
3507
3508// Called after matching a vector shuffle against a particular pattern.
3509// Both the original shuffle and the pattern have two vector operands.
3510// OpNos[0] is the operand of the original shuffle that should be used for
3511// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3512// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3513// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3514// for operands 0 and 1 of the pattern.
3515static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3516 if (OpNos[0] < 0) {
3517 if (OpNos[1] < 0)
3518 return false;
3519 OpNo0 = OpNo1 = OpNos[1];
3520 } else if (OpNos[1] < 0) {
3521 OpNo0 = OpNo1 = OpNos[0];
3522 } else {
3523 OpNo0 = OpNos[0];
3524 OpNo1 = OpNos[1];
3525 }
3526 return true;
3527}
3528
3529// Bytes is a VPERM-like permute vector, except that -1 is used for
3530// undefined bytes. Return true if the VPERM can be implemented using P.
3531// When returning true set OpNo0 to the VPERM operand that should be
3532// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3533//
3534// For example, if swapping the VPERM operands allows P to match, OpNo0
3535// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3536// operand, but rewriting it to use two duplicated operands allows it to
3537// match P, then OpNo0 and OpNo1 will be the same.
3538static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3539 unsigned &OpNo0, unsigned &OpNo1) {
3540 int OpNos[] = { -1, -1 };
3541 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3542 int Elt = Bytes[I];
3543 if (Elt >= 0) {
3544 // Make sure that the two permute vectors use the same suboperand
3545 // byte number. Only the operand numbers (the high bits) are
3546 // allowed to differ.
3547 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3548 return false;
3549 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3550 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3551 // Make sure that the operand mappings are consistent with previous
3552 // elements.
3553 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3554 return false;
3555 OpNos[ModelOpNo] = RealOpNo;
3556 }
3557 }
3558 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3559}
3560
3561// As above, but search for a matching permute.
3562static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3563 unsigned &OpNo0, unsigned &OpNo1) {
3564 for (auto &P : PermuteForms)
3565 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3566 return &P;
3567 return nullptr;
3568}
3569
3570// Bytes is a VPERM-like permute vector, except that -1 is used for
3571// undefined bytes. This permute is an operand of an outer permute.
3572// See whether redistributing the -1 bytes gives a shuffle that can be
3573// implemented using P. If so, set Transform to a VPERM-like permute vector
3574// that, when applied to the result of P, gives the original permute in Bytes.
3575static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3576 const Permute &P,
3577 SmallVectorImpl<int> &Transform) {
3578 unsigned To = 0;
3579 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3580 int Elt = Bytes[From];
3581 if (Elt < 0)
3582 // Byte number From of the result is undefined.
3583 Transform[From] = -1;
3584 else {
3585 while (P.Bytes[To] != Elt) {
3586 To += 1;
3587 if (To == SystemZ::VectorBytes)
3588 return false;
3589 }
3590 Transform[From] = To;
3591 }
3592 }
3593 return true;
3594}
3595
3596// As above, but search for a matching permute.
3597static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3598 SmallVectorImpl<int> &Transform) {
3599 for (auto &P : PermuteForms)
3600 if (matchDoublePermute(Bytes, P, Transform))
3601 return &P;
3602 return nullptr;
3603}
3604
3605// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3606// as if it had type vNi8.
3607static void getVPermMask(ShuffleVectorSDNode *VSN,
3608 SmallVectorImpl<int> &Bytes) {
3609 EVT VT = VSN->getValueType(0);
3610 unsigned NumElements = VT.getVectorNumElements();
3611 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3612 Bytes.resize(NumElements * BytesPerElement, -1);
3613 for (unsigned I = 0; I < NumElements; ++I) {
3614 int Index = VSN->getMaskElt(I);
3615 if (Index >= 0)
3616 for (unsigned J = 0; J < BytesPerElement; ++J)
3617 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3618 }
3619}
3620
3621// Bytes is a VPERM-like permute vector, except that -1 is used for
3622// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3623// the result come from a contiguous sequence of bytes from one input.
3624// Set Base to the selector for the first byte if so.
3625static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3626 unsigned BytesPerElement, int &Base) {
3627 Base = -1;
3628 for (unsigned I = 0; I < BytesPerElement; ++I) {
3629 if (Bytes[Start + I] >= 0) {
3630 unsigned Elem = Bytes[Start + I];
3631 if (Base < 0) {
3632 Base = Elem - I;
3633 // Make sure the bytes would come from one input operand.
3634 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3635 return false;
3636 } else if (unsigned(Base) != Elem - I)
3637 return false;
3638 }
3639 }
3640 return true;
3641}
3642
3643// Bytes is a VPERM-like permute vector, except that -1 is used for
3644// undefined bytes. Return true if it can be performed using VSLDI.
3645// When returning true, set StartIndex to the shift amount and OpNo0
3646// and OpNo1 to the VPERM operands that should be used as the first
3647// and second shift operand respectively.
3648static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3649 unsigned &StartIndex, unsigned &OpNo0,
3650 unsigned &OpNo1) {
3651 int OpNos[] = { -1, -1 };
3652 int Shift = -1;
3653 for (unsigned I = 0; I < 16; ++I) {
3654 int Index = Bytes[I];
3655 if (Index >= 0) {
3656 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3657 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3658 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3659 if (Shift < 0)
3660 Shift = ExpectedShift;
3661 else if (Shift != ExpectedShift)
3662 return false;
3663 // Make sure that the operand mappings are consistent with previous
3664 // elements.
3665 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3666 return false;
3667 OpNos[ModelOpNo] = RealOpNo;
3668 }
3669 }
3670 StartIndex = Shift;
3671 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3672}
3673
3674// Create a node that performs P on operands Op0 and Op1, casting the
3675// operands to the appropriate type. The type of the result is determined by P.
3676static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3677 const Permute &P, SDValue Op0, SDValue Op1) {
3678 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3679 // elements of a PACK are twice as wide as the outputs.
3680 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3681 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3682 P.Operand);
3683 // Cast both operands to the appropriate type.
3684 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3685 SystemZ::VectorBytes / InBytes);
3686 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3687 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3688 SDValue Op;
3689 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3690 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3691 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3692 } else if (P.Opcode == SystemZISD::PACK) {
3693 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3694 SystemZ::VectorBytes / P.Operand);
3695 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3696 } else {
3697 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3698 }
3699 return Op;
3700}
3701
3702// Bytes is a VPERM-like permute vector, except that -1 is used for
3703// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3704// VSLDI or VPERM.
3705static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3706 const SmallVectorImpl<int> &Bytes) {
3707 for (unsigned I = 0; I < 2; ++I)
3708 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3709
3710 // First see whether VSLDI can be used.
3711 unsigned StartIndex, OpNo0, OpNo1;
3712 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3713 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3714 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3715
3716 // Fall back on VPERM. Construct an SDNode for the permute vector.
3717 SDValue IndexNodes[SystemZ::VectorBytes];
3718 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3719 if (Bytes[I] >= 0)
3720 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3721 else
3722 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3723 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3724 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3725}
3726
3727namespace {
3728// Describes a general N-operand vector shuffle.
3729struct GeneralShuffle {
3730 GeneralShuffle(EVT vt) : VT(vt) {}
3731 void addUndef();
3732 void add(SDValue, unsigned);
3733 SDValue getNode(SelectionDAG &, SDLoc);
3734
3735 // The operands of the shuffle.
3736 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3737
3738 // Index I is -1 if byte I of the result is undefined. Otherwise the
3739 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3740 // Bytes[I] / SystemZ::VectorBytes.
3741 SmallVector<int, SystemZ::VectorBytes> Bytes;
3742
3743 // The type of the shuffle result.
3744 EVT VT;
3745};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003746}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003747
3748// Add an extra undefined element to the shuffle.
3749void GeneralShuffle::addUndef() {
3750 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3751 for (unsigned I = 0; I < BytesPerElement; ++I)
3752 Bytes.push_back(-1);
3753}
3754
3755// Add an extra element to the shuffle, taking it from element Elem of Op.
3756// A null Op indicates a vector input whose value will be calculated later;
3757// there is at most one such input per shuffle and it always has the same
3758// type as the result.
3759void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3760 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3761
3762 // The source vector can have wider elements than the result,
3763 // either through an explicit TRUNCATE or because of type legalization.
3764 // We want the least significant part.
3765 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3766 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3767 assert(FromBytesPerElement >= BytesPerElement &&
3768 "Invalid EXTRACT_VECTOR_ELT");
3769 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3770 (FromBytesPerElement - BytesPerElement));
3771
3772 // Look through things like shuffles and bitcasts.
3773 while (Op.getNode()) {
3774 if (Op.getOpcode() == ISD::BITCAST)
3775 Op = Op.getOperand(0);
3776 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3777 // See whether the bytes we need come from a contiguous part of one
3778 // operand.
3779 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3780 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3781 int NewByte;
3782 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3783 break;
3784 if (NewByte < 0) {
3785 addUndef();
3786 return;
3787 }
3788 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3789 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003790 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003791 addUndef();
3792 return;
3793 } else
3794 break;
3795 }
3796
3797 // Make sure that the source of the extraction is in Ops.
3798 unsigned OpNo = 0;
3799 for (; OpNo < Ops.size(); ++OpNo)
3800 if (Ops[OpNo] == Op)
3801 break;
3802 if (OpNo == Ops.size())
3803 Ops.push_back(Op);
3804
3805 // Add the element to Bytes.
3806 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3807 for (unsigned I = 0; I < BytesPerElement; ++I)
3808 Bytes.push_back(Base + I);
3809}
3810
3811// Return SDNodes for the completed shuffle.
3812SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3813 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3814
3815 if (Ops.size() == 0)
3816 return DAG.getUNDEF(VT);
3817
3818 // Make sure that there are at least two shuffle operands.
3819 if (Ops.size() == 1)
3820 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3821
3822 // Create a tree of shuffles, deferring root node until after the loop.
3823 // Try to redistribute the undefined elements of non-root nodes so that
3824 // the non-root shuffles match something like a pack or merge, then adjust
3825 // the parent node's permute vector to compensate for the new order.
3826 // Among other things, this copes with vectors like <2 x i16> that were
3827 // padded with undefined elements during type legalization.
3828 //
3829 // In the best case this redistribution will lead to the whole tree
3830 // using packs and merges. It should rarely be a loss in other cases.
3831 unsigned Stride = 1;
3832 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3833 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3834 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3835
3836 // Create a mask for just these two operands.
3837 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3838 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3839 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3840 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3841 if (OpNo == I)
3842 NewBytes[J] = Byte;
3843 else if (OpNo == I + Stride)
3844 NewBytes[J] = SystemZ::VectorBytes + Byte;
3845 else
3846 NewBytes[J] = -1;
3847 }
3848 // See if it would be better to reorganize NewMask to avoid using VPERM.
3849 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3850 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3851 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3852 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3853 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3854 if (NewBytes[J] >= 0) {
3855 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3856 "Invalid double permute");
3857 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3858 } else
3859 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3860 }
3861 } else {
3862 // Just use NewBytes on the operands.
3863 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3864 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3865 if (NewBytes[J] >= 0)
3866 Bytes[J] = I * SystemZ::VectorBytes + J;
3867 }
3868 }
3869 }
3870
3871 // Now we just have 2 inputs. Put the second operand in Ops[1].
3872 if (Stride > 1) {
3873 Ops[1] = Ops[Stride];
3874 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3875 if (Bytes[I] >= int(SystemZ::VectorBytes))
3876 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3877 }
3878
3879 // Look for an instruction that can do the permute without resorting
3880 // to VPERM.
3881 unsigned OpNo0, OpNo1;
3882 SDValue Op;
3883 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3884 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3885 else
3886 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3887 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3888}
3889
Ulrich Weigandcd808232015-05-05 19:26:48 +00003890// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3891static bool isScalarToVector(SDValue Op) {
3892 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003893 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003894 return false;
3895 return true;
3896}
3897
3898// Return a vector of type VT that contains Value in the first element.
3899// The other elements don't matter.
3900static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3901 SDValue Value) {
3902 // If we have a constant, replicate it to all elements and let the
3903 // BUILD_VECTOR lowering take care of it.
3904 if (Value.getOpcode() == ISD::Constant ||
3905 Value.getOpcode() == ISD::ConstantFP) {
3906 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3907 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3908 }
Sanjay Patel57195842016-03-14 17:28:46 +00003909 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003910 return DAG.getUNDEF(VT);
3911 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3912}
3913
3914// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3915// element 1. Used for cases in which replication is cheap.
3916static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3917 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003918 if (Op0.isUndef()) {
3919 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003920 return DAG.getUNDEF(VT);
3921 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3922 }
Sanjay Patel57195842016-03-14 17:28:46 +00003923 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003924 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3925 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3926 buildScalarToVector(DAG, DL, VT, Op0),
3927 buildScalarToVector(DAG, DL, VT, Op1));
3928}
3929
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003930// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3931// vector for them.
3932static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3933 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003934 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003935 return DAG.getUNDEF(MVT::v2i64);
3936 // If one of the two inputs is undefined then replicate the other one,
3937 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00003938 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003939 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00003940 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003941 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3942 else {
3943 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3944 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3945 }
3946 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3947}
3948
3949// Try to represent constant BUILD_VECTOR node BVN using a
3950// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3951// on success.
3952static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3953 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3954 unsigned BytesPerElement = ElemVT.getStoreSize();
3955 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3956 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00003957 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003958 uint64_t Value;
3959 if (Op.getOpcode() == ISD::Constant)
3960 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3961 else if (Op.getOpcode() == ISD::ConstantFP)
3962 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3963 .getZExtValue());
3964 else
3965 return false;
3966 for (unsigned J = 0; J < BytesPerElement; ++J) {
3967 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3968 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00003969 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003970 else if (Byte != 0)
3971 return false;
3972 }
3973 }
3974 }
3975 return true;
3976}
3977
3978// Try to load a vector constant in which BitsPerElement-bit value Value
3979// is replicated to fill the vector. VT is the type of the resulting
3980// constant, which may have elements of a different size from BitsPerElement.
3981// Return the SDValue of the constant on success, otherwise return
3982// an empty value.
3983static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3984 const SystemZInstrInfo *TII,
3985 SDLoc DL, EVT VT, uint64_t Value,
3986 unsigned BitsPerElement) {
3987 // Signed 16-bit values can be replicated using VREPI.
3988 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3989 if (isInt<16>(SignedValue)) {
3990 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3991 SystemZ::VectorBits / BitsPerElement);
3992 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3993 DAG.getConstant(SignedValue, DL, MVT::i32));
3994 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3995 }
3996 // See whether rotating the constant left some N places gives a value that
3997 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3998 // If so we can use VGM.
3999 unsigned Start, End;
4000 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
4001 // isRxSBGMask returns the bit numbers for a full 64-bit value,
4002 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
4003 // bit numbers for an BitsPerElement value, so that 0 denotes
4004 // 1 << (BitsPerElement-1).
4005 Start -= 64 - BitsPerElement;
4006 End -= 64 - BitsPerElement;
4007 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
4008 SystemZ::VectorBits / BitsPerElement);
4009 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
4010 DAG.getConstant(Start, DL, MVT::i32),
4011 DAG.getConstant(End, DL, MVT::i32));
4012 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4013 }
4014 return SDValue();
4015}
4016
4017// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
4018// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
4019// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
4020// would benefit from this representation and return it if so.
4021static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
4022 BuildVectorSDNode *BVN) {
4023 EVT VT = BVN->getValueType(0);
4024 unsigned NumElements = VT.getVectorNumElements();
4025
4026 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
4027 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
4028 // need a BUILD_VECTOR, add an additional placeholder operand for that
4029 // BUILD_VECTOR and store its operands in ResidueOps.
4030 GeneralShuffle GS(VT);
4031 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
4032 bool FoundOne = false;
4033 for (unsigned I = 0; I < NumElements; ++I) {
4034 SDValue Op = BVN->getOperand(I);
4035 if (Op.getOpcode() == ISD::TRUNCATE)
4036 Op = Op.getOperand(0);
4037 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4038 Op.getOperand(1).getOpcode() == ISD::Constant) {
4039 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4040 GS.add(Op.getOperand(0), Elem);
4041 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004042 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004043 GS.addUndef();
4044 } else {
4045 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00004046 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004047 }
4048 }
4049
4050 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4051 if (!FoundOne)
4052 return SDValue();
4053
4054 // Create the BUILD_VECTOR for the remaining elements, if any.
4055 if (!ResidueOps.empty()) {
4056 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004057 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004058 for (auto &Op : GS.Ops) {
4059 if (!Op.getNode()) {
4060 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
4061 break;
4062 }
4063 }
4064 }
4065 return GS.getNode(DAG, SDLoc(BVN));
4066}
4067
4068// Combine GPR scalar values Elems into a vector of type VT.
4069static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
4070 SmallVectorImpl<SDValue> &Elems) {
4071 // See whether there is a single replicated value.
4072 SDValue Single;
4073 unsigned int NumElements = Elems.size();
4074 unsigned int Count = 0;
4075 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004076 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004077 if (!Single.getNode())
4078 Single = Elem;
4079 else if (Elem != Single) {
4080 Single = SDValue();
4081 break;
4082 }
4083 Count += 1;
4084 }
4085 }
4086 // There are three cases here:
4087 //
4088 // - if the only defined element is a loaded one, the best sequence
4089 // is a replicating load.
4090 //
4091 // - otherwise, if the only defined element is an i64 value, we will
4092 // end up with the same VLVGP sequence regardless of whether we short-cut
4093 // for replication or fall through to the later code.
4094 //
4095 // - otherwise, if the only defined element is an i32 or smaller value,
4096 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4097 // This is only a win if the single defined element is used more than once.
4098 // In other cases we're better off using a single VLVGx.
4099 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4100 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4101
4102 // The best way of building a v2i64 from two i64s is to use VLVGP.
4103 if (VT == MVT::v2i64)
4104 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4105
Ulrich Weigandcd808232015-05-05 19:26:48 +00004106 // Use a 64-bit merge high to combine two doubles.
4107 if (VT == MVT::v2f64)
4108 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4109
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004110 // Build v4f32 values directly from the FPRs:
4111 //
4112 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4113 // V V VMRHF
4114 // <ABxx> <CDxx>
4115 // V VMRHG
4116 // <ABCD>
4117 if (VT == MVT::v4f32) {
4118 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4119 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4120 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004121 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004122 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004123 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004124 Op23 = Op01;
4125 // Merging identical replications is a no-op.
4126 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4127 return Op01;
4128 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4129 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4130 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4131 DL, MVT::v2i64, Op01, Op23);
4132 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4133 }
4134
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004135 // Collect the constant terms.
4136 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4137 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4138
4139 unsigned NumConstants = 0;
4140 for (unsigned I = 0; I < NumElements; ++I) {
4141 SDValue Elem = Elems[I];
4142 if (Elem.getOpcode() == ISD::Constant ||
4143 Elem.getOpcode() == ISD::ConstantFP) {
4144 NumConstants += 1;
4145 Constants[I] = Elem;
4146 Done[I] = true;
4147 }
4148 }
4149 // If there was at least one constant, fill in the other elements of
4150 // Constants with undefs to get a full vector constant and use that
4151 // as the starting point.
4152 SDValue Result;
4153 if (NumConstants > 0) {
4154 for (unsigned I = 0; I < NumElements; ++I)
4155 if (!Constants[I].getNode())
4156 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4157 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4158 } else {
4159 // Otherwise try to use VLVGP to start the sequence in order to
4160 // avoid a false dependency on any previous contents of the vector
4161 // register. This only makes sense if one of the associated elements
4162 // is defined.
4163 unsigned I1 = NumElements / 2 - 1;
4164 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004165 bool Def1 = !Elems[I1].isUndef();
4166 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004167 if (Def1 || Def2) {
4168 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4169 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4170 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4171 joinDwords(DAG, DL, Elem1, Elem2));
4172 Done[I1] = true;
4173 Done[I2] = true;
4174 } else
4175 Result = DAG.getUNDEF(VT);
4176 }
4177
4178 // Use VLVGx to insert the other elements.
4179 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004180 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004181 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4182 DAG.getConstant(I, DL, MVT::i32));
4183 return Result;
4184}
4185
4186SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4187 SelectionDAG &DAG) const {
4188 const SystemZInstrInfo *TII =
4189 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4190 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4191 SDLoc DL(Op);
4192 EVT VT = Op.getValueType();
4193
4194 if (BVN->isConstant()) {
4195 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4196 // preferred way of creating all-zero and all-one vectors so give it
4197 // priority over other methods below.
4198 uint64_t Mask = 0;
4199 if (tryBuildVectorByteMask(BVN, Mask)) {
4200 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4201 DAG.getConstant(Mask, DL, MVT::i32));
4202 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4203 }
4204
4205 // Try using some form of replication.
4206 APInt SplatBits, SplatUndef;
4207 unsigned SplatBitSize;
4208 bool HasAnyUndefs;
4209 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4210 8, true) &&
4211 SplatBitSize <= 64) {
4212 // First try assuming that any undefined bits above the highest set bit
4213 // and below the lowest set bit are 1s. This increases the likelihood of
4214 // being able to use a sign-extended element value in VECTOR REPLICATE
4215 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4216 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4217 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4218 uint64_t Lower = (SplatUndefZ
4219 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4220 uint64_t Upper = (SplatUndefZ
4221 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4222 uint64_t Value = SplatBitsZ | Upper | Lower;
4223 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4224 SplatBitSize);
4225 if (Op.getNode())
4226 return Op;
4227
4228 // Now try assuming that any undefined bits between the first and
4229 // last defined set bits are set. This increases the chances of
4230 // using a non-wraparound mask.
4231 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4232 Value = SplatBitsZ | Middle;
4233 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4234 if (Op.getNode())
4235 return Op;
4236 }
4237
4238 // Fall back to loading it from memory.
4239 return SDValue();
4240 }
4241
4242 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004243 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004244 return Res;
4245
Ulrich Weigandcd808232015-05-05 19:26:48 +00004246 // Detect SCALAR_TO_VECTOR conversions.
4247 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4248 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4249
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004250 // Otherwise use buildVector to build the vector up from GPRs.
4251 unsigned NumElements = Op.getNumOperands();
4252 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4253 for (unsigned I = 0; I < NumElements; ++I)
4254 Ops[I] = Op.getOperand(I);
4255 return buildVector(DAG, DL, VT, Ops);
4256}
4257
4258SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4259 SelectionDAG &DAG) const {
4260 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4261 SDLoc DL(Op);
4262 EVT VT = Op.getValueType();
4263 unsigned NumElements = VT.getVectorNumElements();
4264
4265 if (VSN->isSplat()) {
4266 SDValue Op0 = Op.getOperand(0);
4267 unsigned Index = VSN->getSplatIndex();
4268 assert(Index < VT.getVectorNumElements() &&
4269 "Splat index should be defined and in first operand");
4270 // See whether the value we're splatting is directly available as a scalar.
4271 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4272 Op0.getOpcode() == ISD::BUILD_VECTOR)
4273 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4274 // Otherwise keep it as a vector-to-vector operation.
4275 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4276 DAG.getConstant(Index, DL, MVT::i32));
4277 }
4278
4279 GeneralShuffle GS(VT);
4280 for (unsigned I = 0; I < NumElements; ++I) {
4281 int Elt = VSN->getMaskElt(I);
4282 if (Elt < 0)
4283 GS.addUndef();
4284 else
4285 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4286 unsigned(Elt) % NumElements);
4287 }
4288 return GS.getNode(DAG, SDLoc(VSN));
4289}
4290
4291SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4292 SelectionDAG &DAG) const {
4293 SDLoc DL(Op);
4294 // Just insert the scalar into element 0 of an undefined vector.
4295 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4296 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4297 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4298}
4299
Ulrich Weigandcd808232015-05-05 19:26:48 +00004300SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4301 SelectionDAG &DAG) const {
4302 // Handle insertions of floating-point values.
4303 SDLoc DL(Op);
4304 SDValue Op0 = Op.getOperand(0);
4305 SDValue Op1 = Op.getOperand(1);
4306 SDValue Op2 = Op.getOperand(2);
4307 EVT VT = Op.getValueType();
4308
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004309 // Insertions into constant indices of a v2f64 can be done using VPDI.
4310 // However, if the inserted value is a bitcast or a constant then it's
4311 // better to use GPRs, as below.
4312 if (VT == MVT::v2f64 &&
4313 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004314 Op1.getOpcode() != ISD::ConstantFP &&
4315 Op2.getOpcode() == ISD::Constant) {
4316 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4317 unsigned Mask = VT.getVectorNumElements() - 1;
4318 if (Index <= Mask)
4319 return Op;
4320 }
4321
4322 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4323 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4324 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4325 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4326 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4327 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4328 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4329}
4330
4331SDValue
4332SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4333 SelectionDAG &DAG) const {
4334 // Handle extractions of floating-point values.
4335 SDLoc DL(Op);
4336 SDValue Op0 = Op.getOperand(0);
4337 SDValue Op1 = Op.getOperand(1);
4338 EVT VT = Op.getValueType();
4339 EVT VecVT = Op0.getValueType();
4340
4341 // Extractions of constant indices can be done directly.
4342 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4343 uint64_t Index = CIndexN->getZExtValue();
4344 unsigned Mask = VecVT.getVectorNumElements() - 1;
4345 if (Index <= Mask)
4346 return Op;
4347 }
4348
4349 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4350 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4351 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4352 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4353 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4354 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4355}
4356
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004357SDValue
4358SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004359 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004360 SDValue PackedOp = Op.getOperand(0);
4361 EVT OutVT = Op.getValueType();
4362 EVT InVT = PackedOp.getValueType();
4363 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4364 unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4365 do {
4366 FromBits *= 2;
4367 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4368 SystemZ::VectorBits / FromBits);
4369 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4370 } while (FromBits != ToBits);
4371 return PackedOp;
4372}
4373
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004374SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4375 unsigned ByScalar) const {
4376 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4377 SDValue Op0 = Op.getOperand(0);
4378 SDValue Op1 = Op.getOperand(1);
4379 SDLoc DL(Op);
4380 EVT VT = Op.getValueType();
4381 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4382
4383 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4384 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4385 APInt SplatBits, SplatUndef;
4386 unsigned SplatBitSize;
4387 bool HasAnyUndefs;
4388 // Check for constant splats. Use ElemBitSize as the minimum element
4389 // width and reject splats that need wider elements.
4390 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4391 ElemBitSize, true) &&
4392 SplatBitSize == ElemBitSize) {
4393 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4394 DL, MVT::i32);
4395 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4396 }
4397 // Check for variable splats.
4398 BitVector UndefElements;
4399 SDValue Splat = BVN->getSplatValue(&UndefElements);
4400 if (Splat) {
4401 // Since i32 is the smallest legal type, we either need a no-op
4402 // or a truncation.
4403 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4404 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4405 }
4406 }
4407
4408 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4409 // and the shift amount is directly available in a GPR.
4410 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4411 if (VSN->isSplat()) {
4412 SDValue VSNOp0 = VSN->getOperand(0);
4413 unsigned Index = VSN->getSplatIndex();
4414 assert(Index < VT.getVectorNumElements() &&
4415 "Splat index should be defined and in first operand");
4416 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4417 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4418 // Since i32 is the smallest legal type, we either need a no-op
4419 // or a truncation.
4420 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4421 VSNOp0.getOperand(Index));
4422 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4423 }
4424 }
4425 }
4426
4427 // Otherwise just treat the current form as legal.
4428 return Op;
4429}
4430
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004431SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4432 SelectionDAG &DAG) const {
4433 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004434 case ISD::FRAMEADDR:
4435 return lowerFRAMEADDR(Op, DAG);
4436 case ISD::RETURNADDR:
4437 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004438 case ISD::BR_CC:
4439 return lowerBR_CC(Op, DAG);
4440 case ISD::SELECT_CC:
4441 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004442 case ISD::SETCC:
4443 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004444 case ISD::GlobalAddress:
4445 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4446 case ISD::GlobalTLSAddress:
4447 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4448 case ISD::BlockAddress:
4449 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4450 case ISD::JumpTable:
4451 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4452 case ISD::ConstantPool:
4453 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4454 case ISD::BITCAST:
4455 return lowerBITCAST(Op, DAG);
4456 case ISD::VASTART:
4457 return lowerVASTART(Op, DAG);
4458 case ISD::VACOPY:
4459 return lowerVACOPY(Op, DAG);
4460 case ISD::DYNAMIC_STACKALLOC:
4461 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004462 case ISD::SMUL_LOHI:
4463 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004464 case ISD::UMUL_LOHI:
4465 return lowerUMUL_LOHI(Op, DAG);
4466 case ISD::SDIVREM:
4467 return lowerSDIVREM(Op, DAG);
4468 case ISD::UDIVREM:
4469 return lowerUDIVREM(Op, DAG);
4470 case ISD::OR:
4471 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004472 case ISD::CTPOP:
4473 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004474 case ISD::CTLZ_ZERO_UNDEF:
4475 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4476 Op.getValueType(), Op.getOperand(0));
4477 case ISD::CTTZ_ZERO_UNDEF:
4478 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4479 Op.getValueType(), Op.getOperand(0));
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004480 case ISD::ATOMIC_FENCE:
4481 return lowerATOMIC_FENCE(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004482 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004483 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4484 case ISD::ATOMIC_STORE:
4485 return lowerATOMIC_STORE(Op, DAG);
4486 case ISD::ATOMIC_LOAD:
4487 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004488 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004489 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004490 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004491 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004492 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004493 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004494 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004495 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004496 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004497 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004498 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004499 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004500 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004501 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004502 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004503 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004504 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004505 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004506 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004507 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004508 case ISD::ATOMIC_CMP_SWAP:
4509 return lowerATOMIC_CMP_SWAP(Op, DAG);
4510 case ISD::STACKSAVE:
4511 return lowerSTACKSAVE(Op, DAG);
4512 case ISD::STACKRESTORE:
4513 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004514 case ISD::PREFETCH:
4515 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004516 case ISD::INTRINSIC_W_CHAIN:
4517 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004518 case ISD::INTRINSIC_WO_CHAIN:
4519 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004520 case ISD::BUILD_VECTOR:
4521 return lowerBUILD_VECTOR(Op, DAG);
4522 case ISD::VECTOR_SHUFFLE:
4523 return lowerVECTOR_SHUFFLE(Op, DAG);
4524 case ISD::SCALAR_TO_VECTOR:
4525 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004526 case ISD::INSERT_VECTOR_ELT:
4527 return lowerINSERT_VECTOR_ELT(Op, DAG);
4528 case ISD::EXTRACT_VECTOR_ELT:
4529 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004530 case ISD::SIGN_EXTEND_VECTOR_INREG:
4531 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4532 case ISD::ZERO_EXTEND_VECTOR_INREG:
4533 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004534 case ISD::SHL:
4535 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4536 case ISD::SRL:
4537 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4538 case ISD::SRA:
4539 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004540 default:
4541 llvm_unreachable("Unexpected node to lower");
4542 }
4543}
4544
4545const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4546#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004547 switch ((SystemZISD::NodeType)Opcode) {
4548 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004549 OPCODE(RET_FLAG);
4550 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004551 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004552 OPCODE(TLS_GDCALL);
4553 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004554 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004555 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004556 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004557 OPCODE(ICMP);
4558 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004559 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004560 OPCODE(BR_CCMASK);
4561 OPCODE(SELECT_CCMASK);
4562 OPCODE(ADJDYNALLOC);
4563 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004564 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004565 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004566 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004567 OPCODE(SDIVREM64);
4568 OPCODE(UDIVREM32);
4569 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004570 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004571 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004572 OPCODE(NC);
4573 OPCODE(NC_LOOP);
4574 OPCODE(OC);
4575 OPCODE(OC_LOOP);
4576 OPCODE(XC);
4577 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004578 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004579 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004580 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004581 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004582 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004583 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004584 OPCODE(SERIALIZE);
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00004585 OPCODE(MEMBARRIER);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004586 OPCODE(TBEGIN);
4587 OPCODE(TBEGIN_NOFLOAT);
4588 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004589 OPCODE(BYTE_MASK);
4590 OPCODE(ROTATE_MASK);
4591 OPCODE(REPLICATE);
4592 OPCODE(JOIN_DWORDS);
4593 OPCODE(SPLAT);
4594 OPCODE(MERGE_HIGH);
4595 OPCODE(MERGE_LOW);
4596 OPCODE(SHL_DOUBLE);
4597 OPCODE(PERMUTE_DWORDS);
4598 OPCODE(PERMUTE);
4599 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004600 OPCODE(PACKS_CC);
4601 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004602 OPCODE(UNPACK_HIGH);
4603 OPCODE(UNPACKL_HIGH);
4604 OPCODE(UNPACK_LOW);
4605 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004606 OPCODE(VSHL_BY_SCALAR);
4607 OPCODE(VSRL_BY_SCALAR);
4608 OPCODE(VSRA_BY_SCALAR);
4609 OPCODE(VSUM);
4610 OPCODE(VICMPE);
4611 OPCODE(VICMPH);
4612 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004613 OPCODE(VICMPES);
4614 OPCODE(VICMPHS);
4615 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004616 OPCODE(VFCMPE);
4617 OPCODE(VFCMPH);
4618 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004619 OPCODE(VFCMPES);
4620 OPCODE(VFCMPHS);
4621 OPCODE(VFCMPHES);
4622 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004623 OPCODE(VEXTEND);
4624 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004625 OPCODE(VTM);
4626 OPCODE(VFAE_CC);
4627 OPCODE(VFAEZ_CC);
4628 OPCODE(VFEE_CC);
4629 OPCODE(VFEEZ_CC);
4630 OPCODE(VFENE_CC);
4631 OPCODE(VFENEZ_CC);
4632 OPCODE(VISTR_CC);
4633 OPCODE(VSTRC_CC);
4634 OPCODE(VSTRCZ_CC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004635 OPCODE(ATOMIC_SWAPW);
4636 OPCODE(ATOMIC_LOADW_ADD);
4637 OPCODE(ATOMIC_LOADW_SUB);
4638 OPCODE(ATOMIC_LOADW_AND);
4639 OPCODE(ATOMIC_LOADW_OR);
4640 OPCODE(ATOMIC_LOADW_XOR);
4641 OPCODE(ATOMIC_LOADW_NAND);
4642 OPCODE(ATOMIC_LOADW_MIN);
4643 OPCODE(ATOMIC_LOADW_MAX);
4644 OPCODE(ATOMIC_LOADW_UMIN);
4645 OPCODE(ATOMIC_LOADW_UMAX);
4646 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004647 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004648 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004649 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004650#undef OPCODE
4651}
4652
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004653// Return true if VT is a vector whose elements are a whole number of bytes
4654// in width.
4655static bool canTreatAsByteVector(EVT VT) {
4656 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4657}
4658
4659// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4660// producing a result of type ResVT. Op is a possibly bitcast version
4661// of the input vector and Index is the index (based on type VecVT) that
4662// should be extracted. Return the new extraction if a simplification
4663// was possible or if Force is true.
4664SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4665 SDValue Op, unsigned Index,
4666 DAGCombinerInfo &DCI,
4667 bool Force) const {
4668 SelectionDAG &DAG = DCI.DAG;
4669
4670 // The number of bytes being extracted.
4671 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4672
4673 for (;;) {
4674 unsigned Opcode = Op.getOpcode();
4675 if (Opcode == ISD::BITCAST)
4676 // Look through bitcasts.
4677 Op = Op.getOperand(0);
4678 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4679 canTreatAsByteVector(Op.getValueType())) {
4680 // Get a VPERM-like permute mask and see whether the bytes covered
4681 // by the extracted element are a contiguous sequence from one
4682 // source operand.
4683 SmallVector<int, SystemZ::VectorBytes> Bytes;
4684 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4685 int First;
4686 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4687 BytesPerElement, First))
4688 break;
4689 if (First < 0)
4690 return DAG.getUNDEF(ResVT);
4691 // Make sure the contiguous sequence starts at a multiple of the
4692 // original element size.
4693 unsigned Byte = unsigned(First) % Bytes.size();
4694 if (Byte % BytesPerElement != 0)
4695 break;
4696 // We can get the extracted value directly from an input.
4697 Index = Byte / BytesPerElement;
4698 Op = Op.getOperand(unsigned(First) / Bytes.size());
4699 Force = true;
4700 } else if (Opcode == ISD::BUILD_VECTOR &&
4701 canTreatAsByteVector(Op.getValueType())) {
4702 // We can only optimize this case if the BUILD_VECTOR elements are
4703 // at least as wide as the extracted value.
4704 EVT OpVT = Op.getValueType();
4705 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4706 if (OpBytesPerElement < BytesPerElement)
4707 break;
4708 // Make sure that the least-significant bit of the extracted value
4709 // is the least significant bit of an input.
4710 unsigned End = (Index + 1) * BytesPerElement;
4711 if (End % OpBytesPerElement != 0)
4712 break;
4713 // We're extracting the low part of one operand of the BUILD_VECTOR.
4714 Op = Op.getOperand(End / OpBytesPerElement - 1);
4715 if (!Op.getValueType().isInteger()) {
4716 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4717 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4718 DCI.AddToWorklist(Op.getNode());
4719 }
4720 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4721 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4722 if (VT != ResVT) {
4723 DCI.AddToWorklist(Op.getNode());
4724 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4725 }
4726 return Op;
4727 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004728 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4729 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4730 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004731 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4732 // Make sure that only the unextended bits are significant.
4733 EVT ExtVT = Op.getValueType();
4734 EVT OpVT = Op.getOperand(0).getValueType();
4735 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4736 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4737 unsigned Byte = Index * BytesPerElement;
4738 unsigned SubByte = Byte % ExtBytesPerElement;
4739 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4740 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004741 SubByte + BytesPerElement > ExtBytesPerElement)
4742 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004743 // Get the byte offset of the unextended element
4744 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4745 // ...then add the byte offset relative to that element.
4746 Byte += SubByte - MinSubByte;
4747 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004748 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004749 Op = Op.getOperand(0);
4750 Index = Byte / BytesPerElement;
4751 Force = true;
4752 } else
4753 break;
4754 }
4755 if (Force) {
4756 if (Op.getValueType() != VecVT) {
4757 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4758 DCI.AddToWorklist(Op.getNode());
4759 }
4760 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4761 DAG.getConstant(Index, DL, MVT::i32));
4762 }
4763 return SDValue();
4764}
4765
4766// Optimize vector operations in scalar value Op on the basis that Op
4767// is truncated to TruncVT.
4768SDValue
4769SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4770 DAGCombinerInfo &DCI) const {
4771 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4772 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4773 // of type TruncVT.
4774 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4775 TruncVT.getSizeInBits() % 8 == 0) {
4776 SDValue Vec = Op.getOperand(0);
4777 EVT VecVT = Vec.getValueType();
4778 if (canTreatAsByteVector(VecVT)) {
4779 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4780 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4781 unsigned TruncBytes = TruncVT.getStoreSize();
4782 if (BytesPerElement % TruncBytes == 0) {
4783 // Calculate the value of Y' in the above description. We are
4784 // splitting the original elements into Scale equal-sized pieces
4785 // and for truncation purposes want the last (least-significant)
4786 // of these pieces for IndexN. This is easiest to do by calculating
4787 // the start index of the following element and then subtracting 1.
4788 unsigned Scale = BytesPerElement / TruncBytes;
4789 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4790
4791 // Defer the creation of the bitcast from X to combineExtract,
4792 // which might be able to optimize the extraction.
4793 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4794 VecVT.getStoreSize() / TruncBytes);
4795 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4796 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4797 }
4798 }
4799 }
4800 }
4801 return SDValue();
4802}
4803
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004804SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4805 DAGCombinerInfo &DCI) const {
4806 SelectionDAG &DAG = DCI.DAG;
4807 unsigned Opcode = N->getOpcode();
4808 if (Opcode == ISD::SIGN_EXTEND) {
4809 // Convert (sext (ashr (shl X, C1), C2)) to
4810 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4811 // cheap as narrower ones.
4812 SDValue N0 = N->getOperand(0);
4813 EVT VT = N->getValueType(0);
4814 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4815 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4816 SDValue Inner = N0.getOperand(0);
4817 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4818 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4819 unsigned Extra = (VT.getSizeInBits() -
4820 N0.getValueType().getSizeInBits());
4821 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4822 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4823 EVT ShiftVT = N0.getOperand(1).getValueType();
4824 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4825 Inner.getOperand(0));
4826 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004827 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4828 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004829 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004830 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004831 }
4832 }
4833 }
4834 }
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004835 if (Opcode == SystemZISD::MERGE_HIGH ||
4836 Opcode == SystemZISD::MERGE_LOW) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004837 SDValue Op0 = N->getOperand(0);
4838 SDValue Op1 = N->getOperand(1);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004839 if (Op0.getOpcode() == ISD::BITCAST)
4840 Op0 = Op0.getOperand(0);
4841 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4842 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4843 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4844 // for v4f32.
4845 if (Op1 == N->getOperand(0))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004846 return Op1;
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004847 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4848 EVT VT = Op1.getValueType();
4849 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4850 if (ElemBytes <= 4) {
4851 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4852 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4853 EVT InVT = VT.changeVectorElementTypeToInteger();
4854 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4855 SystemZ::VectorBytes / ElemBytes / 2);
4856 if (VT != InVT) {
4857 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4858 DCI.AddToWorklist(Op1.getNode());
4859 }
4860 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4861 DCI.AddToWorklist(Op.getNode());
4862 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4863 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004864 }
4865 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004866 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4867 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4868 // If X has wider elements then convert it to:
4869 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4870 if (Opcode == ISD::STORE) {
4871 auto *SN = cast<StoreSDNode>(N);
4872 EVT MemVT = SN->getMemoryVT();
4873 if (MemVT.isInteger()) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004874 if (SDValue Value =
4875 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004876 DCI.AddToWorklist(Value.getNode());
4877
4878 // Rewrite the store with the new form of stored value.
4879 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4880 SN->getBasePtr(), SN->getMemoryVT(),
4881 SN->getMemOperand());
4882 }
4883 }
4884 }
4885 // Try to simplify a vector extraction.
4886 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4887 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4888 SDValue Op0 = N->getOperand(0);
4889 EVT VecVT = Op0.getValueType();
4890 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4891 IndexN->getZExtValue(), DCI, false);
4892 }
4893 }
4894 // (join_dwords X, X) == (replicate X)
4895 if (Opcode == SystemZISD::JOIN_DWORDS &&
4896 N->getOperand(0) == N->getOperand(1))
4897 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4898 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004899 // (fround (extract_vector_elt X 0))
4900 // (fround (extract_vector_elt X 1)) ->
4901 // (extract_vector_elt (VROUND X) 0)
4902 // (extract_vector_elt (VROUND X) 1)
4903 //
4904 // This is a special case since the target doesn't really support v2f32s.
4905 if (Opcode == ISD::FP_ROUND) {
4906 SDValue Op0 = N->getOperand(0);
4907 if (N->getValueType(0) == MVT::f32 &&
4908 Op0.hasOneUse() &&
4909 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4910 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4911 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4912 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4913 SDValue Vec = Op0.getOperand(0);
4914 for (auto *U : Vec->uses()) {
4915 if (U != Op0.getNode() &&
4916 U->hasOneUse() &&
4917 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4918 U->getOperand(0) == Vec &&
4919 U->getOperand(1).getOpcode() == ISD::Constant &&
4920 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4921 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4922 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4923 OtherRound.getOperand(0) == SDValue(U, 0) &&
4924 OtherRound.getValueType() == MVT::f32) {
4925 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4926 MVT::v4f32, Vec);
4927 DCI.AddToWorklist(VRound.getNode());
4928 SDValue Extract1 =
4929 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4930 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4931 DCI.AddToWorklist(Extract1.getNode());
4932 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4933 SDValue Extract0 =
4934 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4935 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4936 return Extract0;
4937 }
4938 }
4939 }
4940 }
4941 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004942 return SDValue();
4943}
4944
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004945//===----------------------------------------------------------------------===//
4946// Custom insertion
4947//===----------------------------------------------------------------------===//
4948
4949// Create a new basic block after MBB.
4950static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4951 MachineFunction &MF = *MBB->getParent();
4952 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004953 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004954 return NewMBB;
4955}
4956
Richard Sandifordbe133a82013-08-28 09:01:51 +00004957// Split MBB after MI and return the new block (the one that contains
4958// instructions after MI).
4959static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4960 MachineBasicBlock *MBB) {
4961 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4962 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004963 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004964 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4965 return NewMBB;
4966}
4967
Richard Sandiford5e318f02013-08-27 09:54:29 +00004968// Split MBB before MI and return the new block (the one that contains MI).
4969static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4970 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004971 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004972 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004973 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4974 return NewMBB;
4975}
4976
Richard Sandiford5e318f02013-08-27 09:54:29 +00004977// Force base value Base into a register before MI. Return the register.
4978static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4979 const SystemZInstrInfo *TII) {
4980 if (Base.isReg())
4981 return Base.getReg();
4982
4983 MachineBasicBlock *MBB = MI->getParent();
4984 MachineFunction &MF = *MBB->getParent();
4985 MachineRegisterInfo &MRI = MF.getRegInfo();
4986
4987 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4988 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4989 .addOperand(Base).addImm(0).addReg(0);
4990 return Reg;
4991}
4992
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004993// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4994MachineBasicBlock *
4995SystemZTargetLowering::emitSelect(MachineInstr *MI,
4996 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004997 const SystemZInstrInfo *TII =
4998 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004999
5000 unsigned DestReg = MI->getOperand(0).getReg();
5001 unsigned TrueReg = MI->getOperand(1).getReg();
5002 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00005003 unsigned CCValid = MI->getOperand(3).getImm();
5004 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005005 DebugLoc DL = MI->getDebugLoc();
5006
5007 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005008 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005009 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5010
5011 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00005012 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005013 // # fallthrough to FalseMBB
5014 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005015 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5016 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005017 MBB->addSuccessor(JoinMBB);
5018 MBB->addSuccessor(FalseMBB);
5019
5020 // FalseMBB:
5021 // # fallthrough to JoinMBB
5022 MBB = FalseMBB;
5023 MBB->addSuccessor(JoinMBB);
5024
5025 // JoinMBB:
5026 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
5027 // ...
5028 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005029 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005030 .addReg(TrueReg).addMBB(StartMBB)
5031 .addReg(FalseReg).addMBB(FalseMBB);
5032
5033 MI->eraseFromParent();
5034 return JoinMBB;
5035}
5036
Richard Sandifordb86a8342013-06-27 09:27:40 +00005037// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5038// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005039// happen when the condition is false rather than true. If a STORE ON
5040// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00005041MachineBasicBlock *
5042SystemZTargetLowering::emitCondStore(MachineInstr *MI,
5043 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005044 unsigned StoreOpcode, unsigned STOCOpcode,
5045 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005046 const SystemZInstrInfo *TII =
5047 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005048
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005049 unsigned SrcReg = MI->getOperand(0).getReg();
5050 MachineOperand Base = MI->getOperand(1);
5051 int64_t Disp = MI->getOperand(2).getImm();
5052 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00005053 unsigned CCValid = MI->getOperand(4).getImm();
5054 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005055 DebugLoc DL = MI->getDebugLoc();
5056
5057 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5058
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005059 // Use STOCOpcode if possible. We could use different store patterns in
5060 // order to avoid matching the index register, but the performance trade-offs
5061 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005062 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005063 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005064 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005065 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00005066 .addReg(SrcReg).addOperand(Base).addImm(Disp)
5067 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005068 MI->eraseFromParent();
5069 return MBB;
5070 }
5071
Richard Sandifordb86a8342013-06-27 09:27:40 +00005072 // Get the condition needed to branch around the store.
5073 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005074 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005075
5076 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005077 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005078 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5079
5080 // StartMBB:
5081 // BRC CCMask, JoinMBB
5082 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005083 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005084 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5085 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005086 MBB->addSuccessor(JoinMBB);
5087 MBB->addSuccessor(FalseMBB);
5088
5089 // FalseMBB:
5090 // store %SrcReg, %Disp(%Index,%Base)
5091 // # fallthrough to JoinMBB
5092 MBB = FalseMBB;
5093 BuildMI(MBB, DL, TII->get(StoreOpcode))
5094 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5095 MBB->addSuccessor(JoinMBB);
5096
5097 MI->eraseFromParent();
5098 return JoinMBB;
5099}
5100
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005101// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5102// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5103// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5104// BitSize is the width of the field in bits, or 0 if this is a partword
5105// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5106// is one of the operands. Invert says whether the field should be
5107// inverted after performing BinOpcode (e.g. for NAND).
5108MachineBasicBlock *
5109SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
5110 MachineBasicBlock *MBB,
5111 unsigned BinOpcode,
5112 unsigned BitSize,
5113 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005114 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005115 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005116 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005117 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005118 bool IsSubWord = (BitSize < 32);
5119
5120 // Extract the operands. Base can be a register or a frame index.
5121 // Src2 can be a register or immediate.
5122 unsigned Dest = MI->getOperand(0).getReg();
5123 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5124 int64_t Disp = MI->getOperand(2).getImm();
5125 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
5126 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5127 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5128 DebugLoc DL = MI->getDebugLoc();
5129 if (IsSubWord)
5130 BitSize = MI->getOperand(6).getImm();
5131
5132 // Subword operations use 32-bit registers.
5133 const TargetRegisterClass *RC = (BitSize <= 32 ?
5134 &SystemZ::GR32BitRegClass :
5135 &SystemZ::GR64BitRegClass);
5136 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5137 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5138
5139 // Get the right opcodes for the displacement.
5140 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5141 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5142 assert(LOpcode && CSOpcode && "Displacement out of range");
5143
5144 // Create virtual registers for temporary results.
5145 unsigned OrigVal = MRI.createVirtualRegister(RC);
5146 unsigned OldVal = MRI.createVirtualRegister(RC);
5147 unsigned NewVal = (BinOpcode || IsSubWord ?
5148 MRI.createVirtualRegister(RC) : Src2.getReg());
5149 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5150 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5151
5152 // Insert a basic block for the main loop.
5153 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005154 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005155 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5156
5157 // StartMBB:
5158 // ...
5159 // %OrigVal = L Disp(%Base)
5160 // # fall through to LoopMMB
5161 MBB = StartMBB;
5162 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5163 .addOperand(Base).addImm(Disp).addReg(0);
5164 MBB->addSuccessor(LoopMBB);
5165
5166 // LoopMBB:
5167 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5168 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5169 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5170 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5171 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5172 // JNE LoopMBB
5173 // # fall through to DoneMMB
5174 MBB = LoopMBB;
5175 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5176 .addReg(OrigVal).addMBB(StartMBB)
5177 .addReg(Dest).addMBB(LoopMBB);
5178 if (IsSubWord)
5179 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5180 .addReg(OldVal).addReg(BitShift).addImm(0);
5181 if (Invert) {
5182 // Perform the operation normally and then invert every bit of the field.
5183 unsigned Tmp = MRI.createVirtualRegister(RC);
5184 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5185 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005186 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005187 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005188 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005189 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005190 else {
5191 // Use LCGR and add -1 to the result, which is more compact than
5192 // an XILF, XILH pair.
5193 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5194 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5195 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5196 .addReg(Tmp2).addImm(-1);
5197 }
5198 } else if (BinOpcode)
5199 // A simply binary operation.
5200 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5201 .addReg(RotatedOldVal).addOperand(Src2);
5202 else if (IsSubWord)
5203 // Use RISBG to rotate Src2 into position and use it to replace the
5204 // field in RotatedOldVal.
5205 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5206 .addReg(RotatedOldVal).addReg(Src2.getReg())
5207 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5208 if (IsSubWord)
5209 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5210 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5211 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5212 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005213 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5214 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005215 MBB->addSuccessor(LoopMBB);
5216 MBB->addSuccessor(DoneMBB);
5217
5218 MI->eraseFromParent();
5219 return DoneMBB;
5220}
5221
5222// Implement EmitInstrWithCustomInserter for pseudo
5223// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5224// instruction that should be used to compare the current field with the
5225// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5226// for when the current field should be kept. BitSize is the width of
5227// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5228MachineBasicBlock *
5229SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5230 MachineBasicBlock *MBB,
5231 unsigned CompareOpcode,
5232 unsigned KeepOldMask,
5233 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005234 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005235 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005236 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005237 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005238 bool IsSubWord = (BitSize < 32);
5239
5240 // Extract the operands. Base can be a register or a frame index.
5241 unsigned Dest = MI->getOperand(0).getReg();
5242 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5243 int64_t Disp = MI->getOperand(2).getImm();
5244 unsigned Src2 = MI->getOperand(3).getReg();
5245 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5246 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5247 DebugLoc DL = MI->getDebugLoc();
5248 if (IsSubWord)
5249 BitSize = MI->getOperand(6).getImm();
5250
5251 // Subword operations use 32-bit registers.
5252 const TargetRegisterClass *RC = (BitSize <= 32 ?
5253 &SystemZ::GR32BitRegClass :
5254 &SystemZ::GR64BitRegClass);
5255 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5256 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5257
5258 // Get the right opcodes for the displacement.
5259 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5260 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5261 assert(LOpcode && CSOpcode && "Displacement out of range");
5262
5263 // Create virtual registers for temporary results.
5264 unsigned OrigVal = MRI.createVirtualRegister(RC);
5265 unsigned OldVal = MRI.createVirtualRegister(RC);
5266 unsigned NewVal = MRI.createVirtualRegister(RC);
5267 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5268 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5269 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5270
5271 // Insert 3 basic blocks for the loop.
5272 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005273 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005274 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5275 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5276 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5277
5278 // StartMBB:
5279 // ...
5280 // %OrigVal = L Disp(%Base)
5281 // # fall through to LoopMMB
5282 MBB = StartMBB;
5283 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5284 .addOperand(Base).addImm(Disp).addReg(0);
5285 MBB->addSuccessor(LoopMBB);
5286
5287 // LoopMBB:
5288 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5289 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5290 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005291 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005292 MBB = LoopMBB;
5293 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5294 .addReg(OrigVal).addMBB(StartMBB)
5295 .addReg(Dest).addMBB(UpdateMBB);
5296 if (IsSubWord)
5297 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5298 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005299 BuildMI(MBB, DL, TII->get(CompareOpcode))
5300 .addReg(RotatedOldVal).addReg(Src2);
5301 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005302 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005303 MBB->addSuccessor(UpdateMBB);
5304 MBB->addSuccessor(UseAltMBB);
5305
5306 // UseAltMBB:
5307 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5308 // # fall through to UpdateMMB
5309 MBB = UseAltMBB;
5310 if (IsSubWord)
5311 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5312 .addReg(RotatedOldVal).addReg(Src2)
5313 .addImm(32).addImm(31 + BitSize).addImm(0);
5314 MBB->addSuccessor(UpdateMBB);
5315
5316 // UpdateMBB:
5317 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5318 // [ %RotatedAltVal, UseAltMBB ]
5319 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5320 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5321 // JNE LoopMBB
5322 // # fall through to DoneMMB
5323 MBB = UpdateMBB;
5324 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5325 .addReg(RotatedOldVal).addMBB(LoopMBB)
5326 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5327 if (IsSubWord)
5328 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5329 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5330 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5331 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005332 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5333 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005334 MBB->addSuccessor(LoopMBB);
5335 MBB->addSuccessor(DoneMBB);
5336
5337 MI->eraseFromParent();
5338 return DoneMBB;
5339}
5340
5341// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5342// instruction MI.
5343MachineBasicBlock *
5344SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5345 MachineBasicBlock *MBB) const {
Ulrich Weiganda9ac6d62016-04-04 12:45:44 +00005346
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005347 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005348 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005349 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005350 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005351
5352 // Extract the operands. Base can be a register or a frame index.
5353 unsigned Dest = MI->getOperand(0).getReg();
5354 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5355 int64_t Disp = MI->getOperand(2).getImm();
5356 unsigned OrigCmpVal = MI->getOperand(3).getReg();
5357 unsigned OrigSwapVal = MI->getOperand(4).getReg();
5358 unsigned BitShift = MI->getOperand(5).getReg();
5359 unsigned NegBitShift = MI->getOperand(6).getReg();
5360 int64_t BitSize = MI->getOperand(7).getImm();
5361 DebugLoc DL = MI->getDebugLoc();
5362
5363 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5364
5365 // Get the right opcodes for the displacement.
5366 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5367 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5368 assert(LOpcode && CSOpcode && "Displacement out of range");
5369
5370 // Create virtual registers for temporary results.
5371 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5372 unsigned OldVal = MRI.createVirtualRegister(RC);
5373 unsigned CmpVal = MRI.createVirtualRegister(RC);
5374 unsigned SwapVal = MRI.createVirtualRegister(RC);
5375 unsigned StoreVal = MRI.createVirtualRegister(RC);
5376 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5377 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5378 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5379
5380 // Insert 2 basic blocks for the loop.
5381 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005382 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005383 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5384 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5385
5386 // StartMBB:
5387 // ...
5388 // %OrigOldVal = L Disp(%Base)
5389 // # fall through to LoopMMB
5390 MBB = StartMBB;
5391 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5392 .addOperand(Base).addImm(Disp).addReg(0);
5393 MBB->addSuccessor(LoopMBB);
5394
5395 // LoopMBB:
5396 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5397 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5398 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5399 // %Dest = RLL %OldVal, BitSize(%BitShift)
5400 // ^^ The low BitSize bits contain the field
5401 // of interest.
5402 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5403 // ^^ Replace the upper 32-BitSize bits of the
5404 // comparison value with those that we loaded,
5405 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005406 // CR %Dest, %RetryCmpVal
5407 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005408 // # Fall through to SetMBB
5409 MBB = LoopMBB;
5410 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5411 .addReg(OrigOldVal).addMBB(StartMBB)
5412 .addReg(RetryOldVal).addMBB(SetMBB);
5413 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5414 .addReg(OrigCmpVal).addMBB(StartMBB)
5415 .addReg(RetryCmpVal).addMBB(SetMBB);
5416 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5417 .addReg(OrigSwapVal).addMBB(StartMBB)
5418 .addReg(RetrySwapVal).addMBB(SetMBB);
5419 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5420 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5421 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5422 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005423 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5424 .addReg(Dest).addReg(RetryCmpVal);
5425 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005426 .addImm(SystemZ::CCMASK_ICMP)
5427 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005428 MBB->addSuccessor(DoneMBB);
5429 MBB->addSuccessor(SetMBB);
5430
5431 // SetMBB:
5432 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5433 // ^^ Replace the upper 32-BitSize bits of the new
5434 // value with those that we loaded.
5435 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5436 // ^^ Rotate the new field to its proper position.
5437 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5438 // JNE LoopMBB
5439 // # fall through to ExitMMB
5440 MBB = SetMBB;
5441 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5442 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5443 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5444 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5445 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5446 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005447 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5448 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005449 MBB->addSuccessor(LoopMBB);
5450 MBB->addSuccessor(DoneMBB);
5451
5452 MI->eraseFromParent();
5453 return DoneMBB;
5454}
5455
5456// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5457// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005458// it's "don't care". SubReg is subreg_l32 when extending a GR32
5459// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005460MachineBasicBlock *
5461SystemZTargetLowering::emitExt128(MachineInstr *MI,
5462 MachineBasicBlock *MBB,
5463 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005464 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005465 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005466 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005467 MachineRegisterInfo &MRI = MF.getRegInfo();
5468 DebugLoc DL = MI->getDebugLoc();
5469
5470 unsigned Dest = MI->getOperand(0).getReg();
5471 unsigned Src = MI->getOperand(1).getReg();
5472 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5473
5474 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5475 if (ClearEven) {
5476 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5477 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5478
5479 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5480 .addImm(0);
5481 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005482 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005483 In128 = NewIn128;
5484 }
5485 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5486 .addReg(In128).addReg(Src).addImm(SubReg);
5487
5488 MI->eraseFromParent();
5489 return MBB;
5490}
5491
Richard Sandifordd131ff82013-07-08 09:35:23 +00005492MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00005493SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5494 MachineBasicBlock *MBB,
5495 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005496 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005497 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005498 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005499 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005500 DebugLoc DL = MI->getDebugLoc();
5501
Richard Sandiford5e318f02013-08-27 09:54:29 +00005502 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005503 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005504 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005505 uint64_t SrcDisp = MI->getOperand(3).getImm();
5506 uint64_t Length = MI->getOperand(4).getImm();
5507
Richard Sandifordbe133a82013-08-28 09:01:51 +00005508 // When generating more than one CLC, all but the last will need to
5509 // branch to the end when a difference is found.
5510 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005511 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005512
Richard Sandiford5e318f02013-08-27 09:54:29 +00005513 // Check for the loop form, in which operand 5 is the trip count.
5514 if (MI->getNumExplicitOperands() > 5) {
5515 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5516
5517 uint64_t StartCountReg = MI->getOperand(5).getReg();
5518 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5519 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5520 forceReg(MI, DestBase, TII));
5521
5522 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5523 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5524 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5525 MRI.createVirtualRegister(RC));
5526 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5527 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5528 MRI.createVirtualRegister(RC));
5529
5530 RC = &SystemZ::GR64BitRegClass;
5531 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5532 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5533
5534 MachineBasicBlock *StartMBB = MBB;
5535 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5536 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005537 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005538
5539 // StartMBB:
5540 // # fall through to LoopMMB
5541 MBB->addSuccessor(LoopMBB);
5542
5543 // LoopMBB:
5544 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005545 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005546 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005547 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005548 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005549 // [ %NextCountReg, NextMBB ]
5550 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005551 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005552 // ( JLH EndMBB )
5553 //
5554 // The prefetch is used only for MVC. The JLH is used only for CLC.
5555 MBB = LoopMBB;
5556
5557 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5558 .addReg(StartDestReg).addMBB(StartMBB)
5559 .addReg(NextDestReg).addMBB(NextMBB);
5560 if (!HaveSingleBase)
5561 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5562 .addReg(StartSrcReg).addMBB(StartMBB)
5563 .addReg(NextSrcReg).addMBB(NextMBB);
5564 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5565 .addReg(StartCountReg).addMBB(StartMBB)
5566 .addReg(NextCountReg).addMBB(NextMBB);
5567 if (Opcode == SystemZ::MVC)
5568 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5569 .addImm(SystemZ::PFD_WRITE)
5570 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5571 BuildMI(MBB, DL, TII->get(Opcode))
5572 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5573 .addReg(ThisSrcReg).addImm(SrcDisp);
5574 if (EndMBB) {
5575 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5576 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5577 .addMBB(EndMBB);
5578 MBB->addSuccessor(EndMBB);
5579 MBB->addSuccessor(NextMBB);
5580 }
5581
5582 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005583 // %NextDestReg = LA 256(%ThisDestReg)
5584 // %NextSrcReg = LA 256(%ThisSrcReg)
5585 // %NextCountReg = AGHI %ThisCountReg, -1
5586 // CGHI %NextCountReg, 0
5587 // JLH LoopMBB
5588 // # fall through to DoneMMB
5589 //
5590 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005591 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005592
Richard Sandiford5e318f02013-08-27 09:54:29 +00005593 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5594 .addReg(ThisDestReg).addImm(256).addReg(0);
5595 if (!HaveSingleBase)
5596 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5597 .addReg(ThisSrcReg).addImm(256).addReg(0);
5598 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5599 .addReg(ThisCountReg).addImm(-1);
5600 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5601 .addReg(NextCountReg).addImm(0);
5602 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5603 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5604 .addMBB(LoopMBB);
5605 MBB->addSuccessor(LoopMBB);
5606 MBB->addSuccessor(DoneMBB);
5607
5608 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5609 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5610 Length &= 255;
5611 MBB = DoneMBB;
5612 }
5613 // Handle any remaining bytes with straight-line code.
5614 while (Length > 0) {
5615 uint64_t ThisLength = std::min(Length, uint64_t(256));
5616 // The previous iteration might have created out-of-range displacements.
5617 // Apply them using LAY if so.
5618 if (!isUInt<12>(DestDisp)) {
5619 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5620 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5621 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5622 DestBase = MachineOperand::CreateReg(Reg, false);
5623 DestDisp = 0;
5624 }
5625 if (!isUInt<12>(SrcDisp)) {
5626 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5627 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5628 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5629 SrcBase = MachineOperand::CreateReg(Reg, false);
5630 SrcDisp = 0;
5631 }
5632 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5633 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5634 .addOperand(SrcBase).addImm(SrcDisp);
5635 DestDisp += ThisLength;
5636 SrcDisp += ThisLength;
5637 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005638 // If there's another CLC to go, branch to the end if a difference
5639 // was found.
5640 if (EndMBB && Length > 0) {
5641 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5642 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5643 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5644 .addMBB(EndMBB);
5645 MBB->addSuccessor(EndMBB);
5646 MBB->addSuccessor(NextMBB);
5647 MBB = NextMBB;
5648 }
5649 }
5650 if (EndMBB) {
5651 MBB->addSuccessor(EndMBB);
5652 MBB = EndMBB;
5653 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005654 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005655
5656 MI->eraseFromParent();
5657 return MBB;
5658}
5659
Richard Sandifordca232712013-08-16 11:21:54 +00005660// Decompose string pseudo-instruction MI into a loop that continually performs
5661// Opcode until CC != 3.
5662MachineBasicBlock *
5663SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5664 MachineBasicBlock *MBB,
5665 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005666 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005667 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005668 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005669 MachineRegisterInfo &MRI = MF.getRegInfo();
5670 DebugLoc DL = MI->getDebugLoc();
5671
5672 uint64_t End1Reg = MI->getOperand(0).getReg();
5673 uint64_t Start1Reg = MI->getOperand(1).getReg();
5674 uint64_t Start2Reg = MI->getOperand(2).getReg();
5675 uint64_t CharReg = MI->getOperand(3).getReg();
5676
5677 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5678 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5679 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5680 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5681
5682 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005683 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005684 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5685
5686 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005687 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005688 MBB->addSuccessor(LoopMBB);
5689
5690 // LoopMBB:
5691 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5692 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005693 // R0L = %CharReg
5694 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005695 // JO LoopMBB
5696 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005697 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005698 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005699 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005700
5701 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5702 .addReg(Start1Reg).addMBB(StartMBB)
5703 .addReg(End1Reg).addMBB(LoopMBB);
5704 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5705 .addReg(Start2Reg).addMBB(StartMBB)
5706 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005707 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005708 BuildMI(MBB, DL, TII->get(Opcode))
5709 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5710 .addReg(This1Reg).addReg(This2Reg);
5711 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5712 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5713 MBB->addSuccessor(LoopMBB);
5714 MBB->addSuccessor(DoneMBB);
5715
5716 DoneMBB->addLiveIn(SystemZ::CC);
5717
5718 MI->eraseFromParent();
5719 return DoneMBB;
5720}
5721
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005722// Update TBEGIN instruction with final opcode and register clobbers.
5723MachineBasicBlock *
5724SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5725 MachineBasicBlock *MBB,
5726 unsigned Opcode,
5727 bool NoFloat) const {
5728 MachineFunction &MF = *MBB->getParent();
5729 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5730 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5731
5732 // Update opcode.
5733 MI->setDesc(TII->get(Opcode));
5734
5735 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5736 // Make sure to add the corresponding GRSM bits if they are missing.
5737 uint64_t Control = MI->getOperand(2).getImm();
5738 static const unsigned GPRControlBit[16] = {
5739 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5740 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5741 };
5742 Control |= GPRControlBit[15];
5743 if (TFI->hasFP(MF))
5744 Control |= GPRControlBit[11];
5745 MI->getOperand(2).setImm(Control);
5746
5747 // Add GPR clobbers.
5748 for (int I = 0; I < 16; I++) {
5749 if ((Control & GPRControlBit[I]) == 0) {
5750 unsigned Reg = SystemZMC::GR64Regs[I];
5751 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5752 }
5753 }
5754
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005755 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005756 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005757 if (Subtarget.hasVector()) {
5758 for (int I = 0; I < 32; I++) {
5759 unsigned Reg = SystemZMC::VR128Regs[I];
5760 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5761 }
5762 } else {
5763 for (int I = 0; I < 16; I++) {
5764 unsigned Reg = SystemZMC::FP64Regs[I];
5765 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5766 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005767 }
5768 }
5769
5770 return MBB;
5771}
5772
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005773MachineBasicBlock *
5774SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
NAKAMURA Takumi50df0c22015-11-02 01:38:12 +00005775 MachineBasicBlock *MBB,
5776 unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005777 MachineFunction &MF = *MBB->getParent();
5778 MachineRegisterInfo *MRI = &MF.getRegInfo();
5779 const SystemZInstrInfo *TII =
5780 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5781 DebugLoc DL = MI->getDebugLoc();
5782
5783 unsigned SrcReg = MI->getOperand(0).getReg();
5784
5785 // Create new virtual register of the same class as source.
5786 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5787 unsigned DstReg = MRI->createVirtualRegister(RC);
5788
5789 // Replace pseudo with a normal load-and-test that models the def as
5790 // well.
5791 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5792 .addReg(SrcReg);
5793 MI->eraseFromParent();
5794
5795 return MBB;
5796}
5797
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005798MachineBasicBlock *SystemZTargetLowering::
5799EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5800 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005801 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005802 case SystemZ::Select32:
5803 case SystemZ::SelectF32:
5804 case SystemZ::Select64:
5805 case SystemZ::SelectF64:
5806 case SystemZ::SelectF128:
5807 return emitSelect(MI, MBB);
5808
Richard Sandiford2896d042013-10-01 14:33:55 +00005809 case SystemZ::CondStore8Mux:
5810 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5811 case SystemZ::CondStore8MuxInv:
5812 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5813 case SystemZ::CondStore16Mux:
5814 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5815 case SystemZ::CondStore16MuxInv:
5816 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005817 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005818 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005819 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005820 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005821 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005822 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005823 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005824 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005825 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005826 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005827 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005828 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005829 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005830 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005831 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005832 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005833 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005834 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005835 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005836 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005837 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005838 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005839 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005840 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005841
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005842 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005843 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005844 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005845 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005846 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005847 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005848
5849 case SystemZ::ATOMIC_SWAPW:
5850 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5851 case SystemZ::ATOMIC_SWAP_32:
5852 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5853 case SystemZ::ATOMIC_SWAP_64:
5854 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5855
5856 case SystemZ::ATOMIC_LOADW_AR:
5857 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5858 case SystemZ::ATOMIC_LOADW_AFI:
5859 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5860 case SystemZ::ATOMIC_LOAD_AR:
5861 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5862 case SystemZ::ATOMIC_LOAD_AHI:
5863 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5864 case SystemZ::ATOMIC_LOAD_AFI:
5865 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5866 case SystemZ::ATOMIC_LOAD_AGR:
5867 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5868 case SystemZ::ATOMIC_LOAD_AGHI:
5869 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5870 case SystemZ::ATOMIC_LOAD_AGFI:
5871 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5872
5873 case SystemZ::ATOMIC_LOADW_SR:
5874 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5875 case SystemZ::ATOMIC_LOAD_SR:
5876 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5877 case SystemZ::ATOMIC_LOAD_SGR:
5878 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5879
5880 case SystemZ::ATOMIC_LOADW_NR:
5881 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5882 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005883 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005884 case SystemZ::ATOMIC_LOAD_NR:
5885 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005886 case SystemZ::ATOMIC_LOAD_NILL:
5887 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5888 case SystemZ::ATOMIC_LOAD_NILH:
5889 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5890 case SystemZ::ATOMIC_LOAD_NILF:
5891 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005892 case SystemZ::ATOMIC_LOAD_NGR:
5893 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005894 case SystemZ::ATOMIC_LOAD_NILL64:
5895 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5896 case SystemZ::ATOMIC_LOAD_NILH64:
5897 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005898 case SystemZ::ATOMIC_LOAD_NIHL64:
5899 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5900 case SystemZ::ATOMIC_LOAD_NIHH64:
5901 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005902 case SystemZ::ATOMIC_LOAD_NILF64:
5903 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005904 case SystemZ::ATOMIC_LOAD_NIHF64:
5905 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005906
5907 case SystemZ::ATOMIC_LOADW_OR:
5908 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5909 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005910 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005911 case SystemZ::ATOMIC_LOAD_OR:
5912 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005913 case SystemZ::ATOMIC_LOAD_OILL:
5914 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5915 case SystemZ::ATOMIC_LOAD_OILH:
5916 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5917 case SystemZ::ATOMIC_LOAD_OILF:
5918 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005919 case SystemZ::ATOMIC_LOAD_OGR:
5920 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005921 case SystemZ::ATOMIC_LOAD_OILL64:
5922 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5923 case SystemZ::ATOMIC_LOAD_OILH64:
5924 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005925 case SystemZ::ATOMIC_LOAD_OIHL64:
5926 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5927 case SystemZ::ATOMIC_LOAD_OIHH64:
5928 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005929 case SystemZ::ATOMIC_LOAD_OILF64:
5930 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005931 case SystemZ::ATOMIC_LOAD_OIHF64:
5932 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005933
5934 case SystemZ::ATOMIC_LOADW_XR:
5935 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5936 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005937 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005938 case SystemZ::ATOMIC_LOAD_XR:
5939 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005940 case SystemZ::ATOMIC_LOAD_XILF:
5941 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005942 case SystemZ::ATOMIC_LOAD_XGR:
5943 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005944 case SystemZ::ATOMIC_LOAD_XILF64:
5945 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005946 case SystemZ::ATOMIC_LOAD_XIHF64:
5947 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005948
5949 case SystemZ::ATOMIC_LOADW_NRi:
5950 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5951 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005952 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005953 case SystemZ::ATOMIC_LOAD_NRi:
5954 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005955 case SystemZ::ATOMIC_LOAD_NILLi:
5956 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5957 case SystemZ::ATOMIC_LOAD_NILHi:
5958 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5959 case SystemZ::ATOMIC_LOAD_NILFi:
5960 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005961 case SystemZ::ATOMIC_LOAD_NGRi:
5962 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005963 case SystemZ::ATOMIC_LOAD_NILL64i:
5964 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5965 case SystemZ::ATOMIC_LOAD_NILH64i:
5966 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005967 case SystemZ::ATOMIC_LOAD_NIHL64i:
5968 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5969 case SystemZ::ATOMIC_LOAD_NIHH64i:
5970 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005971 case SystemZ::ATOMIC_LOAD_NILF64i:
5972 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005973 case SystemZ::ATOMIC_LOAD_NIHF64i:
5974 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005975
5976 case SystemZ::ATOMIC_LOADW_MIN:
5977 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5978 SystemZ::CCMASK_CMP_LE, 0);
5979 case SystemZ::ATOMIC_LOAD_MIN_32:
5980 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5981 SystemZ::CCMASK_CMP_LE, 32);
5982 case SystemZ::ATOMIC_LOAD_MIN_64:
5983 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5984 SystemZ::CCMASK_CMP_LE, 64);
5985
5986 case SystemZ::ATOMIC_LOADW_MAX:
5987 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5988 SystemZ::CCMASK_CMP_GE, 0);
5989 case SystemZ::ATOMIC_LOAD_MAX_32:
5990 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5991 SystemZ::CCMASK_CMP_GE, 32);
5992 case SystemZ::ATOMIC_LOAD_MAX_64:
5993 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5994 SystemZ::CCMASK_CMP_GE, 64);
5995
5996 case SystemZ::ATOMIC_LOADW_UMIN:
5997 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5998 SystemZ::CCMASK_CMP_LE, 0);
5999 case SystemZ::ATOMIC_LOAD_UMIN_32:
6000 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6001 SystemZ::CCMASK_CMP_LE, 32);
6002 case SystemZ::ATOMIC_LOAD_UMIN_64:
6003 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6004 SystemZ::CCMASK_CMP_LE, 64);
6005
6006 case SystemZ::ATOMIC_LOADW_UMAX:
6007 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6008 SystemZ::CCMASK_CMP_GE, 0);
6009 case SystemZ::ATOMIC_LOAD_UMAX_32:
6010 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
6011 SystemZ::CCMASK_CMP_GE, 32);
6012 case SystemZ::ATOMIC_LOAD_UMAX_64:
6013 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
6014 SystemZ::CCMASK_CMP_GE, 64);
6015
6016 case SystemZ::ATOMIC_CMP_SWAPW:
6017 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006018 case SystemZ::MVCSequence:
6019 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006020 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00006021 case SystemZ::NCSequence:
6022 case SystemZ::NCLoop:
6023 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
6024 case SystemZ::OCSequence:
6025 case SystemZ::OCLoop:
6026 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
6027 case SystemZ::XCSequence:
6028 case SystemZ::XCLoop:
6029 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00006030 case SystemZ::CLCSequence:
6031 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00006032 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00006033 case SystemZ::CLSTLoop:
6034 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00006035 case SystemZ::MVSTLoop:
6036 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006037 case SystemZ::SRSTLoop:
6038 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006039 case SystemZ::TBEGIN:
6040 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6041 case SystemZ::TBEGIN_nofloat:
6042 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6043 case SystemZ::TBEGINC:
6044 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006045 case SystemZ::LTEBRCompare_VecPseudo:
6046 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6047 case SystemZ::LTDBRCompare_VecPseudo:
6048 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6049 case SystemZ::LTXBRCompare_VecPseudo:
6050 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6051
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006052 default:
6053 llvm_unreachable("Unexpected instr type to insert");
6054 }
6055}