blob: 654f66a79667248689a285354935dc0cce933256 [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
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000219 // z10 has instructions for signed but not unsigned FP conversion.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000220 // Handle unsigned 32-bit types as signed 64-bit types.
Richard Sandiforddc6c2c92014-03-21 10:56:30 +0000221 if (!Subtarget.hasFPExtension()) {
222 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
223 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
224 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225
226 // We have native support for a 64-bit CTLZ, via FLOGR.
227 setOperationAction(ISD::CTLZ, MVT::i32, Promote);
228 setOperationAction(ISD::CTLZ, MVT::i64, Legal);
229
230 // Give LowerOperation the chance to replace 64-bit ORs with subregs.
231 setOperationAction(ISD::OR, MVT::i64, Custom);
232
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000233 // FIXME: Can we support these natively?
234 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
235 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
236 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
237
238 // We have native instructions for i8, i16 and i32 extensions, but not i1.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000239 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000240 for (MVT VT : MVT::integer_valuetypes()) {
241 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
242 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
243 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
244 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000245
246 // Handle the various types of symbolic address.
247 setOperationAction(ISD::ConstantPool, PtrVT, Custom);
248 setOperationAction(ISD::GlobalAddress, PtrVT, Custom);
249 setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
250 setOperationAction(ISD::BlockAddress, PtrVT, Custom);
251 setOperationAction(ISD::JumpTable, PtrVT, Custom);
252
253 // We need to handle dynamic allocations specially because of the
254 // 160-byte area at the bottom of the stack.
255 setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
256
257 // Use custom expanders so that we can force the function to use
258 // a frame pointer.
259 setOperationAction(ISD::STACKSAVE, MVT::Other, Custom);
260 setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
261
Richard Sandiford03481332013-08-23 11:36:42 +0000262 // Handle prefetches with PFD or PFDRL.
263 setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
264
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000265 for (MVT VT : MVT::vector_valuetypes()) {
266 // Assume by default that all vector operations need to be expanded.
267 for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
268 if (getOperationAction(Opcode, VT) == Legal)
269 setOperationAction(Opcode, VT, Expand);
270
271 // Likewise all truncating stores and extending loads.
272 for (MVT InnerVT : MVT::vector_valuetypes()) {
273 setTruncStoreAction(VT, InnerVT, Expand);
274 setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
275 setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
276 setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
277 }
278
279 if (isTypeLegal(VT)) {
280 // These operations are legal for anything that can be stored in a
281 // vector register, even if there is no native support for the format
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000282 // as such. In particular, we can do these for v4f32 even though there
283 // are no specific instructions for that format.
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000284 setOperationAction(ISD::LOAD, VT, Legal);
285 setOperationAction(ISD::STORE, VT, Legal);
286 setOperationAction(ISD::VSELECT, VT, Legal);
287 setOperationAction(ISD::BITCAST, VT, Legal);
288 setOperationAction(ISD::UNDEF, VT, Legal);
289
290 // Likewise, except that we need to replace the nodes with something
291 // more specific.
292 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
293 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
294 }
295 }
296
297 // Handle integer vector types.
298 for (MVT VT : MVT::integer_vector_valuetypes()) {
299 if (isTypeLegal(VT)) {
300 // These operations have direct equivalents.
301 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
302 setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
303 setOperationAction(ISD::ADD, VT, Legal);
304 setOperationAction(ISD::SUB, VT, Legal);
305 if (VT != MVT::v2i64)
306 setOperationAction(ISD::MUL, VT, Legal);
307 setOperationAction(ISD::AND, VT, Legal);
308 setOperationAction(ISD::OR, VT, Legal);
309 setOperationAction(ISD::XOR, VT, Legal);
310 setOperationAction(ISD::CTPOP, VT, Custom);
311 setOperationAction(ISD::CTTZ, VT, Legal);
312 setOperationAction(ISD::CTLZ, VT, Legal);
313 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
314 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
315
316 // Convert a GPR scalar to a vector by inserting it into element 0.
317 setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
318
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000319 // Use a series of unpacks for extensions.
320 setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
321 setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
322
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000323 // Detect shifts by a scalar amount and convert them into
324 // V*_BY_SCALAR.
325 setOperationAction(ISD::SHL, VT, Custom);
326 setOperationAction(ISD::SRA, VT, Custom);
327 setOperationAction(ISD::SRL, VT, Custom);
328
329 // At present ROTL isn't matched by DAGCombiner. ROTR should be
330 // converted into ROTL.
331 setOperationAction(ISD::ROTL, VT, Expand);
332 setOperationAction(ISD::ROTR, VT, Expand);
333
334 // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
335 // and inverting the result as necessary.
336 setOperationAction(ISD::SETCC, VT, Custom);
337 }
338 }
339
Ulrich Weigandcd808232015-05-05 19:26:48 +0000340 if (Subtarget.hasVector()) {
341 // There should be no need to check for float types other than v2f64
342 // since <2 x f32> isn't a legal type.
343 setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
344 setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
345 setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
346 setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
347 }
348
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349 // Handle floating-point types.
350 for (unsigned I = MVT::FIRST_FP_VALUETYPE;
351 I <= MVT::LAST_FP_VALUETYPE;
352 ++I) {
353 MVT VT = MVT::SimpleValueType(I);
354 if (isTypeLegal(VT)) {
355 // We can use FI for FRINT.
356 setOperationAction(ISD::FRINT, VT, Legal);
357
Richard Sandifordaf5f66a2013-08-21 09:04:20 +0000358 // We can use the extended form of FI for other rounding operations.
359 if (Subtarget.hasFPExtension()) {
360 setOperationAction(ISD::FNEARBYINT, VT, Legal);
361 setOperationAction(ISD::FFLOOR, VT, Legal);
362 setOperationAction(ISD::FCEIL, VT, Legal);
363 setOperationAction(ISD::FTRUNC, VT, Legal);
364 setOperationAction(ISD::FROUND, VT, Legal);
365 }
366
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000367 // No special instructions for these.
368 setOperationAction(ISD::FSIN, VT, Expand);
369 setOperationAction(ISD::FCOS, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000370 setOperationAction(ISD::FSINCOS, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000371 setOperationAction(ISD::FREM, VT, Expand);
Ulrich Weigand126caeb2015-09-21 17:35:45 +0000372 setOperationAction(ISD::FPOW, VT, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000373 }
374 }
375
Ulrich Weigandcd808232015-05-05 19:26:48 +0000376 // Handle floating-point vector types.
377 if (Subtarget.hasVector()) {
378 // Scalar-to-vector conversion is just a subreg.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000379 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000380 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
381
382 // Some insertions and extractions can be done directly but others
383 // need to go via integers.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000384 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000385 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Ulrich Weigandcd808232015-05-05 19:26:48 +0000387 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
388
389 // These operations have direct equivalents.
390 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
391 setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
392 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
393 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
394 setOperationAction(ISD::FMA, MVT::v2f64, Legal);
395 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
396 setOperationAction(ISD::FABS, MVT::v2f64, Legal);
397 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
398 setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
399 setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
400 setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
401 setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
402 setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
403 setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
404 }
405
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000406 // We have fused multiply-addition for f32 and f64 but not f128.
407 setOperationAction(ISD::FMA, MVT::f32, Legal);
408 setOperationAction(ISD::FMA, MVT::f64, Legal);
409 setOperationAction(ISD::FMA, MVT::f128, Expand);
410
411 // Needed so that we don't try to implement f128 constant loads using
412 // a load-and-extend of a f80 constant (in cases where the constant
413 // would fit in an f80).
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000414 for (MVT VT : MVT::fp_valuetypes())
415 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000416
417 // Floating-point truncation and stores need to be done separately.
418 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
419 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
420 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
421
422 // We have 64-bit FPR<->GPR moves, but need special handling for
423 // 32-bit forms.
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000424 if (!Subtarget.hasVector()) {
425 setOperationAction(ISD::BITCAST, MVT::i32, Custom);
426 setOperationAction(ISD::BITCAST, MVT::f32, Custom);
427 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000428
429 // VASTART and VACOPY need to deal with the SystemZ-specific varargs
430 // structure, but VAEND is a no-op.
431 setOperationAction(ISD::VASTART, MVT::Other, Custom);
432 setOperationAction(ISD::VACOPY, MVT::Other, Custom);
433 setOperationAction(ISD::VAEND, MVT::Other, Expand);
Richard Sandifordd131ff82013-07-08 09:35:23 +0000434
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000435 // Codes for which we want to perform some z-specific combinations.
436 setTargetDAGCombine(ISD::SIGN_EXTEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000437 setTargetDAGCombine(ISD::STORE);
438 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000439 setTargetDAGCombine(ISD::FP_ROUND);
Richard Sandiford95bc5f92014-03-07 11:34:35 +0000440
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000441 // Handle intrinsics.
442 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
Ulrich Weigandc1708b22015-05-05 19:31:09 +0000443 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Ulrich Weigand57c85f52015-04-01 12:51:43 +0000444
Richard Sandifordd131ff82013-07-08 09:35:23 +0000445 // We want to use MVC in preference to even a single load/store pair.
446 MaxStoresPerMemcpy = 0;
447 MaxStoresPerMemcpyOptSize = 0;
Richard Sandiford47660c12013-07-09 09:32:42 +0000448
449 // The main memset sequence is a byte store followed by an MVC.
450 // Two STC or MV..I stores win over that, but the kind of fused stores
451 // generated by target-independent code don't when the byte value is
452 // variable. E.g. "STC <reg>;MHI <reg>,257;STH <reg>" is not better
453 // than "STC;MVC". Handle the choice in target-specific code instead.
454 MaxStoresPerMemset = 0;
455 MaxStoresPerMemsetOptSize = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456}
457
Mehdi Amini44ede332015-07-09 02:09:04 +0000458EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
459 LLVMContext &, EVT VT) const {
Richard Sandifordabc010b2013-11-06 12:16:02 +0000460 if (!VT.isVector())
461 return MVT::i32;
462 return VT.changeVectorElementTypeToInteger();
463}
464
465bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
Stephen Lin73de7bf2013-07-09 18:16:56 +0000466 VT = VT.getScalarType();
467
468 if (!VT.isSimple())
469 return false;
470
471 switch (VT.getSimpleVT().SimpleTy) {
472 case MVT::f32:
473 case MVT::f64:
474 return true;
475 case MVT::f128:
476 return false;
477 default:
478 break;
479 }
480
481 return false;
482}
483
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000484bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
485 // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
486 return Imm.isZero() || Imm.isNegZero();
487}
488
Ulrich Weigand1f6666a2015-03-31 12:52:27 +0000489bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
490 // We can use CGFI or CLGFI.
491 return isInt<32>(Imm) || isUInt<32>(Imm);
492}
493
494bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
495 // We can use ALGFI or SLGFI.
496 return isUInt<32>(Imm) || isUInt<32>(-Imm);
497}
498
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000499bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
500 unsigned,
501 unsigned,
502 bool *Fast) const {
Richard Sandiford46af5a22013-05-30 09:45:42 +0000503 // Unaligned accesses should never be slower than the expanded version.
504 // We check specifically for aligned accesses in the few cases where
505 // they are required.
506 if (Fast)
507 *Fast = true;
508 return true;
509}
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000510
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000511bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
512 const AddrMode &AM, Type *Ty,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000513 unsigned AS) const {
Richard Sandiford791bea42013-07-31 12:58:26 +0000514 // Punt on globals for now, although they can be used in limited
515 // RELATIVE LONG cases.
516 if (AM.BaseGV)
517 return false;
518
519 // Require a 20-bit signed offset.
520 if (!isInt<20>(AM.BaseOffs))
521 return false;
522
523 // Indexing is OK but no scale factor can be applied.
524 return AM.Scale == 0 || AM.Scale == 1;
525}
526
Richard Sandiford709bda62013-08-19 12:42:31 +0000527bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
528 if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
529 return false;
530 unsigned FromBits = FromType->getPrimitiveSizeInBits();
531 unsigned ToBits = ToType->getPrimitiveSizeInBits();
532 return FromBits > ToBits;
533}
534
535bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
536 if (!FromVT.isInteger() || !ToVT.isInteger())
537 return false;
538 unsigned FromBits = FromVT.getSizeInBits();
539 unsigned ToBits = ToVT.getSizeInBits();
540 return FromBits > ToBits;
541}
542
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000543//===----------------------------------------------------------------------===//
544// Inline asm support
545//===----------------------------------------------------------------------===//
546
547TargetLowering::ConstraintType
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000548SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000549 if (Constraint.size() == 1) {
550 switch (Constraint[0]) {
551 case 'a': // Address register
552 case 'd': // Data register (equivalent to 'r')
553 case 'f': // Floating-point register
Richard Sandiford0755c932013-10-01 11:26:28 +0000554 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000555 case 'r': // General-purpose register
556 return C_RegisterClass;
557
558 case 'Q': // Memory with base and unsigned 12-bit displacement
559 case 'R': // Likewise, plus an index
560 case 'S': // Memory with base and signed 20-bit displacement
561 case 'T': // Likewise, plus an index
562 case 'm': // Equivalent to 'T'.
563 return C_Memory;
564
565 case 'I': // Unsigned 8-bit constant
566 case 'J': // Unsigned 12-bit constant
567 case 'K': // Signed 16-bit constant
568 case 'L': // Signed 20-bit displacement (on all targets we support)
569 case 'M': // 0x7fffffff
570 return C_Other;
571
572 default:
573 break;
574 }
575 }
576 return TargetLowering::getConstraintType(Constraint);
577}
578
579TargetLowering::ConstraintWeight SystemZTargetLowering::
580getSingleConstraintMatchWeight(AsmOperandInfo &info,
581 const char *constraint) const {
582 ConstraintWeight weight = CW_Invalid;
583 Value *CallOperandVal = info.CallOperandVal;
584 // If we don't have a value, we can't do a match,
585 // but allow it at the lowest weight.
Craig Topper062a2ba2014-04-25 05:30:21 +0000586 if (!CallOperandVal)
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000587 return CW_Default;
588 Type *type = CallOperandVal->getType();
589 // Look at the constraint type.
590 switch (*constraint) {
591 default:
592 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
593 break;
594
595 case 'a': // Address register
596 case 'd': // Data register (equivalent to 'r')
Richard Sandiford0755c932013-10-01 11:26:28 +0000597 case 'h': // High-part register
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000598 case 'r': // General-purpose register
599 if (CallOperandVal->getType()->isIntegerTy())
600 weight = CW_Register;
601 break;
602
603 case 'f': // Floating-point register
604 if (type->isFloatingPointTy())
605 weight = CW_Register;
606 break;
607
608 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000609 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000610 if (isUInt<8>(C->getZExtValue()))
611 weight = CW_Constant;
612 break;
613
614 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000615 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000616 if (isUInt<12>(C->getZExtValue()))
617 weight = CW_Constant;
618 break;
619
620 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000621 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622 if (isInt<16>(C->getSExtValue()))
623 weight = CW_Constant;
624 break;
625
626 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000627 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628 if (isInt<20>(C->getSExtValue()))
629 weight = CW_Constant;
630 break;
631
632 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000633 if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000634 if (C->getZExtValue() == 0x7fffffff)
635 weight = CW_Constant;
636 break;
637 }
638 return weight;
639}
640
Richard Sandifordb8204052013-07-12 09:08:12 +0000641// Parse a "{tNNN}" register constraint for which the register type "t"
642// has already been verified. MC is the class associated with "t" and
643// Map maps 0-based register numbers to LLVM register numbers.
644static std::pair<unsigned, const TargetRegisterClass *>
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000645parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
646 const unsigned *Map) {
Richard Sandifordb8204052013-07-12 09:08:12 +0000647 assert(*(Constraint.end()-1) == '}' && "Missing '}'");
648 if (isdigit(Constraint[2])) {
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000649 unsigned Index;
650 bool Failed =
651 Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
652 if (!Failed && Index < 16 && Map[Index])
Richard Sandifordb8204052013-07-12 09:08:12 +0000653 return std::make_pair(Map[Index], RC);
654 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000655 return std::make_pair(0U, nullptr);
Richard Sandifordb8204052013-07-12 09:08:12 +0000656}
657
Eric Christopher11e4df72015-02-26 22:38:43 +0000658std::pair<unsigned, const TargetRegisterClass *>
659SystemZTargetLowering::getRegForInlineAsmConstraint(
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000660 const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000661 if (Constraint.size() == 1) {
662 // GCC Constraint Letters
663 switch (Constraint[0]) {
664 default: break;
665 case 'd': // Data register (equivalent to 'r')
666 case 'r': // General-purpose register
667 if (VT == MVT::i64)
668 return std::make_pair(0U, &SystemZ::GR64BitRegClass);
669 else if (VT == MVT::i128)
670 return std::make_pair(0U, &SystemZ::GR128BitRegClass);
671 return std::make_pair(0U, &SystemZ::GR32BitRegClass);
672
673 case 'a': // Address register
674 if (VT == MVT::i64)
675 return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
676 else if (VT == MVT::i128)
677 return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
678 return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
679
Richard Sandiford0755c932013-10-01 11:26:28 +0000680 case 'h': // High-part register (an LLVM extension)
681 return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
682
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000683 case 'f': // Floating-point register
684 if (VT == MVT::f64)
685 return std::make_pair(0U, &SystemZ::FP64BitRegClass);
686 else if (VT == MVT::f128)
687 return std::make_pair(0U, &SystemZ::FP128BitRegClass);
688 return std::make_pair(0U, &SystemZ::FP32BitRegClass);
689 }
690 }
Benjamin Kramer9bfb6272015-07-05 19:29:18 +0000691 if (Constraint.size() > 0 && Constraint[0] == '{') {
Richard Sandifordb8204052013-07-12 09:08:12 +0000692 // We need to override the default register parsing for GPRs and FPRs
693 // because the interpretation depends on VT. The internal names of
694 // the registers are also different from the external names
695 // (F0D and F0S instead of F0, etc.).
696 if (Constraint[1] == 'r') {
697 if (VT == MVT::i32)
698 return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
699 SystemZMC::GR32Regs);
700 if (VT == MVT::i128)
701 return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
702 SystemZMC::GR128Regs);
703 return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
704 SystemZMC::GR64Regs);
705 }
706 if (Constraint[1] == 'f') {
707 if (VT == MVT::f32)
708 return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
709 SystemZMC::FP32Regs);
710 if (VT == MVT::f128)
711 return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
712 SystemZMC::FP128Regs);
713 return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
714 SystemZMC::FP64Regs);
715 }
716 }
Eric Christopher11e4df72015-02-26 22:38:43 +0000717 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000718}
719
720void SystemZTargetLowering::
721LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
722 std::vector<SDValue> &Ops,
723 SelectionDAG &DAG) const {
724 // Only support length 1 constraints for now.
725 if (Constraint.length() == 1) {
726 switch (Constraint[0]) {
727 case 'I': // Unsigned 8-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000728 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000729 if (isUInt<8>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000730 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000731 Op.getValueType()));
732 return;
733
734 case 'J': // Unsigned 12-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000735 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000736 if (isUInt<12>(C->getZExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000737 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000738 Op.getValueType()));
739 return;
740
741 case 'K': // Signed 16-bit constant
Richard Sandiford21f5d682014-03-06 11:22:58 +0000742 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000743 if (isInt<16>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000744 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000745 Op.getValueType()));
746 return;
747
748 case 'L': // Signed 20-bit displacement (on all targets we support)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000749 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000750 if (isInt<20>(C->getSExtValue()))
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000751 Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000752 Op.getValueType()));
753 return;
754
755 case 'M': // 0x7fffffff
Richard Sandiford21f5d682014-03-06 11:22:58 +0000756 if (auto *C = dyn_cast<ConstantSDNode>(Op))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000757 if (C->getZExtValue() == 0x7fffffff)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000758 Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000759 Op.getValueType()));
760 return;
761 }
762 }
763 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
764}
765
766//===----------------------------------------------------------------------===//
767// Calling conventions
768//===----------------------------------------------------------------------===//
769
770#include "SystemZGenCallingConv.inc"
771
Richard Sandiford709bda62013-08-19 12:42:31 +0000772bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
773 Type *ToType) const {
774 return isTruncateFree(FromType, ToType);
775}
776
777bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
Ulrich Weigand19d24d22015-11-13 13:00:27 +0000778 return CI->isTailCall();
Richard Sandiford709bda62013-08-19 12:42:31 +0000779}
780
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000781// We do not yet support 128-bit single-element vector types. If the user
782// attempts to use such types as function argument or return type, prefer
783// to error out instead of emitting code violating the ABI.
784static void VerifyVectorType(MVT VT, EVT ArgVT) {
785 if (ArgVT.isVector() && !VT.isVector())
786 report_fatal_error("Unsupported vector argument or return type");
787}
788
789static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
790 for (unsigned i = 0; i < Ins.size(); ++i)
791 VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
792}
793
794static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
795 for (unsigned i = 0; i < Outs.size(); ++i)
796 VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
797}
798
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000799// Value is a value that has been passed to us in the location described by VA
800// (and so has type VA.getLocVT()). Convert Value to VA.getValVT(), chaining
801// any loads onto Chain.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000802static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000803 CCValAssign &VA, SDValue Chain,
804 SDValue Value) {
805 // If the argument has been promoted from a smaller type, insert an
806 // assertion to capture this.
807 if (VA.getLocInfo() == CCValAssign::SExt)
808 Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
809 DAG.getValueType(VA.getValVT()));
810 else if (VA.getLocInfo() == CCValAssign::ZExt)
811 Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
812 DAG.getValueType(VA.getValVT()));
813
814 if (VA.isExtInLoc())
815 Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000816 else if (VA.getLocInfo() == CCValAssign::BCvt) {
817 // If this is a short vector argument loaded from the stack,
818 // extend from i64 to full vector size and then bitcast.
819 assert(VA.getLocVT() == MVT::i64);
820 assert(VA.getValVT().isVector());
821 Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64,
822 Value, DAG.getUNDEF(MVT::i64));
823 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
824 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000825 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
826 return Value;
827}
828
829// Value is a value of type VA.getValVT() that we need to copy into
830// the location described by VA. Return a copy of Value converted to
831// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000832static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000833 CCValAssign &VA, SDValue Value) {
834 switch (VA.getLocInfo()) {
835 case CCValAssign::SExt:
836 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
837 case CCValAssign::ZExt:
838 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
839 case CCValAssign::AExt:
840 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000841 case CCValAssign::BCvt:
842 // If this is a short vector argument to be stored to the stack,
843 // bitcast to v2i64 and then extract first element.
844 assert(VA.getLocVT() == MVT::i64);
845 assert(VA.getValVT().isVector());
846 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
847 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
848 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000849 case CCValAssign::Full:
850 return Value;
851 default:
852 llvm_unreachable("Unhandled getLocInfo()");
853 }
854}
855
856SDValue SystemZTargetLowering::
857LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
858 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000859 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000860 SmallVectorImpl<SDValue> &InVals) const {
861 MachineFunction &MF = DAG.getMachineFunction();
862 MachineFrameInfo *MFI = MF.getFrameInfo();
863 MachineRegisterInfo &MRI = MF.getRegInfo();
864 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000865 MF.getInfo<SystemZMachineFunctionInfo>();
866 auto *TFL =
867 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000868 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000869
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000870 // Detect unsupported vector argument types.
871 if (Subtarget.hasVector())
872 VerifyVectorTypes(Ins);
873
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000874 // Assign locations to all of the incoming arguments.
875 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000876 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000877 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
878
879 unsigned NumFixedGPRs = 0;
880 unsigned NumFixedFPRs = 0;
881 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
882 SDValue ArgValue;
883 CCValAssign &VA = ArgLocs[I];
884 EVT LocVT = VA.getLocVT();
885 if (VA.isRegLoc()) {
886 // Arguments passed in registers
887 const TargetRegisterClass *RC;
888 switch (LocVT.getSimpleVT().SimpleTy) {
889 default:
890 // Integers smaller than i64 should be promoted to i64.
891 llvm_unreachable("Unexpected argument type");
892 case MVT::i32:
893 NumFixedGPRs += 1;
894 RC = &SystemZ::GR32BitRegClass;
895 break;
896 case MVT::i64:
897 NumFixedGPRs += 1;
898 RC = &SystemZ::GR64BitRegClass;
899 break;
900 case MVT::f32:
901 NumFixedFPRs += 1;
902 RC = &SystemZ::FP32BitRegClass;
903 break;
904 case MVT::f64:
905 NumFixedFPRs += 1;
906 RC = &SystemZ::FP64BitRegClass;
907 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000908 case MVT::v16i8:
909 case MVT::v8i16:
910 case MVT::v4i32:
911 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000912 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000913 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000914 RC = &SystemZ::VR128BitRegClass;
915 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000916 }
917
918 unsigned VReg = MRI.createVirtualRegister(RC);
919 MRI.addLiveIn(VA.getLocReg(), VReg);
920 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
921 } else {
922 assert(VA.isMemLoc() && "Argument not register or memory");
923
924 // Create the frame index object for this incoming parameter.
925 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
926 VA.getLocMemOffset(), true);
927
928 // Create the SelectionDAG nodes corresponding to a load
929 // from this parameter. Unpromoted ints and floats are
930 // passed as right-justified 8-byte values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000931 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
932 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000933 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
934 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000935 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000936 MachinePointerInfo::getFixedStack(MF, FI), false,
937 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000938 }
939
940 // Convert the value of the argument register into the value that's
941 // being passed.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +0000942 if (VA.getLocInfo() == CCValAssign::Indirect) {
943 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
944 ArgValue, MachinePointerInfo(),
945 false, false, false, 0));
946 // If the original argument was split (e.g. i128), we need
947 // to load all parts of it here (using the same address).
948 unsigned ArgIndex = Ins[I].OrigArgIndex;
949 assert (Ins[I].PartOffset == 0);
950 while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
951 CCValAssign &PartVA = ArgLocs[I + 1];
952 unsigned PartOffset = Ins[I + 1].PartOffset;
953 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
954 DAG.getIntPtrConstant(PartOffset, DL));
955 InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain,
956 Address, MachinePointerInfo(),
957 false, false, false, 0));
958 ++I;
959 }
960 } else
961 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000962 }
963
964 if (IsVarArg) {
965 // Save the number of non-varargs registers for later use by va_start, etc.
966 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
967 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
968
969 // Likewise the address (in the form of a frame index) of where the
970 // first stack vararg would be. The 1-byte size here is arbitrary.
971 int64_t StackSize = CCInfo.getNextStackOffset();
972 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
973
974 // ...and a similar frame index for the caller-allocated save area
975 // that will be used to store the incoming registers.
976 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
977 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
978 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
979
980 // Store the FPR varargs in the reserved frame slots. (We store the
981 // GPRs as part of the prologue.)
982 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
983 SDValue MemOps[SystemZ::NumArgFPRs];
984 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
985 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
986 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000987 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000988 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
989 &SystemZ::FP64BitRegClass);
990 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
991 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000992 MachinePointerInfo::getFixedStack(MF, FI),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000993 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000994 }
995 // Join the stores, which are independent of one another.
996 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000997 makeArrayRef(&MemOps[NumFixedFPRs],
998 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000999 }
1000 }
1001
1002 return Chain;
1003}
1004
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +00001005static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +00001006 SmallVectorImpl<CCValAssign> &ArgLocs) {
1007 // Punt if there are any indirect or stack arguments, or if the call
1008 // needs the call-saved argument register R6.
1009 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1010 CCValAssign &VA = ArgLocs[I];
1011 if (VA.getLocInfo() == CCValAssign::Indirect)
1012 return false;
1013 if (!VA.isRegLoc())
1014 return false;
1015 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001016 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001017 return false;
1018 }
1019 return true;
1020}
1021
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001022SDValue
1023SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1024 SmallVectorImpl<SDValue> &InVals) const {
1025 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001026 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001027 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1028 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1029 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001030 SDValue Chain = CLI.Chain;
1031 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001032 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001033 CallingConv::ID CallConv = CLI.CallConv;
1034 bool IsVarArg = CLI.IsVarArg;
1035 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001036 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001037
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001038 // Detect unsupported vector argument and return types.
1039 if (Subtarget.hasVector()) {
1040 VerifyVectorTypes(Outs);
1041 VerifyVectorTypes(Ins);
1042 }
1043
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001044 // Analyze the operands of the call, assigning locations to each operand.
1045 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001046 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001047 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1048
Richard Sandiford709bda62013-08-19 12:42:31 +00001049 // We don't support GuaranteedTailCallOpt, only automatically-detected
1050 // sibling calls.
1051 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
1052 IsTailCall = false;
1053
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001054 // Get a count of how many bytes are to be pushed on the stack.
1055 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1056
1057 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001058 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001059 Chain = DAG.getCALLSEQ_START(Chain,
1060 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001061 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001062
1063 // Copy argument values to their designated locations.
1064 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1065 SmallVector<SDValue, 8> MemOpChains;
1066 SDValue StackPtr;
1067 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1068 CCValAssign &VA = ArgLocs[I];
1069 SDValue ArgValue = OutVals[I];
1070
1071 if (VA.getLocInfo() == CCValAssign::Indirect) {
1072 // Store the argument in a stack slot and pass its address.
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001073 SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001074 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001075 MemOpChains.push_back(DAG.getStore(
1076 Chain, DL, ArgValue, SpillSlot,
1077 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001078 // If the original argument was split (e.g. i128), we need
1079 // to store all parts of it here (and pass just one address).
1080 unsigned ArgIndex = Outs[I].OrigArgIndex;
1081 assert (Outs[I].PartOffset == 0);
1082 while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1083 SDValue PartValue = OutVals[I + 1];
1084 unsigned PartOffset = Outs[I + 1].PartOffset;
1085 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1086 DAG.getIntPtrConstant(PartOffset, DL));
1087 MemOpChains.push_back(DAG.getStore(
1088 Chain, DL, PartValue, Address,
1089 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
1090 ++I;
1091 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001092 ArgValue = SpillSlot;
1093 } else
1094 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1095
1096 if (VA.isRegLoc())
1097 // Queue up the argument copies and emit them at the end.
1098 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1099 else {
1100 assert(VA.isMemLoc() && "Argument not register or memory");
1101
1102 // Work out the address of the stack slot. Unpromoted ints and
1103 // floats are passed as right-justified 8-byte values.
1104 if (!StackPtr.getNode())
1105 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1106 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1107 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1108 Offset += 4;
1109 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001110 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001111
1112 // Emit the store.
1113 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1114 MachinePointerInfo(),
1115 false, false, 0));
1116 }
1117 }
1118
1119 // Join the stores, which are independent of one another.
1120 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001121 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001122
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001123 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001124 // associated Target* opcodes. Force %r1 to be used for indirect
1125 // tail calls.
1126 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001127 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001128 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1129 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001130 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001131 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1132 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001133 } else if (IsTailCall) {
1134 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1135 Glue = Chain.getValue(1);
1136 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1137 }
1138
1139 // Build a sequence of copy-to-reg nodes, chained and glued together.
1140 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1141 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1142 RegsToPass[I].second, Glue);
1143 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001144 }
1145
1146 // The first call operand is the chain and the second is the target address.
1147 SmallVector<SDValue, 8> Ops;
1148 Ops.push_back(Chain);
1149 Ops.push_back(Callee);
1150
1151 // Add argument registers to the end of the list so that they are
1152 // known live into the call.
1153 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1154 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1155 RegsToPass[I].second.getValueType()));
1156
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001157 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001158 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001159 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001160 assert(Mask && "Missing call preserved mask for calling convention");
1161 Ops.push_back(DAG.getRegisterMask(Mask));
1162
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001163 // Glue the call to the argument copies, if any.
1164 if (Glue.getNode())
1165 Ops.push_back(Glue);
1166
1167 // Emit the call.
1168 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001169 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001170 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1171 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001172 Glue = Chain.getValue(1);
1173
1174 // Mark the end of the call, which is glued to the call itself.
1175 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001176 DAG.getConstant(NumBytes, DL, PtrVT, true),
1177 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001178 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001179 Glue = Chain.getValue(1);
1180
1181 // Assign locations to each value returned by this call.
1182 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001183 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001184 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1185
1186 // Copy all of the result registers out of their specified physreg.
1187 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1188 CCValAssign &VA = RetLocs[I];
1189
1190 // Copy the value out, gluing the copy to the end of the call sequence.
1191 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1192 VA.getLocVT(), Glue);
1193 Chain = RetValue.getValue(1);
1194 Glue = RetValue.getValue(2);
1195
1196 // Convert the value of the return register into the value that's
1197 // being returned.
1198 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1199 }
1200
1201 return Chain;
1202}
1203
Ulrich Weiganda887f062015-08-13 13:37:06 +00001204bool SystemZTargetLowering::
1205CanLowerReturn(CallingConv::ID CallConv,
1206 MachineFunction &MF, bool isVarArg,
1207 const SmallVectorImpl<ISD::OutputArg> &Outs,
1208 LLVMContext &Context) const {
1209 // Detect unsupported vector return types.
1210 if (Subtarget.hasVector())
1211 VerifyVectorTypes(Outs);
1212
Ulrich Weigandcfa1d2b2016-02-19 14:10:21 +00001213 // Special case that we cannot easily detect in RetCC_SystemZ since
1214 // i128 is not a legal type.
1215 for (auto &Out : Outs)
1216 if (Out.ArgVT == MVT::i128)
1217 return false;
1218
Ulrich Weiganda887f062015-08-13 13:37:06 +00001219 SmallVector<CCValAssign, 16> RetLocs;
1220 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1221 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1222}
1223
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001224SDValue
1225SystemZTargetLowering::LowerReturn(SDValue Chain,
1226 CallingConv::ID CallConv, bool IsVarArg,
1227 const SmallVectorImpl<ISD::OutputArg> &Outs,
1228 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001229 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001230 MachineFunction &MF = DAG.getMachineFunction();
1231
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001232 // Detect unsupported vector return types.
1233 if (Subtarget.hasVector())
1234 VerifyVectorTypes(Outs);
1235
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001236 // Assign locations to each returned value.
1237 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001238 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001239 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1240
1241 // Quick exit for void returns
1242 if (RetLocs.empty())
1243 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1244
1245 // Copy the result values into the output registers.
1246 SDValue Glue;
1247 SmallVector<SDValue, 4> RetOps;
1248 RetOps.push_back(Chain);
1249 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1250 CCValAssign &VA = RetLocs[I];
1251 SDValue RetValue = OutVals[I];
1252
1253 // Make the return register live on exit.
1254 assert(VA.isRegLoc() && "Can only return in registers!");
1255
1256 // Promote the value as required.
1257 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1258
1259 // Chain and glue the copies together.
1260 unsigned Reg = VA.getLocReg();
1261 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1262 Glue = Chain.getValue(1);
1263 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1264 }
1265
1266 // Update chain and glue.
1267 RetOps[0] = Chain;
1268 if (Glue.getNode())
1269 RetOps.push_back(Glue);
1270
Craig Topper48d114b2014-04-26 18:35:24 +00001271 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001272}
1273
Richard Sandiford9afe6132013-12-10 10:36:34 +00001274SDValue SystemZTargetLowering::
1275prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1276 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1277}
1278
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001279// Return true if Op is an intrinsic node with chain that returns the CC value
1280// as its only (other) argument. Provide the associated SystemZISD opcode and
1281// the mask of valid CC values if so.
1282static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1283 unsigned &CCValid) {
1284 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1285 switch (Id) {
1286 case Intrinsic::s390_tbegin:
1287 Opcode = SystemZISD::TBEGIN;
1288 CCValid = SystemZ::CCMASK_TBEGIN;
1289 return true;
1290
1291 case Intrinsic::s390_tbegin_nofloat:
1292 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1293 CCValid = SystemZ::CCMASK_TBEGIN;
1294 return true;
1295
1296 case Intrinsic::s390_tend:
1297 Opcode = SystemZISD::TEND;
1298 CCValid = SystemZ::CCMASK_TEND;
1299 return true;
1300
1301 default:
1302 return false;
1303 }
1304}
1305
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001306// Return true if Op is an intrinsic node without chain that returns the
1307// CC value as its final argument. Provide the associated SystemZISD
1308// opcode and the mask of valid CC values if so.
1309static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1310 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1311 switch (Id) {
1312 case Intrinsic::s390_vpkshs:
1313 case Intrinsic::s390_vpksfs:
1314 case Intrinsic::s390_vpksgs:
1315 Opcode = SystemZISD::PACKS_CC;
1316 CCValid = SystemZ::CCMASK_VCMP;
1317 return true;
1318
1319 case Intrinsic::s390_vpklshs:
1320 case Intrinsic::s390_vpklsfs:
1321 case Intrinsic::s390_vpklsgs:
1322 Opcode = SystemZISD::PACKLS_CC;
1323 CCValid = SystemZ::CCMASK_VCMP;
1324 return true;
1325
1326 case Intrinsic::s390_vceqbs:
1327 case Intrinsic::s390_vceqhs:
1328 case Intrinsic::s390_vceqfs:
1329 case Intrinsic::s390_vceqgs:
1330 Opcode = SystemZISD::VICMPES;
1331 CCValid = SystemZ::CCMASK_VCMP;
1332 return true;
1333
1334 case Intrinsic::s390_vchbs:
1335 case Intrinsic::s390_vchhs:
1336 case Intrinsic::s390_vchfs:
1337 case Intrinsic::s390_vchgs:
1338 Opcode = SystemZISD::VICMPHS;
1339 CCValid = SystemZ::CCMASK_VCMP;
1340 return true;
1341
1342 case Intrinsic::s390_vchlbs:
1343 case Intrinsic::s390_vchlhs:
1344 case Intrinsic::s390_vchlfs:
1345 case Intrinsic::s390_vchlgs:
1346 Opcode = SystemZISD::VICMPHLS;
1347 CCValid = SystemZ::CCMASK_VCMP;
1348 return true;
1349
1350 case Intrinsic::s390_vtm:
1351 Opcode = SystemZISD::VTM;
1352 CCValid = SystemZ::CCMASK_VCMP;
1353 return true;
1354
1355 case Intrinsic::s390_vfaebs:
1356 case Intrinsic::s390_vfaehs:
1357 case Intrinsic::s390_vfaefs:
1358 Opcode = SystemZISD::VFAE_CC;
1359 CCValid = SystemZ::CCMASK_ANY;
1360 return true;
1361
1362 case Intrinsic::s390_vfaezbs:
1363 case Intrinsic::s390_vfaezhs:
1364 case Intrinsic::s390_vfaezfs:
1365 Opcode = SystemZISD::VFAEZ_CC;
1366 CCValid = SystemZ::CCMASK_ANY;
1367 return true;
1368
1369 case Intrinsic::s390_vfeebs:
1370 case Intrinsic::s390_vfeehs:
1371 case Intrinsic::s390_vfeefs:
1372 Opcode = SystemZISD::VFEE_CC;
1373 CCValid = SystemZ::CCMASK_ANY;
1374 return true;
1375
1376 case Intrinsic::s390_vfeezbs:
1377 case Intrinsic::s390_vfeezhs:
1378 case Intrinsic::s390_vfeezfs:
1379 Opcode = SystemZISD::VFEEZ_CC;
1380 CCValid = SystemZ::CCMASK_ANY;
1381 return true;
1382
1383 case Intrinsic::s390_vfenebs:
1384 case Intrinsic::s390_vfenehs:
1385 case Intrinsic::s390_vfenefs:
1386 Opcode = SystemZISD::VFENE_CC;
1387 CCValid = SystemZ::CCMASK_ANY;
1388 return true;
1389
1390 case Intrinsic::s390_vfenezbs:
1391 case Intrinsic::s390_vfenezhs:
1392 case Intrinsic::s390_vfenezfs:
1393 Opcode = SystemZISD::VFENEZ_CC;
1394 CCValid = SystemZ::CCMASK_ANY;
1395 return true;
1396
1397 case Intrinsic::s390_vistrbs:
1398 case Intrinsic::s390_vistrhs:
1399 case Intrinsic::s390_vistrfs:
1400 Opcode = SystemZISD::VISTR_CC;
1401 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1402 return true;
1403
1404 case Intrinsic::s390_vstrcbs:
1405 case Intrinsic::s390_vstrchs:
1406 case Intrinsic::s390_vstrcfs:
1407 Opcode = SystemZISD::VSTRC_CC;
1408 CCValid = SystemZ::CCMASK_ANY;
1409 return true;
1410
1411 case Intrinsic::s390_vstrczbs:
1412 case Intrinsic::s390_vstrczhs:
1413 case Intrinsic::s390_vstrczfs:
1414 Opcode = SystemZISD::VSTRCZ_CC;
1415 CCValid = SystemZ::CCMASK_ANY;
1416 return true;
1417
1418 case Intrinsic::s390_vfcedbs:
1419 Opcode = SystemZISD::VFCMPES;
1420 CCValid = SystemZ::CCMASK_VCMP;
1421 return true;
1422
1423 case Intrinsic::s390_vfchdbs:
1424 Opcode = SystemZISD::VFCMPHS;
1425 CCValid = SystemZ::CCMASK_VCMP;
1426 return true;
1427
1428 case Intrinsic::s390_vfchedbs:
1429 Opcode = SystemZISD::VFCMPHES;
1430 CCValid = SystemZ::CCMASK_VCMP;
1431 return true;
1432
1433 case Intrinsic::s390_vftcidb:
1434 Opcode = SystemZISD::VFTCI;
1435 CCValid = SystemZ::CCMASK_VCMP;
1436 return true;
1437
1438 default:
1439 return false;
1440 }
1441}
1442
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001443// Emit an intrinsic with chain with a glued value instead of its CC result.
1444static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1445 unsigned Opcode) {
1446 // Copy all operands except the intrinsic ID.
1447 unsigned NumOps = Op.getNumOperands();
1448 SmallVector<SDValue, 6> Ops;
1449 Ops.reserve(NumOps - 1);
1450 Ops.push_back(Op.getOperand(0));
1451 for (unsigned I = 2; I < NumOps; ++I)
1452 Ops.push_back(Op.getOperand(I));
1453
1454 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1455 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1456 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1457 SDValue OldChain = SDValue(Op.getNode(), 1);
1458 SDValue NewChain = SDValue(Intr.getNode(), 0);
1459 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1460 return Intr;
1461}
1462
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001463// Emit an intrinsic with a glued value instead of its CC result.
1464static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1465 unsigned Opcode) {
1466 // Copy all operands except the intrinsic ID.
1467 unsigned NumOps = Op.getNumOperands();
1468 SmallVector<SDValue, 6> Ops;
1469 Ops.reserve(NumOps - 1);
1470 for (unsigned I = 1; I < NumOps; ++I)
1471 Ops.push_back(Op.getOperand(I));
1472
1473 if (Op->getNumValues() == 1)
1474 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1475 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1476 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1477 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1478}
1479
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001480// CC is a comparison that will be implemented using an integer or
1481// floating-point comparison. Return the condition code mask for
1482// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1483// unsigned comparisons and clear for signed ones. In the floating-point
1484// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1485static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1486#define CONV(X) \
1487 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1488 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1489 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1490
1491 switch (CC) {
1492 default:
1493 llvm_unreachable("Invalid integer condition!");
1494
1495 CONV(EQ);
1496 CONV(NE);
1497 CONV(GT);
1498 CONV(GE);
1499 CONV(LT);
1500 CONV(LE);
1501
1502 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1503 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1504 }
1505#undef CONV
1506}
1507
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001508// Return a sequence for getting a 1 from an IPM result when CC has a
1509// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1510// The handling of CC values outside CCValid doesn't matter.
1511static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1512 // Deal with cases where the result can be taken directly from a bit
1513 // of the IPM result.
1514 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1515 return IPMConversion(0, 0, SystemZ::IPM_CC);
1516 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1517 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1518
1519 // Deal with cases where we can add a value to force the sign bit
1520 // to contain the right value. Putting the bit in 31 means we can
1521 // use SRL rather than RISBG(L), and also makes it easier to get a
1522 // 0/-1 value, so it has priority over the other tests below.
1523 //
1524 // These sequences rely on the fact that the upper two bits of the
1525 // IPM result are zero.
1526 uint64_t TopBit = uint64_t(1) << 31;
1527 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1528 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1529 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1530 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1531 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1532 | SystemZ::CCMASK_1
1533 | SystemZ::CCMASK_2)))
1534 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1535 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1536 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1537 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1538 | SystemZ::CCMASK_2
1539 | SystemZ::CCMASK_3)))
1540 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1541
1542 // Next try inverting the value and testing a bit. 0/1 could be
1543 // handled this way too, but we dealt with that case above.
1544 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1545 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1546
1547 // Handle cases where adding a value forces a non-sign bit to contain
1548 // the right value.
1549 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1550 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1551 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1552 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1553
Alp Tokercb402912014-01-24 17:20:08 +00001554 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001555 // can be done by inverting the low CC bit and applying one of the
1556 // sign-based extractions above.
1557 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1558 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1559 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1560 return IPMConversion(1 << SystemZ::IPM_CC,
1561 TopBit - (3 << SystemZ::IPM_CC), 31);
1562 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1563 | SystemZ::CCMASK_1
1564 | SystemZ::CCMASK_3)))
1565 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1566 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1567 | SystemZ::CCMASK_2
1568 | SystemZ::CCMASK_3)))
1569 return IPMConversion(1 << SystemZ::IPM_CC,
1570 TopBit - (1 << SystemZ::IPM_CC), 31);
1571
1572 llvm_unreachable("Unexpected CC combination");
1573}
1574
Richard Sandifordd420f732013-12-13 15:28:45 +00001575// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001576// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001577static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001578 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001579 return;
1580
Richard Sandiford21f5d682014-03-06 11:22:58 +00001581 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001582 if (!ConstOp1)
1583 return;
1584
1585 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001586 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1587 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1588 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1589 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1590 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001591 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001592 }
1593}
1594
Richard Sandifordd420f732013-12-13 15:28:45 +00001595// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1596// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001597static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001598 // For us to make any changes, it must a comparison between a single-use
1599 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001600 if (!C.Op0.hasOneUse() ||
1601 C.Op0.getOpcode() != ISD::LOAD ||
1602 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001603 return;
1604
1605 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001606 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001607 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1608 if (NumBits != 8 && NumBits != 16)
1609 return;
1610
1611 // The load must be an extending one and the constant must be within the
1612 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001613 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001614 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001615 uint64_t Mask = (1 << NumBits) - 1;
1616 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001617 // Make sure that ConstOp1 is in range of C.Op0.
1618 int64_t SignedValue = ConstOp1->getSExtValue();
1619 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001620 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001621 if (C.ICmpType != SystemZICMP::SignedOnly) {
1622 // Unsigned comparison between two sign-extended values is equivalent
1623 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001624 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001625 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001626 // Try to treat the comparison as unsigned, so that we can use CLI.
1627 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001628 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001629 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001630 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1631 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001632 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001633 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001634 else
1635 // No instruction exists for this combination.
1636 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001637 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001638 }
1639 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1640 if (Value > Mask)
1641 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001642 // If the constant is in range, we can use any comparison.
1643 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001644 } else
1645 return;
1646
1647 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001648 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1649 ISD::SEXTLOAD :
1650 ISD::ZEXTLOAD);
1651 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001652 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001653 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1654 Load->getChain(), Load->getBasePtr(),
1655 Load->getPointerInfo(), Load->getMemoryVT(),
1656 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001657 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001658
1659 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001660 if (C.Op1.getValueType() != MVT::i32 ||
1661 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001662 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001663}
1664
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001665// Return true if Op is either an unextended load, or a load suitable
1666// for integer register-memory comparisons of type ICmpType.
1667static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001668 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001669 if (Load) {
1670 // There are no instructions to compare a register with a memory byte.
1671 if (Load->getMemoryVT() == MVT::i8)
1672 return false;
1673 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001674 switch (Load->getExtensionType()) {
1675 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001676 return true;
1677 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001678 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001679 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001680 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001681 default:
1682 break;
1683 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001684 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001685 return false;
1686}
1687
Richard Sandifordd420f732013-12-13 15:28:45 +00001688// Return true if it is better to swap the operands of C.
1689static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001690 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001691 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001692 return false;
1693
1694 // Always keep a floating-point constant second, since comparisons with
1695 // zero can use LOAD TEST and comparisons with other constants make a
1696 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001697 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001698 return false;
1699
1700 // Never swap comparisons with zero since there are many ways to optimize
1701 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001702 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001703 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001704 return false;
1705
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001706 // Also keep natural memory operands second if the loaded value is
1707 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001708 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001709 return false;
1710
Richard Sandiford24e597b2013-08-23 11:27:19 +00001711 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1712 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001713 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001714 // The only exceptions are when the second operand is a constant and
1715 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001716 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001717 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001718 // The unsigned memory-immediate instructions can handle 16-bit
1719 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001720 if (C.ICmpType != SystemZICMP::SignedOnly &&
1721 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001722 return false;
1723 // The signed memory-immediate instructions can handle 16-bit
1724 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001725 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1726 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001727 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001728 return true;
1729 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001730
1731 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001732 unsigned Opcode0 = C.Op0.getOpcode();
1733 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001734 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001735 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001736 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001737 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001738 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001739 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1740 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001741 return true;
1742
Richard Sandiford24e597b2013-08-23 11:27:19 +00001743 return false;
1744}
1745
Richard Sandiford73170f82013-12-11 11:45:08 +00001746// Return a version of comparison CC mask CCMask in which the LT and GT
1747// actions are swapped.
1748static unsigned reverseCCMask(unsigned CCMask) {
1749 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1750 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1751 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1752 (CCMask & SystemZ::CCMASK_CMP_UO));
1753}
1754
Richard Sandiford0847c452013-12-13 15:50:30 +00001755// Check whether C tests for equality between X and Y and whether X - Y
1756// or Y - X is also computed. In that case it's better to compare the
1757// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001758static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001759 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1760 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001761 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001762 SDNode *N = *I;
1763 if (N->getOpcode() == ISD::SUB &&
1764 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1765 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1766 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001767 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001768 return;
1769 }
1770 }
1771 }
1772}
1773
Richard Sandifordd420f732013-12-13 15:28:45 +00001774// Check whether C compares a floating-point value with zero and if that
1775// floating-point value is also negated. In this case we can use the
1776// negation to set CC, so avoiding separate LOAD AND TEST and
1777// LOAD (NEGATIVE/COMPLEMENT) instructions.
1778static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001779 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001780 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001781 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001782 SDNode *N = *I;
1783 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001784 C.Op0 = SDValue(N, 0);
1785 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001786 return;
1787 }
1788 }
1789 }
1790}
1791
Richard Sandifordd420f732013-12-13 15:28:45 +00001792// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001793// also sign-extended. In that case it is better to test the result
1794// of the sign extension using LTGFR.
1795//
1796// This case is important because InstCombine transforms a comparison
1797// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001798static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001799 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001800 if (C.Op0.getOpcode() == ISD::SHL &&
1801 C.Op0.getValueType() == MVT::i64 &&
1802 C.Op1.getOpcode() == ISD::Constant &&
1803 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001804 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001805 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001806 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001807 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001808 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001809 SDNode *N = *I;
1810 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1811 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001812 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001813 return;
1814 }
1815 }
1816 }
1817 }
1818}
1819
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001820// If C compares the truncation of an extending load, try to compare
1821// the untruncated value instead. This exposes more opportunities to
1822// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001823static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001824 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1825 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1826 C.Op1.getOpcode() == ISD::Constant &&
1827 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001828 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001829 if (L->getMemoryVT().getStoreSizeInBits()
1830 <= C.Op0.getValueType().getSizeInBits()) {
1831 unsigned Type = L->getExtensionType();
1832 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1833 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1834 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001835 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001836 }
1837 }
1838 }
1839}
1840
Richard Sandiford030c1652013-09-13 09:09:50 +00001841// Return true if shift operation N has an in-range constant shift value.
1842// Store it in ShiftVal if so.
1843static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001844 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001845 if (!Shift)
1846 return false;
1847
1848 uint64_t Amount = Shift->getZExtValue();
1849 if (Amount >= N.getValueType().getSizeInBits())
1850 return false;
1851
1852 ShiftVal = Amount;
1853 return true;
1854}
1855
1856// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1857// instruction and whether the CC value is descriptive enough to handle
1858// a comparison of type Opcode between the AND result and CmpVal.
1859// CCMask says which comparison result is being tested and BitSize is
1860// the number of bits in the operands. If TEST UNDER MASK can be used,
1861// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001862static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1863 uint64_t Mask, uint64_t CmpVal,
1864 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001865 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1866
Richard Sandiford030c1652013-09-13 09:09:50 +00001867 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1868 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1869 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1870 return 0;
1871
Richard Sandiford113c8702013-09-03 15:38:35 +00001872 // Work out the masks for the lowest and highest bits.
1873 unsigned HighShift = 63 - countLeadingZeros(Mask);
1874 uint64_t High = uint64_t(1) << HighShift;
1875 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1876
1877 // Signed ordered comparisons are effectively unsigned if the sign
1878 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001879 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001880
1881 // Check for equality comparisons with 0, or the equivalent.
1882 if (CmpVal == 0) {
1883 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1884 return SystemZ::CCMASK_TM_ALL_0;
1885 if (CCMask == SystemZ::CCMASK_CMP_NE)
1886 return SystemZ::CCMASK_TM_SOME_1;
1887 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001888 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001889 if (CCMask == SystemZ::CCMASK_CMP_LT)
1890 return SystemZ::CCMASK_TM_ALL_0;
1891 if (CCMask == SystemZ::CCMASK_CMP_GE)
1892 return SystemZ::CCMASK_TM_SOME_1;
1893 }
1894 if (EffectivelyUnsigned && CmpVal < Low) {
1895 if (CCMask == SystemZ::CCMASK_CMP_LE)
1896 return SystemZ::CCMASK_TM_ALL_0;
1897 if (CCMask == SystemZ::CCMASK_CMP_GT)
1898 return SystemZ::CCMASK_TM_SOME_1;
1899 }
1900
1901 // Check for equality comparisons with the mask, or the equivalent.
1902 if (CmpVal == Mask) {
1903 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1904 return SystemZ::CCMASK_TM_ALL_1;
1905 if (CCMask == SystemZ::CCMASK_CMP_NE)
1906 return SystemZ::CCMASK_TM_SOME_0;
1907 }
1908 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1909 if (CCMask == SystemZ::CCMASK_CMP_GT)
1910 return SystemZ::CCMASK_TM_ALL_1;
1911 if (CCMask == SystemZ::CCMASK_CMP_LE)
1912 return SystemZ::CCMASK_TM_SOME_0;
1913 }
1914 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1915 if (CCMask == SystemZ::CCMASK_CMP_GE)
1916 return SystemZ::CCMASK_TM_ALL_1;
1917 if (CCMask == SystemZ::CCMASK_CMP_LT)
1918 return SystemZ::CCMASK_TM_SOME_0;
1919 }
1920
1921 // Check for ordered comparisons with the top bit.
1922 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1923 if (CCMask == SystemZ::CCMASK_CMP_LE)
1924 return SystemZ::CCMASK_TM_MSB_0;
1925 if (CCMask == SystemZ::CCMASK_CMP_GT)
1926 return SystemZ::CCMASK_TM_MSB_1;
1927 }
1928 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1929 if (CCMask == SystemZ::CCMASK_CMP_LT)
1930 return SystemZ::CCMASK_TM_MSB_0;
1931 if (CCMask == SystemZ::CCMASK_CMP_GE)
1932 return SystemZ::CCMASK_TM_MSB_1;
1933 }
1934
1935 // If there are just two bits, we can do equality checks for Low and High
1936 // as well.
1937 if (Mask == Low + High) {
1938 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1939 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1940 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1941 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1942 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1943 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1944 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1945 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1946 }
1947
1948 // Looks like we've exhausted our options.
1949 return 0;
1950}
1951
Richard Sandifordd420f732013-12-13 15:28:45 +00001952// See whether C can be implemented as a TEST UNDER MASK instruction.
1953// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001954static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001955 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001956 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001957 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001958 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001959 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001960
1961 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001962 Comparison NewC(C);
1963 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001964 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001965 if (C.Op0.getOpcode() == ISD::AND) {
1966 NewC.Op0 = C.Op0.getOperand(0);
1967 NewC.Op1 = C.Op0.getOperand(1);
1968 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1969 if (!Mask)
1970 return;
1971 MaskVal = Mask->getZExtValue();
1972 } else {
1973 // There is no instruction to compare with a 64-bit immediate
1974 // so use TMHH instead if possible. We need an unsigned ordered
1975 // comparison with an i64 immediate.
1976 if (NewC.Op0.getValueType() != MVT::i64 ||
1977 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1978 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1979 NewC.ICmpType == SystemZICMP::SignedOnly)
1980 return;
1981 // Convert LE and GT comparisons into LT and GE.
1982 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1983 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1984 if (CmpVal == uint64_t(-1))
1985 return;
1986 CmpVal += 1;
1987 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1988 }
1989 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1990 // be masked off without changing the result.
1991 MaskVal = -(CmpVal & -CmpVal);
1992 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1993 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001994 if (!MaskVal)
1995 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001996
Richard Sandiford113c8702013-09-03 15:38:35 +00001997 // Check whether the combination of mask, comparison value and comparison
1998 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001999 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00002000 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002001 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2002 NewC.Op0.getOpcode() == ISD::SHL &&
2003 isSimpleShift(NewC.Op0, ShiftVal) &&
2004 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2005 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00002006 CmpVal >> ShiftVal,
2007 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002008 NewC.Op0 = NewC.Op0.getOperand(0);
2009 MaskVal >>= ShiftVal;
2010 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2011 NewC.Op0.getOpcode() == ISD::SRL &&
2012 isSimpleShift(NewC.Op0, ShiftVal) &&
2013 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00002014 MaskVal << ShiftVal,
2015 CmpVal << ShiftVal,
2016 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002017 NewC.Op0 = NewC.Op0.getOperand(0);
2018 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00002019 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002020 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2021 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00002022 if (!NewCCMask)
2023 return;
2024 }
Richard Sandiford113c8702013-09-03 15:38:35 +00002025
Richard Sandiford35b9be22013-08-28 10:31:43 +00002026 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00002027 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00002028 C.Op0 = NewC.Op0;
2029 if (Mask && Mask->getZExtValue() == MaskVal)
2030 C.Op1 = SDValue(Mask, 0);
2031 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002032 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00002033 C.CCValid = SystemZ::CCMASK_TM;
2034 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00002035}
2036
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002037// Return a Comparison that tests the condition-code result of intrinsic
2038// node Call against constant integer CC using comparison code Cond.
2039// Opcode is the opcode of the SystemZISD operation for the intrinsic
2040// and CCValid is the set of possible condition-code results.
2041static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2042 SDValue Call, unsigned CCValid, uint64_t CC,
2043 ISD::CondCode Cond) {
2044 Comparison C(Call, SDValue());
2045 C.Opcode = Opcode;
2046 C.CCValid = CCValid;
2047 if (Cond == ISD::SETEQ)
2048 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2049 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2050 else if (Cond == ISD::SETNE)
2051 // ...and the inverse of that.
2052 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2053 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2054 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2055 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002056 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002057 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2058 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002059 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002060 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2061 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2062 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002063 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002064 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2065 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002066 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002067 else
2068 llvm_unreachable("Unexpected integer comparison type");
2069 C.CCMask &= CCValid;
2070 return C;
2071}
2072
Richard Sandifordd420f732013-12-13 15:28:45 +00002073// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2074static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002075 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002076 if (CmpOp1.getOpcode() == ISD::Constant) {
2077 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2078 unsigned Opcode, CCValid;
2079 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2080 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2081 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2082 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002083 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2084 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2085 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2086 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002087 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002088 Comparison C(CmpOp0, CmpOp1);
2089 C.CCMask = CCMaskForCondCode(Cond);
2090 if (C.Op0.getValueType().isFloatingPoint()) {
2091 C.CCValid = SystemZ::CCMASK_FCMP;
2092 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002093 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002094 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002095 C.CCValid = SystemZ::CCMASK_ICMP;
2096 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002097 // Choose the type of comparison. Equality and inequality tests can
2098 // use either signed or unsigned comparisons. The choice also doesn't
2099 // matter if both sign bits are known to be clear. In those cases we
2100 // want to give the main isel code the freedom to choose whichever
2101 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002102 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2103 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2104 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2105 C.ICmpType = SystemZICMP::Any;
2106 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2107 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002108 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002109 C.ICmpType = SystemZICMP::SignedOnly;
2110 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002111 adjustZeroCmp(DAG, DL, C);
2112 adjustSubwordCmp(DAG, DL, C);
2113 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002114 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002115 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002116 }
2117
Richard Sandifordd420f732013-12-13 15:28:45 +00002118 if (shouldSwapCmpOperands(C)) {
2119 std::swap(C.Op0, C.Op1);
2120 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002121 }
2122
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002123 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002124 return C;
2125}
2126
2127// Emit the comparison instruction described by C.
2128static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002129 if (!C.Op1.getNode()) {
2130 SDValue Op;
2131 switch (C.Op0.getOpcode()) {
2132 case ISD::INTRINSIC_W_CHAIN:
2133 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2134 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002135 case ISD::INTRINSIC_WO_CHAIN:
2136 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2137 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002138 default:
2139 llvm_unreachable("Invalid comparison operands");
2140 }
2141 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2142 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002143 if (C.Opcode == SystemZISD::ICMP)
2144 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002145 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002146 if (C.Opcode == SystemZISD::TM) {
2147 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2148 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2149 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002150 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002151 }
2152 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002153}
2154
Richard Sandiford7d86e472013-08-21 09:34:56 +00002155// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2156// 64 bits. Extend is the extension type to use. Store the high part
2157// in Hi and the low part in Lo.
2158static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
2159 unsigned Extend, SDValue Op0, SDValue Op1,
2160 SDValue &Hi, SDValue &Lo) {
2161 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2162 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2163 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002164 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2165 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002166 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2167 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2168}
2169
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002170// Lower a binary operation that produces two VT results, one in each
2171// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2172// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2173// on the extended Op0 and (unextended) Op1. Store the even register result
2174// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002175static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002176 unsigned Extend, unsigned Opcode,
2177 SDValue Op0, SDValue Op1,
2178 SDValue &Even, SDValue &Odd) {
2179 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2180 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2181 SDValue(In128, 0), Op1);
2182 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002183 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2184 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002185}
2186
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002187// Return an i32 value that is 1 if the CC value produced by Glue is
2188// in the mask CCMask and 0 otherwise. CC is known to have a value
2189// in CCValid, so other values can be ignored.
2190static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
2191 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002192 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2193 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2194
2195 if (Conversion.XORValue)
2196 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002197 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002198
2199 if (Conversion.AddValue)
2200 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002201 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002202
2203 // The SHR/AND sequence should get optimized to an RISBG.
2204 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002205 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002206 if (Conversion.Bit != 31)
2207 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002208 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002209 return Result;
2210}
2211
Ulrich Weigandcd808232015-05-05 19:26:48 +00002212// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2213// be done directly. IsFP is true if CC is for a floating-point rather than
2214// integer comparison.
2215static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002216 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002217 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002218 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002219 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002220
Ulrich Weigandcd808232015-05-05 19:26:48 +00002221 case ISD::SETOGE:
2222 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002223 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002224
2225 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002226 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002227 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002228
2229 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002230 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002231
2232 default:
2233 return 0;
2234 }
2235}
2236
2237// Return the SystemZISD vector comparison operation for CC or its inverse,
2238// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002239// result is for the inverse of CC. IsFP is true if CC is for a
2240// floating-point rather than integer comparison.
2241static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2242 bool &Invert) {
2243 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002244 Invert = false;
2245 return Opcode;
2246 }
2247
Ulrich Weigandcd808232015-05-05 19:26:48 +00002248 CC = ISD::getSetCCInverse(CC, !IsFP);
2249 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002250 Invert = true;
2251 return Opcode;
2252 }
2253
2254 return 0;
2255}
2256
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002257// Return a v2f64 that contains the extended form of elements Start and Start+1
2258// of v4f32 value Op.
2259static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
2260 SDValue Op) {
2261 int Mask[] = { Start, -1, Start + 1, -1 };
2262 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2263 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2264}
2265
2266// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2267// producing a result of type VT.
2268static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2269 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2270 // There is no hardware support for v4f32, so extend the vector into
2271 // two v2f64s and compare those.
2272 if (CmpOp0.getValueType() == MVT::v4f32) {
2273 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2274 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2275 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2276 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2277 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2278 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2279 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2280 }
2281 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2282}
2283
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002284// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2285// an integer mask of type VT.
2286static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2287 ISD::CondCode CC, SDValue CmpOp0,
2288 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002289 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002290 bool Invert = false;
2291 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002292 switch (CC) {
2293 // Handle tests for order using (or (ogt y x) (oge x y)).
2294 case ISD::SETUO:
2295 Invert = true;
2296 case ISD::SETO: {
2297 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002298 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2299 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002300 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2301 break;
2302 }
2303
2304 // Handle <> tests using (or (ogt y x) (ogt x y)).
2305 case ISD::SETUEQ:
2306 Invert = true;
2307 case ISD::SETONE: {
2308 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002309 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2310 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002311 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2312 break;
2313 }
2314
2315 // Otherwise a single comparison is enough. It doesn't really
2316 // matter whether we try the inversion or the swap first, since
2317 // there are no cases where both work.
2318 default:
2319 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002320 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002321 else {
2322 CC = ISD::getSetCCSwappedOperands(CC);
2323 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002324 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002325 else
2326 llvm_unreachable("Unhandled comparison");
2327 }
2328 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002329 }
2330 if (Invert) {
2331 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2332 DAG.getConstant(65535, DL, MVT::i32));
2333 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2334 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2335 }
2336 return Cmp;
2337}
2338
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002339SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2340 SelectionDAG &DAG) const {
2341 SDValue CmpOp0 = Op.getOperand(0);
2342 SDValue CmpOp1 = Op.getOperand(1);
2343 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2344 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002345 EVT VT = Op.getValueType();
2346 if (VT.isVector())
2347 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002348
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002349 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002350 SDValue Glue = emitCmp(DAG, DL, C);
2351 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002352}
2353
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002354SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002355 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2356 SDValue CmpOp0 = Op.getOperand(2);
2357 SDValue CmpOp1 = Op.getOperand(3);
2358 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002359 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002360
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002361 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002362 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002363 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002364 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2365 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002366}
2367
Richard Sandiford57485472013-12-13 15:35:00 +00002368// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2369// allowing Pos and Neg to be wider than CmpOp.
2370static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2371 return (Neg.getOpcode() == ISD::SUB &&
2372 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2373 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2374 Neg.getOperand(1) == Pos &&
2375 (Pos == CmpOp ||
2376 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2377 Pos.getOperand(0) == CmpOp)));
2378}
2379
2380// Return the absolute or negative absolute of Op; IsNegative decides which.
2381static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2382 bool IsNegative) {
2383 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2384 if (IsNegative)
2385 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002386 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002387 return Op;
2388}
2389
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002390SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2391 SelectionDAG &DAG) const {
2392 SDValue CmpOp0 = Op.getOperand(0);
2393 SDValue CmpOp1 = Op.getOperand(1);
2394 SDValue TrueOp = Op.getOperand(2);
2395 SDValue FalseOp = Op.getOperand(3);
2396 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002397 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002398
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002399 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002400
2401 // Check for absolute and negative-absolute selections, including those
2402 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2403 // This check supplements the one in DAGCombiner.
2404 if (C.Opcode == SystemZISD::ICMP &&
2405 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2406 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2407 C.Op1.getOpcode() == ISD::Constant &&
2408 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2409 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2410 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2411 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2412 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2413 }
2414
Richard Sandifordd420f732013-12-13 15:28:45 +00002415 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002416
2417 // Special case for handling -1/0 results. The shifts we use here
2418 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002419 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2420 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002421 if (TrueC && FalseC) {
2422 int64_t TrueVal = TrueC->getSExtValue();
2423 int64_t FalseVal = FalseC->getSExtValue();
2424 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2425 // Invert the condition if we want -1 on false.
2426 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002427 C.CCMask ^= C.CCValid;
2428 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002429 EVT VT = Op.getValueType();
2430 // Extend the result to VT. Upper bits are ignored.
2431 if (!is32Bit(VT))
2432 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2433 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002434 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002435 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2436 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2437 }
2438 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002439
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002440 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2441 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002442
2443 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002444 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002445}
2446
2447SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2448 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002449 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002450 const GlobalValue *GV = Node->getGlobal();
2451 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002452 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002453 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2454 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002455
2456 SDValue Result;
2457 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002458 // Assign anchors at 1<<12 byte boundaries.
2459 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2460 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2461 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2462
2463 // The offset can be folded into the address if it is aligned to a halfword.
2464 Offset -= Anchor;
2465 if (Offset != 0 && (Offset & 1) == 0) {
2466 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2467 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002468 Offset = 0;
2469 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002470 } else {
2471 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2472 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2473 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002474 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2475 false, false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002476 }
2477
2478 // If there was a non-zero offset that we didn't fold, create an explicit
2479 // addition for it.
2480 if (Offset != 0)
2481 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002482 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002483
2484 return Result;
2485}
2486
Ulrich Weigand7db69182015-02-18 09:13:27 +00002487SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2488 SelectionDAG &DAG,
2489 unsigned Opcode,
2490 SDValue GOTOffset) const {
2491 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002492 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002493 SDValue Chain = DAG.getEntryNode();
2494 SDValue Glue;
2495
2496 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2497 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2498 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2499 Glue = Chain.getValue(1);
2500 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2501 Glue = Chain.getValue(1);
2502
2503 // The first call operand is the chain and the second is the TLS symbol.
2504 SmallVector<SDValue, 8> Ops;
2505 Ops.push_back(Chain);
2506 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2507 Node->getValueType(0),
2508 0, 0));
2509
2510 // Add argument registers to the end of the list so that they are
2511 // known live into the call.
2512 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2513 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2514
2515 // Add a register mask operand representing the call-preserved registers.
2516 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002517 const uint32_t *Mask =
2518 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002519 assert(Mask && "Missing call preserved mask for calling convention");
2520 Ops.push_back(DAG.getRegisterMask(Mask));
2521
2522 // Glue the call to the argument copies.
2523 Ops.push_back(Glue);
2524
2525 // Emit the call.
2526 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2527 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2528 Glue = Chain.getValue(1);
2529
2530 // Copy the return value from %r2.
2531 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2532}
2533
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002534SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002535 SelectionDAG &DAG) const {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002536 if (DAG.getTarget().Options.EmulatedTLS)
2537 return LowerToTLSEmulatedModel(Node, DAG);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002538 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002539 const GlobalValue *GV = Node->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002540 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002541 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002542
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002543 // The high part of the thread pointer is in access register 0.
2544 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002545 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002546 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2547
2548 // The low part of the thread pointer is in access register 1.
2549 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002550 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002551 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2552
2553 // Merge them into a single 64-bit address.
2554 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002555 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002556 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2557
Ulrich Weigand7db69182015-02-18 09:13:27 +00002558 // Get the offset of GA from the thread pointer, based on the TLS model.
2559 SDValue Offset;
2560 switch (model) {
2561 case TLSModel::GeneralDynamic: {
2562 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2563 SystemZConstantPoolValue *CPV =
2564 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002565
Ulrich Weigand7db69182015-02-18 09:13:27 +00002566 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002567 Offset = DAG.getLoad(
2568 PtrVT, DL, DAG.getEntryNode(), Offset,
2569 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2570 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002571
2572 // Call __tls_get_offset to retrieve the offset.
2573 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2574 break;
2575 }
2576
2577 case TLSModel::LocalDynamic: {
2578 // Load the GOT offset of the module ID.
2579 SystemZConstantPoolValue *CPV =
2580 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2581
2582 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002583 Offset = DAG.getLoad(
2584 PtrVT, DL, DAG.getEntryNode(), Offset,
2585 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2586 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002587
2588 // Call __tls_get_offset to retrieve the module base offset.
2589 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2590
2591 // Note: The SystemZLDCleanupPass will remove redundant computations
2592 // of the module base offset. Count total number of local-dynamic
2593 // accesses to trigger execution of that pass.
2594 SystemZMachineFunctionInfo* MFI =
2595 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2596 MFI->incNumLocalDynamicTLSAccesses();
2597
2598 // Add the per-symbol offset.
2599 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2600
2601 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002602 DTPOffset = DAG.getLoad(
2603 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2604 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2605 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002606
2607 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2608 break;
2609 }
2610
2611 case TLSModel::InitialExec: {
2612 // Load the offset from the GOT.
2613 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2614 SystemZII::MO_INDNTPOFF);
2615 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002616 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2617 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Ulrich Weigand7db69182015-02-18 09:13:27 +00002618 false, false, false, 0);
2619 break;
2620 }
2621
2622 case TLSModel::LocalExec: {
2623 // Force the offset into the constant pool and load it from there.
2624 SystemZConstantPoolValue *CPV =
2625 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2626
2627 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002628 Offset = DAG.getLoad(
2629 PtrVT, DL, DAG.getEntryNode(), Offset,
2630 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2631 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002632 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002633 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002634 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002635
2636 // Add the base and offset together.
2637 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2638}
2639
2640SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2641 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002643 const BlockAddress *BA = Node->getBlockAddress();
2644 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002645 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002646
2647 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2648 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2649 return Result;
2650}
2651
2652SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2653 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002654 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002655 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002656 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2657
2658 // Use LARL to load the address of the table.
2659 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2660}
2661
2662SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2663 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002664 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002665 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002666
2667 SDValue Result;
2668 if (CP->isMachineConstantPoolEntry())
2669 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002670 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002671 else
2672 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002673 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002674
2675 // Use LARL to load the address of the constant pool entry.
2676 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2677}
2678
Ulrich Weigandf557d082016-04-04 12:44:55 +00002679SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
2680 SelectionDAG &DAG) const {
2681 MachineFunction &MF = DAG.getMachineFunction();
2682 MachineFrameInfo *MFI = MF.getFrameInfo();
2683 MFI->setFrameAddressIsTaken(true);
2684
2685 SDLoc DL(Op);
2686 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2687 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2688
2689 // If the back chain frame index has not been allocated yet, do so.
2690 SystemZMachineFunctionInfo *FI = MF.getInfo<SystemZMachineFunctionInfo>();
2691 int BackChainIdx = FI->getFramePointerSaveIndex();
2692 if (!BackChainIdx) {
2693 // By definition, the frame address is the address of the back chain.
2694 BackChainIdx = MFI->CreateFixedObject(8, -SystemZMC::CallFrameSize, false);
2695 FI->setFramePointerSaveIndex(BackChainIdx);
2696 }
2697 SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
2698
2699 // FIXME The frontend should detect this case.
2700 if (Depth > 0) {
2701 report_fatal_error("Unsupported stack frame traversal count");
2702 }
2703
2704 return BackChain;
2705}
2706
2707SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
2708 SelectionDAG &DAG) const {
2709 MachineFunction &MF = DAG.getMachineFunction();
2710 MachineFrameInfo *MFI = MF.getFrameInfo();
2711 MFI->setReturnAddressIsTaken(true);
2712
2713 if (verifyReturnAddressArgumentIsConstant(Op, DAG))
2714 return SDValue();
2715
2716 SDLoc DL(Op);
2717 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2718 EVT PtrVT = getPointerTy(DAG.getDataLayout());
2719
2720 // FIXME The frontend should detect this case.
2721 if (Depth > 0) {
2722 report_fatal_error("Unsupported stack frame traversal count");
2723 }
2724
2725 // Return R14D, which has the return address. Mark it an implicit live-in.
2726 unsigned LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
2727 return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
2728}
2729
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002730SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2731 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002732 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002733 SDValue In = Op.getOperand(0);
2734 EVT InVT = In.getValueType();
2735 EVT ResVT = Op.getValueType();
2736
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002737 // Convert loads directly. This is normally done by DAGCombiner,
2738 // but we need this case for bitcasts that are created during lowering
2739 // and which are then lowered themselves.
2740 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2741 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2742 LoadN->getMemOperand());
2743
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002744 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002745 SDValue In64;
2746 if (Subtarget.hasHighWord()) {
2747 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2748 MVT::i64);
2749 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2750 MVT::i64, SDValue(U64, 0), In);
2751 } else {
2752 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2753 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002754 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002755 }
2756 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002757 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002758 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002759 }
2760 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2761 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002762 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002763 MVT::f64, SDValue(U64, 0), In);
2764 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002765 if (Subtarget.hasHighWord())
2766 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2767 MVT::i32, Out64);
2768 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002769 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002770 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002771 }
2772 llvm_unreachable("Unexpected bitcast combination");
2773}
2774
2775SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2776 SelectionDAG &DAG) const {
2777 MachineFunction &MF = DAG.getMachineFunction();
2778 SystemZMachineFunctionInfo *FuncInfo =
2779 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002780 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002781
2782 SDValue Chain = Op.getOperand(0);
2783 SDValue Addr = Op.getOperand(1);
2784 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002785 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002786
2787 // The initial values of each field.
2788 const unsigned NumFields = 4;
2789 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002790 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2791 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002792 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2793 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2794 };
2795
2796 // Store each field into its respective slot.
2797 SDValue MemOps[NumFields];
2798 unsigned Offset = 0;
2799 for (unsigned I = 0; I < NumFields; ++I) {
2800 SDValue FieldAddr = Addr;
2801 if (Offset != 0)
2802 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002803 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002804 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2805 MachinePointerInfo(SV, Offset),
2806 false, false, 0);
2807 Offset += 8;
2808 }
Craig Topper48d114b2014-04-26 18:35:24 +00002809 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002810}
2811
2812SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2813 SelectionDAG &DAG) const {
2814 SDValue Chain = Op.getOperand(0);
2815 SDValue DstPtr = Op.getOperand(1);
2816 SDValue SrcPtr = Op.getOperand(2);
2817 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2818 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002819 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002820
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002821 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002822 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002823 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002824 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2825}
2826
2827SDValue SystemZTargetLowering::
2828lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002829 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2830 bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2831 hasFnAttribute("no-realign-stack");
2832
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002833 SDValue Chain = Op.getOperand(0);
2834 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002835 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002836 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002837
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002838 // If user has set the no alignment function attribute, ignore
2839 // alloca alignments.
2840 uint64_t AlignVal = (RealignOpt ?
2841 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2842
2843 uint64_t StackAlign = TFI->getStackAlignment();
2844 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2845 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2846
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002847 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002848 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002849
2850 // Get a reference to the stack pointer.
2851 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2852
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002853 // Add extra space for alignment if needed.
2854 if (ExtraAlignSpace)
2855 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2856 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2857
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002858 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002859 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002860
2861 // Copy the new stack pointer back.
2862 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2863
2864 // The allocated data lives above the 160 bytes allocated for the standard
2865 // frame, plus any outgoing stack arguments. We don't know how much that
2866 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2867 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2868 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2869
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002870 // Dynamically realign if needed.
2871 if (RequiredAlign > StackAlign) {
2872 Result =
2873 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2874 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2875 Result =
2876 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2877 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2878 }
2879
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002880 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002881 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002882}
2883
Richard Sandiford7d86e472013-08-21 09:34:56 +00002884SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2885 SelectionDAG &DAG) const {
2886 EVT VT = Op.getValueType();
2887 SDLoc DL(Op);
2888 SDValue Ops[2];
2889 if (is32Bit(VT))
2890 // Just do a normal 64-bit multiplication and extract the results.
2891 // We define this so that it can be used for constant division.
2892 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2893 Op.getOperand(1), Ops[1], Ops[0]);
2894 else {
2895 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2896 //
2897 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2898 //
2899 // but using the fact that the upper halves are either all zeros
2900 // or all ones:
2901 //
2902 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2903 //
2904 // and grouping the right terms together since they are quicker than the
2905 // multiplication:
2906 //
2907 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002908 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002909 SDValue LL = Op.getOperand(0);
2910 SDValue RL = Op.getOperand(1);
2911 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2912 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2913 // UMUL_LOHI64 returns the low result in the odd register and the high
2914 // result in the even register. SMUL_LOHI is defined to return the
2915 // low half first, so the results are in reverse order.
2916 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2917 LL, RL, Ops[1], Ops[0]);
2918 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2919 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2920 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2921 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2922 }
Craig Topper64941d92014-04-27 19:20:57 +00002923 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002924}
2925
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002926SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2927 SelectionDAG &DAG) const {
2928 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002929 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002930 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002931 if (is32Bit(VT))
2932 // Just do a normal 64-bit multiplication and extract the results.
2933 // We define this so that it can be used for constant division.
2934 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2935 Op.getOperand(1), Ops[1], Ops[0]);
2936 else
2937 // UMUL_LOHI64 returns the low result in the odd register and the high
2938 // result in the even register. UMUL_LOHI is defined to return the
2939 // low half first, so the results are in reverse order.
2940 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2941 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002942 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002943}
2944
2945SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2946 SelectionDAG &DAG) const {
2947 SDValue Op0 = Op.getOperand(0);
2948 SDValue Op1 = Op.getOperand(1);
2949 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002950 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002951 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002952
2953 // We use DSGF for 32-bit division.
2954 if (is32Bit(VT)) {
2955 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002956 Opcode = SystemZISD::SDIVREM32;
2957 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2958 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2959 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002960 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00002961 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002962
2963 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2964 // input is "don't care". The instruction returns the remainder in
2965 // the even register and the quotient in the odd register.
2966 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002967 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002968 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002969 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002970}
2971
2972SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2973 SelectionDAG &DAG) const {
2974 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002975 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002976
2977 // DL(G) uses a double-width dividend, so we need to clear the even
2978 // register in the GR128 input. The instruction returns the remainder
2979 // in the even register and the quotient in the odd register.
2980 SDValue Ops[2];
2981 if (is32Bit(VT))
2982 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2983 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2984 else
2985 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2986 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002987 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002988}
2989
2990SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2991 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2992
2993 // Get the known-zero masks for each operand.
2994 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2995 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002996 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2997 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002998
2999 // See if the upper 32 bits of one operand and the lower 32 bits of the
3000 // other are known zero. They are the low and high operands respectively.
3001 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
3002 KnownZero[1].getZExtValue() };
3003 unsigned High, Low;
3004 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
3005 High = 1, Low = 0;
3006 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
3007 High = 0, Low = 1;
3008 else
3009 return Op;
3010
3011 SDValue LowOp = Ops[Low];
3012 SDValue HighOp = Ops[High];
3013
3014 // If the high part is a constant, we're better off using IILH.
3015 if (HighOp.getOpcode() == ISD::Constant)
3016 return Op;
3017
3018 // If the low part is a constant that is outside the range of LHI,
3019 // then we're better off using IILF.
3020 if (LowOp.getOpcode() == ISD::Constant) {
3021 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
3022 if (!isInt<16>(Value))
3023 return Op;
3024 }
3025
3026 // Check whether the high part is an AND that doesn't change the
3027 // high 32 bits and just masks out low bits. We can skip it if so.
3028 if (HighOp.getOpcode() == ISD::AND &&
3029 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00003030 SDValue HighOp0 = HighOp.getOperand(0);
3031 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
3032 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
3033 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003034 }
3035
3036 // Take advantage of the fact that all GR32 operations only change the
3037 // low 32 bits by truncating Low to an i32 and inserting it directly
3038 // using a subreg. The interesting cases are those where the truncation
3039 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00003040 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003041 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00003042 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00003043 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003044}
3045
Ulrich Weigandb4012182015-03-31 12:56:33 +00003046SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
3047 SelectionDAG &DAG) const {
3048 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00003049 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003050 Op = Op.getOperand(0);
3051
3052 // Handle vector types via VPOPCT.
3053 if (VT.isVector()) {
3054 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3055 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
3056 switch (VT.getVectorElementType().getSizeInBits()) {
3057 case 8:
3058 break;
3059 case 16: {
3060 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3061 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3062 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3063 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3064 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3065 break;
3066 }
3067 case 32: {
3068 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3069 DAG.getConstant(0, DL, MVT::i32));
3070 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3071 break;
3072 }
3073 case 64: {
3074 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3075 DAG.getConstant(0, DL, MVT::i32));
3076 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3077 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3078 break;
3079 }
3080 default:
3081 llvm_unreachable("Unexpected type");
3082 }
3083 return Op;
3084 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00003085
3086 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003087 APInt KnownZero, KnownOne;
3088 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003089 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3090 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003091 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003092
3093 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003094 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003095 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3096 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003097
3098 // The POPCNT instruction counts the number of bits in each byte.
3099 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3100 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3101 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3102
3103 // Add up per-byte counts in a binary tree. All bits of Op at
3104 // position larger than BitSize remain zero throughout.
3105 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003106 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003107 if (BitSize != OrigBitSize)
3108 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003109 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003110 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3111 }
3112
3113 // Extract overall result from high byte.
3114 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003115 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3116 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003117
3118 return Op;
3119}
3120
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003121// Op is an atomic load. Lower it into a normal volatile load.
3122SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3123 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003124 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003125 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3126 Node->getChain(), Node->getBasePtr(),
3127 Node->getMemoryVT(), Node->getMemOperand());
3128}
3129
3130// Op is an atomic store. Lower it into a normal volatile store followed
3131// by a serialization.
3132SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3133 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003134 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003135 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3136 Node->getBasePtr(), Node->getMemoryVT(),
3137 Node->getMemOperand());
3138 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3139 Chain), 0);
3140}
3141
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003142// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3143// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003144SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3145 SelectionDAG &DAG,
3146 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003147 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003148
3149 // 32-bit operations need no code outside the main loop.
3150 EVT NarrowVT = Node->getMemoryVT();
3151 EVT WideVT = MVT::i32;
3152 if (NarrowVT == WideVT)
3153 return Op;
3154
3155 int64_t BitSize = NarrowVT.getSizeInBits();
3156 SDValue ChainIn = Node->getChain();
3157 SDValue Addr = Node->getBasePtr();
3158 SDValue Src2 = Node->getVal();
3159 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003160 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003161 EVT PtrVT = Addr.getValueType();
3162
3163 // Convert atomic subtracts of constants into additions.
3164 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003165 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003166 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003167 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003168 }
3169
3170 // Get the address of the containing word.
3171 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003172 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003173
3174 // Get the number of bits that the word must be rotated left in order
3175 // to bring the field to the top bits of a GR32.
3176 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003177 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003178 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3179
3180 // Get the complementing shift amount, for rotating a field in the top
3181 // bits back to its proper position.
3182 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003183 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003184
3185 // Extend the source operand to 32 bits and prepare it for the inner loop.
3186 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3187 // operations require the source to be shifted in advance. (This shift
3188 // can be folded if the source is constant.) For AND and NAND, the lower
3189 // bits must be set, while for other opcodes they should be left clear.
3190 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3191 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003192 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003193 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3194 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3195 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003196 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003197
3198 // Construct the ATOMIC_LOADW_* node.
3199 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3200 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003201 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003202 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003203 NarrowVT, MMO);
3204
3205 // Rotate the result of the final CS so that the field is in the lower
3206 // bits of a GR32, then truncate it.
3207 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003208 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003209 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3210
3211 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003212 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003213}
3214
Richard Sandiford41350a52013-12-24 15:18:04 +00003215// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003216// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003217// operations into additions.
3218SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3219 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003220 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003221 EVT MemVT = Node->getMemoryVT();
3222 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3223 // A full-width operation.
3224 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3225 SDValue Src2 = Node->getVal();
3226 SDValue NegSrc2;
3227 SDLoc DL(Src2);
3228
Richard Sandiford21f5d682014-03-06 11:22:58 +00003229 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003230 // Use an addition if the operand is constant and either LAA(G) is
3231 // available or the negative value is in the range of A(G)FHI.
3232 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003233 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003234 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003235 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003236 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003237 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003238 Src2);
3239
3240 if (NegSrc2.getNode())
3241 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3242 Node->getChain(), Node->getBasePtr(), NegSrc2,
3243 Node->getMemOperand(), Node->getOrdering(),
3244 Node->getSynchScope());
3245
3246 // Use the node as-is.
3247 return Op;
3248 }
3249
3250 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3251}
3252
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003253// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3254// into a fullword ATOMIC_CMP_SWAPW operation.
3255SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3256 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003257 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003258
3259 // We have native support for 32-bit compare and swap.
3260 EVT NarrowVT = Node->getMemoryVT();
3261 EVT WideVT = MVT::i32;
3262 if (NarrowVT == WideVT)
3263 return Op;
3264
3265 int64_t BitSize = NarrowVT.getSizeInBits();
3266 SDValue ChainIn = Node->getOperand(0);
3267 SDValue Addr = Node->getOperand(1);
3268 SDValue CmpVal = Node->getOperand(2);
3269 SDValue SwapVal = Node->getOperand(3);
3270 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003271 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003272 EVT PtrVT = Addr.getValueType();
3273
3274 // Get the address of the containing word.
3275 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003276 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003277
3278 // Get the number of bits that the word must be rotated left in order
3279 // to bring the field to the top bits of a GR32.
3280 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003281 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003282 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3283
3284 // Get the complementing shift amount, for rotating a field in the top
3285 // bits back to its proper position.
3286 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003287 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003288
3289 // Construct the ATOMIC_CMP_SWAPW node.
3290 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3291 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003292 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003293 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003294 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003295 return AtomicOp;
3296}
3297
3298SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3299 SelectionDAG &DAG) const {
3300 MachineFunction &MF = DAG.getMachineFunction();
3301 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003302 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003303 SystemZ::R15D, Op.getValueType());
3304}
3305
3306SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3307 SelectionDAG &DAG) const {
3308 MachineFunction &MF = DAG.getMachineFunction();
3309 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003310 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003311 SystemZ::R15D, Op.getOperand(1));
3312}
3313
Richard Sandiford03481332013-08-23 11:36:42 +00003314SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3315 SelectionDAG &DAG) const {
3316 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3317 if (!IsData)
3318 // Just preserve the chain.
3319 return Op.getOperand(0);
3320
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003321 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003322 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3323 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003324 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003325 SDValue Ops[] = {
3326 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003327 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003328 Op.getOperand(1)
3329 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003330 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003331 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003332 Node->getMemoryVT(), Node->getMemOperand());
3333}
3334
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003335// Return an i32 that contains the value of CC immediately after After,
3336// whose final operand must be MVT::Glue.
3337static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003338 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003339 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003340 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3341 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3342 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003343}
3344
3345SDValue
3346SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3347 SelectionDAG &DAG) const {
3348 unsigned Opcode, CCValid;
3349 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3350 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3351 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3352 SDValue CC = getCCResult(DAG, Glued.getNode());
3353 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3354 return SDValue();
3355 }
3356
3357 return SDValue();
3358}
3359
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003360SDValue
3361SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3362 SelectionDAG &DAG) const {
3363 unsigned Opcode, CCValid;
3364 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3365 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3366 SDValue CC = getCCResult(DAG, Glued.getNode());
3367 if (Op->getNumValues() == 1)
3368 return CC;
3369 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003370 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3371 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003372 }
3373
3374 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3375 switch (Id) {
3376 case Intrinsic::s390_vpdi:
3377 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3378 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3379
3380 case Intrinsic::s390_vperm:
3381 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3382 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3383
3384 case Intrinsic::s390_vuphb:
3385 case Intrinsic::s390_vuphh:
3386 case Intrinsic::s390_vuphf:
3387 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3388 Op.getOperand(1));
3389
3390 case Intrinsic::s390_vuplhb:
3391 case Intrinsic::s390_vuplhh:
3392 case Intrinsic::s390_vuplhf:
3393 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3394 Op.getOperand(1));
3395
3396 case Intrinsic::s390_vuplb:
3397 case Intrinsic::s390_vuplhw:
3398 case Intrinsic::s390_vuplf:
3399 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3400 Op.getOperand(1));
3401
3402 case Intrinsic::s390_vupllb:
3403 case Intrinsic::s390_vupllh:
3404 case Intrinsic::s390_vupllf:
3405 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3406 Op.getOperand(1));
3407
3408 case Intrinsic::s390_vsumb:
3409 case Intrinsic::s390_vsumh:
3410 case Intrinsic::s390_vsumgh:
3411 case Intrinsic::s390_vsumgf:
3412 case Intrinsic::s390_vsumqf:
3413 case Intrinsic::s390_vsumqg:
3414 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3415 Op.getOperand(1), Op.getOperand(2));
3416 }
3417
3418 return SDValue();
3419}
3420
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003421namespace {
3422// Says that SystemZISD operation Opcode can be used to perform the equivalent
3423// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3424// Operand is the constant third operand, otherwise it is the number of
3425// bytes in each element of the result.
3426struct Permute {
3427 unsigned Opcode;
3428 unsigned Operand;
3429 unsigned char Bytes[SystemZ::VectorBytes];
3430};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003431}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003432
3433static const Permute PermuteForms[] = {
3434 // VMRHG
3435 { SystemZISD::MERGE_HIGH, 8,
3436 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3437 // VMRHF
3438 { SystemZISD::MERGE_HIGH, 4,
3439 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3440 // VMRHH
3441 { SystemZISD::MERGE_HIGH, 2,
3442 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3443 // VMRHB
3444 { SystemZISD::MERGE_HIGH, 1,
3445 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3446 // VMRLG
3447 { SystemZISD::MERGE_LOW, 8,
3448 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3449 // VMRLF
3450 { SystemZISD::MERGE_LOW, 4,
3451 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3452 // VMRLH
3453 { SystemZISD::MERGE_LOW, 2,
3454 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3455 // VMRLB
3456 { SystemZISD::MERGE_LOW, 1,
3457 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3458 // VPKG
3459 { SystemZISD::PACK, 4,
3460 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3461 // VPKF
3462 { SystemZISD::PACK, 2,
3463 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3464 // VPKH
3465 { SystemZISD::PACK, 1,
3466 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3467 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3468 { SystemZISD::PERMUTE_DWORDS, 4,
3469 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3470 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3471 { SystemZISD::PERMUTE_DWORDS, 1,
3472 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3473};
3474
3475// Called after matching a vector shuffle against a particular pattern.
3476// Both the original shuffle and the pattern have two vector operands.
3477// OpNos[0] is the operand of the original shuffle that should be used for
3478// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3479// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3480// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3481// for operands 0 and 1 of the pattern.
3482static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3483 if (OpNos[0] < 0) {
3484 if (OpNos[1] < 0)
3485 return false;
3486 OpNo0 = OpNo1 = OpNos[1];
3487 } else if (OpNos[1] < 0) {
3488 OpNo0 = OpNo1 = OpNos[0];
3489 } else {
3490 OpNo0 = OpNos[0];
3491 OpNo1 = OpNos[1];
3492 }
3493 return true;
3494}
3495
3496// Bytes is a VPERM-like permute vector, except that -1 is used for
3497// undefined bytes. Return true if the VPERM can be implemented using P.
3498// When returning true set OpNo0 to the VPERM operand that should be
3499// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3500//
3501// For example, if swapping the VPERM operands allows P to match, OpNo0
3502// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3503// operand, but rewriting it to use two duplicated operands allows it to
3504// match P, then OpNo0 and OpNo1 will be the same.
3505static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3506 unsigned &OpNo0, unsigned &OpNo1) {
3507 int OpNos[] = { -1, -1 };
3508 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3509 int Elt = Bytes[I];
3510 if (Elt >= 0) {
3511 // Make sure that the two permute vectors use the same suboperand
3512 // byte number. Only the operand numbers (the high bits) are
3513 // allowed to differ.
3514 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3515 return false;
3516 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3517 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3518 // Make sure that the operand mappings are consistent with previous
3519 // elements.
3520 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3521 return false;
3522 OpNos[ModelOpNo] = RealOpNo;
3523 }
3524 }
3525 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3526}
3527
3528// As above, but search for a matching permute.
3529static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3530 unsigned &OpNo0, unsigned &OpNo1) {
3531 for (auto &P : PermuteForms)
3532 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3533 return &P;
3534 return nullptr;
3535}
3536
3537// Bytes is a VPERM-like permute vector, except that -1 is used for
3538// undefined bytes. This permute is an operand of an outer permute.
3539// See whether redistributing the -1 bytes gives a shuffle that can be
3540// implemented using P. If so, set Transform to a VPERM-like permute vector
3541// that, when applied to the result of P, gives the original permute in Bytes.
3542static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3543 const Permute &P,
3544 SmallVectorImpl<int> &Transform) {
3545 unsigned To = 0;
3546 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3547 int Elt = Bytes[From];
3548 if (Elt < 0)
3549 // Byte number From of the result is undefined.
3550 Transform[From] = -1;
3551 else {
3552 while (P.Bytes[To] != Elt) {
3553 To += 1;
3554 if (To == SystemZ::VectorBytes)
3555 return false;
3556 }
3557 Transform[From] = To;
3558 }
3559 }
3560 return true;
3561}
3562
3563// As above, but search for a matching permute.
3564static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3565 SmallVectorImpl<int> &Transform) {
3566 for (auto &P : PermuteForms)
3567 if (matchDoublePermute(Bytes, P, Transform))
3568 return &P;
3569 return nullptr;
3570}
3571
3572// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3573// as if it had type vNi8.
3574static void getVPermMask(ShuffleVectorSDNode *VSN,
3575 SmallVectorImpl<int> &Bytes) {
3576 EVT VT = VSN->getValueType(0);
3577 unsigned NumElements = VT.getVectorNumElements();
3578 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3579 Bytes.resize(NumElements * BytesPerElement, -1);
3580 for (unsigned I = 0; I < NumElements; ++I) {
3581 int Index = VSN->getMaskElt(I);
3582 if (Index >= 0)
3583 for (unsigned J = 0; J < BytesPerElement; ++J)
3584 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3585 }
3586}
3587
3588// Bytes is a VPERM-like permute vector, except that -1 is used for
3589// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3590// the result come from a contiguous sequence of bytes from one input.
3591// Set Base to the selector for the first byte if so.
3592static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3593 unsigned BytesPerElement, int &Base) {
3594 Base = -1;
3595 for (unsigned I = 0; I < BytesPerElement; ++I) {
3596 if (Bytes[Start + I] >= 0) {
3597 unsigned Elem = Bytes[Start + I];
3598 if (Base < 0) {
3599 Base = Elem - I;
3600 // Make sure the bytes would come from one input operand.
3601 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3602 return false;
3603 } else if (unsigned(Base) != Elem - I)
3604 return false;
3605 }
3606 }
3607 return true;
3608}
3609
3610// Bytes is a VPERM-like permute vector, except that -1 is used for
3611// undefined bytes. Return true if it can be performed using VSLDI.
3612// When returning true, set StartIndex to the shift amount and OpNo0
3613// and OpNo1 to the VPERM operands that should be used as the first
3614// and second shift operand respectively.
3615static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3616 unsigned &StartIndex, unsigned &OpNo0,
3617 unsigned &OpNo1) {
3618 int OpNos[] = { -1, -1 };
3619 int Shift = -1;
3620 for (unsigned I = 0; I < 16; ++I) {
3621 int Index = Bytes[I];
3622 if (Index >= 0) {
3623 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3624 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3625 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3626 if (Shift < 0)
3627 Shift = ExpectedShift;
3628 else if (Shift != ExpectedShift)
3629 return false;
3630 // Make sure that the operand mappings are consistent with previous
3631 // elements.
3632 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3633 return false;
3634 OpNos[ModelOpNo] = RealOpNo;
3635 }
3636 }
3637 StartIndex = Shift;
3638 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3639}
3640
3641// Create a node that performs P on operands Op0 and Op1, casting the
3642// operands to the appropriate type. The type of the result is determined by P.
3643static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3644 const Permute &P, SDValue Op0, SDValue Op1) {
3645 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3646 // elements of a PACK are twice as wide as the outputs.
3647 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3648 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3649 P.Operand);
3650 // Cast both operands to the appropriate type.
3651 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3652 SystemZ::VectorBytes / InBytes);
3653 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3654 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3655 SDValue Op;
3656 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3657 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3658 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3659 } else if (P.Opcode == SystemZISD::PACK) {
3660 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3661 SystemZ::VectorBytes / P.Operand);
3662 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3663 } else {
3664 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3665 }
3666 return Op;
3667}
3668
3669// Bytes is a VPERM-like permute vector, except that -1 is used for
3670// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3671// VSLDI or VPERM.
3672static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3673 const SmallVectorImpl<int> &Bytes) {
3674 for (unsigned I = 0; I < 2; ++I)
3675 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3676
3677 // First see whether VSLDI can be used.
3678 unsigned StartIndex, OpNo0, OpNo1;
3679 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3680 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3681 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3682
3683 // Fall back on VPERM. Construct an SDNode for the permute vector.
3684 SDValue IndexNodes[SystemZ::VectorBytes];
3685 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3686 if (Bytes[I] >= 0)
3687 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3688 else
3689 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3690 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3691 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3692}
3693
3694namespace {
3695// Describes a general N-operand vector shuffle.
3696struct GeneralShuffle {
3697 GeneralShuffle(EVT vt) : VT(vt) {}
3698 void addUndef();
3699 void add(SDValue, unsigned);
3700 SDValue getNode(SelectionDAG &, SDLoc);
3701
3702 // The operands of the shuffle.
3703 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3704
3705 // Index I is -1 if byte I of the result is undefined. Otherwise the
3706 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3707 // Bytes[I] / SystemZ::VectorBytes.
3708 SmallVector<int, SystemZ::VectorBytes> Bytes;
3709
3710 // The type of the shuffle result.
3711 EVT VT;
3712};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003713}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003714
3715// Add an extra undefined element to the shuffle.
3716void GeneralShuffle::addUndef() {
3717 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3718 for (unsigned I = 0; I < BytesPerElement; ++I)
3719 Bytes.push_back(-1);
3720}
3721
3722// Add an extra element to the shuffle, taking it from element Elem of Op.
3723// A null Op indicates a vector input whose value will be calculated later;
3724// there is at most one such input per shuffle and it always has the same
3725// type as the result.
3726void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3727 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3728
3729 // The source vector can have wider elements than the result,
3730 // either through an explicit TRUNCATE or because of type legalization.
3731 // We want the least significant part.
3732 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3733 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3734 assert(FromBytesPerElement >= BytesPerElement &&
3735 "Invalid EXTRACT_VECTOR_ELT");
3736 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3737 (FromBytesPerElement - BytesPerElement));
3738
3739 // Look through things like shuffles and bitcasts.
3740 while (Op.getNode()) {
3741 if (Op.getOpcode() == ISD::BITCAST)
3742 Op = Op.getOperand(0);
3743 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3744 // See whether the bytes we need come from a contiguous part of one
3745 // operand.
3746 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3747 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3748 int NewByte;
3749 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3750 break;
3751 if (NewByte < 0) {
3752 addUndef();
3753 return;
3754 }
3755 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3756 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
Sanjay Patel57195842016-03-14 17:28:46 +00003757 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003758 addUndef();
3759 return;
3760 } else
3761 break;
3762 }
3763
3764 // Make sure that the source of the extraction is in Ops.
3765 unsigned OpNo = 0;
3766 for (; OpNo < Ops.size(); ++OpNo)
3767 if (Ops[OpNo] == Op)
3768 break;
3769 if (OpNo == Ops.size())
3770 Ops.push_back(Op);
3771
3772 // Add the element to Bytes.
3773 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3774 for (unsigned I = 0; I < BytesPerElement; ++I)
3775 Bytes.push_back(Base + I);
3776}
3777
3778// Return SDNodes for the completed shuffle.
3779SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3780 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3781
3782 if (Ops.size() == 0)
3783 return DAG.getUNDEF(VT);
3784
3785 // Make sure that there are at least two shuffle operands.
3786 if (Ops.size() == 1)
3787 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3788
3789 // Create a tree of shuffles, deferring root node until after the loop.
3790 // Try to redistribute the undefined elements of non-root nodes so that
3791 // the non-root shuffles match something like a pack or merge, then adjust
3792 // the parent node's permute vector to compensate for the new order.
3793 // Among other things, this copes with vectors like <2 x i16> that were
3794 // padded with undefined elements during type legalization.
3795 //
3796 // In the best case this redistribution will lead to the whole tree
3797 // using packs and merges. It should rarely be a loss in other cases.
3798 unsigned Stride = 1;
3799 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3800 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3801 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3802
3803 // Create a mask for just these two operands.
3804 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3805 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3806 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3807 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3808 if (OpNo == I)
3809 NewBytes[J] = Byte;
3810 else if (OpNo == I + Stride)
3811 NewBytes[J] = SystemZ::VectorBytes + Byte;
3812 else
3813 NewBytes[J] = -1;
3814 }
3815 // See if it would be better to reorganize NewMask to avoid using VPERM.
3816 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3817 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3818 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3819 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3820 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3821 if (NewBytes[J] >= 0) {
3822 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3823 "Invalid double permute");
3824 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3825 } else
3826 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3827 }
3828 } else {
3829 // Just use NewBytes on the operands.
3830 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3831 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3832 if (NewBytes[J] >= 0)
3833 Bytes[J] = I * SystemZ::VectorBytes + J;
3834 }
3835 }
3836 }
3837
3838 // Now we just have 2 inputs. Put the second operand in Ops[1].
3839 if (Stride > 1) {
3840 Ops[1] = Ops[Stride];
3841 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3842 if (Bytes[I] >= int(SystemZ::VectorBytes))
3843 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3844 }
3845
3846 // Look for an instruction that can do the permute without resorting
3847 // to VPERM.
3848 unsigned OpNo0, OpNo1;
3849 SDValue Op;
3850 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3851 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3852 else
3853 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3854 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3855}
3856
Ulrich Weigandcd808232015-05-05 19:26:48 +00003857// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3858static bool isScalarToVector(SDValue Op) {
3859 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00003860 if (!Op.getOperand(I).isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003861 return false;
3862 return true;
3863}
3864
3865// Return a vector of type VT that contains Value in the first element.
3866// The other elements don't matter.
3867static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3868 SDValue Value) {
3869 // If we have a constant, replicate it to all elements and let the
3870 // BUILD_VECTOR lowering take care of it.
3871 if (Value.getOpcode() == ISD::Constant ||
3872 Value.getOpcode() == ISD::ConstantFP) {
3873 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3874 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3875 }
Sanjay Patel57195842016-03-14 17:28:46 +00003876 if (Value.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003877 return DAG.getUNDEF(VT);
3878 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3879}
3880
3881// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3882// element 1. Used for cases in which replication is cheap.
3883static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3884 SDValue Op0, SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003885 if (Op0.isUndef()) {
3886 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003887 return DAG.getUNDEF(VT);
3888 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3889 }
Sanjay Patel57195842016-03-14 17:28:46 +00003890 if (Op1.isUndef())
Ulrich Weigandcd808232015-05-05 19:26:48 +00003891 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3892 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3893 buildScalarToVector(DAG, DL, VT, Op0),
3894 buildScalarToVector(DAG, DL, VT, Op1));
3895}
3896
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003897// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3898// vector for them.
3899static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3900 SDValue Op1) {
Sanjay Patel57195842016-03-14 17:28:46 +00003901 if (Op0.isUndef() && Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003902 return DAG.getUNDEF(MVT::v2i64);
3903 // If one of the two inputs is undefined then replicate the other one,
3904 // in order to avoid using another register unnecessarily.
Sanjay Patel57195842016-03-14 17:28:46 +00003905 if (Op0.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003906 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
Sanjay Patel57195842016-03-14 17:28:46 +00003907 else if (Op1.isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003908 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3909 else {
3910 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3911 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3912 }
3913 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3914}
3915
3916// Try to represent constant BUILD_VECTOR node BVN using a
3917// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3918// on success.
3919static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3920 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3921 unsigned BytesPerElement = ElemVT.getStoreSize();
3922 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3923 SDValue Op = BVN->getOperand(I);
Sanjay Patel75068522016-03-14 18:09:43 +00003924 if (!Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003925 uint64_t Value;
3926 if (Op.getOpcode() == ISD::Constant)
3927 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3928 else if (Op.getOpcode() == ISD::ConstantFP)
3929 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3930 .getZExtValue());
3931 else
3932 return false;
3933 for (unsigned J = 0; J < BytesPerElement; ++J) {
3934 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3935 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00003936 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003937 else if (Byte != 0)
3938 return false;
3939 }
3940 }
3941 }
3942 return true;
3943}
3944
3945// Try to load a vector constant in which BitsPerElement-bit value Value
3946// is replicated to fill the vector. VT is the type of the resulting
3947// constant, which may have elements of a different size from BitsPerElement.
3948// Return the SDValue of the constant on success, otherwise return
3949// an empty value.
3950static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3951 const SystemZInstrInfo *TII,
3952 SDLoc DL, EVT VT, uint64_t Value,
3953 unsigned BitsPerElement) {
3954 // Signed 16-bit values can be replicated using VREPI.
3955 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3956 if (isInt<16>(SignedValue)) {
3957 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3958 SystemZ::VectorBits / BitsPerElement);
3959 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3960 DAG.getConstant(SignedValue, DL, MVT::i32));
3961 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3962 }
3963 // See whether rotating the constant left some N places gives a value that
3964 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3965 // If so we can use VGM.
3966 unsigned Start, End;
3967 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3968 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3969 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3970 // bit numbers for an BitsPerElement value, so that 0 denotes
3971 // 1 << (BitsPerElement-1).
3972 Start -= 64 - BitsPerElement;
3973 End -= 64 - BitsPerElement;
3974 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3975 SystemZ::VectorBits / BitsPerElement);
3976 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3977 DAG.getConstant(Start, DL, MVT::i32),
3978 DAG.getConstant(End, DL, MVT::i32));
3979 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3980 }
3981 return SDValue();
3982}
3983
3984// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3985// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3986// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
3987// would benefit from this representation and return it if so.
3988static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3989 BuildVectorSDNode *BVN) {
3990 EVT VT = BVN->getValueType(0);
3991 unsigned NumElements = VT.getVectorNumElements();
3992
3993 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3994 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
3995 // need a BUILD_VECTOR, add an additional placeholder operand for that
3996 // BUILD_VECTOR and store its operands in ResidueOps.
3997 GeneralShuffle GS(VT);
3998 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3999 bool FoundOne = false;
4000 for (unsigned I = 0; I < NumElements; ++I) {
4001 SDValue Op = BVN->getOperand(I);
4002 if (Op.getOpcode() == ISD::TRUNCATE)
4003 Op = Op.getOperand(0);
4004 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4005 Op.getOperand(1).getOpcode() == ISD::Constant) {
4006 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4007 GS.add(Op.getOperand(0), Elem);
4008 FoundOne = true;
Sanjay Patel57195842016-03-14 17:28:46 +00004009 } else if (Op.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004010 GS.addUndef();
4011 } else {
4012 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00004013 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004014 }
4015 }
4016
4017 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
4018 if (!FoundOne)
4019 return SDValue();
4020
4021 // Create the BUILD_VECTOR for the remaining elements, if any.
4022 if (!ResidueOps.empty()) {
4023 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00004024 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004025 for (auto &Op : GS.Ops) {
4026 if (!Op.getNode()) {
4027 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
4028 break;
4029 }
4030 }
4031 }
4032 return GS.getNode(DAG, SDLoc(BVN));
4033}
4034
4035// Combine GPR scalar values Elems into a vector of type VT.
4036static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
4037 SmallVectorImpl<SDValue> &Elems) {
4038 // See whether there is a single replicated value.
4039 SDValue Single;
4040 unsigned int NumElements = Elems.size();
4041 unsigned int Count = 0;
4042 for (auto Elem : Elems) {
Sanjay Patel75068522016-03-14 18:09:43 +00004043 if (!Elem.isUndef()) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004044 if (!Single.getNode())
4045 Single = Elem;
4046 else if (Elem != Single) {
4047 Single = SDValue();
4048 break;
4049 }
4050 Count += 1;
4051 }
4052 }
4053 // There are three cases here:
4054 //
4055 // - if the only defined element is a loaded one, the best sequence
4056 // is a replicating load.
4057 //
4058 // - otherwise, if the only defined element is an i64 value, we will
4059 // end up with the same VLVGP sequence regardless of whether we short-cut
4060 // for replication or fall through to the later code.
4061 //
4062 // - otherwise, if the only defined element is an i32 or smaller value,
4063 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4064 // This is only a win if the single defined element is used more than once.
4065 // In other cases we're better off using a single VLVGx.
4066 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4067 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4068
4069 // The best way of building a v2i64 from two i64s is to use VLVGP.
4070 if (VT == MVT::v2i64)
4071 return joinDwords(DAG, DL, Elems[0], Elems[1]);
4072
Ulrich Weigandcd808232015-05-05 19:26:48 +00004073 // Use a 64-bit merge high to combine two doubles.
4074 if (VT == MVT::v2f64)
4075 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4076
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004077 // Build v4f32 values directly from the FPRs:
4078 //
4079 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4080 // V V VMRHF
4081 // <ABxx> <CDxx>
4082 // V VMRHG
4083 // <ABCD>
4084 if (VT == MVT::v4f32) {
4085 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4086 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4087 // Avoid unnecessary undefs by reusing the other operand.
Sanjay Patel57195842016-03-14 17:28:46 +00004088 if (Op01.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004089 Op01 = Op23;
Sanjay Patel57195842016-03-14 17:28:46 +00004090 else if (Op23.isUndef())
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004091 Op23 = Op01;
4092 // Merging identical replications is a no-op.
4093 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4094 return Op01;
4095 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4096 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4097 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4098 DL, MVT::v2i64, Op01, Op23);
4099 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4100 }
4101
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004102 // Collect the constant terms.
4103 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4104 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4105
4106 unsigned NumConstants = 0;
4107 for (unsigned I = 0; I < NumElements; ++I) {
4108 SDValue Elem = Elems[I];
4109 if (Elem.getOpcode() == ISD::Constant ||
4110 Elem.getOpcode() == ISD::ConstantFP) {
4111 NumConstants += 1;
4112 Constants[I] = Elem;
4113 Done[I] = true;
4114 }
4115 }
4116 // If there was at least one constant, fill in the other elements of
4117 // Constants with undefs to get a full vector constant and use that
4118 // as the starting point.
4119 SDValue Result;
4120 if (NumConstants > 0) {
4121 for (unsigned I = 0; I < NumElements; ++I)
4122 if (!Constants[I].getNode())
4123 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4124 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4125 } else {
4126 // Otherwise try to use VLVGP to start the sequence in order to
4127 // avoid a false dependency on any previous contents of the vector
4128 // register. This only makes sense if one of the associated elements
4129 // is defined.
4130 unsigned I1 = NumElements / 2 - 1;
4131 unsigned I2 = NumElements - 1;
Sanjay Patel75068522016-03-14 18:09:43 +00004132 bool Def1 = !Elems[I1].isUndef();
4133 bool Def2 = !Elems[I2].isUndef();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004134 if (Def1 || Def2) {
4135 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4136 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4137 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4138 joinDwords(DAG, DL, Elem1, Elem2));
4139 Done[I1] = true;
4140 Done[I2] = true;
4141 } else
4142 Result = DAG.getUNDEF(VT);
4143 }
4144
4145 // Use VLVGx to insert the other elements.
4146 for (unsigned I = 0; I < NumElements; ++I)
Sanjay Patel75068522016-03-14 18:09:43 +00004147 if (!Done[I] && !Elems[I].isUndef())
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004148 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4149 DAG.getConstant(I, DL, MVT::i32));
4150 return Result;
4151}
4152
4153SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4154 SelectionDAG &DAG) const {
4155 const SystemZInstrInfo *TII =
4156 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4157 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4158 SDLoc DL(Op);
4159 EVT VT = Op.getValueType();
4160
4161 if (BVN->isConstant()) {
4162 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4163 // preferred way of creating all-zero and all-one vectors so give it
4164 // priority over other methods below.
4165 uint64_t Mask = 0;
4166 if (tryBuildVectorByteMask(BVN, Mask)) {
4167 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4168 DAG.getConstant(Mask, DL, MVT::i32));
4169 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4170 }
4171
4172 // Try using some form of replication.
4173 APInt SplatBits, SplatUndef;
4174 unsigned SplatBitSize;
4175 bool HasAnyUndefs;
4176 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4177 8, true) &&
4178 SplatBitSize <= 64) {
4179 // First try assuming that any undefined bits above the highest set bit
4180 // and below the lowest set bit are 1s. This increases the likelihood of
4181 // being able to use a sign-extended element value in VECTOR REPLICATE
4182 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4183 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4184 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4185 uint64_t Lower = (SplatUndefZ
4186 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4187 uint64_t Upper = (SplatUndefZ
4188 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4189 uint64_t Value = SplatBitsZ | Upper | Lower;
4190 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4191 SplatBitSize);
4192 if (Op.getNode())
4193 return Op;
4194
4195 // Now try assuming that any undefined bits between the first and
4196 // last defined set bits are set. This increases the chances of
4197 // using a non-wraparound mask.
4198 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4199 Value = SplatBitsZ | Middle;
4200 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4201 if (Op.getNode())
4202 return Op;
4203 }
4204
4205 // Fall back to loading it from memory.
4206 return SDValue();
4207 }
4208
4209 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004210 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004211 return Res;
4212
Ulrich Weigandcd808232015-05-05 19:26:48 +00004213 // Detect SCALAR_TO_VECTOR conversions.
4214 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4215 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4216
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004217 // Otherwise use buildVector to build the vector up from GPRs.
4218 unsigned NumElements = Op.getNumOperands();
4219 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4220 for (unsigned I = 0; I < NumElements; ++I)
4221 Ops[I] = Op.getOperand(I);
4222 return buildVector(DAG, DL, VT, Ops);
4223}
4224
4225SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4226 SelectionDAG &DAG) const {
4227 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4228 SDLoc DL(Op);
4229 EVT VT = Op.getValueType();
4230 unsigned NumElements = VT.getVectorNumElements();
4231
4232 if (VSN->isSplat()) {
4233 SDValue Op0 = Op.getOperand(0);
4234 unsigned Index = VSN->getSplatIndex();
4235 assert(Index < VT.getVectorNumElements() &&
4236 "Splat index should be defined and in first operand");
4237 // See whether the value we're splatting is directly available as a scalar.
4238 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4239 Op0.getOpcode() == ISD::BUILD_VECTOR)
4240 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4241 // Otherwise keep it as a vector-to-vector operation.
4242 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4243 DAG.getConstant(Index, DL, MVT::i32));
4244 }
4245
4246 GeneralShuffle GS(VT);
4247 for (unsigned I = 0; I < NumElements; ++I) {
4248 int Elt = VSN->getMaskElt(I);
4249 if (Elt < 0)
4250 GS.addUndef();
4251 else
4252 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4253 unsigned(Elt) % NumElements);
4254 }
4255 return GS.getNode(DAG, SDLoc(VSN));
4256}
4257
4258SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4259 SelectionDAG &DAG) const {
4260 SDLoc DL(Op);
4261 // Just insert the scalar into element 0 of an undefined vector.
4262 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4263 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4264 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4265}
4266
Ulrich Weigandcd808232015-05-05 19:26:48 +00004267SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4268 SelectionDAG &DAG) const {
4269 // Handle insertions of floating-point values.
4270 SDLoc DL(Op);
4271 SDValue Op0 = Op.getOperand(0);
4272 SDValue Op1 = Op.getOperand(1);
4273 SDValue Op2 = Op.getOperand(2);
4274 EVT VT = Op.getValueType();
4275
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004276 // Insertions into constant indices of a v2f64 can be done using VPDI.
4277 // However, if the inserted value is a bitcast or a constant then it's
4278 // better to use GPRs, as below.
4279 if (VT == MVT::v2f64 &&
4280 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004281 Op1.getOpcode() != ISD::ConstantFP &&
4282 Op2.getOpcode() == ISD::Constant) {
4283 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4284 unsigned Mask = VT.getVectorNumElements() - 1;
4285 if (Index <= Mask)
4286 return Op;
4287 }
4288
4289 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4290 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4291 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4292 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4293 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4294 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4295 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4296}
4297
4298SDValue
4299SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4300 SelectionDAG &DAG) const {
4301 // Handle extractions of floating-point values.
4302 SDLoc DL(Op);
4303 SDValue Op0 = Op.getOperand(0);
4304 SDValue Op1 = Op.getOperand(1);
4305 EVT VT = Op.getValueType();
4306 EVT VecVT = Op0.getValueType();
4307
4308 // Extractions of constant indices can be done directly.
4309 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4310 uint64_t Index = CIndexN->getZExtValue();
4311 unsigned Mask = VecVT.getVectorNumElements() - 1;
4312 if (Index <= Mask)
4313 return Op;
4314 }
4315
4316 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4317 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4318 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4319 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4320 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4321 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4322}
4323
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004324SDValue
4325SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004326 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004327 SDValue PackedOp = Op.getOperand(0);
4328 EVT OutVT = Op.getValueType();
4329 EVT InVT = PackedOp.getValueType();
4330 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4331 unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4332 do {
4333 FromBits *= 2;
4334 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4335 SystemZ::VectorBits / FromBits);
4336 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4337 } while (FromBits != ToBits);
4338 return PackedOp;
4339}
4340
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004341SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4342 unsigned ByScalar) const {
4343 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4344 SDValue Op0 = Op.getOperand(0);
4345 SDValue Op1 = Op.getOperand(1);
4346 SDLoc DL(Op);
4347 EVT VT = Op.getValueType();
4348 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4349
4350 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4351 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4352 APInt SplatBits, SplatUndef;
4353 unsigned SplatBitSize;
4354 bool HasAnyUndefs;
4355 // Check for constant splats. Use ElemBitSize as the minimum element
4356 // width and reject splats that need wider elements.
4357 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4358 ElemBitSize, true) &&
4359 SplatBitSize == ElemBitSize) {
4360 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4361 DL, MVT::i32);
4362 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4363 }
4364 // Check for variable splats.
4365 BitVector UndefElements;
4366 SDValue Splat = BVN->getSplatValue(&UndefElements);
4367 if (Splat) {
4368 // Since i32 is the smallest legal type, we either need a no-op
4369 // or a truncation.
4370 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4371 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4372 }
4373 }
4374
4375 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4376 // and the shift amount is directly available in a GPR.
4377 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4378 if (VSN->isSplat()) {
4379 SDValue VSNOp0 = VSN->getOperand(0);
4380 unsigned Index = VSN->getSplatIndex();
4381 assert(Index < VT.getVectorNumElements() &&
4382 "Splat index should be defined and in first operand");
4383 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4384 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4385 // Since i32 is the smallest legal type, we either need a no-op
4386 // or a truncation.
4387 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4388 VSNOp0.getOperand(Index));
4389 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4390 }
4391 }
4392 }
4393
4394 // Otherwise just treat the current form as legal.
4395 return Op;
4396}
4397
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004398SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4399 SelectionDAG &DAG) const {
4400 switch (Op.getOpcode()) {
Ulrich Weigandf557d082016-04-04 12:44:55 +00004401 case ISD::FRAMEADDR:
4402 return lowerFRAMEADDR(Op, DAG);
4403 case ISD::RETURNADDR:
4404 return lowerRETURNADDR(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004405 case ISD::BR_CC:
4406 return lowerBR_CC(Op, DAG);
4407 case ISD::SELECT_CC:
4408 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004409 case ISD::SETCC:
4410 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004411 case ISD::GlobalAddress:
4412 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4413 case ISD::GlobalTLSAddress:
4414 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4415 case ISD::BlockAddress:
4416 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4417 case ISD::JumpTable:
4418 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4419 case ISD::ConstantPool:
4420 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4421 case ISD::BITCAST:
4422 return lowerBITCAST(Op, DAG);
4423 case ISD::VASTART:
4424 return lowerVASTART(Op, DAG);
4425 case ISD::VACOPY:
4426 return lowerVACOPY(Op, DAG);
4427 case ISD::DYNAMIC_STACKALLOC:
4428 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004429 case ISD::SMUL_LOHI:
4430 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004431 case ISD::UMUL_LOHI:
4432 return lowerUMUL_LOHI(Op, DAG);
4433 case ISD::SDIVREM:
4434 return lowerSDIVREM(Op, DAG);
4435 case ISD::UDIVREM:
4436 return lowerUDIVREM(Op, DAG);
4437 case ISD::OR:
4438 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004439 case ISD::CTPOP:
4440 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004441 case ISD::CTLZ_ZERO_UNDEF:
4442 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4443 Op.getValueType(), Op.getOperand(0));
4444 case ISD::CTTZ_ZERO_UNDEF:
4445 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4446 Op.getValueType(), Op.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004447 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004448 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4449 case ISD::ATOMIC_STORE:
4450 return lowerATOMIC_STORE(Op, DAG);
4451 case ISD::ATOMIC_LOAD:
4452 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004453 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004454 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004455 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004456 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004457 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004458 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004459 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004460 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004461 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004462 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004463 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004464 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004465 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004466 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004467 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004468 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004469 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004470 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004471 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004472 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004473 case ISD::ATOMIC_CMP_SWAP:
4474 return lowerATOMIC_CMP_SWAP(Op, DAG);
4475 case ISD::STACKSAVE:
4476 return lowerSTACKSAVE(Op, DAG);
4477 case ISD::STACKRESTORE:
4478 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004479 case ISD::PREFETCH:
4480 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004481 case ISD::INTRINSIC_W_CHAIN:
4482 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004483 case ISD::INTRINSIC_WO_CHAIN:
4484 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004485 case ISD::BUILD_VECTOR:
4486 return lowerBUILD_VECTOR(Op, DAG);
4487 case ISD::VECTOR_SHUFFLE:
4488 return lowerVECTOR_SHUFFLE(Op, DAG);
4489 case ISD::SCALAR_TO_VECTOR:
4490 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004491 case ISD::INSERT_VECTOR_ELT:
4492 return lowerINSERT_VECTOR_ELT(Op, DAG);
4493 case ISD::EXTRACT_VECTOR_ELT:
4494 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004495 case ISD::SIGN_EXTEND_VECTOR_INREG:
4496 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4497 case ISD::ZERO_EXTEND_VECTOR_INREG:
4498 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004499 case ISD::SHL:
4500 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4501 case ISD::SRL:
4502 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4503 case ISD::SRA:
4504 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004505 default:
4506 llvm_unreachable("Unexpected node to lower");
4507 }
4508}
4509
4510const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4511#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004512 switch ((SystemZISD::NodeType)Opcode) {
4513 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004514 OPCODE(RET_FLAG);
4515 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004516 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004517 OPCODE(TLS_GDCALL);
4518 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004519 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004520 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004521 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004522 OPCODE(ICMP);
4523 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004524 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004525 OPCODE(BR_CCMASK);
4526 OPCODE(SELECT_CCMASK);
4527 OPCODE(ADJDYNALLOC);
4528 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004529 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004530 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004531 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004532 OPCODE(SDIVREM64);
4533 OPCODE(UDIVREM32);
4534 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004535 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004536 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004537 OPCODE(NC);
4538 OPCODE(NC_LOOP);
4539 OPCODE(OC);
4540 OPCODE(OC_LOOP);
4541 OPCODE(XC);
4542 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004543 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004544 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004545 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004546 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004547 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004548 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004549 OPCODE(SERIALIZE);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004550 OPCODE(TBEGIN);
4551 OPCODE(TBEGIN_NOFLOAT);
4552 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004553 OPCODE(BYTE_MASK);
4554 OPCODE(ROTATE_MASK);
4555 OPCODE(REPLICATE);
4556 OPCODE(JOIN_DWORDS);
4557 OPCODE(SPLAT);
4558 OPCODE(MERGE_HIGH);
4559 OPCODE(MERGE_LOW);
4560 OPCODE(SHL_DOUBLE);
4561 OPCODE(PERMUTE_DWORDS);
4562 OPCODE(PERMUTE);
4563 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004564 OPCODE(PACKS_CC);
4565 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004566 OPCODE(UNPACK_HIGH);
4567 OPCODE(UNPACKL_HIGH);
4568 OPCODE(UNPACK_LOW);
4569 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004570 OPCODE(VSHL_BY_SCALAR);
4571 OPCODE(VSRL_BY_SCALAR);
4572 OPCODE(VSRA_BY_SCALAR);
4573 OPCODE(VSUM);
4574 OPCODE(VICMPE);
4575 OPCODE(VICMPH);
4576 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004577 OPCODE(VICMPES);
4578 OPCODE(VICMPHS);
4579 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004580 OPCODE(VFCMPE);
4581 OPCODE(VFCMPH);
4582 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004583 OPCODE(VFCMPES);
4584 OPCODE(VFCMPHS);
4585 OPCODE(VFCMPHES);
4586 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004587 OPCODE(VEXTEND);
4588 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004589 OPCODE(VTM);
4590 OPCODE(VFAE_CC);
4591 OPCODE(VFAEZ_CC);
4592 OPCODE(VFEE_CC);
4593 OPCODE(VFEEZ_CC);
4594 OPCODE(VFENE_CC);
4595 OPCODE(VFENEZ_CC);
4596 OPCODE(VISTR_CC);
4597 OPCODE(VSTRC_CC);
4598 OPCODE(VSTRCZ_CC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004599 OPCODE(ATOMIC_SWAPW);
4600 OPCODE(ATOMIC_LOADW_ADD);
4601 OPCODE(ATOMIC_LOADW_SUB);
4602 OPCODE(ATOMIC_LOADW_AND);
4603 OPCODE(ATOMIC_LOADW_OR);
4604 OPCODE(ATOMIC_LOADW_XOR);
4605 OPCODE(ATOMIC_LOADW_NAND);
4606 OPCODE(ATOMIC_LOADW_MIN);
4607 OPCODE(ATOMIC_LOADW_MAX);
4608 OPCODE(ATOMIC_LOADW_UMIN);
4609 OPCODE(ATOMIC_LOADW_UMAX);
4610 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004611 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004612 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004613 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004614#undef OPCODE
4615}
4616
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004617// Return true if VT is a vector whose elements are a whole number of bytes
4618// in width.
4619static bool canTreatAsByteVector(EVT VT) {
4620 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4621}
4622
4623// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4624// producing a result of type ResVT. Op is a possibly bitcast version
4625// of the input vector and Index is the index (based on type VecVT) that
4626// should be extracted. Return the new extraction if a simplification
4627// was possible or if Force is true.
4628SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4629 SDValue Op, unsigned Index,
4630 DAGCombinerInfo &DCI,
4631 bool Force) const {
4632 SelectionDAG &DAG = DCI.DAG;
4633
4634 // The number of bytes being extracted.
4635 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4636
4637 for (;;) {
4638 unsigned Opcode = Op.getOpcode();
4639 if (Opcode == ISD::BITCAST)
4640 // Look through bitcasts.
4641 Op = Op.getOperand(0);
4642 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4643 canTreatAsByteVector(Op.getValueType())) {
4644 // Get a VPERM-like permute mask and see whether the bytes covered
4645 // by the extracted element are a contiguous sequence from one
4646 // source operand.
4647 SmallVector<int, SystemZ::VectorBytes> Bytes;
4648 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4649 int First;
4650 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4651 BytesPerElement, First))
4652 break;
4653 if (First < 0)
4654 return DAG.getUNDEF(ResVT);
4655 // Make sure the contiguous sequence starts at a multiple of the
4656 // original element size.
4657 unsigned Byte = unsigned(First) % Bytes.size();
4658 if (Byte % BytesPerElement != 0)
4659 break;
4660 // We can get the extracted value directly from an input.
4661 Index = Byte / BytesPerElement;
4662 Op = Op.getOperand(unsigned(First) / Bytes.size());
4663 Force = true;
4664 } else if (Opcode == ISD::BUILD_VECTOR &&
4665 canTreatAsByteVector(Op.getValueType())) {
4666 // We can only optimize this case if the BUILD_VECTOR elements are
4667 // at least as wide as the extracted value.
4668 EVT OpVT = Op.getValueType();
4669 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4670 if (OpBytesPerElement < BytesPerElement)
4671 break;
4672 // Make sure that the least-significant bit of the extracted value
4673 // is the least significant bit of an input.
4674 unsigned End = (Index + 1) * BytesPerElement;
4675 if (End % OpBytesPerElement != 0)
4676 break;
4677 // We're extracting the low part of one operand of the BUILD_VECTOR.
4678 Op = Op.getOperand(End / OpBytesPerElement - 1);
4679 if (!Op.getValueType().isInteger()) {
4680 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4681 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4682 DCI.AddToWorklist(Op.getNode());
4683 }
4684 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4685 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4686 if (VT != ResVT) {
4687 DCI.AddToWorklist(Op.getNode());
4688 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4689 }
4690 return Op;
4691 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004692 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4693 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4694 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004695 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4696 // Make sure that only the unextended bits are significant.
4697 EVT ExtVT = Op.getValueType();
4698 EVT OpVT = Op.getOperand(0).getValueType();
4699 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4700 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4701 unsigned Byte = Index * BytesPerElement;
4702 unsigned SubByte = Byte % ExtBytesPerElement;
4703 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4704 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004705 SubByte + BytesPerElement > ExtBytesPerElement)
4706 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004707 // Get the byte offset of the unextended element
4708 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4709 // ...then add the byte offset relative to that element.
4710 Byte += SubByte - MinSubByte;
4711 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004712 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004713 Op = Op.getOperand(0);
4714 Index = Byte / BytesPerElement;
4715 Force = true;
4716 } else
4717 break;
4718 }
4719 if (Force) {
4720 if (Op.getValueType() != VecVT) {
4721 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4722 DCI.AddToWorklist(Op.getNode());
4723 }
4724 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4725 DAG.getConstant(Index, DL, MVT::i32));
4726 }
4727 return SDValue();
4728}
4729
4730// Optimize vector operations in scalar value Op on the basis that Op
4731// is truncated to TruncVT.
4732SDValue
4733SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4734 DAGCombinerInfo &DCI) const {
4735 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4736 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4737 // of type TruncVT.
4738 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4739 TruncVT.getSizeInBits() % 8 == 0) {
4740 SDValue Vec = Op.getOperand(0);
4741 EVT VecVT = Vec.getValueType();
4742 if (canTreatAsByteVector(VecVT)) {
4743 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4744 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4745 unsigned TruncBytes = TruncVT.getStoreSize();
4746 if (BytesPerElement % TruncBytes == 0) {
4747 // Calculate the value of Y' in the above description. We are
4748 // splitting the original elements into Scale equal-sized pieces
4749 // and for truncation purposes want the last (least-significant)
4750 // of these pieces for IndexN. This is easiest to do by calculating
4751 // the start index of the following element and then subtracting 1.
4752 unsigned Scale = BytesPerElement / TruncBytes;
4753 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4754
4755 // Defer the creation of the bitcast from X to combineExtract,
4756 // which might be able to optimize the extraction.
4757 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4758 VecVT.getStoreSize() / TruncBytes);
4759 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4760 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4761 }
4762 }
4763 }
4764 }
4765 return SDValue();
4766}
4767
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004768SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4769 DAGCombinerInfo &DCI) const {
4770 SelectionDAG &DAG = DCI.DAG;
4771 unsigned Opcode = N->getOpcode();
4772 if (Opcode == ISD::SIGN_EXTEND) {
4773 // Convert (sext (ashr (shl X, C1), C2)) to
4774 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4775 // cheap as narrower ones.
4776 SDValue N0 = N->getOperand(0);
4777 EVT VT = N->getValueType(0);
4778 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4779 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4780 SDValue Inner = N0.getOperand(0);
4781 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4782 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4783 unsigned Extra = (VT.getSizeInBits() -
4784 N0.getValueType().getSizeInBits());
4785 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4786 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4787 EVT ShiftVT = N0.getOperand(1).getValueType();
4788 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4789 Inner.getOperand(0));
4790 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004791 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4792 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004793 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004794 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004795 }
4796 }
4797 }
4798 }
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004799 if (Opcode == SystemZISD::MERGE_HIGH ||
4800 Opcode == SystemZISD::MERGE_LOW) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004801 SDValue Op0 = N->getOperand(0);
4802 SDValue Op1 = N->getOperand(1);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004803 if (Op0.getOpcode() == ISD::BITCAST)
4804 Op0 = Op0.getOperand(0);
4805 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4806 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4807 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4808 // for v4f32.
4809 if (Op1 == N->getOperand(0))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004810 return Op1;
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004811 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4812 EVT VT = Op1.getValueType();
4813 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4814 if (ElemBytes <= 4) {
4815 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4816 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4817 EVT InVT = VT.changeVectorElementTypeToInteger();
4818 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4819 SystemZ::VectorBytes / ElemBytes / 2);
4820 if (VT != InVT) {
4821 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4822 DCI.AddToWorklist(Op1.getNode());
4823 }
4824 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4825 DCI.AddToWorklist(Op.getNode());
4826 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4827 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004828 }
4829 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004830 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4831 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4832 // If X has wider elements then convert it to:
4833 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4834 if (Opcode == ISD::STORE) {
4835 auto *SN = cast<StoreSDNode>(N);
4836 EVT MemVT = SN->getMemoryVT();
4837 if (MemVT.isInteger()) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004838 if (SDValue Value =
4839 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004840 DCI.AddToWorklist(Value.getNode());
4841
4842 // Rewrite the store with the new form of stored value.
4843 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4844 SN->getBasePtr(), SN->getMemoryVT(),
4845 SN->getMemOperand());
4846 }
4847 }
4848 }
4849 // Try to simplify a vector extraction.
4850 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4851 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4852 SDValue Op0 = N->getOperand(0);
4853 EVT VecVT = Op0.getValueType();
4854 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4855 IndexN->getZExtValue(), DCI, false);
4856 }
4857 }
4858 // (join_dwords X, X) == (replicate X)
4859 if (Opcode == SystemZISD::JOIN_DWORDS &&
4860 N->getOperand(0) == N->getOperand(1))
4861 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4862 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004863 // (fround (extract_vector_elt X 0))
4864 // (fround (extract_vector_elt X 1)) ->
4865 // (extract_vector_elt (VROUND X) 0)
4866 // (extract_vector_elt (VROUND X) 1)
4867 //
4868 // This is a special case since the target doesn't really support v2f32s.
4869 if (Opcode == ISD::FP_ROUND) {
4870 SDValue Op0 = N->getOperand(0);
4871 if (N->getValueType(0) == MVT::f32 &&
4872 Op0.hasOneUse() &&
4873 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4874 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4875 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4876 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4877 SDValue Vec = Op0.getOperand(0);
4878 for (auto *U : Vec->uses()) {
4879 if (U != Op0.getNode() &&
4880 U->hasOneUse() &&
4881 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4882 U->getOperand(0) == Vec &&
4883 U->getOperand(1).getOpcode() == ISD::Constant &&
4884 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4885 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4886 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4887 OtherRound.getOperand(0) == SDValue(U, 0) &&
4888 OtherRound.getValueType() == MVT::f32) {
4889 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4890 MVT::v4f32, Vec);
4891 DCI.AddToWorklist(VRound.getNode());
4892 SDValue Extract1 =
4893 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4894 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4895 DCI.AddToWorklist(Extract1.getNode());
4896 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4897 SDValue Extract0 =
4898 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4899 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4900 return Extract0;
4901 }
4902 }
4903 }
4904 }
4905 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004906 return SDValue();
4907}
4908
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004909//===----------------------------------------------------------------------===//
4910// Custom insertion
4911//===----------------------------------------------------------------------===//
4912
4913// Create a new basic block after MBB.
4914static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4915 MachineFunction &MF = *MBB->getParent();
4916 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004917 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004918 return NewMBB;
4919}
4920
Richard Sandifordbe133a82013-08-28 09:01:51 +00004921// Split MBB after MI and return the new block (the one that contains
4922// instructions after MI).
4923static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4924 MachineBasicBlock *MBB) {
4925 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4926 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004927 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004928 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4929 return NewMBB;
4930}
4931
Richard Sandiford5e318f02013-08-27 09:54:29 +00004932// Split MBB before MI and return the new block (the one that contains MI).
4933static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4934 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004935 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004936 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004937 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4938 return NewMBB;
4939}
4940
Richard Sandiford5e318f02013-08-27 09:54:29 +00004941// Force base value Base into a register before MI. Return the register.
4942static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4943 const SystemZInstrInfo *TII) {
4944 if (Base.isReg())
4945 return Base.getReg();
4946
4947 MachineBasicBlock *MBB = MI->getParent();
4948 MachineFunction &MF = *MBB->getParent();
4949 MachineRegisterInfo &MRI = MF.getRegInfo();
4950
4951 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4952 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4953 .addOperand(Base).addImm(0).addReg(0);
4954 return Reg;
4955}
4956
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004957// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4958MachineBasicBlock *
4959SystemZTargetLowering::emitSelect(MachineInstr *MI,
4960 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004961 const SystemZInstrInfo *TII =
4962 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004963
4964 unsigned DestReg = MI->getOperand(0).getReg();
4965 unsigned TrueReg = MI->getOperand(1).getReg();
4966 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004967 unsigned CCValid = MI->getOperand(3).getImm();
4968 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004969 DebugLoc DL = MI->getDebugLoc();
4970
4971 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004972 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004973 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4974
4975 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00004976 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004977 // # fallthrough to FalseMBB
4978 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004979 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4980 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004981 MBB->addSuccessor(JoinMBB);
4982 MBB->addSuccessor(FalseMBB);
4983
4984 // FalseMBB:
4985 // # fallthrough to JoinMBB
4986 MBB = FalseMBB;
4987 MBB->addSuccessor(JoinMBB);
4988
4989 // JoinMBB:
4990 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4991 // ...
4992 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004993 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004994 .addReg(TrueReg).addMBB(StartMBB)
4995 .addReg(FalseReg).addMBB(FalseMBB);
4996
4997 MI->eraseFromParent();
4998 return JoinMBB;
4999}
5000
Richard Sandifordb86a8342013-06-27 09:27:40 +00005001// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
5002// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005003// happen when the condition is false rather than true. If a STORE ON
5004// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00005005MachineBasicBlock *
5006SystemZTargetLowering::emitCondStore(MachineInstr *MI,
5007 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005008 unsigned StoreOpcode, unsigned STOCOpcode,
5009 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00005010 const SystemZInstrInfo *TII =
5011 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00005012
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005013 unsigned SrcReg = MI->getOperand(0).getReg();
5014 MachineOperand Base = MI->getOperand(1);
5015 int64_t Disp = MI->getOperand(2).getImm();
5016 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00005017 unsigned CCValid = MI->getOperand(4).getImm();
5018 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00005019 DebugLoc DL = MI->getDebugLoc();
5020
5021 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
5022
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005023 // Use STOCOpcode if possible. We could use different store patterns in
5024 // order to avoid matching the index register, but the performance trade-offs
5025 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00005026 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005027 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005028 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005029 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00005030 .addReg(SrcReg).addOperand(Base).addImm(Disp)
5031 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005032 MI->eraseFromParent();
5033 return MBB;
5034 }
5035
Richard Sandifordb86a8342013-06-27 09:27:40 +00005036 // Get the condition needed to branch around the store.
5037 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00005038 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00005039
5040 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005041 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005042 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
5043
5044 // StartMBB:
5045 // BRC CCMask, JoinMBB
5046 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00005047 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00005048 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5049 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005050 MBB->addSuccessor(JoinMBB);
5051 MBB->addSuccessor(FalseMBB);
5052
5053 // FalseMBB:
5054 // store %SrcReg, %Disp(%Index,%Base)
5055 // # fallthrough to JoinMBB
5056 MBB = FalseMBB;
5057 BuildMI(MBB, DL, TII->get(StoreOpcode))
5058 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5059 MBB->addSuccessor(JoinMBB);
5060
5061 MI->eraseFromParent();
5062 return JoinMBB;
5063}
5064
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005065// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5066// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
5067// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5068// BitSize is the width of the field in bits, or 0 if this is a partword
5069// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5070// is one of the operands. Invert says whether the field should be
5071// inverted after performing BinOpcode (e.g. for NAND).
5072MachineBasicBlock *
5073SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
5074 MachineBasicBlock *MBB,
5075 unsigned BinOpcode,
5076 unsigned BitSize,
5077 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005078 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005079 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005080 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005081 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005082 bool IsSubWord = (BitSize < 32);
5083
5084 // Extract the operands. Base can be a register or a frame index.
5085 // Src2 can be a register or immediate.
5086 unsigned Dest = MI->getOperand(0).getReg();
5087 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5088 int64_t Disp = MI->getOperand(2).getImm();
5089 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
5090 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5091 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5092 DebugLoc DL = MI->getDebugLoc();
5093 if (IsSubWord)
5094 BitSize = MI->getOperand(6).getImm();
5095
5096 // Subword operations use 32-bit registers.
5097 const TargetRegisterClass *RC = (BitSize <= 32 ?
5098 &SystemZ::GR32BitRegClass :
5099 &SystemZ::GR64BitRegClass);
5100 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5101 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5102
5103 // Get the right opcodes for the displacement.
5104 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5105 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5106 assert(LOpcode && CSOpcode && "Displacement out of range");
5107
5108 // Create virtual registers for temporary results.
5109 unsigned OrigVal = MRI.createVirtualRegister(RC);
5110 unsigned OldVal = MRI.createVirtualRegister(RC);
5111 unsigned NewVal = (BinOpcode || IsSubWord ?
5112 MRI.createVirtualRegister(RC) : Src2.getReg());
5113 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5114 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5115
5116 // Insert a basic block for the main loop.
5117 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005118 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005119 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5120
5121 // StartMBB:
5122 // ...
5123 // %OrigVal = L Disp(%Base)
5124 // # fall through to LoopMMB
5125 MBB = StartMBB;
5126 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5127 .addOperand(Base).addImm(Disp).addReg(0);
5128 MBB->addSuccessor(LoopMBB);
5129
5130 // LoopMBB:
5131 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5132 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5133 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5134 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5135 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5136 // JNE LoopMBB
5137 // # fall through to DoneMMB
5138 MBB = LoopMBB;
5139 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5140 .addReg(OrigVal).addMBB(StartMBB)
5141 .addReg(Dest).addMBB(LoopMBB);
5142 if (IsSubWord)
5143 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5144 .addReg(OldVal).addReg(BitShift).addImm(0);
5145 if (Invert) {
5146 // Perform the operation normally and then invert every bit of the field.
5147 unsigned Tmp = MRI.createVirtualRegister(RC);
5148 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5149 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005150 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005151 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005152 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005153 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005154 else {
5155 // Use LCGR and add -1 to the result, which is more compact than
5156 // an XILF, XILH pair.
5157 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5158 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5159 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5160 .addReg(Tmp2).addImm(-1);
5161 }
5162 } else if (BinOpcode)
5163 // A simply binary operation.
5164 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5165 .addReg(RotatedOldVal).addOperand(Src2);
5166 else if (IsSubWord)
5167 // Use RISBG to rotate Src2 into position and use it to replace the
5168 // field in RotatedOldVal.
5169 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5170 .addReg(RotatedOldVal).addReg(Src2.getReg())
5171 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5172 if (IsSubWord)
5173 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5174 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5175 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5176 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005177 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5178 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005179 MBB->addSuccessor(LoopMBB);
5180 MBB->addSuccessor(DoneMBB);
5181
5182 MI->eraseFromParent();
5183 return DoneMBB;
5184}
5185
5186// Implement EmitInstrWithCustomInserter for pseudo
5187// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5188// instruction that should be used to compare the current field with the
5189// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5190// for when the current field should be kept. BitSize is the width of
5191// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5192MachineBasicBlock *
5193SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5194 MachineBasicBlock *MBB,
5195 unsigned CompareOpcode,
5196 unsigned KeepOldMask,
5197 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005198 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005199 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005200 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005201 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005202 bool IsSubWord = (BitSize < 32);
5203
5204 // Extract the operands. Base can be a register or a frame index.
5205 unsigned Dest = MI->getOperand(0).getReg();
5206 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5207 int64_t Disp = MI->getOperand(2).getImm();
5208 unsigned Src2 = MI->getOperand(3).getReg();
5209 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5210 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5211 DebugLoc DL = MI->getDebugLoc();
5212 if (IsSubWord)
5213 BitSize = MI->getOperand(6).getImm();
5214
5215 // Subword operations use 32-bit registers.
5216 const TargetRegisterClass *RC = (BitSize <= 32 ?
5217 &SystemZ::GR32BitRegClass :
5218 &SystemZ::GR64BitRegClass);
5219 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5220 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5221
5222 // Get the right opcodes for the displacement.
5223 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5224 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5225 assert(LOpcode && CSOpcode && "Displacement out of range");
5226
5227 // Create virtual registers for temporary results.
5228 unsigned OrigVal = MRI.createVirtualRegister(RC);
5229 unsigned OldVal = MRI.createVirtualRegister(RC);
5230 unsigned NewVal = MRI.createVirtualRegister(RC);
5231 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5232 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5233 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5234
5235 // Insert 3 basic blocks for the loop.
5236 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005237 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005238 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5239 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5240 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5241
5242 // StartMBB:
5243 // ...
5244 // %OrigVal = L Disp(%Base)
5245 // # fall through to LoopMMB
5246 MBB = StartMBB;
5247 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5248 .addOperand(Base).addImm(Disp).addReg(0);
5249 MBB->addSuccessor(LoopMBB);
5250
5251 // LoopMBB:
5252 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5253 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5254 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005255 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005256 MBB = LoopMBB;
5257 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5258 .addReg(OrigVal).addMBB(StartMBB)
5259 .addReg(Dest).addMBB(UpdateMBB);
5260 if (IsSubWord)
5261 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5262 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005263 BuildMI(MBB, DL, TII->get(CompareOpcode))
5264 .addReg(RotatedOldVal).addReg(Src2);
5265 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005266 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005267 MBB->addSuccessor(UpdateMBB);
5268 MBB->addSuccessor(UseAltMBB);
5269
5270 // UseAltMBB:
5271 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5272 // # fall through to UpdateMMB
5273 MBB = UseAltMBB;
5274 if (IsSubWord)
5275 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5276 .addReg(RotatedOldVal).addReg(Src2)
5277 .addImm(32).addImm(31 + BitSize).addImm(0);
5278 MBB->addSuccessor(UpdateMBB);
5279
5280 // UpdateMBB:
5281 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5282 // [ %RotatedAltVal, UseAltMBB ]
5283 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5284 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5285 // JNE LoopMBB
5286 // # fall through to DoneMMB
5287 MBB = UpdateMBB;
5288 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5289 .addReg(RotatedOldVal).addMBB(LoopMBB)
5290 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5291 if (IsSubWord)
5292 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5293 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5294 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5295 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005296 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5297 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005298 MBB->addSuccessor(LoopMBB);
5299 MBB->addSuccessor(DoneMBB);
5300
5301 MI->eraseFromParent();
5302 return DoneMBB;
5303}
5304
5305// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5306// instruction MI.
5307MachineBasicBlock *
5308SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5309 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005310 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005311 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005312 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005313 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005314
5315 // Extract the operands. Base can be a register or a frame index.
5316 unsigned Dest = MI->getOperand(0).getReg();
5317 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5318 int64_t Disp = MI->getOperand(2).getImm();
5319 unsigned OrigCmpVal = MI->getOperand(3).getReg();
5320 unsigned OrigSwapVal = MI->getOperand(4).getReg();
5321 unsigned BitShift = MI->getOperand(5).getReg();
5322 unsigned NegBitShift = MI->getOperand(6).getReg();
5323 int64_t BitSize = MI->getOperand(7).getImm();
5324 DebugLoc DL = MI->getDebugLoc();
5325
5326 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5327
5328 // Get the right opcodes for the displacement.
5329 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5330 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5331 assert(LOpcode && CSOpcode && "Displacement out of range");
5332
5333 // Create virtual registers for temporary results.
5334 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5335 unsigned OldVal = MRI.createVirtualRegister(RC);
5336 unsigned CmpVal = MRI.createVirtualRegister(RC);
5337 unsigned SwapVal = MRI.createVirtualRegister(RC);
5338 unsigned StoreVal = MRI.createVirtualRegister(RC);
5339 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5340 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5341 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5342
5343 // Insert 2 basic blocks for the loop.
5344 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005345 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005346 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5347 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5348
5349 // StartMBB:
5350 // ...
5351 // %OrigOldVal = L Disp(%Base)
5352 // # fall through to LoopMMB
5353 MBB = StartMBB;
5354 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5355 .addOperand(Base).addImm(Disp).addReg(0);
5356 MBB->addSuccessor(LoopMBB);
5357
5358 // LoopMBB:
5359 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5360 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5361 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5362 // %Dest = RLL %OldVal, BitSize(%BitShift)
5363 // ^^ The low BitSize bits contain the field
5364 // of interest.
5365 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5366 // ^^ Replace the upper 32-BitSize bits of the
5367 // comparison value with those that we loaded,
5368 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005369 // CR %Dest, %RetryCmpVal
5370 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005371 // # Fall through to SetMBB
5372 MBB = LoopMBB;
5373 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5374 .addReg(OrigOldVal).addMBB(StartMBB)
5375 .addReg(RetryOldVal).addMBB(SetMBB);
5376 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5377 .addReg(OrigCmpVal).addMBB(StartMBB)
5378 .addReg(RetryCmpVal).addMBB(SetMBB);
5379 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5380 .addReg(OrigSwapVal).addMBB(StartMBB)
5381 .addReg(RetrySwapVal).addMBB(SetMBB);
5382 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5383 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5384 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5385 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005386 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5387 .addReg(Dest).addReg(RetryCmpVal);
5388 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005389 .addImm(SystemZ::CCMASK_ICMP)
5390 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005391 MBB->addSuccessor(DoneMBB);
5392 MBB->addSuccessor(SetMBB);
5393
5394 // SetMBB:
5395 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5396 // ^^ Replace the upper 32-BitSize bits of the new
5397 // value with those that we loaded.
5398 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5399 // ^^ Rotate the new field to its proper position.
5400 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5401 // JNE LoopMBB
5402 // # fall through to ExitMMB
5403 MBB = SetMBB;
5404 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5405 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5406 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5407 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5408 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5409 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005410 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5411 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005412 MBB->addSuccessor(LoopMBB);
5413 MBB->addSuccessor(DoneMBB);
5414
5415 MI->eraseFromParent();
5416 return DoneMBB;
5417}
5418
5419// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5420// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005421// it's "don't care". SubReg is subreg_l32 when extending a GR32
5422// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005423MachineBasicBlock *
5424SystemZTargetLowering::emitExt128(MachineInstr *MI,
5425 MachineBasicBlock *MBB,
5426 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005427 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005428 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005429 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005430 MachineRegisterInfo &MRI = MF.getRegInfo();
5431 DebugLoc DL = MI->getDebugLoc();
5432
5433 unsigned Dest = MI->getOperand(0).getReg();
5434 unsigned Src = MI->getOperand(1).getReg();
5435 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5436
5437 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5438 if (ClearEven) {
5439 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5440 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5441
5442 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5443 .addImm(0);
5444 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005445 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005446 In128 = NewIn128;
5447 }
5448 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5449 .addReg(In128).addReg(Src).addImm(SubReg);
5450
5451 MI->eraseFromParent();
5452 return MBB;
5453}
5454
Richard Sandifordd131ff82013-07-08 09:35:23 +00005455MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00005456SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5457 MachineBasicBlock *MBB,
5458 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005459 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005460 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005461 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005462 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005463 DebugLoc DL = MI->getDebugLoc();
5464
Richard Sandiford5e318f02013-08-27 09:54:29 +00005465 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005466 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005467 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005468 uint64_t SrcDisp = MI->getOperand(3).getImm();
5469 uint64_t Length = MI->getOperand(4).getImm();
5470
Richard Sandifordbe133a82013-08-28 09:01:51 +00005471 // When generating more than one CLC, all but the last will need to
5472 // branch to the end when a difference is found.
5473 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005474 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005475
Richard Sandiford5e318f02013-08-27 09:54:29 +00005476 // Check for the loop form, in which operand 5 is the trip count.
5477 if (MI->getNumExplicitOperands() > 5) {
5478 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5479
5480 uint64_t StartCountReg = MI->getOperand(5).getReg();
5481 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5482 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5483 forceReg(MI, DestBase, TII));
5484
5485 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5486 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5487 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5488 MRI.createVirtualRegister(RC));
5489 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5490 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5491 MRI.createVirtualRegister(RC));
5492
5493 RC = &SystemZ::GR64BitRegClass;
5494 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5495 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5496
5497 MachineBasicBlock *StartMBB = MBB;
5498 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5499 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005500 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005501
5502 // StartMBB:
5503 // # fall through to LoopMMB
5504 MBB->addSuccessor(LoopMBB);
5505
5506 // LoopMBB:
5507 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005508 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005509 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005510 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005511 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005512 // [ %NextCountReg, NextMBB ]
5513 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005514 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005515 // ( JLH EndMBB )
5516 //
5517 // The prefetch is used only for MVC. The JLH is used only for CLC.
5518 MBB = LoopMBB;
5519
5520 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5521 .addReg(StartDestReg).addMBB(StartMBB)
5522 .addReg(NextDestReg).addMBB(NextMBB);
5523 if (!HaveSingleBase)
5524 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5525 .addReg(StartSrcReg).addMBB(StartMBB)
5526 .addReg(NextSrcReg).addMBB(NextMBB);
5527 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5528 .addReg(StartCountReg).addMBB(StartMBB)
5529 .addReg(NextCountReg).addMBB(NextMBB);
5530 if (Opcode == SystemZ::MVC)
5531 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5532 .addImm(SystemZ::PFD_WRITE)
5533 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5534 BuildMI(MBB, DL, TII->get(Opcode))
5535 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5536 .addReg(ThisSrcReg).addImm(SrcDisp);
5537 if (EndMBB) {
5538 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5539 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5540 .addMBB(EndMBB);
5541 MBB->addSuccessor(EndMBB);
5542 MBB->addSuccessor(NextMBB);
5543 }
5544
5545 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005546 // %NextDestReg = LA 256(%ThisDestReg)
5547 // %NextSrcReg = LA 256(%ThisSrcReg)
5548 // %NextCountReg = AGHI %ThisCountReg, -1
5549 // CGHI %NextCountReg, 0
5550 // JLH LoopMBB
5551 // # fall through to DoneMMB
5552 //
5553 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005554 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005555
Richard Sandiford5e318f02013-08-27 09:54:29 +00005556 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5557 .addReg(ThisDestReg).addImm(256).addReg(0);
5558 if (!HaveSingleBase)
5559 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5560 .addReg(ThisSrcReg).addImm(256).addReg(0);
5561 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5562 .addReg(ThisCountReg).addImm(-1);
5563 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5564 .addReg(NextCountReg).addImm(0);
5565 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5566 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5567 .addMBB(LoopMBB);
5568 MBB->addSuccessor(LoopMBB);
5569 MBB->addSuccessor(DoneMBB);
5570
5571 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5572 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5573 Length &= 255;
5574 MBB = DoneMBB;
5575 }
5576 // Handle any remaining bytes with straight-line code.
5577 while (Length > 0) {
5578 uint64_t ThisLength = std::min(Length, uint64_t(256));
5579 // The previous iteration might have created out-of-range displacements.
5580 // Apply them using LAY if so.
5581 if (!isUInt<12>(DestDisp)) {
5582 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5583 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5584 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5585 DestBase = MachineOperand::CreateReg(Reg, false);
5586 DestDisp = 0;
5587 }
5588 if (!isUInt<12>(SrcDisp)) {
5589 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5590 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5591 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5592 SrcBase = MachineOperand::CreateReg(Reg, false);
5593 SrcDisp = 0;
5594 }
5595 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5596 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5597 .addOperand(SrcBase).addImm(SrcDisp);
5598 DestDisp += ThisLength;
5599 SrcDisp += ThisLength;
5600 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005601 // If there's another CLC to go, branch to the end if a difference
5602 // was found.
5603 if (EndMBB && Length > 0) {
5604 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5605 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5606 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5607 .addMBB(EndMBB);
5608 MBB->addSuccessor(EndMBB);
5609 MBB->addSuccessor(NextMBB);
5610 MBB = NextMBB;
5611 }
5612 }
5613 if (EndMBB) {
5614 MBB->addSuccessor(EndMBB);
5615 MBB = EndMBB;
5616 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005617 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005618
5619 MI->eraseFromParent();
5620 return MBB;
5621}
5622
Richard Sandifordca232712013-08-16 11:21:54 +00005623// Decompose string pseudo-instruction MI into a loop that continually performs
5624// Opcode until CC != 3.
5625MachineBasicBlock *
5626SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5627 MachineBasicBlock *MBB,
5628 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005629 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005630 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005631 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005632 MachineRegisterInfo &MRI = MF.getRegInfo();
5633 DebugLoc DL = MI->getDebugLoc();
5634
5635 uint64_t End1Reg = MI->getOperand(0).getReg();
5636 uint64_t Start1Reg = MI->getOperand(1).getReg();
5637 uint64_t Start2Reg = MI->getOperand(2).getReg();
5638 uint64_t CharReg = MI->getOperand(3).getReg();
5639
5640 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5641 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5642 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5643 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5644
5645 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005646 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005647 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5648
5649 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005650 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005651 MBB->addSuccessor(LoopMBB);
5652
5653 // LoopMBB:
5654 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5655 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005656 // R0L = %CharReg
5657 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005658 // JO LoopMBB
5659 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005660 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005661 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005662 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005663
5664 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5665 .addReg(Start1Reg).addMBB(StartMBB)
5666 .addReg(End1Reg).addMBB(LoopMBB);
5667 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5668 .addReg(Start2Reg).addMBB(StartMBB)
5669 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005670 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005671 BuildMI(MBB, DL, TII->get(Opcode))
5672 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5673 .addReg(This1Reg).addReg(This2Reg);
5674 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5675 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5676 MBB->addSuccessor(LoopMBB);
5677 MBB->addSuccessor(DoneMBB);
5678
5679 DoneMBB->addLiveIn(SystemZ::CC);
5680
5681 MI->eraseFromParent();
5682 return DoneMBB;
5683}
5684
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005685// Update TBEGIN instruction with final opcode and register clobbers.
5686MachineBasicBlock *
5687SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5688 MachineBasicBlock *MBB,
5689 unsigned Opcode,
5690 bool NoFloat) const {
5691 MachineFunction &MF = *MBB->getParent();
5692 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5693 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5694
5695 // Update opcode.
5696 MI->setDesc(TII->get(Opcode));
5697
5698 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5699 // Make sure to add the corresponding GRSM bits if they are missing.
5700 uint64_t Control = MI->getOperand(2).getImm();
5701 static const unsigned GPRControlBit[16] = {
5702 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5703 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5704 };
5705 Control |= GPRControlBit[15];
5706 if (TFI->hasFP(MF))
5707 Control |= GPRControlBit[11];
5708 MI->getOperand(2).setImm(Control);
5709
5710 // Add GPR clobbers.
5711 for (int I = 0; I < 16; I++) {
5712 if ((Control & GPRControlBit[I]) == 0) {
5713 unsigned Reg = SystemZMC::GR64Regs[I];
5714 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5715 }
5716 }
5717
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005718 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005719 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005720 if (Subtarget.hasVector()) {
5721 for (int I = 0; I < 32; I++) {
5722 unsigned Reg = SystemZMC::VR128Regs[I];
5723 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5724 }
5725 } else {
5726 for (int I = 0; I < 16; I++) {
5727 unsigned Reg = SystemZMC::FP64Regs[I];
5728 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5729 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005730 }
5731 }
5732
5733 return MBB;
5734}
5735
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005736MachineBasicBlock *
5737SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
NAKAMURA Takumi50df0c22015-11-02 01:38:12 +00005738 MachineBasicBlock *MBB,
5739 unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005740 MachineFunction &MF = *MBB->getParent();
5741 MachineRegisterInfo *MRI = &MF.getRegInfo();
5742 const SystemZInstrInfo *TII =
5743 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5744 DebugLoc DL = MI->getDebugLoc();
5745
5746 unsigned SrcReg = MI->getOperand(0).getReg();
5747
5748 // Create new virtual register of the same class as source.
5749 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5750 unsigned DstReg = MRI->createVirtualRegister(RC);
5751
5752 // Replace pseudo with a normal load-and-test that models the def as
5753 // well.
5754 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5755 .addReg(SrcReg);
5756 MI->eraseFromParent();
5757
5758 return MBB;
5759}
5760
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005761MachineBasicBlock *SystemZTargetLowering::
5762EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5763 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005764 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005765 case SystemZ::Select32:
5766 case SystemZ::SelectF32:
5767 case SystemZ::Select64:
5768 case SystemZ::SelectF64:
5769 case SystemZ::SelectF128:
5770 return emitSelect(MI, MBB);
5771
Richard Sandiford2896d042013-10-01 14:33:55 +00005772 case SystemZ::CondStore8Mux:
5773 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5774 case SystemZ::CondStore8MuxInv:
5775 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5776 case SystemZ::CondStore16Mux:
5777 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5778 case SystemZ::CondStore16MuxInv:
5779 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005780 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005781 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005782 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005783 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005784 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005785 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005786 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005787 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005788 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005789 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005790 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005791 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005792 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005793 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005794 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005795 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005796 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005797 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005798 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005799 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005800 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005801 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005802 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005803 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005804
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005805 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005806 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005807 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005808 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005809 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005810 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005811
5812 case SystemZ::ATOMIC_SWAPW:
5813 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5814 case SystemZ::ATOMIC_SWAP_32:
5815 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5816 case SystemZ::ATOMIC_SWAP_64:
5817 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5818
5819 case SystemZ::ATOMIC_LOADW_AR:
5820 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5821 case SystemZ::ATOMIC_LOADW_AFI:
5822 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5823 case SystemZ::ATOMIC_LOAD_AR:
5824 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5825 case SystemZ::ATOMIC_LOAD_AHI:
5826 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5827 case SystemZ::ATOMIC_LOAD_AFI:
5828 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5829 case SystemZ::ATOMIC_LOAD_AGR:
5830 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5831 case SystemZ::ATOMIC_LOAD_AGHI:
5832 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5833 case SystemZ::ATOMIC_LOAD_AGFI:
5834 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5835
5836 case SystemZ::ATOMIC_LOADW_SR:
5837 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5838 case SystemZ::ATOMIC_LOAD_SR:
5839 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5840 case SystemZ::ATOMIC_LOAD_SGR:
5841 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5842
5843 case SystemZ::ATOMIC_LOADW_NR:
5844 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5845 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005846 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005847 case SystemZ::ATOMIC_LOAD_NR:
5848 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005849 case SystemZ::ATOMIC_LOAD_NILL:
5850 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5851 case SystemZ::ATOMIC_LOAD_NILH:
5852 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5853 case SystemZ::ATOMIC_LOAD_NILF:
5854 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005855 case SystemZ::ATOMIC_LOAD_NGR:
5856 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005857 case SystemZ::ATOMIC_LOAD_NILL64:
5858 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5859 case SystemZ::ATOMIC_LOAD_NILH64:
5860 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005861 case SystemZ::ATOMIC_LOAD_NIHL64:
5862 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5863 case SystemZ::ATOMIC_LOAD_NIHH64:
5864 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005865 case SystemZ::ATOMIC_LOAD_NILF64:
5866 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005867 case SystemZ::ATOMIC_LOAD_NIHF64:
5868 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005869
5870 case SystemZ::ATOMIC_LOADW_OR:
5871 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5872 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005873 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005874 case SystemZ::ATOMIC_LOAD_OR:
5875 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005876 case SystemZ::ATOMIC_LOAD_OILL:
5877 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5878 case SystemZ::ATOMIC_LOAD_OILH:
5879 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5880 case SystemZ::ATOMIC_LOAD_OILF:
5881 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005882 case SystemZ::ATOMIC_LOAD_OGR:
5883 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005884 case SystemZ::ATOMIC_LOAD_OILL64:
5885 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5886 case SystemZ::ATOMIC_LOAD_OILH64:
5887 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005888 case SystemZ::ATOMIC_LOAD_OIHL64:
5889 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5890 case SystemZ::ATOMIC_LOAD_OIHH64:
5891 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005892 case SystemZ::ATOMIC_LOAD_OILF64:
5893 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005894 case SystemZ::ATOMIC_LOAD_OIHF64:
5895 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005896
5897 case SystemZ::ATOMIC_LOADW_XR:
5898 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5899 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005900 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005901 case SystemZ::ATOMIC_LOAD_XR:
5902 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005903 case SystemZ::ATOMIC_LOAD_XILF:
5904 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005905 case SystemZ::ATOMIC_LOAD_XGR:
5906 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005907 case SystemZ::ATOMIC_LOAD_XILF64:
5908 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005909 case SystemZ::ATOMIC_LOAD_XIHF64:
5910 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005911
5912 case SystemZ::ATOMIC_LOADW_NRi:
5913 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5914 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005915 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005916 case SystemZ::ATOMIC_LOAD_NRi:
5917 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005918 case SystemZ::ATOMIC_LOAD_NILLi:
5919 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5920 case SystemZ::ATOMIC_LOAD_NILHi:
5921 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5922 case SystemZ::ATOMIC_LOAD_NILFi:
5923 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005924 case SystemZ::ATOMIC_LOAD_NGRi:
5925 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005926 case SystemZ::ATOMIC_LOAD_NILL64i:
5927 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5928 case SystemZ::ATOMIC_LOAD_NILH64i:
5929 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005930 case SystemZ::ATOMIC_LOAD_NIHL64i:
5931 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5932 case SystemZ::ATOMIC_LOAD_NIHH64i:
5933 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005934 case SystemZ::ATOMIC_LOAD_NILF64i:
5935 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005936 case SystemZ::ATOMIC_LOAD_NIHF64i:
5937 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005938
5939 case SystemZ::ATOMIC_LOADW_MIN:
5940 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5941 SystemZ::CCMASK_CMP_LE, 0);
5942 case SystemZ::ATOMIC_LOAD_MIN_32:
5943 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5944 SystemZ::CCMASK_CMP_LE, 32);
5945 case SystemZ::ATOMIC_LOAD_MIN_64:
5946 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5947 SystemZ::CCMASK_CMP_LE, 64);
5948
5949 case SystemZ::ATOMIC_LOADW_MAX:
5950 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5951 SystemZ::CCMASK_CMP_GE, 0);
5952 case SystemZ::ATOMIC_LOAD_MAX_32:
5953 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5954 SystemZ::CCMASK_CMP_GE, 32);
5955 case SystemZ::ATOMIC_LOAD_MAX_64:
5956 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5957 SystemZ::CCMASK_CMP_GE, 64);
5958
5959 case SystemZ::ATOMIC_LOADW_UMIN:
5960 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5961 SystemZ::CCMASK_CMP_LE, 0);
5962 case SystemZ::ATOMIC_LOAD_UMIN_32:
5963 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5964 SystemZ::CCMASK_CMP_LE, 32);
5965 case SystemZ::ATOMIC_LOAD_UMIN_64:
5966 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5967 SystemZ::CCMASK_CMP_LE, 64);
5968
5969 case SystemZ::ATOMIC_LOADW_UMAX:
5970 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5971 SystemZ::CCMASK_CMP_GE, 0);
5972 case SystemZ::ATOMIC_LOAD_UMAX_32:
5973 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5974 SystemZ::CCMASK_CMP_GE, 32);
5975 case SystemZ::ATOMIC_LOAD_UMAX_64:
5976 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5977 SystemZ::CCMASK_CMP_GE, 64);
5978
5979 case SystemZ::ATOMIC_CMP_SWAPW:
5980 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005981 case SystemZ::MVCSequence:
5982 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005983 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00005984 case SystemZ::NCSequence:
5985 case SystemZ::NCLoop:
5986 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5987 case SystemZ::OCSequence:
5988 case SystemZ::OCLoop:
5989 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5990 case SystemZ::XCSequence:
5991 case SystemZ::XCLoop:
5992 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005993 case SystemZ::CLCSequence:
5994 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005995 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00005996 case SystemZ::CLSTLoop:
5997 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00005998 case SystemZ::MVSTLoop:
5999 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00006000 case SystemZ::SRSTLoop:
6001 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00006002 case SystemZ::TBEGIN:
6003 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
6004 case SystemZ::TBEGIN_nofloat:
6005 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
6006 case SystemZ::TBEGINC:
6007 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00006008 case SystemZ::LTEBRCompare_VecPseudo:
6009 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
6010 case SystemZ::LTDBRCompare_VecPseudo:
6011 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
6012 case SystemZ::LTXBRCompare_VecPseudo:
6013 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
6014
Ulrich Weigand5f613df2013-05-06 16:15:19 +00006015 default:
6016 llvm_unreachable("Unexpected instr type to insert");
6017 }
6018}