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