blob: 4140be7bad0eb87fd7f6dc05b675365764523667 [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);
816 else if (VA.getLocInfo() == CCValAssign::Indirect)
817 Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
818 MachinePointerInfo(), false, false, false, 0);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000819 else if (VA.getLocInfo() == CCValAssign::BCvt) {
820 // If this is a short vector argument loaded from the stack,
821 // extend from i64 to full vector size and then bitcast.
822 assert(VA.getLocVT() == MVT::i64);
823 assert(VA.getValVT().isVector());
824 Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64,
825 Value, DAG.getUNDEF(MVT::i64));
826 Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
827 } else
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000828 assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
829 return Value;
830}
831
832// Value is a value of type VA.getValVT() that we need to copy into
833// the location described by VA. Return a copy of Value converted to
834// VA.getValVT(). The caller is responsible for handling indirect values.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000835static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000836 CCValAssign &VA, SDValue Value) {
837 switch (VA.getLocInfo()) {
838 case CCValAssign::SExt:
839 return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
840 case CCValAssign::ZExt:
841 return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
842 case CCValAssign::AExt:
843 return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +0000844 case CCValAssign::BCvt:
845 // If this is a short vector argument to be stored to the stack,
846 // bitcast to v2i64 and then extract first element.
847 assert(VA.getLocVT() == MVT::i64);
848 assert(VA.getValVT().isVector());
849 Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
850 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
851 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000852 case CCValAssign::Full:
853 return Value;
854 default:
855 llvm_unreachable("Unhandled getLocInfo()");
856 }
857}
858
859SDValue SystemZTargetLowering::
860LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
861 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000862 SDLoc DL, SelectionDAG &DAG,
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000863 SmallVectorImpl<SDValue> &InVals) const {
864 MachineFunction &MF = DAG.getMachineFunction();
865 MachineFrameInfo *MFI = MF.getFrameInfo();
866 MachineRegisterInfo &MRI = MF.getRegInfo();
867 SystemZMachineFunctionInfo *FuncInfo =
Eric Christophera6734172015-01-31 00:06:45 +0000868 MF.getInfo<SystemZMachineFunctionInfo>();
869 auto *TFL =
870 static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000871
Ulrich Weigand5211f9f2015-05-05 19:30:05 +0000872 // Detect unsupported vector argument types.
873 if (Subtarget.hasVector())
874 VerifyVectorTypes(Ins);
875
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000876 // Assign locations to all of the incoming arguments.
877 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000878 SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000879 CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
880
881 unsigned NumFixedGPRs = 0;
882 unsigned NumFixedFPRs = 0;
883 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
884 SDValue ArgValue;
885 CCValAssign &VA = ArgLocs[I];
886 EVT LocVT = VA.getLocVT();
887 if (VA.isRegLoc()) {
888 // Arguments passed in registers
889 const TargetRegisterClass *RC;
890 switch (LocVT.getSimpleVT().SimpleTy) {
891 default:
892 // Integers smaller than i64 should be promoted to i64.
893 llvm_unreachable("Unexpected argument type");
894 case MVT::i32:
895 NumFixedGPRs += 1;
896 RC = &SystemZ::GR32BitRegClass;
897 break;
898 case MVT::i64:
899 NumFixedGPRs += 1;
900 RC = &SystemZ::GR64BitRegClass;
901 break;
902 case MVT::f32:
903 NumFixedFPRs += 1;
904 RC = &SystemZ::FP32BitRegClass;
905 break;
906 case MVT::f64:
907 NumFixedFPRs += 1;
908 RC = &SystemZ::FP64BitRegClass;
909 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000910 case MVT::v16i8:
911 case MVT::v8i16:
912 case MVT::v4i32:
913 case MVT::v2i64:
Ulrich Weigand80b3af72015-05-05 19:27:45 +0000914 case MVT::v4f32:
Ulrich Weigandcd808232015-05-05 19:26:48 +0000915 case MVT::v2f64:
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000916 RC = &SystemZ::VR128BitRegClass;
917 break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000918 }
919
920 unsigned VReg = MRI.createVirtualRegister(RC);
921 MRI.addLiveIn(VA.getLocReg(), VReg);
922 ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
923 } else {
924 assert(VA.isMemLoc() && "Argument not register or memory");
925
926 // Create the frame index object for this incoming parameter.
927 int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
928 VA.getLocMemOffset(), true);
929
930 // Create the SelectionDAG nodes corresponding to a load
931 // from this parameter. Unpromoted ints and floats are
932 // passed as right-justified 8-byte values.
Mehdi Amini44ede332015-07-09 02:09:04 +0000933 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000934 SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
935 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000936 FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
937 DAG.getIntPtrConstant(4, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000938 ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000939 MachinePointerInfo::getFixedStack(MF, FI), false,
940 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000941 }
942
943 // Convert the value of the argument register into the value that's
944 // being passed.
945 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
946 }
947
948 if (IsVarArg) {
949 // Save the number of non-varargs registers for later use by va_start, etc.
950 FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
951 FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
952
953 // Likewise the address (in the form of a frame index) of where the
954 // first stack vararg would be. The 1-byte size here is arbitrary.
955 int64_t StackSize = CCInfo.getNextStackOffset();
956 FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
957
958 // ...and a similar frame index for the caller-allocated save area
959 // that will be used to store the incoming registers.
960 int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
961 unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
962 FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
963
964 // Store the FPR varargs in the reserved frame slots. (We store the
965 // GPRs as part of the prologue.)
966 if (NumFixedFPRs < SystemZ::NumArgFPRs) {
967 SDValue MemOps[SystemZ::NumArgFPRs];
968 for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
969 unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
970 int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
Mehdi Amini44ede332015-07-09 02:09:04 +0000971 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000972 unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
973 &SystemZ::FP64BitRegClass);
974 SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
975 MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
Alex Lorenze40c8a22015-08-11 23:09:45 +0000976 MachinePointerInfo::getFixedStack(MF, FI),
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000977 false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000978 }
979 // Join the stores, which are independent of one another.
980 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
Craig Topper2d2aa0c2014-04-30 07:17:30 +0000981 makeArrayRef(&MemOps[NumFixedFPRs],
982 SystemZ::NumArgFPRs-NumFixedFPRs));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000983 }
984 }
985
986 return Chain;
987}
988
Benjamin Kramerc6cc58e2014-10-04 16:55:56 +0000989static bool canUseSiblingCall(const CCState &ArgCCInfo,
Richard Sandiford709bda62013-08-19 12:42:31 +0000990 SmallVectorImpl<CCValAssign> &ArgLocs) {
991 // Punt if there are any indirect or stack arguments, or if the call
992 // needs the call-saved argument register R6.
993 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
994 CCValAssign &VA = ArgLocs[I];
995 if (VA.getLocInfo() == CCValAssign::Indirect)
996 return false;
997 if (!VA.isRegLoc())
998 return false;
999 unsigned Reg = VA.getLocReg();
Richard Sandiford0755c932013-10-01 11:26:28 +00001000 if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
Richard Sandiford709bda62013-08-19 12:42:31 +00001001 return false;
1002 }
1003 return true;
1004}
1005
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001006SDValue
1007SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1008 SmallVectorImpl<SDValue> &InVals) const {
1009 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001010 SDLoc &DL = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +00001011 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1012 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1013 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001014 SDValue Chain = CLI.Chain;
1015 SDValue Callee = CLI.Callee;
Richard Sandiford709bda62013-08-19 12:42:31 +00001016 bool &IsTailCall = CLI.IsTailCall;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001017 CallingConv::ID CallConv = CLI.CallConv;
1018 bool IsVarArg = CLI.IsVarArg;
1019 MachineFunction &MF = DAG.getMachineFunction();
Mehdi Amini44ede332015-07-09 02:09:04 +00001020 EVT PtrVT = getPointerTy(MF.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001021
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001022 // Detect unsupported vector argument and return types.
1023 if (Subtarget.hasVector()) {
1024 VerifyVectorTypes(Outs);
1025 VerifyVectorTypes(Ins);
1026 }
1027
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001028 // Analyze the operands of the call, assigning locations to each operand.
1029 SmallVector<CCValAssign, 16> ArgLocs;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001030 SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001031 ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1032
Richard Sandiford709bda62013-08-19 12:42:31 +00001033 // We don't support GuaranteedTailCallOpt, only automatically-detected
1034 // sibling calls.
1035 if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
1036 IsTailCall = false;
1037
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001038 // Get a count of how many bytes are to be pushed on the stack.
1039 unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1040
1041 // Mark the start of the call.
Richard Sandiford709bda62013-08-19 12:42:31 +00001042 if (!IsTailCall)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001043 Chain = DAG.getCALLSEQ_START(Chain,
1044 DAG.getConstant(NumBytes, DL, PtrVT, true),
Richard Sandiford709bda62013-08-19 12:42:31 +00001045 DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001046
1047 // Copy argument values to their designated locations.
1048 SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1049 SmallVector<SDValue, 8> MemOpChains;
1050 SDValue StackPtr;
1051 for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1052 CCValAssign &VA = ArgLocs[I];
1053 SDValue ArgValue = OutVals[I];
1054
1055 if (VA.getLocInfo() == CCValAssign::Indirect) {
1056 // Store the argument in a stack slot and pass its address.
1057 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
1058 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
Alex Lorenze40c8a22015-08-11 23:09:45 +00001059 MemOpChains.push_back(DAG.getStore(
1060 Chain, DL, ArgValue, SpillSlot,
1061 MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001062 ArgValue = SpillSlot;
1063 } else
1064 ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1065
1066 if (VA.isRegLoc())
1067 // Queue up the argument copies and emit them at the end.
1068 RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1069 else {
1070 assert(VA.isMemLoc() && "Argument not register or memory");
1071
1072 // Work out the address of the stack slot. Unpromoted ints and
1073 // floats are passed as right-justified 8-byte values.
1074 if (!StackPtr.getNode())
1075 StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1076 unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1077 if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1078 Offset += 4;
1079 SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001080 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001081
1082 // Emit the store.
1083 MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1084 MachinePointerInfo(),
1085 false, false, 0));
1086 }
1087 }
1088
1089 // Join the stores, which are independent of one another.
1090 if (!MemOpChains.empty())
Craig Topper48d114b2014-04-26 18:35:24 +00001091 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001092
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001093 // Accept direct calls by converting symbolic call addresses to the
Richard Sandiford709bda62013-08-19 12:42:31 +00001094 // associated Target* opcodes. Force %r1 to be used for indirect
1095 // tail calls.
1096 SDValue Glue;
Richard Sandiford21f5d682014-03-06 11:22:58 +00001097 if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001098 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1099 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford21f5d682014-03-06 11:22:58 +00001100 } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001101 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1102 Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
Richard Sandiford709bda62013-08-19 12:42:31 +00001103 } else if (IsTailCall) {
1104 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1105 Glue = Chain.getValue(1);
1106 Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1107 }
1108
1109 // Build a sequence of copy-to-reg nodes, chained and glued together.
1110 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1111 Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1112 RegsToPass[I].second, Glue);
1113 Glue = Chain.getValue(1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001114 }
1115
1116 // The first call operand is the chain and the second is the target address.
1117 SmallVector<SDValue, 8> Ops;
1118 Ops.push_back(Chain);
1119 Ops.push_back(Callee);
1120
1121 // Add argument registers to the end of the list so that they are
1122 // known live into the call.
1123 for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1124 Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1125 RegsToPass[I].second.getValueType()));
1126
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001127 // Add a register mask operand representing the call-preserved registers.
Eric Christophera6734172015-01-31 00:06:45 +00001128 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00001129 const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
Richard Sandiford02bb0ec2014-07-10 11:44:37 +00001130 assert(Mask && "Missing call preserved mask for calling convention");
1131 Ops.push_back(DAG.getRegisterMask(Mask));
1132
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001133 // Glue the call to the argument copies, if any.
1134 if (Glue.getNode())
1135 Ops.push_back(Glue);
1136
1137 // Emit the call.
1138 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
Richard Sandiford709bda62013-08-19 12:42:31 +00001139 if (IsTailCall)
Craig Topper48d114b2014-04-26 18:35:24 +00001140 return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1141 Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001142 Glue = Chain.getValue(1);
1143
1144 // Mark the end of the call, which is glued to the call itself.
1145 Chain = DAG.getCALLSEQ_END(Chain,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001146 DAG.getConstant(NumBytes, DL, PtrVT, true),
1147 DAG.getConstant(0, DL, PtrVT, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001148 Glue, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001149 Glue = Chain.getValue(1);
1150
1151 // Assign locations to each value returned by this call.
1152 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001153 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001154 RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1155
1156 // Copy all of the result registers out of their specified physreg.
1157 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1158 CCValAssign &VA = RetLocs[I];
1159
1160 // Copy the value out, gluing the copy to the end of the call sequence.
1161 SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1162 VA.getLocVT(), Glue);
1163 Chain = RetValue.getValue(1);
1164 Glue = RetValue.getValue(2);
1165
1166 // Convert the value of the return register into the value that's
1167 // being returned.
1168 InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1169 }
1170
1171 return Chain;
1172}
1173
Ulrich Weiganda887f062015-08-13 13:37:06 +00001174bool SystemZTargetLowering::
1175CanLowerReturn(CallingConv::ID CallConv,
1176 MachineFunction &MF, bool isVarArg,
1177 const SmallVectorImpl<ISD::OutputArg> &Outs,
1178 LLVMContext &Context) const {
1179 // Detect unsupported vector return types.
1180 if (Subtarget.hasVector())
1181 VerifyVectorTypes(Outs);
1182
1183 SmallVector<CCValAssign, 16> RetLocs;
1184 CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1185 return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1186}
1187
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001188SDValue
1189SystemZTargetLowering::LowerReturn(SDValue Chain,
1190 CallingConv::ID CallConv, bool IsVarArg,
1191 const SmallVectorImpl<ISD::OutputArg> &Outs,
1192 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001193 SDLoc DL, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001194 MachineFunction &MF = DAG.getMachineFunction();
1195
Ulrich Weigand5211f9f2015-05-05 19:30:05 +00001196 // Detect unsupported vector return types.
1197 if (Subtarget.hasVector())
1198 VerifyVectorTypes(Outs);
1199
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001200 // Assign locations to each returned value.
1201 SmallVector<CCValAssign, 16> RetLocs;
Eric Christopherb5217502014-08-06 18:45:26 +00001202 CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001203 RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1204
1205 // Quick exit for void returns
1206 if (RetLocs.empty())
1207 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1208
1209 // Copy the result values into the output registers.
1210 SDValue Glue;
1211 SmallVector<SDValue, 4> RetOps;
1212 RetOps.push_back(Chain);
1213 for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1214 CCValAssign &VA = RetLocs[I];
1215 SDValue RetValue = OutVals[I];
1216
1217 // Make the return register live on exit.
1218 assert(VA.isRegLoc() && "Can only return in registers!");
1219
1220 // Promote the value as required.
1221 RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1222
1223 // Chain and glue the copies together.
1224 unsigned Reg = VA.getLocReg();
1225 Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1226 Glue = Chain.getValue(1);
1227 RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1228 }
1229
1230 // Update chain and glue.
1231 RetOps[0] = Chain;
1232 if (Glue.getNode())
1233 RetOps.push_back(Glue);
1234
Craig Topper48d114b2014-04-26 18:35:24 +00001235 return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001236}
1237
Richard Sandiford9afe6132013-12-10 10:36:34 +00001238SDValue SystemZTargetLowering::
1239prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1240 return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1241}
1242
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001243// Return true if Op is an intrinsic node with chain that returns the CC value
1244// as its only (other) argument. Provide the associated SystemZISD opcode and
1245// the mask of valid CC values if so.
1246static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1247 unsigned &CCValid) {
1248 unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1249 switch (Id) {
1250 case Intrinsic::s390_tbegin:
1251 Opcode = SystemZISD::TBEGIN;
1252 CCValid = SystemZ::CCMASK_TBEGIN;
1253 return true;
1254
1255 case Intrinsic::s390_tbegin_nofloat:
1256 Opcode = SystemZISD::TBEGIN_NOFLOAT;
1257 CCValid = SystemZ::CCMASK_TBEGIN;
1258 return true;
1259
1260 case Intrinsic::s390_tend:
1261 Opcode = SystemZISD::TEND;
1262 CCValid = SystemZ::CCMASK_TEND;
1263 return true;
1264
1265 default:
1266 return false;
1267 }
1268}
1269
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001270// Return true if Op is an intrinsic node without chain that returns the
1271// CC value as its final argument. Provide the associated SystemZISD
1272// opcode and the mask of valid CC values if so.
1273static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1274 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1275 switch (Id) {
1276 case Intrinsic::s390_vpkshs:
1277 case Intrinsic::s390_vpksfs:
1278 case Intrinsic::s390_vpksgs:
1279 Opcode = SystemZISD::PACKS_CC;
1280 CCValid = SystemZ::CCMASK_VCMP;
1281 return true;
1282
1283 case Intrinsic::s390_vpklshs:
1284 case Intrinsic::s390_vpklsfs:
1285 case Intrinsic::s390_vpklsgs:
1286 Opcode = SystemZISD::PACKLS_CC;
1287 CCValid = SystemZ::CCMASK_VCMP;
1288 return true;
1289
1290 case Intrinsic::s390_vceqbs:
1291 case Intrinsic::s390_vceqhs:
1292 case Intrinsic::s390_vceqfs:
1293 case Intrinsic::s390_vceqgs:
1294 Opcode = SystemZISD::VICMPES;
1295 CCValid = SystemZ::CCMASK_VCMP;
1296 return true;
1297
1298 case Intrinsic::s390_vchbs:
1299 case Intrinsic::s390_vchhs:
1300 case Intrinsic::s390_vchfs:
1301 case Intrinsic::s390_vchgs:
1302 Opcode = SystemZISD::VICMPHS;
1303 CCValid = SystemZ::CCMASK_VCMP;
1304 return true;
1305
1306 case Intrinsic::s390_vchlbs:
1307 case Intrinsic::s390_vchlhs:
1308 case Intrinsic::s390_vchlfs:
1309 case Intrinsic::s390_vchlgs:
1310 Opcode = SystemZISD::VICMPHLS;
1311 CCValid = SystemZ::CCMASK_VCMP;
1312 return true;
1313
1314 case Intrinsic::s390_vtm:
1315 Opcode = SystemZISD::VTM;
1316 CCValid = SystemZ::CCMASK_VCMP;
1317 return true;
1318
1319 case Intrinsic::s390_vfaebs:
1320 case Intrinsic::s390_vfaehs:
1321 case Intrinsic::s390_vfaefs:
1322 Opcode = SystemZISD::VFAE_CC;
1323 CCValid = SystemZ::CCMASK_ANY;
1324 return true;
1325
1326 case Intrinsic::s390_vfaezbs:
1327 case Intrinsic::s390_vfaezhs:
1328 case Intrinsic::s390_vfaezfs:
1329 Opcode = SystemZISD::VFAEZ_CC;
1330 CCValid = SystemZ::CCMASK_ANY;
1331 return true;
1332
1333 case Intrinsic::s390_vfeebs:
1334 case Intrinsic::s390_vfeehs:
1335 case Intrinsic::s390_vfeefs:
1336 Opcode = SystemZISD::VFEE_CC;
1337 CCValid = SystemZ::CCMASK_ANY;
1338 return true;
1339
1340 case Intrinsic::s390_vfeezbs:
1341 case Intrinsic::s390_vfeezhs:
1342 case Intrinsic::s390_vfeezfs:
1343 Opcode = SystemZISD::VFEEZ_CC;
1344 CCValid = SystemZ::CCMASK_ANY;
1345 return true;
1346
1347 case Intrinsic::s390_vfenebs:
1348 case Intrinsic::s390_vfenehs:
1349 case Intrinsic::s390_vfenefs:
1350 Opcode = SystemZISD::VFENE_CC;
1351 CCValid = SystemZ::CCMASK_ANY;
1352 return true;
1353
1354 case Intrinsic::s390_vfenezbs:
1355 case Intrinsic::s390_vfenezhs:
1356 case Intrinsic::s390_vfenezfs:
1357 Opcode = SystemZISD::VFENEZ_CC;
1358 CCValid = SystemZ::CCMASK_ANY;
1359 return true;
1360
1361 case Intrinsic::s390_vistrbs:
1362 case Intrinsic::s390_vistrhs:
1363 case Intrinsic::s390_vistrfs:
1364 Opcode = SystemZISD::VISTR_CC;
1365 CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1366 return true;
1367
1368 case Intrinsic::s390_vstrcbs:
1369 case Intrinsic::s390_vstrchs:
1370 case Intrinsic::s390_vstrcfs:
1371 Opcode = SystemZISD::VSTRC_CC;
1372 CCValid = SystemZ::CCMASK_ANY;
1373 return true;
1374
1375 case Intrinsic::s390_vstrczbs:
1376 case Intrinsic::s390_vstrczhs:
1377 case Intrinsic::s390_vstrczfs:
1378 Opcode = SystemZISD::VSTRCZ_CC;
1379 CCValid = SystemZ::CCMASK_ANY;
1380 return true;
1381
1382 case Intrinsic::s390_vfcedbs:
1383 Opcode = SystemZISD::VFCMPES;
1384 CCValid = SystemZ::CCMASK_VCMP;
1385 return true;
1386
1387 case Intrinsic::s390_vfchdbs:
1388 Opcode = SystemZISD::VFCMPHS;
1389 CCValid = SystemZ::CCMASK_VCMP;
1390 return true;
1391
1392 case Intrinsic::s390_vfchedbs:
1393 Opcode = SystemZISD::VFCMPHES;
1394 CCValid = SystemZ::CCMASK_VCMP;
1395 return true;
1396
1397 case Intrinsic::s390_vftcidb:
1398 Opcode = SystemZISD::VFTCI;
1399 CCValid = SystemZ::CCMASK_VCMP;
1400 return true;
1401
1402 default:
1403 return false;
1404 }
1405}
1406
Ulrich Weigand57c85f52015-04-01 12:51:43 +00001407// Emit an intrinsic with chain with a glued value instead of its CC result.
1408static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1409 unsigned Opcode) {
1410 // Copy all operands except the intrinsic ID.
1411 unsigned NumOps = Op.getNumOperands();
1412 SmallVector<SDValue, 6> Ops;
1413 Ops.reserve(NumOps - 1);
1414 Ops.push_back(Op.getOperand(0));
1415 for (unsigned I = 2; I < NumOps; ++I)
1416 Ops.push_back(Op.getOperand(I));
1417
1418 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1419 SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1420 SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1421 SDValue OldChain = SDValue(Op.getNode(), 1);
1422 SDValue NewChain = SDValue(Intr.getNode(), 0);
1423 DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1424 return Intr;
1425}
1426
Ulrich Weigandc1708b22015-05-05 19:31:09 +00001427// Emit an intrinsic with a glued value instead of its CC result.
1428static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1429 unsigned Opcode) {
1430 // Copy all operands except the intrinsic ID.
1431 unsigned NumOps = Op.getNumOperands();
1432 SmallVector<SDValue, 6> Ops;
1433 Ops.reserve(NumOps - 1);
1434 for (unsigned I = 1; I < NumOps; ++I)
1435 Ops.push_back(Op.getOperand(I));
1436
1437 if (Op->getNumValues() == 1)
1438 return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1439 assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1440 SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1441 return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1442}
1443
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001444// CC is a comparison that will be implemented using an integer or
1445// floating-point comparison. Return the condition code mask for
1446// a branch on true. In the integer case, CCMASK_CMP_UO is set for
1447// unsigned comparisons and clear for signed ones. In the floating-point
1448// case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1449static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1450#define CONV(X) \
1451 case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1452 case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1453 case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1454
1455 switch (CC) {
1456 default:
1457 llvm_unreachable("Invalid integer condition!");
1458
1459 CONV(EQ);
1460 CONV(NE);
1461 CONV(GT);
1462 CONV(GE);
1463 CONV(LT);
1464 CONV(LE);
1465
1466 case ISD::SETO: return SystemZ::CCMASK_CMP_O;
1467 case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1468 }
1469#undef CONV
1470}
1471
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001472// Return a sequence for getting a 1 from an IPM result when CC has a
1473// value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1474// The handling of CC values outside CCValid doesn't matter.
1475static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1476 // Deal with cases where the result can be taken directly from a bit
1477 // of the IPM result.
1478 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1479 return IPMConversion(0, 0, SystemZ::IPM_CC);
1480 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1481 return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1482
1483 // Deal with cases where we can add a value to force the sign bit
1484 // to contain the right value. Putting the bit in 31 means we can
1485 // use SRL rather than RISBG(L), and also makes it easier to get a
1486 // 0/-1 value, so it has priority over the other tests below.
1487 //
1488 // These sequences rely on the fact that the upper two bits of the
1489 // IPM result are zero.
1490 uint64_t TopBit = uint64_t(1) << 31;
1491 if (CCMask == (CCValid & SystemZ::CCMASK_0))
1492 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1493 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1494 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1495 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1496 | SystemZ::CCMASK_1
1497 | SystemZ::CCMASK_2)))
1498 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1499 if (CCMask == (CCValid & SystemZ::CCMASK_3))
1500 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1501 if (CCMask == (CCValid & (SystemZ::CCMASK_1
1502 | SystemZ::CCMASK_2
1503 | SystemZ::CCMASK_3)))
1504 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1505
1506 // Next try inverting the value and testing a bit. 0/1 could be
1507 // handled this way too, but we dealt with that case above.
1508 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1509 return IPMConversion(-1, 0, SystemZ::IPM_CC);
1510
1511 // Handle cases where adding a value forces a non-sign bit to contain
1512 // the right value.
1513 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1514 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1515 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1516 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1517
Alp Tokercb402912014-01-24 17:20:08 +00001518 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are
Richard Sandifordf722a8e302013-10-16 11:10:55 +00001519 // can be done by inverting the low CC bit and applying one of the
1520 // sign-based extractions above.
1521 if (CCMask == (CCValid & SystemZ::CCMASK_1))
1522 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1523 if (CCMask == (CCValid & SystemZ::CCMASK_2))
1524 return IPMConversion(1 << SystemZ::IPM_CC,
1525 TopBit - (3 << SystemZ::IPM_CC), 31);
1526 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1527 | SystemZ::CCMASK_1
1528 | SystemZ::CCMASK_3)))
1529 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1530 if (CCMask == (CCValid & (SystemZ::CCMASK_0
1531 | SystemZ::CCMASK_2
1532 | SystemZ::CCMASK_3)))
1533 return IPMConversion(1 << SystemZ::IPM_CC,
1534 TopBit - (1 << SystemZ::IPM_CC), 31);
1535
1536 llvm_unreachable("Unexpected CC combination");
1537}
1538
Richard Sandifordd420f732013-12-13 15:28:45 +00001539// If C can be converted to a comparison against zero, adjust the operands
Richard Sandiforda0757082013-08-01 10:29:45 +00001540// as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001541static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001542 if (C.ICmpType == SystemZICMP::UnsignedOnly)
Richard Sandiforda0757082013-08-01 10:29:45 +00001543 return;
1544
Richard Sandiford21f5d682014-03-06 11:22:58 +00001545 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
Richard Sandiforda0757082013-08-01 10:29:45 +00001546 if (!ConstOp1)
1547 return;
1548
1549 int64_t Value = ConstOp1->getSExtValue();
Richard Sandifordd420f732013-12-13 15:28:45 +00001550 if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1551 (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1552 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1553 (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1554 C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001555 C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
Richard Sandiforda0757082013-08-01 10:29:45 +00001556 }
1557}
1558
Richard Sandifordd420f732013-12-13 15:28:45 +00001559// If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1560// adjust the operands as necessary.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001561static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001562 // For us to make any changes, it must a comparison between a single-use
1563 // load and a constant.
Richard Sandifordd420f732013-12-13 15:28:45 +00001564 if (!C.Op0.hasOneUse() ||
1565 C.Op0.getOpcode() != ISD::LOAD ||
1566 C.Op1.getOpcode() != ISD::Constant)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001567 return;
1568
1569 // We must have an 8- or 16-bit load.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001570 auto *Load = cast<LoadSDNode>(C.Op0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001571 unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1572 if (NumBits != 8 && NumBits != 16)
1573 return;
1574
1575 // The load must be an extending one and the constant must be within the
1576 // range of the unextended value.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001577 auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001578 uint64_t Value = ConstOp1->getZExtValue();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001579 uint64_t Mask = (1 << NumBits) - 1;
1580 if (Load->getExtensionType() == ISD::SEXTLOAD) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001581 // Make sure that ConstOp1 is in range of C.Op0.
1582 int64_t SignedValue = ConstOp1->getSExtValue();
1583 if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001584 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001585 if (C.ICmpType != SystemZICMP::SignedOnly) {
1586 // Unsigned comparison between two sign-extended values is equivalent
1587 // to unsigned comparison between two zero-extended values.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001588 Value &= Mask;
Richard Sandifordd420f732013-12-13 15:28:45 +00001589 } else if (NumBits == 8) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001590 // Try to treat the comparison as unsigned, so that we can use CLI.
1591 // Adjust CCMask and Value as necessary.
Richard Sandifordd420f732013-12-13 15:28:45 +00001592 if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001593 // Test whether the high bit of the byte is set.
Richard Sandifordd420f732013-12-13 15:28:45 +00001594 Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1595 else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001596 // Test whether the high bit of the byte is clear.
Richard Sandifordd420f732013-12-13 15:28:45 +00001597 Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001598 else
1599 // No instruction exists for this combination.
1600 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001601 C.ICmpType = SystemZICMP::UnsignedOnly;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001602 }
1603 } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1604 if (Value > Mask)
1605 return;
Ulrich Weigand47f36492015-12-16 18:04:06 +00001606 // If the constant is in range, we can use any comparison.
1607 C.ICmpType = SystemZICMP::Any;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001608 } else
1609 return;
1610
1611 // Make sure that the first operand is an i32 of the right extension type.
Richard Sandifordd420f732013-12-13 15:28:45 +00001612 ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1613 ISD::SEXTLOAD :
1614 ISD::ZEXTLOAD);
1615 if (C.Op0.getValueType() != MVT::i32 ||
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001616 Load->getExtensionType() != ExtType)
Richard Sandifordd420f732013-12-13 15:28:45 +00001617 C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1618 Load->getChain(), Load->getBasePtr(),
1619 Load->getPointerInfo(), Load->getMemoryVT(),
1620 Load->isVolatile(), Load->isNonTemporal(),
Louis Gerbarg67474e32014-07-31 21:45:05 +00001621 Load->isInvariant(), Load->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001622
1623 // Make sure that the second operand is an i32 with the right value.
Richard Sandifordd420f732013-12-13 15:28:45 +00001624 if (C.Op1.getValueType() != MVT::i32 ||
1625 Value != ConstOp1->getZExtValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001627}
1628
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001629// Return true if Op is either an unextended load, or a load suitable
1630// for integer register-memory comparisons of type ICmpType.
1631static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001632 auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001633 if (Load) {
1634 // There are no instructions to compare a register with a memory byte.
1635 if (Load->getMemoryVT() == MVT::i8)
1636 return false;
1637 // Otherwise decide on extension type.
Richard Sandiford24e597b2013-08-23 11:27:19 +00001638 switch (Load->getExtensionType()) {
1639 case ISD::NON_EXTLOAD:
Richard Sandiford24e597b2013-08-23 11:27:19 +00001640 return true;
1641 case ISD::SEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001642 return ICmpType != SystemZICMP::UnsignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001643 case ISD::ZEXTLOAD:
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001644 return ICmpType != SystemZICMP::SignedOnly;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001645 default:
1646 break;
1647 }
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001648 }
Richard Sandiford24e597b2013-08-23 11:27:19 +00001649 return false;
1650}
1651
Richard Sandifordd420f732013-12-13 15:28:45 +00001652// Return true if it is better to swap the operands of C.
1653static bool shouldSwapCmpOperands(const Comparison &C) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001654 // Leave f128 comparisons alone, since they have no memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001655 if (C.Op0.getValueType() == MVT::f128)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001656 return false;
1657
1658 // Always keep a floating-point constant second, since comparisons with
1659 // zero can use LOAD TEST and comparisons with other constants make a
1660 // natural memory operand.
Richard Sandifordd420f732013-12-13 15:28:45 +00001661 if (isa<ConstantFPSDNode>(C.Op1))
Richard Sandiford24e597b2013-08-23 11:27:19 +00001662 return false;
1663
1664 // Never swap comparisons with zero since there are many ways to optimize
1665 // those later.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001666 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001667 if (ConstOp1 && ConstOp1->getZExtValue() == 0)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001668 return false;
1669
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001670 // Also keep natural memory operands second if the loaded value is
1671 // only used here. Several comparisons have memory forms.
Richard Sandifordd420f732013-12-13 15:28:45 +00001672 if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001673 return false;
1674
Richard Sandiford24e597b2013-08-23 11:27:19 +00001675 // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1676 // In that case we generally prefer the memory to be second.
Richard Sandifordd420f732013-12-13 15:28:45 +00001677 if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
Richard Sandiford24e597b2013-08-23 11:27:19 +00001678 // The only exceptions are when the second operand is a constant and
1679 // we can use things like CHHSI.
Richard Sandifordd420f732013-12-13 15:28:45 +00001680 if (!ConstOp1)
Richard Sandiford24e597b2013-08-23 11:27:19 +00001681 return true;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001682 // The unsigned memory-immediate instructions can handle 16-bit
1683 // unsigned integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001684 if (C.ICmpType != SystemZICMP::SignedOnly &&
1685 isUInt<16>(ConstOp1->getZExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001686 return false;
1687 // The signed memory-immediate instructions can handle 16-bit
1688 // signed integers.
Richard Sandifordd420f732013-12-13 15:28:45 +00001689 if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1690 isInt<16>(ConstOp1->getSExtValue()))
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001691 return false;
Richard Sandiford24e597b2013-08-23 11:27:19 +00001692 return true;
1693 }
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001694
1695 // Try to promote the use of CGFR and CLGFR.
Richard Sandifordd420f732013-12-13 15:28:45 +00001696 unsigned Opcode0 = C.Op0.getOpcode();
1697 if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001698 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001699 if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001700 return true;
Richard Sandifordd420f732013-12-13 15:28:45 +00001701 if (C.ICmpType != SystemZICMP::SignedOnly &&
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001702 Opcode0 == ISD::AND &&
Richard Sandifordd420f732013-12-13 15:28:45 +00001703 C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1704 cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
Richard Sandiford7b4118a2013-12-06 09:56:50 +00001705 return true;
1706
Richard Sandiford24e597b2013-08-23 11:27:19 +00001707 return false;
1708}
1709
Richard Sandiford73170f82013-12-11 11:45:08 +00001710// Return a version of comparison CC mask CCMask in which the LT and GT
1711// actions are swapped.
1712static unsigned reverseCCMask(unsigned CCMask) {
1713 return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1714 (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1715 (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1716 (CCMask & SystemZ::CCMASK_CMP_UO));
1717}
1718
Richard Sandiford0847c452013-12-13 15:50:30 +00001719// Check whether C tests for equality between X and Y and whether X - Y
1720// or Y - X is also computed. In that case it's better to compare the
1721// result of the subtraction against zero.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001722static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001723 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1724 C.CCMask == SystemZ::CCMASK_CMP_NE) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001725 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford0847c452013-12-13 15:50:30 +00001726 SDNode *N = *I;
1727 if (N->getOpcode() == ISD::SUB &&
1728 ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1729 (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1730 C.Op0 = SDValue(N, 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001731 C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
Richard Sandiford0847c452013-12-13 15:50:30 +00001732 return;
1733 }
1734 }
1735 }
1736}
1737
Richard Sandifordd420f732013-12-13 15:28:45 +00001738// Check whether C compares a floating-point value with zero and if that
1739// floating-point value is also negated. In this case we can use the
1740// negation to set CC, so avoiding separate LOAD AND TEST and
1741// LOAD (NEGATIVE/COMPLEMENT) instructions.
1742static void adjustForFNeg(Comparison &C) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001743 auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
Richard Sandiford73170f82013-12-11 11:45:08 +00001744 if (C1 && C1->isZero()) {
Richard Sandiford28c111e2014-03-06 11:00:15 +00001745 for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
Richard Sandiford73170f82013-12-11 11:45:08 +00001746 SDNode *N = *I;
1747 if (N->getOpcode() == ISD::FNEG) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001748 C.Op0 = SDValue(N, 0);
1749 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford73170f82013-12-11 11:45:08 +00001750 return;
1751 }
1752 }
1753 }
1754}
1755
Richard Sandifordd420f732013-12-13 15:28:45 +00001756// Check whether C compares (shl X, 32) with 0 and whether X is
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001757// also sign-extended. In that case it is better to test the result
1758// of the sign extension using LTGFR.
1759//
1760// This case is important because InstCombine transforms a comparison
1761// with (sext (trunc X)) into a comparison with (shl X, 32).
Richard Sandifordd420f732013-12-13 15:28:45 +00001762static void adjustForLTGFR(Comparison &C) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001763 // Check for a comparison between (shl X, 32) and 0.
Richard Sandifordd420f732013-12-13 15:28:45 +00001764 if (C.Op0.getOpcode() == ISD::SHL &&
1765 C.Op0.getValueType() == MVT::i64 &&
1766 C.Op1.getOpcode() == ISD::Constant &&
1767 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001768 auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001769 if (C1 && C1->getZExtValue() == 32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001770 SDValue ShlOp0 = C.Op0.getOperand(0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001771 // See whether X has any SIGN_EXTEND_INREG uses.
Richard Sandiford28c111e2014-03-06 11:00:15 +00001772 for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001773 SDNode *N = *I;
1774 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1775 cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
Richard Sandifordd420f732013-12-13 15:28:45 +00001776 C.Op0 = SDValue(N, 0);
Richard Sandifordbd2f0e92013-12-13 15:07:39 +00001777 return;
1778 }
1779 }
1780 }
1781 }
1782}
1783
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001784// If C compares the truncation of an extending load, try to compare
1785// the untruncated value instead. This exposes more opportunities to
1786// reuse CC.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001787static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001788 if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1789 C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1790 C.Op1.getOpcode() == ISD::Constant &&
1791 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001792 auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001793 if (L->getMemoryVT().getStoreSizeInBits()
1794 <= C.Op0.getValueType().getSizeInBits()) {
1795 unsigned Type = L->getExtensionType();
1796 if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1797 (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1798 C.Op0 = C.Op0.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001799 C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00001800 }
1801 }
1802 }
1803}
1804
Richard Sandiford030c1652013-09-13 09:09:50 +00001805// Return true if shift operation N has an in-range constant shift value.
1806// Store it in ShiftVal if so.
1807static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001808 auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
Richard Sandiford030c1652013-09-13 09:09:50 +00001809 if (!Shift)
1810 return false;
1811
1812 uint64_t Amount = Shift->getZExtValue();
1813 if (Amount >= N.getValueType().getSizeInBits())
1814 return false;
1815
1816 ShiftVal = Amount;
1817 return true;
1818}
1819
1820// Check whether an AND with Mask is suitable for a TEST UNDER MASK
1821// instruction and whether the CC value is descriptive enough to handle
1822// a comparison of type Opcode between the AND result and CmpVal.
1823// CCMask says which comparison result is being tested and BitSize is
1824// the number of bits in the operands. If TEST UNDER MASK can be used,
1825// return the corresponding CC mask, otherwise return 0.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001826static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1827 uint64_t Mask, uint64_t CmpVal,
1828 unsigned ICmpType) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001829 assert(Mask != 0 && "ANDs with zero should have been removed by now");
1830
Richard Sandiford030c1652013-09-13 09:09:50 +00001831 // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1832 if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1833 !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1834 return 0;
1835
Richard Sandiford113c8702013-09-03 15:38:35 +00001836 // Work out the masks for the lowest and highest bits.
1837 unsigned HighShift = 63 - countLeadingZeros(Mask);
1838 uint64_t High = uint64_t(1) << HighShift;
1839 uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1840
1841 // Signed ordered comparisons are effectively unsigned if the sign
1842 // bit is dropped.
Richard Sandiford5bc670b2013-09-06 11:51:39 +00001843 bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
Richard Sandiford113c8702013-09-03 15:38:35 +00001844
1845 // Check for equality comparisons with 0, or the equivalent.
1846 if (CmpVal == 0) {
1847 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1848 return SystemZ::CCMASK_TM_ALL_0;
1849 if (CCMask == SystemZ::CCMASK_CMP_NE)
1850 return SystemZ::CCMASK_TM_SOME_1;
1851 }
Ulrich Weigand4a4d4ab2016-02-01 18:31:19 +00001852 if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001853 if (CCMask == SystemZ::CCMASK_CMP_LT)
1854 return SystemZ::CCMASK_TM_ALL_0;
1855 if (CCMask == SystemZ::CCMASK_CMP_GE)
1856 return SystemZ::CCMASK_TM_SOME_1;
1857 }
1858 if (EffectivelyUnsigned && CmpVal < Low) {
1859 if (CCMask == SystemZ::CCMASK_CMP_LE)
1860 return SystemZ::CCMASK_TM_ALL_0;
1861 if (CCMask == SystemZ::CCMASK_CMP_GT)
1862 return SystemZ::CCMASK_TM_SOME_1;
1863 }
1864
1865 // Check for equality comparisons with the mask, or the equivalent.
1866 if (CmpVal == Mask) {
1867 if (CCMask == SystemZ::CCMASK_CMP_EQ)
1868 return SystemZ::CCMASK_TM_ALL_1;
1869 if (CCMask == SystemZ::CCMASK_CMP_NE)
1870 return SystemZ::CCMASK_TM_SOME_0;
1871 }
1872 if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1873 if (CCMask == SystemZ::CCMASK_CMP_GT)
1874 return SystemZ::CCMASK_TM_ALL_1;
1875 if (CCMask == SystemZ::CCMASK_CMP_LE)
1876 return SystemZ::CCMASK_TM_SOME_0;
1877 }
1878 if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1879 if (CCMask == SystemZ::CCMASK_CMP_GE)
1880 return SystemZ::CCMASK_TM_ALL_1;
1881 if (CCMask == SystemZ::CCMASK_CMP_LT)
1882 return SystemZ::CCMASK_TM_SOME_0;
1883 }
1884
1885 // Check for ordered comparisons with the top bit.
1886 if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1887 if (CCMask == SystemZ::CCMASK_CMP_LE)
1888 return SystemZ::CCMASK_TM_MSB_0;
1889 if (CCMask == SystemZ::CCMASK_CMP_GT)
1890 return SystemZ::CCMASK_TM_MSB_1;
1891 }
1892 if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1893 if (CCMask == SystemZ::CCMASK_CMP_LT)
1894 return SystemZ::CCMASK_TM_MSB_0;
1895 if (CCMask == SystemZ::CCMASK_CMP_GE)
1896 return SystemZ::CCMASK_TM_MSB_1;
1897 }
1898
1899 // If there are just two bits, we can do equality checks for Low and High
1900 // as well.
1901 if (Mask == Low + High) {
1902 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1903 return SystemZ::CCMASK_TM_MIXED_MSB_0;
1904 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1905 return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1906 if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1907 return SystemZ::CCMASK_TM_MIXED_MSB_1;
1908 if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1909 return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1910 }
1911
1912 // Looks like we've exhausted our options.
1913 return 0;
1914}
1915
Richard Sandifordd420f732013-12-13 15:28:45 +00001916// See whether C can be implemented as a TEST UNDER MASK instruction.
1917// Update the arguments with the TM version if so.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001918static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Richard Sandiford113c8702013-09-03 15:38:35 +00001919 // Check that we have a comparison with a constant.
Richard Sandiford21f5d682014-03-06 11:22:58 +00001920 auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
Richard Sandifordd420f732013-12-13 15:28:45 +00001921 if (!ConstOp1)
Richard Sandiford35b9be22013-08-28 10:31:43 +00001922 return;
Richard Sandifordd420f732013-12-13 15:28:45 +00001923 uint64_t CmpVal = ConstOp1->getZExtValue();
Richard Sandiford35b9be22013-08-28 10:31:43 +00001924
1925 // Check whether the nonconstant input is an AND with a constant mask.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001926 Comparison NewC(C);
1927 uint64_t MaskVal;
Craig Topper062a2ba2014-04-25 05:30:21 +00001928 ConstantSDNode *Mask = nullptr;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001929 if (C.Op0.getOpcode() == ISD::AND) {
1930 NewC.Op0 = C.Op0.getOperand(0);
1931 NewC.Op1 = C.Op0.getOperand(1);
1932 Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1933 if (!Mask)
1934 return;
1935 MaskVal = Mask->getZExtValue();
1936 } else {
1937 // There is no instruction to compare with a 64-bit immediate
1938 // so use TMHH instead if possible. We need an unsigned ordered
1939 // comparison with an i64 immediate.
1940 if (NewC.Op0.getValueType() != MVT::i64 ||
1941 NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1942 NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1943 NewC.ICmpType == SystemZICMP::SignedOnly)
1944 return;
1945 // Convert LE and GT comparisons into LT and GE.
1946 if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1947 NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1948 if (CmpVal == uint64_t(-1))
1949 return;
1950 CmpVal += 1;
1951 NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1952 }
1953 // If the low N bits of Op1 are zero than the low N bits of Op0 can
1954 // be masked off without changing the result.
1955 MaskVal = -(CmpVal & -CmpVal);
1956 NewC.ICmpType = SystemZICMP::UnsignedOnly;
1957 }
Ulrich Weigandb8d76fb2015-03-30 13:46:59 +00001958 if (!MaskVal)
1959 return;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001960
Richard Sandiford113c8702013-09-03 15:38:35 +00001961 // Check whether the combination of mask, comparison value and comparison
1962 // type are suitable.
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001963 unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
Richard Sandiford030c1652013-09-13 09:09:50 +00001964 unsigned NewCCMask, ShiftVal;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001965 if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1966 NewC.Op0.getOpcode() == ISD::SHL &&
1967 isSimpleShift(NewC.Op0, ShiftVal) &&
1968 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1969 MaskVal >> ShiftVal,
Richard Sandiford030c1652013-09-13 09:09:50 +00001970 CmpVal >> ShiftVal,
1971 SystemZICMP::Any))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001972 NewC.Op0 = NewC.Op0.getOperand(0);
1973 MaskVal >>= ShiftVal;
1974 } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1975 NewC.Op0.getOpcode() == ISD::SRL &&
1976 isSimpleShift(NewC.Op0, ShiftVal) &&
1977 (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
Richard Sandiford030c1652013-09-13 09:09:50 +00001978 MaskVal << ShiftVal,
1979 CmpVal << ShiftVal,
1980 SystemZICMP::UnsignedOnly))) {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001981 NewC.Op0 = NewC.Op0.getOperand(0);
1982 MaskVal <<= ShiftVal;
Richard Sandiford030c1652013-09-13 09:09:50 +00001983 } else {
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001984 NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1985 NewC.ICmpType);
Richard Sandiford030c1652013-09-13 09:09:50 +00001986 if (!NewCCMask)
1987 return;
1988 }
Richard Sandiford113c8702013-09-03 15:38:35 +00001989
Richard Sandiford35b9be22013-08-28 10:31:43 +00001990 // Go ahead and make the change.
Richard Sandifordd420f732013-12-13 15:28:45 +00001991 C.Opcode = SystemZISD::TM;
Richard Sandifordc3dc4472013-12-13 15:46:55 +00001992 C.Op0 = NewC.Op0;
1993 if (Mask && Mask->getZExtValue() == MaskVal)
1994 C.Op1 = SDValue(Mask, 0);
1995 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001996 C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
Richard Sandifordd420f732013-12-13 15:28:45 +00001997 C.CCValid = SystemZ::CCMASK_TM;
1998 C.CCMask = NewCCMask;
Richard Sandiford35b9be22013-08-28 10:31:43 +00001999}
2000
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002001// Return a Comparison that tests the condition-code result of intrinsic
2002// node Call against constant integer CC using comparison code Cond.
2003// Opcode is the opcode of the SystemZISD operation for the intrinsic
2004// and CCValid is the set of possible condition-code results.
2005static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2006 SDValue Call, unsigned CCValid, uint64_t CC,
2007 ISD::CondCode Cond) {
2008 Comparison C(Call, SDValue());
2009 C.Opcode = Opcode;
2010 C.CCValid = CCValid;
2011 if (Cond == ISD::SETEQ)
2012 // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2013 C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2014 else if (Cond == ISD::SETNE)
2015 // ...and the inverse of that.
2016 C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2017 else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2018 // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2019 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002020 C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002021 else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2022 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002023 C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002024 else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2025 // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2026 // always true for CC>3.
Justin Bognera6d38362015-06-23 15:38:24 +00002027 C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002028 else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2029 // ...and the inverse of that.
Justin Bognera6d38362015-06-23 15:38:24 +00002030 C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002031 else
2032 llvm_unreachable("Unexpected integer comparison type");
2033 C.CCMask &= CCValid;
2034 return C;
2035}
2036
Richard Sandifordd420f732013-12-13 15:28:45 +00002037// Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2038static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002039 ISD::CondCode Cond, SDLoc DL) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002040 if (CmpOp1.getOpcode() == ISD::Constant) {
2041 uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2042 unsigned Opcode, CCValid;
2043 if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2044 CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2045 isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2046 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002047 if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2048 CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2049 isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2050 return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002051 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002052 Comparison C(CmpOp0, CmpOp1);
2053 C.CCMask = CCMaskForCondCode(Cond);
2054 if (C.Op0.getValueType().isFloatingPoint()) {
2055 C.CCValid = SystemZ::CCMASK_FCMP;
2056 C.Opcode = SystemZISD::FCMP;
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002057 adjustForFNeg(C);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002058 } else {
Richard Sandifordd420f732013-12-13 15:28:45 +00002059 C.CCValid = SystemZ::CCMASK_ICMP;
2060 C.Opcode = SystemZISD::ICMP;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002061 // Choose the type of comparison. Equality and inequality tests can
2062 // use either signed or unsigned comparisons. The choice also doesn't
2063 // matter if both sign bits are known to be clear. In those cases we
2064 // want to give the main isel code the freedom to choose whichever
2065 // form fits best.
Richard Sandifordd420f732013-12-13 15:28:45 +00002066 if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2067 C.CCMask == SystemZ::CCMASK_CMP_NE ||
2068 (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2069 C.ICmpType = SystemZICMP::Any;
2070 else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2071 C.ICmpType = SystemZICMP::UnsignedOnly;
Richard Sandiford5bc670b2013-09-06 11:51:39 +00002072 else
Richard Sandifordd420f732013-12-13 15:28:45 +00002073 C.ICmpType = SystemZICMP::SignedOnly;
2074 C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002075 adjustZeroCmp(DAG, DL, C);
2076 adjustSubwordCmp(DAG, DL, C);
2077 adjustForSubtraction(DAG, DL, C);
Richard Sandiford83a0b6a2013-12-20 11:56:02 +00002078 adjustForLTGFR(C);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002079 adjustICmpTruncate(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002080 }
2081
Richard Sandifordd420f732013-12-13 15:28:45 +00002082 if (shouldSwapCmpOperands(C)) {
2083 std::swap(C.Op0, C.Op1);
2084 C.CCMask = reverseCCMask(C.CCMask);
Richard Sandiford24e597b2013-08-23 11:27:19 +00002085 }
2086
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002087 adjustForTestUnderMask(DAG, DL, C);
Richard Sandifordd420f732013-12-13 15:28:45 +00002088 return C;
2089}
2090
2091// Emit the comparison instruction described by C.
2092static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002093 if (!C.Op1.getNode()) {
2094 SDValue Op;
2095 switch (C.Op0.getOpcode()) {
2096 case ISD::INTRINSIC_W_CHAIN:
2097 Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2098 break;
Ulrich Weigandc1708b22015-05-05 19:31:09 +00002099 case ISD::INTRINSIC_WO_CHAIN:
2100 Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2101 break;
Ulrich Weigand57c85f52015-04-01 12:51:43 +00002102 default:
2103 llvm_unreachable("Invalid comparison operands");
2104 }
2105 return SDValue(Op.getNode(), Op->getNumValues() - 1);
2106 }
Richard Sandifordd420f732013-12-13 15:28:45 +00002107 if (C.Opcode == SystemZISD::ICMP)
2108 return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002109 DAG.getConstant(C.ICmpType, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002110 if (C.Opcode == SystemZISD::TM) {
2111 bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2112 bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2113 return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002114 DAG.getConstant(RegisterOnly, DL, MVT::i32));
Richard Sandifordd420f732013-12-13 15:28:45 +00002115 }
2116 return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002117}
2118
Richard Sandiford7d86e472013-08-21 09:34:56 +00002119// Implement a 32-bit *MUL_LOHI operation by extending both operands to
2120// 64 bits. Extend is the extension type to use. Store the high part
2121// in Hi and the low part in Lo.
2122static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
2123 unsigned Extend, SDValue Op0, SDValue Op1,
2124 SDValue &Hi, SDValue &Lo) {
2125 Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2126 Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2127 SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002128 Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2129 DAG.getConstant(32, DL, MVT::i64));
Richard Sandiford7d86e472013-08-21 09:34:56 +00002130 Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2131 Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2132}
2133
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002134// Lower a binary operation that produces two VT results, one in each
2135// half of a GR128 pair. Op0 and Op1 are the VT operands to the operation,
2136// Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2137// on the extended Op0 and (unextended) Op1. Store the even register result
2138// in Even and the odd register result in Odd.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002139static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002140 unsigned Extend, unsigned Opcode,
2141 SDValue Op0, SDValue Op1,
2142 SDValue &Even, SDValue &Odd) {
2143 SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2144 SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2145 SDValue(In128, 0), Op1);
2146 bool Is32Bit = is32Bit(VT);
Richard Sandifordd8163202013-09-13 09:12:44 +00002147 Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2148 Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002149}
2150
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002151// Return an i32 value that is 1 if the CC value produced by Glue is
2152// in the mask CCMask and 0 otherwise. CC is known to have a value
2153// in CCValid, so other values can be ignored.
2154static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
2155 unsigned CCValid, unsigned CCMask) {
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002156 IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2157 SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2158
2159 if (Conversion.XORValue)
2160 Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002161 DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002162
2163 if (Conversion.AddValue)
2164 Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002165 DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002166
2167 // The SHR/AND sequence should get optimized to an RISBG.
2168 Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002169 DAG.getConstant(Conversion.Bit, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002170 if (Conversion.Bit != 31)
2171 Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002172 DAG.getConstant(1, DL, MVT::i32));
Richard Sandifordf722a8e302013-10-16 11:10:55 +00002173 return Result;
2174}
2175
Ulrich Weigandcd808232015-05-05 19:26:48 +00002176// Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2177// be done directly. IsFP is true if CC is for a floating-point rather than
2178// integer comparison.
2179static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002180 switch (CC) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002181 case ISD::SETOEQ:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002182 case ISD::SETEQ:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002183 return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002184
Ulrich Weigandcd808232015-05-05 19:26:48 +00002185 case ISD::SETOGE:
2186 case ISD::SETGE:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002187 return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002188
2189 case ISD::SETOGT:
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002190 case ISD::SETGT:
Ulrich Weigandcd808232015-05-05 19:26:48 +00002191 return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002192
2193 case ISD::SETUGT:
Saleem Abdulrasoolee33c492015-05-10 00:53:41 +00002194 return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002195
2196 default:
2197 return 0;
2198 }
2199}
2200
2201// Return the SystemZISD vector comparison operation for CC or its inverse,
2202// or 0 if neither can be done directly. Indicate in Invert whether the
Ulrich Weigandcd808232015-05-05 19:26:48 +00002203// result is for the inverse of CC. IsFP is true if CC is for a
2204// floating-point rather than integer comparison.
2205static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2206 bool &Invert) {
2207 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002208 Invert = false;
2209 return Opcode;
2210 }
2211
Ulrich Weigandcd808232015-05-05 19:26:48 +00002212 CC = ISD::getSetCCInverse(CC, !IsFP);
2213 if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002214 Invert = true;
2215 return Opcode;
2216 }
2217
2218 return 0;
2219}
2220
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002221// Return a v2f64 that contains the extended form of elements Start and Start+1
2222// of v4f32 value Op.
2223static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
2224 SDValue Op) {
2225 int Mask[] = { Start, -1, Start + 1, -1 };
2226 Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2227 return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2228}
2229
2230// Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2231// producing a result of type VT.
2232static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2233 EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2234 // There is no hardware support for v4f32, so extend the vector into
2235 // two v2f64s and compare those.
2236 if (CmpOp0.getValueType() == MVT::v4f32) {
2237 SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2238 SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2239 SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2240 SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2241 SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2242 SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2243 return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2244 }
2245 return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2246}
2247
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002248// Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2249// an integer mask of type VT.
2250static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2251 ISD::CondCode CC, SDValue CmpOp0,
2252 SDValue CmpOp1) {
Ulrich Weigandcd808232015-05-05 19:26:48 +00002253 bool IsFP = CmpOp0.getValueType().isFloatingPoint();
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002254 bool Invert = false;
2255 SDValue Cmp;
Ulrich Weigandcd808232015-05-05 19:26:48 +00002256 switch (CC) {
2257 // Handle tests for order using (or (ogt y x) (oge x y)).
2258 case ISD::SETUO:
2259 Invert = true;
2260 case ISD::SETO: {
2261 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002262 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2263 SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002264 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2265 break;
2266 }
2267
2268 // Handle <> tests using (or (ogt y x) (ogt x y)).
2269 case ISD::SETUEQ:
2270 Invert = true;
2271 case ISD::SETONE: {
2272 assert(IsFP && "Unexpected integer comparison");
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002273 SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2274 SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002275 Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2276 break;
2277 }
2278
2279 // Otherwise a single comparison is enough. It doesn't really
2280 // matter whether we try the inversion or the swap first, since
2281 // there are no cases where both work.
2282 default:
2283 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002284 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002285 else {
2286 CC = ISD::getSetCCSwappedOperands(CC);
2287 if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00002288 Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
Ulrich Weigandcd808232015-05-05 19:26:48 +00002289 else
2290 llvm_unreachable("Unhandled comparison");
2291 }
2292 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002293 }
2294 if (Invert) {
2295 SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2296 DAG.getConstant(65535, DL, MVT::i32));
2297 Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2298 Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2299 }
2300 return Cmp;
2301}
2302
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002303SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2304 SelectionDAG &DAG) const {
2305 SDValue CmpOp0 = Op.getOperand(0);
2306 SDValue CmpOp1 = Op.getOperand(1);
2307 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2308 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002309 EVT VT = Op.getValueType();
2310 if (VT.isVector())
2311 return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002312
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002313 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002314 SDValue Glue = emitCmp(DAG, DL, C);
2315 return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002316}
2317
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002318SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002319 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2320 SDValue CmpOp0 = Op.getOperand(2);
2321 SDValue CmpOp1 = Op.getOperand(3);
2322 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002323 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002324
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002325 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandifordd420f732013-12-13 15:28:45 +00002326 SDValue Glue = emitCmp(DAG, DL, C);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002327 return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002328 Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2329 DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002330}
2331
Richard Sandiford57485472013-12-13 15:35:00 +00002332// Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2333// allowing Pos and Neg to be wider than CmpOp.
2334static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2335 return (Neg.getOpcode() == ISD::SUB &&
2336 Neg.getOperand(0).getOpcode() == ISD::Constant &&
2337 cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2338 Neg.getOperand(1) == Pos &&
2339 (Pos == CmpOp ||
2340 (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2341 Pos.getOperand(0) == CmpOp)));
2342}
2343
2344// Return the absolute or negative absolute of Op; IsNegative decides which.
2345static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2346 bool IsNegative) {
2347 Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2348 if (IsNegative)
2349 Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002350 DAG.getConstant(0, DL, Op.getValueType()), Op);
Richard Sandiford57485472013-12-13 15:35:00 +00002351 return Op;
2352}
2353
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002354SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2355 SelectionDAG &DAG) const {
2356 SDValue CmpOp0 = Op.getOperand(0);
2357 SDValue CmpOp1 = Op.getOperand(1);
2358 SDValue TrueOp = Op.getOperand(2);
2359 SDValue FalseOp = Op.getOperand(3);
2360 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002361 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002362
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002363 Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
Richard Sandiford57485472013-12-13 15:35:00 +00002364
2365 // Check for absolute and negative-absolute selections, including those
2366 // where the comparison value is sign-extended (for LPGFR and LNGFR).
2367 // This check supplements the one in DAGCombiner.
2368 if (C.Opcode == SystemZISD::ICMP &&
2369 C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2370 C.CCMask != SystemZ::CCMASK_CMP_NE &&
2371 C.Op1.getOpcode() == ISD::Constant &&
2372 cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2373 if (isAbsolute(C.Op0, TrueOp, FalseOp))
2374 return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2375 if (isAbsolute(C.Op0, FalseOp, TrueOp))
2376 return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2377 }
2378
Richard Sandifordd420f732013-12-13 15:28:45 +00002379 SDValue Glue = emitCmp(DAG, DL, C);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002380
2381 // Special case for handling -1/0 results. The shifts we use here
2382 // should get optimized with the IPM conversion sequence.
Richard Sandiford21f5d682014-03-06 11:22:58 +00002383 auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2384 auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002385 if (TrueC && FalseC) {
2386 int64_t TrueVal = TrueC->getSExtValue();
2387 int64_t FalseVal = FalseC->getSExtValue();
2388 if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2389 // Invert the condition if we want -1 on false.
2390 if (TrueVal == 0)
Richard Sandifordd420f732013-12-13 15:28:45 +00002391 C.CCMask ^= C.CCValid;
2392 SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002393 EVT VT = Op.getValueType();
2394 // Extend the result to VT. Upper bits are ignored.
2395 if (!is32Bit(VT))
2396 Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2397 // Sign-extend from the low bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002398 SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
Richard Sandiford48ef6ab2013-12-06 09:53:09 +00002399 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2400 return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2401 }
2402 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002403
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002404 SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2405 DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002406
2407 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
Craig Topper48d114b2014-04-26 18:35:24 +00002408 return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002409}
2410
2411SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2412 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002413 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002414 const GlobalValue *GV = Node->getGlobal();
2415 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002416 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002417 Reloc::Model RM = DAG.getTarget().getRelocationModel();
2418 CodeModel::Model CM = DAG.getTarget().getCodeModel();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002419
2420 SDValue Result;
2421 if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
Richard Sandiford54b36912013-09-27 15:14:04 +00002422 // Assign anchors at 1<<12 byte boundaries.
2423 uint64_t Anchor = Offset & ~uint64_t(0xfff);
2424 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2425 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2426
2427 // The offset can be folded into the address if it is aligned to a halfword.
2428 Offset -= Anchor;
2429 if (Offset != 0 && (Offset & 1) == 0) {
2430 SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2431 Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002432 Offset = 0;
2433 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002434 } else {
2435 Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2436 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2437 Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
Alex Lorenze40c8a22015-08-11 23:09:45 +00002438 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2439 false, false, false, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002440 }
2441
2442 // If there was a non-zero offset that we didn't fold, create an explicit
2443 // addition for it.
2444 if (Offset != 0)
2445 Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002446 DAG.getConstant(Offset, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002447
2448 return Result;
2449}
2450
Ulrich Weigand7db69182015-02-18 09:13:27 +00002451SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2452 SelectionDAG &DAG,
2453 unsigned Opcode,
2454 SDValue GOTOffset) const {
2455 SDLoc DL(Node);
Mehdi Amini44ede332015-07-09 02:09:04 +00002456 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand7db69182015-02-18 09:13:27 +00002457 SDValue Chain = DAG.getEntryNode();
2458 SDValue Glue;
2459
2460 // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2461 SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2462 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2463 Glue = Chain.getValue(1);
2464 Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2465 Glue = Chain.getValue(1);
2466
2467 // The first call operand is the chain and the second is the TLS symbol.
2468 SmallVector<SDValue, 8> Ops;
2469 Ops.push_back(Chain);
2470 Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2471 Node->getValueType(0),
2472 0, 0));
2473
2474 // Add argument registers to the end of the list so that they are
2475 // known live into the call.
2476 Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2477 Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2478
2479 // Add a register mask operand representing the call-preserved registers.
2480 const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
Eric Christopher9deb75d2015-03-11 22:42:13 +00002481 const uint32_t *Mask =
2482 TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002483 assert(Mask && "Missing call preserved mask for calling convention");
2484 Ops.push_back(DAG.getRegisterMask(Mask));
2485
2486 // Glue the call to the argument copies.
2487 Ops.push_back(Glue);
2488
2489 // Emit the call.
2490 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2491 Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2492 Glue = Chain.getValue(1);
2493
2494 // Copy the return value from %r2.
2495 return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2496}
2497
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002498SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002499 SelectionDAG &DAG) const {
Chih-Hung Hsieh1e859582015-07-28 16:24:05 +00002500 if (DAG.getTarget().Options.EmulatedTLS)
2501 return LowerToTLSEmulatedModel(Node, DAG);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002502 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002503 const GlobalValue *GV = Node->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00002504 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Eric Christopher93bf97c2014-06-27 07:38:01 +00002505 TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002506
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002507 // The high part of the thread pointer is in access register 0.
2508 SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002509 DAG.getConstant(0, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002510 TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2511
2512 // The low part of the thread pointer is in access register 1.
2513 SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002514 DAG.getConstant(1, DL, MVT::i32));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002515 TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2516
2517 // Merge them into a single 64-bit address.
2518 SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002519 DAG.getConstant(32, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002520 SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2521
Ulrich Weigand7db69182015-02-18 09:13:27 +00002522 // Get the offset of GA from the thread pointer, based on the TLS model.
2523 SDValue Offset;
2524 switch (model) {
2525 case TLSModel::GeneralDynamic: {
2526 // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2527 SystemZConstantPoolValue *CPV =
2528 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002529
Ulrich Weigand7db69182015-02-18 09:13:27 +00002530 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002531 Offset = DAG.getLoad(
2532 PtrVT, DL, DAG.getEntryNode(), Offset,
2533 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2534 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002535
2536 // Call __tls_get_offset to retrieve the offset.
2537 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2538 break;
2539 }
2540
2541 case TLSModel::LocalDynamic: {
2542 // Load the GOT offset of the module ID.
2543 SystemZConstantPoolValue *CPV =
2544 SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2545
2546 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002547 Offset = DAG.getLoad(
2548 PtrVT, DL, DAG.getEntryNode(), Offset,
2549 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2550 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002551
2552 // Call __tls_get_offset to retrieve the module base offset.
2553 Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2554
2555 // Note: The SystemZLDCleanupPass will remove redundant computations
2556 // of the module base offset. Count total number of local-dynamic
2557 // accesses to trigger execution of that pass.
2558 SystemZMachineFunctionInfo* MFI =
2559 DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2560 MFI->incNumLocalDynamicTLSAccesses();
2561
2562 // Add the per-symbol offset.
2563 CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2564
2565 SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002566 DTPOffset = DAG.getLoad(
2567 PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2568 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2569 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002570
2571 Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2572 break;
2573 }
2574
2575 case TLSModel::InitialExec: {
2576 // Load the offset from the GOT.
2577 Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2578 SystemZII::MO_INDNTPOFF);
2579 Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002580 Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2581 MachinePointerInfo::getGOT(DAG.getMachineFunction()),
Ulrich Weigand7db69182015-02-18 09:13:27 +00002582 false, false, false, 0);
2583 break;
2584 }
2585
2586 case TLSModel::LocalExec: {
2587 // Force the offset into the constant pool and load it from there.
2588 SystemZConstantPoolValue *CPV =
2589 SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2590
2591 Offset = DAG.getConstantPool(CPV, PtrVT, 8);
Alex Lorenze40c8a22015-08-11 23:09:45 +00002592 Offset = DAG.getLoad(
2593 PtrVT, DL, DAG.getEntryNode(), Offset,
2594 MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2595 false, false, 0);
Ulrich Weigand7db69182015-02-18 09:13:27 +00002596 break;
Ulrich Weigandb7e59092015-02-18 09:42:23 +00002597 }
Ulrich Weigand7db69182015-02-18 09:13:27 +00002598 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002599
2600 // Add the base and offset together.
2601 return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2602}
2603
2604SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2605 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002606 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002607 const BlockAddress *BA = Node->getBlockAddress();
2608 int64_t Offset = Node->getOffset();
Mehdi Amini44ede332015-07-09 02:09:04 +00002609 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002610
2611 SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2612 Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2613 return Result;
2614}
2615
2616SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2617 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002618 SDLoc DL(JT);
Mehdi Amini44ede332015-07-09 02:09:04 +00002619 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002620 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2621
2622 // Use LARL to load the address of the table.
2623 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2624}
2625
2626SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2627 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002628 SDLoc DL(CP);
Mehdi Amini44ede332015-07-09 02:09:04 +00002629 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002630
2631 SDValue Result;
2632 if (CP->isMachineConstantPoolEntry())
2633 Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002634 CP->getAlignment());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002635 else
2636 Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00002637 CP->getAlignment(), CP->getOffset());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002638
2639 // Use LARL to load the address of the constant pool entry.
2640 return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2641}
2642
2643SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2644 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002645 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002646 SDValue In = Op.getOperand(0);
2647 EVT InVT = In.getValueType();
2648 EVT ResVT = Op.getValueType();
2649
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002650 // Convert loads directly. This is normally done by DAGCombiner,
2651 // but we need this case for bitcasts that are created during lowering
2652 // and which are then lowered themselves.
2653 if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2654 return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2655 LoadN->getMemOperand());
2656
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002657 if (InVT == MVT::i32 && ResVT == MVT::f32) {
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002658 SDValue In64;
2659 if (Subtarget.hasHighWord()) {
2660 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2661 MVT::i64);
2662 In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2663 MVT::i64, SDValue(U64, 0), In);
2664 } else {
2665 In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2666 In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002667 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002668 }
2669 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002670 return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
Richard Sandifordd8163202013-09-13 09:12:44 +00002671 DL, MVT::f32, Out64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002672 }
2673 if (InVT == MVT::f32 && ResVT == MVT::i32) {
2674 SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
Ulrich Weigand9ac2f9b2015-05-04 17:41:22 +00002675 SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002676 MVT::f64, SDValue(U64, 0), In);
2677 SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002678 if (Subtarget.hasHighWord())
2679 return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2680 MVT::i32, Out64);
2681 SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002682 DAG.getConstant(32, DL, MVT::i64));
Richard Sandifordf6377fb2013-10-01 14:31:11 +00002683 return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002684 }
2685 llvm_unreachable("Unexpected bitcast combination");
2686}
2687
2688SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2689 SelectionDAG &DAG) const {
2690 MachineFunction &MF = DAG.getMachineFunction();
2691 SystemZMachineFunctionInfo *FuncInfo =
2692 MF.getInfo<SystemZMachineFunctionInfo>();
Mehdi Amini44ede332015-07-09 02:09:04 +00002693 EVT PtrVT = getPointerTy(DAG.getDataLayout());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002694
2695 SDValue Chain = Op.getOperand(0);
2696 SDValue Addr = Op.getOperand(1);
2697 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002698 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002699
2700 // The initial values of each field.
2701 const unsigned NumFields = 4;
2702 SDValue Fields[NumFields] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002703 DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2704 DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002705 DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2706 DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2707 };
2708
2709 // Store each field into its respective slot.
2710 SDValue MemOps[NumFields];
2711 unsigned Offset = 0;
2712 for (unsigned I = 0; I < NumFields; ++I) {
2713 SDValue FieldAddr = Addr;
2714 if (Offset != 0)
2715 FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002716 DAG.getIntPtrConstant(Offset, DL));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002717 MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2718 MachinePointerInfo(SV, Offset),
2719 false, false, 0);
2720 Offset += 8;
2721 }
Craig Topper48d114b2014-04-26 18:35:24 +00002722 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002723}
2724
2725SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2726 SelectionDAG &DAG) const {
2727 SDValue Chain = Op.getOperand(0);
2728 SDValue DstPtr = Op.getOperand(1);
2729 SDValue SrcPtr = Op.getOperand(2);
2730 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2731 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002732 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002733
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002734 return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002735 /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +00002736 /*isTailCall*/false,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002737 MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2738}
2739
2740SDValue SystemZTargetLowering::
2741lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002742 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2743 bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2744 hasFnAttribute("no-realign-stack");
2745
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002746 SDValue Chain = Op.getOperand(0);
2747 SDValue Size = Op.getOperand(1);
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002748 SDValue Align = Op.getOperand(2);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002749 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002750
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002751 // If user has set the no alignment function attribute, ignore
2752 // alloca alignments.
2753 uint64_t AlignVal = (RealignOpt ?
2754 dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2755
2756 uint64_t StackAlign = TFI->getStackAlignment();
2757 uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2758 uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2759
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002760 unsigned SPReg = getStackPointerRegisterToSaveRestore();
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002761 SDValue NeededSpace = Size;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002762
2763 // Get a reference to the stack pointer.
2764 SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2765
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002766 // Add extra space for alignment if needed.
2767 if (ExtraAlignSpace)
2768 NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2769 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2770
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002771 // Get the new stack pointer value.
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002772 SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002773
2774 // Copy the new stack pointer back.
2775 Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2776
2777 // The allocated data lives above the 160 bytes allocated for the standard
2778 // frame, plus any outgoing stack arguments. We don't know how much that
2779 // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2780 SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2781 SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2782
Jonas Paulssonf12b9252015-11-28 11:02:32 +00002783 // Dynamically realign if needed.
2784 if (RequiredAlign > StackAlign) {
2785 Result =
2786 DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2787 DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2788 Result =
2789 DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2790 DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2791 }
2792
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002793 SDValue Ops[2] = { Result, Chain };
Craig Topper64941d92014-04-27 19:20:57 +00002794 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002795}
2796
Richard Sandiford7d86e472013-08-21 09:34:56 +00002797SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2798 SelectionDAG &DAG) const {
2799 EVT VT = Op.getValueType();
2800 SDLoc DL(Op);
2801 SDValue Ops[2];
2802 if (is32Bit(VT))
2803 // Just do a normal 64-bit multiplication and extract the results.
2804 // We define this so that it can be used for constant division.
2805 lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2806 Op.getOperand(1), Ops[1], Ops[0]);
2807 else {
2808 // Do a full 128-bit multiplication based on UMUL_LOHI64:
2809 //
2810 // (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2811 //
2812 // but using the fact that the upper halves are either all zeros
2813 // or all ones:
2814 //
2815 // (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2816 //
2817 // and grouping the right terms together since they are quicker than the
2818 // multiplication:
2819 //
2820 // (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002821 SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002822 SDValue LL = Op.getOperand(0);
2823 SDValue RL = Op.getOperand(1);
2824 SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2825 SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2826 // UMUL_LOHI64 returns the low result in the odd register and the high
2827 // result in the even register. SMUL_LOHI is defined to return the
2828 // low half first, so the results are in reverse order.
2829 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2830 LL, RL, Ops[1], Ops[0]);
2831 SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2832 SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2833 SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2834 Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2835 }
Craig Topper64941d92014-04-27 19:20:57 +00002836 return DAG.getMergeValues(Ops, DL);
Richard Sandiford7d86e472013-08-21 09:34:56 +00002837}
2838
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002839SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2840 SelectionDAG &DAG) const {
2841 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002842 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002843 SDValue Ops[2];
Richard Sandiford7d86e472013-08-21 09:34:56 +00002844 if (is32Bit(VT))
2845 // Just do a normal 64-bit multiplication and extract the results.
2846 // We define this so that it can be used for constant division.
2847 lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2848 Op.getOperand(1), Ops[1], Ops[0]);
2849 else
2850 // UMUL_LOHI64 returns the low result in the odd register and the high
2851 // result in the even register. UMUL_LOHI is defined to return the
2852 // low half first, so the results are in reverse order.
2853 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2854 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002855 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002856}
2857
2858SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2859 SelectionDAG &DAG) const {
2860 SDValue Op0 = Op.getOperand(0);
2861 SDValue Op1 = Op.getOperand(1);
2862 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002863 SDLoc DL(Op);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002864 unsigned Opcode;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002865
2866 // We use DSGF for 32-bit division.
2867 if (is32Bit(VT)) {
2868 Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
Richard Sandiforde6e78852013-07-02 15:40:22 +00002869 Opcode = SystemZISD::SDIVREM32;
2870 } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2871 Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2872 Opcode = SystemZISD::SDIVREM32;
NAKAMURA Takumi10c80e72015-09-22 11:19:03 +00002873 } else
Richard Sandiforde6e78852013-07-02 15:40:22 +00002874 Opcode = SystemZISD::SDIVREM64;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002875
2876 // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2877 // input is "don't care". The instruction returns the remainder in
2878 // the even register and the quotient in the odd register.
2879 SDValue Ops[2];
Richard Sandiforde6e78852013-07-02 15:40:22 +00002880 lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002881 Op0, Op1, Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002882 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002883}
2884
2885SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2886 SelectionDAG &DAG) const {
2887 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002888 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002889
2890 // DL(G) uses a double-width dividend, so we need to clear the even
2891 // register in the GR128 input. The instruction returns the remainder
2892 // in the even register and the quotient in the odd register.
2893 SDValue Ops[2];
2894 if (is32Bit(VT))
2895 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2896 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2897 else
2898 lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2899 Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
Craig Topper64941d92014-04-27 19:20:57 +00002900 return DAG.getMergeValues(Ops, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002901}
2902
2903SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2904 assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2905
2906 // Get the known-zero masks for each operand.
2907 SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2908 APInt KnownZero[2], KnownOne[2];
Jay Foada0653a32014-05-14 21:14:37 +00002909 DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2910 DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002911
2912 // See if the upper 32 bits of one operand and the lower 32 bits of the
2913 // other are known zero. They are the low and high operands respectively.
2914 uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2915 KnownZero[1].getZExtValue() };
2916 unsigned High, Low;
2917 if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2918 High = 1, Low = 0;
2919 else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2920 High = 0, Low = 1;
2921 else
2922 return Op;
2923
2924 SDValue LowOp = Ops[Low];
2925 SDValue HighOp = Ops[High];
2926
2927 // If the high part is a constant, we're better off using IILH.
2928 if (HighOp.getOpcode() == ISD::Constant)
2929 return Op;
2930
2931 // If the low part is a constant that is outside the range of LHI,
2932 // then we're better off using IILF.
2933 if (LowOp.getOpcode() == ISD::Constant) {
2934 int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2935 if (!isInt<16>(Value))
2936 return Op;
2937 }
2938
2939 // Check whether the high part is an AND that doesn't change the
2940 // high 32 bits and just masks out low bits. We can skip it if so.
2941 if (HighOp.getOpcode() == ISD::AND &&
2942 HighOp.getOperand(1).getOpcode() == ISD::Constant) {
Richard Sandifordccc2a7c2013-12-03 11:01:54 +00002943 SDValue HighOp0 = HighOp.getOperand(0);
2944 uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2945 if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2946 HighOp = HighOp0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002947 }
2948
2949 // Take advantage of the fact that all GR32 operations only change the
2950 // low 32 bits by truncating Low to an i32 and inserting it directly
2951 // using a subreg. The interesting cases are those where the truncation
2952 // can be folded.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002953 SDLoc DL(Op);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002954 SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
Richard Sandiford87a44362013-09-30 10:28:35 +00002955 return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
Richard Sandifordd8163202013-09-13 09:12:44 +00002956 MVT::i64, HighOp, Low32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00002957}
2958
Ulrich Weigandb4012182015-03-31 12:56:33 +00002959SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
2960 SelectionDAG &DAG) const {
2961 EVT VT = Op.getValueType();
Ulrich Weigandb4012182015-03-31 12:56:33 +00002962 SDLoc DL(Op);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00002963 Op = Op.getOperand(0);
2964
2965 // Handle vector types via VPOPCT.
2966 if (VT.isVector()) {
2967 Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
2968 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
2969 switch (VT.getVectorElementType().getSizeInBits()) {
2970 case 8:
2971 break;
2972 case 16: {
2973 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
2974 SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
2975 SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
2976 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
2977 Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
2978 break;
2979 }
2980 case 32: {
2981 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2982 DAG.getConstant(0, DL, MVT::i32));
2983 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2984 break;
2985 }
2986 case 64: {
2987 SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2988 DAG.getConstant(0, DL, MVT::i32));
2989 Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
2990 Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
2991 break;
2992 }
2993 default:
2994 llvm_unreachable("Unexpected type");
2995 }
2996 return Op;
2997 }
Ulrich Weigandb4012182015-03-31 12:56:33 +00002998
2999 // Get the known-zero mask for the operand.
Ulrich Weigandb4012182015-03-31 12:56:33 +00003000 APInt KnownZero, KnownOne;
3001 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Ulrich Weigand050527b2015-03-31 19:28:50 +00003002 unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3003 if (NumSignificantBits == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003004 return DAG.getConstant(0, DL, VT);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003005
3006 // Skip known-zero high parts of the operand.
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003007 int64_t OrigBitSize = VT.getSizeInBits();
Ulrich Weigand050527b2015-03-31 19:28:50 +00003008 int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3009 BitSize = std::min(BitSize, OrigBitSize);
Ulrich Weigandb4012182015-03-31 12:56:33 +00003010
3011 // The POPCNT instruction counts the number of bits in each byte.
3012 Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3013 Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3014 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3015
3016 // Add up per-byte counts in a binary tree. All bits of Op at
3017 // position larger than BitSize remain zero throughout.
3018 for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003019 SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003020 if (BitSize != OrigBitSize)
3021 Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003022 DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003023 Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3024 }
3025
3026 // Extract overall result from high byte.
3027 if (BitSize > 8)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003028 Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3029 DAG.getConstant(BitSize - 8, DL, VT));
Ulrich Weigandb4012182015-03-31 12:56:33 +00003030
3031 return Op;
3032}
3033
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003034// Op is an atomic load. Lower it into a normal volatile load.
3035SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3036 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003037 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003038 return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3039 Node->getChain(), Node->getBasePtr(),
3040 Node->getMemoryVT(), Node->getMemOperand());
3041}
3042
3043// Op is an atomic store. Lower it into a normal volatile store followed
3044// by a serialization.
3045SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3046 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003047 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003048 SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3049 Node->getBasePtr(), Node->getMemoryVT(),
3050 Node->getMemOperand());
3051 return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3052 Chain), 0);
3053}
3054
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003055// Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation. Lower the first
3056// two into the fullword ATOMIC_LOADW_* operation given by Opcode.
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00003057SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3058 SelectionDAG &DAG,
3059 unsigned Opcode) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003060 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003061
3062 // 32-bit operations need no code outside the main loop.
3063 EVT NarrowVT = Node->getMemoryVT();
3064 EVT WideVT = MVT::i32;
3065 if (NarrowVT == WideVT)
3066 return Op;
3067
3068 int64_t BitSize = NarrowVT.getSizeInBits();
3069 SDValue ChainIn = Node->getChain();
3070 SDValue Addr = Node->getBasePtr();
3071 SDValue Src2 = Node->getVal();
3072 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003073 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003074 EVT PtrVT = Addr.getValueType();
3075
3076 // Convert atomic subtracts of constants into additions.
3077 if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
Richard Sandiford21f5d682014-03-06 11:22:58 +00003078 if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003079 Opcode = SystemZISD::ATOMIC_LOADW_ADD;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003080 Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003081 }
3082
3083 // Get the address of the containing word.
3084 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003085 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003086
3087 // Get the number of bits that the word must be rotated left in order
3088 // to bring the field to the top bits of a GR32.
3089 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003090 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003091 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3092
3093 // Get the complementing shift amount, for rotating a field in the top
3094 // bits back to its proper position.
3095 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003096 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003097
3098 // Extend the source operand to 32 bits and prepare it for the inner loop.
3099 // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3100 // operations require the source to be shifted in advance. (This shift
3101 // can be folded if the source is constant.) For AND and NAND, the lower
3102 // bits must be set, while for other opcodes they should be left clear.
3103 if (Opcode != SystemZISD::ATOMIC_SWAPW)
3104 Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003105 DAG.getConstant(32 - BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003106 if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3107 Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3108 Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003109 DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003110
3111 // Construct the ATOMIC_LOADW_* node.
3112 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3113 SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003114 DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003115 SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003116 NarrowVT, MMO);
3117
3118 // Rotate the result of the final CS so that the field is in the lower
3119 // bits of a GR32, then truncate it.
3120 SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003121 DAG.getConstant(BitSize, DL, WideVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003122 SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3123
3124 SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
Craig Topper64941d92014-04-27 19:20:57 +00003125 return DAG.getMergeValues(RetOps, DL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003126}
3127
Richard Sandiford41350a52013-12-24 15:18:04 +00003128// Op is an ATOMIC_LOAD_SUB operation. Lower 8- and 16-bit operations
Richard Sandiford002019a2013-12-24 15:22:39 +00003129// into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
Richard Sandiford41350a52013-12-24 15:18:04 +00003130// operations into additions.
3131SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3132 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003133 auto *Node = cast<AtomicSDNode>(Op.getNode());
Richard Sandiford41350a52013-12-24 15:18:04 +00003134 EVT MemVT = Node->getMemoryVT();
3135 if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3136 // A full-width operation.
3137 assert(Op.getValueType() == MemVT && "Mismatched VTs");
3138 SDValue Src2 = Node->getVal();
3139 SDValue NegSrc2;
3140 SDLoc DL(Src2);
3141
Richard Sandiford21f5d682014-03-06 11:22:58 +00003142 if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
Richard Sandiford41350a52013-12-24 15:18:04 +00003143 // Use an addition if the operand is constant and either LAA(G) is
3144 // available or the negative value is in the range of A(G)FHI.
3145 int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
Eric Christopher93bf97c2014-06-27 07:38:01 +00003146 if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003147 NegSrc2 = DAG.getConstant(Value, DL, MemVT);
Eric Christopher93bf97c2014-06-27 07:38:01 +00003148 } else if (Subtarget.hasInterlockedAccess1())
Richard Sandiford41350a52013-12-24 15:18:04 +00003149 // Use LAA(G) if available.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003150 NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
Richard Sandiford41350a52013-12-24 15:18:04 +00003151 Src2);
3152
3153 if (NegSrc2.getNode())
3154 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3155 Node->getChain(), Node->getBasePtr(), NegSrc2,
3156 Node->getMemOperand(), Node->getOrdering(),
3157 Node->getSynchScope());
3158
3159 // Use the node as-is.
3160 return Op;
3161 }
3162
3163 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3164}
3165
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003166// Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation. Lower the first two
3167// into a fullword ATOMIC_CMP_SWAPW operation.
3168SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3169 SelectionDAG &DAG) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00003170 auto *Node = cast<AtomicSDNode>(Op.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003171
3172 // We have native support for 32-bit compare and swap.
3173 EVT NarrowVT = Node->getMemoryVT();
3174 EVT WideVT = MVT::i32;
3175 if (NarrowVT == WideVT)
3176 return Op;
3177
3178 int64_t BitSize = NarrowVT.getSizeInBits();
3179 SDValue ChainIn = Node->getOperand(0);
3180 SDValue Addr = Node->getOperand(1);
3181 SDValue CmpVal = Node->getOperand(2);
3182 SDValue SwapVal = Node->getOperand(3);
3183 MachineMemOperand *MMO = Node->getMemOperand();
Andrew Trickef9de2a2013-05-25 02:42:55 +00003184 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003185 EVT PtrVT = Addr.getValueType();
3186
3187 // Get the address of the containing word.
3188 SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003189 DAG.getConstant(-4, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003190
3191 // Get the number of bits that the word must be rotated left in order
3192 // to bring the field to the top bits of a GR32.
3193 SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003194 DAG.getConstant(3, DL, PtrVT));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003195 BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3196
3197 // Get the complementing shift amount, for rotating a field in the top
3198 // bits back to its proper position.
3199 SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003200 DAG.getConstant(0, DL, WideVT), BitShift);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003201
3202 // Construct the ATOMIC_CMP_SWAPW node.
3203 SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3204 SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003205 NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003206 SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003207 VTList, Ops, NarrowVT, MMO);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003208 return AtomicOp;
3209}
3210
3211SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3212 SelectionDAG &DAG) const {
3213 MachineFunction &MF = DAG.getMachineFunction();
3214 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003215 return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003216 SystemZ::R15D, Op.getValueType());
3217}
3218
3219SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3220 SelectionDAG &DAG) const {
3221 MachineFunction &MF = DAG.getMachineFunction();
3222 MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
Andrew Trickef9de2a2013-05-25 02:42:55 +00003223 return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
Ulrich Weigand5f613df2013-05-06 16:15:19 +00003224 SystemZ::R15D, Op.getOperand(1));
3225}
3226
Richard Sandiford03481332013-08-23 11:36:42 +00003227SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3228 SelectionDAG &DAG) const {
3229 bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3230 if (!IsData)
3231 // Just preserve the chain.
3232 return Op.getOperand(0);
3233
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003234 SDLoc DL(Op);
Richard Sandiford03481332013-08-23 11:36:42 +00003235 bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3236 unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
Richard Sandiford21f5d682014-03-06 11:22:58 +00003237 auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
Richard Sandiford03481332013-08-23 11:36:42 +00003238 SDValue Ops[] = {
3239 Op.getOperand(0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003240 DAG.getConstant(Code, DL, MVT::i32),
Richard Sandiford03481332013-08-23 11:36:42 +00003241 Op.getOperand(1)
3242 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003243 return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
Craig Topper206fcd42014-04-26 19:29:41 +00003244 Node->getVTList(), Ops,
Richard Sandiford03481332013-08-23 11:36:42 +00003245 Node->getMemoryVT(), Node->getMemOperand());
3246}
3247
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003248// Return an i32 that contains the value of CC immediately after After,
3249// whose final operand must be MVT::Glue.
3250static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003251 SDLoc DL(After);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003252 SDValue Glue = SDValue(After, After->getNumValues() - 1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003253 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3254 return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3255 DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
Ulrich Weigand57c85f52015-04-01 12:51:43 +00003256}
3257
3258SDValue
3259SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3260 SelectionDAG &DAG) const {
3261 unsigned Opcode, CCValid;
3262 if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3263 assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3264 SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3265 SDValue CC = getCCResult(DAG, Glued.getNode());
3266 DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3267 return SDValue();
3268 }
3269
3270 return SDValue();
3271}
3272
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003273SDValue
3274SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3275 SelectionDAG &DAG) const {
3276 unsigned Opcode, CCValid;
3277 if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3278 SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3279 SDValue CC = getCCResult(DAG, Glued.getNode());
3280 if (Op->getNumValues() == 1)
3281 return CC;
3282 assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00003283 return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3284 CC);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00003285 }
3286
3287 unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3288 switch (Id) {
3289 case Intrinsic::s390_vpdi:
3290 return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3291 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3292
3293 case Intrinsic::s390_vperm:
3294 return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3295 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3296
3297 case Intrinsic::s390_vuphb:
3298 case Intrinsic::s390_vuphh:
3299 case Intrinsic::s390_vuphf:
3300 return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3301 Op.getOperand(1));
3302
3303 case Intrinsic::s390_vuplhb:
3304 case Intrinsic::s390_vuplhh:
3305 case Intrinsic::s390_vuplhf:
3306 return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3307 Op.getOperand(1));
3308
3309 case Intrinsic::s390_vuplb:
3310 case Intrinsic::s390_vuplhw:
3311 case Intrinsic::s390_vuplf:
3312 return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3313 Op.getOperand(1));
3314
3315 case Intrinsic::s390_vupllb:
3316 case Intrinsic::s390_vupllh:
3317 case Intrinsic::s390_vupllf:
3318 return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3319 Op.getOperand(1));
3320
3321 case Intrinsic::s390_vsumb:
3322 case Intrinsic::s390_vsumh:
3323 case Intrinsic::s390_vsumgh:
3324 case Intrinsic::s390_vsumgf:
3325 case Intrinsic::s390_vsumqf:
3326 case Intrinsic::s390_vsumqg:
3327 return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3328 Op.getOperand(1), Op.getOperand(2));
3329 }
3330
3331 return SDValue();
3332}
3333
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003334namespace {
3335// Says that SystemZISD operation Opcode can be used to perform the equivalent
3336// of a VPERM with permute vector Bytes. If Opcode takes three operands,
3337// Operand is the constant third operand, otherwise it is the number of
3338// bytes in each element of the result.
3339struct Permute {
3340 unsigned Opcode;
3341 unsigned Operand;
3342 unsigned char Bytes[SystemZ::VectorBytes];
3343};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003344}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003345
3346static const Permute PermuteForms[] = {
3347 // VMRHG
3348 { SystemZISD::MERGE_HIGH, 8,
3349 { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3350 // VMRHF
3351 { SystemZISD::MERGE_HIGH, 4,
3352 { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3353 // VMRHH
3354 { SystemZISD::MERGE_HIGH, 2,
3355 { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3356 // VMRHB
3357 { SystemZISD::MERGE_HIGH, 1,
3358 { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3359 // VMRLG
3360 { SystemZISD::MERGE_LOW, 8,
3361 { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3362 // VMRLF
3363 { SystemZISD::MERGE_LOW, 4,
3364 { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3365 // VMRLH
3366 { SystemZISD::MERGE_LOW, 2,
3367 { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3368 // VMRLB
3369 { SystemZISD::MERGE_LOW, 1,
3370 { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3371 // VPKG
3372 { SystemZISD::PACK, 4,
3373 { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3374 // VPKF
3375 { SystemZISD::PACK, 2,
3376 { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3377 // VPKH
3378 { SystemZISD::PACK, 1,
3379 { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3380 // VPDI V1, V2, 4 (low half of V1, high half of V2)
3381 { SystemZISD::PERMUTE_DWORDS, 4,
3382 { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3383 // VPDI V1, V2, 1 (high half of V1, low half of V2)
3384 { SystemZISD::PERMUTE_DWORDS, 1,
3385 { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3386};
3387
3388// Called after matching a vector shuffle against a particular pattern.
3389// Both the original shuffle and the pattern have two vector operands.
3390// OpNos[0] is the operand of the original shuffle that should be used for
3391// operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3392// OpNos[1] is the same for operand 1 of the pattern. Resolve these -1s and
3393// set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3394// for operands 0 and 1 of the pattern.
3395static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3396 if (OpNos[0] < 0) {
3397 if (OpNos[1] < 0)
3398 return false;
3399 OpNo0 = OpNo1 = OpNos[1];
3400 } else if (OpNos[1] < 0) {
3401 OpNo0 = OpNo1 = OpNos[0];
3402 } else {
3403 OpNo0 = OpNos[0];
3404 OpNo1 = OpNos[1];
3405 }
3406 return true;
3407}
3408
3409// Bytes is a VPERM-like permute vector, except that -1 is used for
3410// undefined bytes. Return true if the VPERM can be implemented using P.
3411// When returning true set OpNo0 to the VPERM operand that should be
3412// used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3413//
3414// For example, if swapping the VPERM operands allows P to match, OpNo0
3415// will be 1 and OpNo1 will be 0. If instead Bytes only refers to one
3416// operand, but rewriting it to use two duplicated operands allows it to
3417// match P, then OpNo0 and OpNo1 will be the same.
3418static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3419 unsigned &OpNo0, unsigned &OpNo1) {
3420 int OpNos[] = { -1, -1 };
3421 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3422 int Elt = Bytes[I];
3423 if (Elt >= 0) {
3424 // Make sure that the two permute vectors use the same suboperand
3425 // byte number. Only the operand numbers (the high bits) are
3426 // allowed to differ.
3427 if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3428 return false;
3429 int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3430 int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3431 // Make sure that the operand mappings are consistent with previous
3432 // elements.
3433 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3434 return false;
3435 OpNos[ModelOpNo] = RealOpNo;
3436 }
3437 }
3438 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3439}
3440
3441// As above, but search for a matching permute.
3442static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3443 unsigned &OpNo0, unsigned &OpNo1) {
3444 for (auto &P : PermuteForms)
3445 if (matchPermute(Bytes, P, OpNo0, OpNo1))
3446 return &P;
3447 return nullptr;
3448}
3449
3450// Bytes is a VPERM-like permute vector, except that -1 is used for
3451// undefined bytes. This permute is an operand of an outer permute.
3452// See whether redistributing the -1 bytes gives a shuffle that can be
3453// implemented using P. If so, set Transform to a VPERM-like permute vector
3454// that, when applied to the result of P, gives the original permute in Bytes.
3455static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3456 const Permute &P,
3457 SmallVectorImpl<int> &Transform) {
3458 unsigned To = 0;
3459 for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3460 int Elt = Bytes[From];
3461 if (Elt < 0)
3462 // Byte number From of the result is undefined.
3463 Transform[From] = -1;
3464 else {
3465 while (P.Bytes[To] != Elt) {
3466 To += 1;
3467 if (To == SystemZ::VectorBytes)
3468 return false;
3469 }
3470 Transform[From] = To;
3471 }
3472 }
3473 return true;
3474}
3475
3476// As above, but search for a matching permute.
3477static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3478 SmallVectorImpl<int> &Transform) {
3479 for (auto &P : PermuteForms)
3480 if (matchDoublePermute(Bytes, P, Transform))
3481 return &P;
3482 return nullptr;
3483}
3484
3485// Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3486// as if it had type vNi8.
3487static void getVPermMask(ShuffleVectorSDNode *VSN,
3488 SmallVectorImpl<int> &Bytes) {
3489 EVT VT = VSN->getValueType(0);
3490 unsigned NumElements = VT.getVectorNumElements();
3491 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3492 Bytes.resize(NumElements * BytesPerElement, -1);
3493 for (unsigned I = 0; I < NumElements; ++I) {
3494 int Index = VSN->getMaskElt(I);
3495 if (Index >= 0)
3496 for (unsigned J = 0; J < BytesPerElement; ++J)
3497 Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3498 }
3499}
3500
3501// Bytes is a VPERM-like permute vector, except that -1 is used for
3502// undefined bytes. See whether bytes [Start, Start + BytesPerElement) of
3503// the result come from a contiguous sequence of bytes from one input.
3504// Set Base to the selector for the first byte if so.
3505static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3506 unsigned BytesPerElement, int &Base) {
3507 Base = -1;
3508 for (unsigned I = 0; I < BytesPerElement; ++I) {
3509 if (Bytes[Start + I] >= 0) {
3510 unsigned Elem = Bytes[Start + I];
3511 if (Base < 0) {
3512 Base = Elem - I;
3513 // Make sure the bytes would come from one input operand.
3514 if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3515 return false;
3516 } else if (unsigned(Base) != Elem - I)
3517 return false;
3518 }
3519 }
3520 return true;
3521}
3522
3523// Bytes is a VPERM-like permute vector, except that -1 is used for
3524// undefined bytes. Return true if it can be performed using VSLDI.
3525// When returning true, set StartIndex to the shift amount and OpNo0
3526// and OpNo1 to the VPERM operands that should be used as the first
3527// and second shift operand respectively.
3528static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3529 unsigned &StartIndex, unsigned &OpNo0,
3530 unsigned &OpNo1) {
3531 int OpNos[] = { -1, -1 };
3532 int Shift = -1;
3533 for (unsigned I = 0; I < 16; ++I) {
3534 int Index = Bytes[I];
3535 if (Index >= 0) {
3536 int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3537 int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3538 int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3539 if (Shift < 0)
3540 Shift = ExpectedShift;
3541 else if (Shift != ExpectedShift)
3542 return false;
3543 // Make sure that the operand mappings are consistent with previous
3544 // elements.
3545 if (OpNos[ModelOpNo] == 1 - RealOpNo)
3546 return false;
3547 OpNos[ModelOpNo] = RealOpNo;
3548 }
3549 }
3550 StartIndex = Shift;
3551 return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3552}
3553
3554// Create a node that performs P on operands Op0 and Op1, casting the
3555// operands to the appropriate type. The type of the result is determined by P.
3556static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3557 const Permute &P, SDValue Op0, SDValue Op1) {
3558 // VPDI (PERMUTE_DWORDS) always operates on v2i64s. The input
3559 // elements of a PACK are twice as wide as the outputs.
3560 unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3561 P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3562 P.Operand);
3563 // Cast both operands to the appropriate type.
3564 MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3565 SystemZ::VectorBytes / InBytes);
3566 Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3567 Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3568 SDValue Op;
3569 if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3570 SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3571 Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3572 } else if (P.Opcode == SystemZISD::PACK) {
3573 MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3574 SystemZ::VectorBytes / P.Operand);
3575 Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3576 } else {
3577 Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3578 }
3579 return Op;
3580}
3581
3582// Bytes is a VPERM-like permute vector, except that -1 is used for
3583// undefined bytes. Implement it on operands Ops[0] and Ops[1] using
3584// VSLDI or VPERM.
3585static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3586 const SmallVectorImpl<int> &Bytes) {
3587 for (unsigned I = 0; I < 2; ++I)
3588 Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3589
3590 // First see whether VSLDI can be used.
3591 unsigned StartIndex, OpNo0, OpNo1;
3592 if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3593 return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3594 Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3595
3596 // Fall back on VPERM. Construct an SDNode for the permute vector.
3597 SDValue IndexNodes[SystemZ::VectorBytes];
3598 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3599 if (Bytes[I] >= 0)
3600 IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3601 else
3602 IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3603 SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3604 return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3605}
3606
3607namespace {
3608// Describes a general N-operand vector shuffle.
3609struct GeneralShuffle {
3610 GeneralShuffle(EVT vt) : VT(vt) {}
3611 void addUndef();
3612 void add(SDValue, unsigned);
3613 SDValue getNode(SelectionDAG &, SDLoc);
3614
3615 // The operands of the shuffle.
3616 SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3617
3618 // Index I is -1 if byte I of the result is undefined. Otherwise the
3619 // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3620 // Bytes[I] / SystemZ::VectorBytes.
3621 SmallVector<int, SystemZ::VectorBytes> Bytes;
3622
3623 // The type of the shuffle result.
3624 EVT VT;
3625};
Alexander Kornienkof00654e2015-06-23 09:49:53 +00003626}
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003627
3628// Add an extra undefined element to the shuffle.
3629void GeneralShuffle::addUndef() {
3630 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3631 for (unsigned I = 0; I < BytesPerElement; ++I)
3632 Bytes.push_back(-1);
3633}
3634
3635// Add an extra element to the shuffle, taking it from element Elem of Op.
3636// A null Op indicates a vector input whose value will be calculated later;
3637// there is at most one such input per shuffle and it always has the same
3638// type as the result.
3639void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3640 unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3641
3642 // The source vector can have wider elements than the result,
3643 // either through an explicit TRUNCATE or because of type legalization.
3644 // We want the least significant part.
3645 EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3646 unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3647 assert(FromBytesPerElement >= BytesPerElement &&
3648 "Invalid EXTRACT_VECTOR_ELT");
3649 unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3650 (FromBytesPerElement - BytesPerElement));
3651
3652 // Look through things like shuffles and bitcasts.
3653 while (Op.getNode()) {
3654 if (Op.getOpcode() == ISD::BITCAST)
3655 Op = Op.getOperand(0);
3656 else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3657 // See whether the bytes we need come from a contiguous part of one
3658 // operand.
3659 SmallVector<int, SystemZ::VectorBytes> OpBytes;
3660 getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3661 int NewByte;
3662 if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3663 break;
3664 if (NewByte < 0) {
3665 addUndef();
3666 return;
3667 }
3668 Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3669 Byte = unsigned(NewByte) % SystemZ::VectorBytes;
3670 } else if (Op.getOpcode() == ISD::UNDEF) {
3671 addUndef();
3672 return;
3673 } else
3674 break;
3675 }
3676
3677 // Make sure that the source of the extraction is in Ops.
3678 unsigned OpNo = 0;
3679 for (; OpNo < Ops.size(); ++OpNo)
3680 if (Ops[OpNo] == Op)
3681 break;
3682 if (OpNo == Ops.size())
3683 Ops.push_back(Op);
3684
3685 // Add the element to Bytes.
3686 unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3687 for (unsigned I = 0; I < BytesPerElement; ++I)
3688 Bytes.push_back(Base + I);
3689}
3690
3691// Return SDNodes for the completed shuffle.
3692SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3693 assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3694
3695 if (Ops.size() == 0)
3696 return DAG.getUNDEF(VT);
3697
3698 // Make sure that there are at least two shuffle operands.
3699 if (Ops.size() == 1)
3700 Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3701
3702 // Create a tree of shuffles, deferring root node until after the loop.
3703 // Try to redistribute the undefined elements of non-root nodes so that
3704 // the non-root shuffles match something like a pack or merge, then adjust
3705 // the parent node's permute vector to compensate for the new order.
3706 // Among other things, this copes with vectors like <2 x i16> that were
3707 // padded with undefined elements during type legalization.
3708 //
3709 // In the best case this redistribution will lead to the whole tree
3710 // using packs and merges. It should rarely be a loss in other cases.
3711 unsigned Stride = 1;
3712 for (; Stride * 2 < Ops.size(); Stride *= 2) {
3713 for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3714 SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3715
3716 // Create a mask for just these two operands.
3717 SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3718 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3719 unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3720 unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3721 if (OpNo == I)
3722 NewBytes[J] = Byte;
3723 else if (OpNo == I + Stride)
3724 NewBytes[J] = SystemZ::VectorBytes + Byte;
3725 else
3726 NewBytes[J] = -1;
3727 }
3728 // See if it would be better to reorganize NewMask to avoid using VPERM.
3729 SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3730 if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3731 Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3732 // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3733 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3734 if (NewBytes[J] >= 0) {
3735 assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3736 "Invalid double permute");
3737 Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3738 } else
3739 assert(NewBytesMap[J] < 0 && "Invalid double permute");
3740 }
3741 } else {
3742 // Just use NewBytes on the operands.
3743 Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3744 for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3745 if (NewBytes[J] >= 0)
3746 Bytes[J] = I * SystemZ::VectorBytes + J;
3747 }
3748 }
3749 }
3750
3751 // Now we just have 2 inputs. Put the second operand in Ops[1].
3752 if (Stride > 1) {
3753 Ops[1] = Ops[Stride];
3754 for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3755 if (Bytes[I] >= int(SystemZ::VectorBytes))
3756 Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3757 }
3758
3759 // Look for an instruction that can do the permute without resorting
3760 // to VPERM.
3761 unsigned OpNo0, OpNo1;
3762 SDValue Op;
3763 if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3764 Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3765 else
3766 Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3767 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3768}
3769
Ulrich Weigandcd808232015-05-05 19:26:48 +00003770// Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3771static bool isScalarToVector(SDValue Op) {
3772 for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
3773 if (Op.getOperand(I).getOpcode() != ISD::UNDEF)
3774 return false;
3775 return true;
3776}
3777
3778// Return a vector of type VT that contains Value in the first element.
3779// The other elements don't matter.
3780static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3781 SDValue Value) {
3782 // If we have a constant, replicate it to all elements and let the
3783 // BUILD_VECTOR lowering take care of it.
3784 if (Value.getOpcode() == ISD::Constant ||
3785 Value.getOpcode() == ISD::ConstantFP) {
3786 SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3787 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3788 }
3789 if (Value.getOpcode() == ISD::UNDEF)
3790 return DAG.getUNDEF(VT);
3791 return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3792}
3793
3794// Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3795// element 1. Used for cases in which replication is cheap.
3796static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3797 SDValue Op0, SDValue Op1) {
3798 if (Op0.getOpcode() == ISD::UNDEF) {
3799 if (Op1.getOpcode() == ISD::UNDEF)
3800 return DAG.getUNDEF(VT);
3801 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3802 }
3803 if (Op1.getOpcode() == ISD::UNDEF)
3804 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3805 return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3806 buildScalarToVector(DAG, DL, VT, Op0),
3807 buildScalarToVector(DAG, DL, VT, Op1));
3808}
3809
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003810// Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3811// vector for them.
3812static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3813 SDValue Op1) {
3814 if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF)
3815 return DAG.getUNDEF(MVT::v2i64);
3816 // If one of the two inputs is undefined then replicate the other one,
3817 // in order to avoid using another register unnecessarily.
3818 if (Op0.getOpcode() == ISD::UNDEF)
3819 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3820 else if (Op1.getOpcode() == ISD::UNDEF)
3821 Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3822 else {
3823 Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3824 Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3825 }
3826 return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3827}
3828
3829// Try to represent constant BUILD_VECTOR node BVN using a
3830// SystemZISD::BYTE_MASK-style mask. Store the mask value in Mask
3831// on success.
3832static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3833 EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3834 unsigned BytesPerElement = ElemVT.getStoreSize();
3835 for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3836 SDValue Op = BVN->getOperand(I);
3837 if (Op.getOpcode() != ISD::UNDEF) {
3838 uint64_t Value;
3839 if (Op.getOpcode() == ISD::Constant)
3840 Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3841 else if (Op.getOpcode() == ISD::ConstantFP)
3842 Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3843 .getZExtValue());
3844 else
3845 return false;
3846 for (unsigned J = 0; J < BytesPerElement; ++J) {
3847 uint64_t Byte = (Value >> (J * 8)) & 0xff;
3848 if (Byte == 0xff)
Aaron Ballman2a3aa1f242015-05-11 12:45:53 +00003849 Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003850 else if (Byte != 0)
3851 return false;
3852 }
3853 }
3854 }
3855 return true;
3856}
3857
3858// Try to load a vector constant in which BitsPerElement-bit value Value
3859// is replicated to fill the vector. VT is the type of the resulting
3860// constant, which may have elements of a different size from BitsPerElement.
3861// Return the SDValue of the constant on success, otherwise return
3862// an empty value.
3863static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3864 const SystemZInstrInfo *TII,
3865 SDLoc DL, EVT VT, uint64_t Value,
3866 unsigned BitsPerElement) {
3867 // Signed 16-bit values can be replicated using VREPI.
3868 int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3869 if (isInt<16>(SignedValue)) {
3870 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3871 SystemZ::VectorBits / BitsPerElement);
3872 SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3873 DAG.getConstant(SignedValue, DL, MVT::i32));
3874 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3875 }
3876 // See whether rotating the constant left some N places gives a value that
3877 // is one less than a power of 2 (i.e. all zeros followed by all ones).
3878 // If so we can use VGM.
3879 unsigned Start, End;
3880 if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3881 // isRxSBGMask returns the bit numbers for a full 64-bit value,
3882 // with 0 denoting 1 << 63 and 63 denoting 1. Convert them to
3883 // bit numbers for an BitsPerElement value, so that 0 denotes
3884 // 1 << (BitsPerElement-1).
3885 Start -= 64 - BitsPerElement;
3886 End -= 64 - BitsPerElement;
3887 MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3888 SystemZ::VectorBits / BitsPerElement);
3889 SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3890 DAG.getConstant(Start, DL, MVT::i32),
3891 DAG.getConstant(End, DL, MVT::i32));
3892 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3893 }
3894 return SDValue();
3895}
3896
3897// If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3898// better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3899// the non-EXTRACT_VECTOR_ELT elements. See if the given BUILD_VECTOR
3900// would benefit from this representation and return it if so.
3901static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3902 BuildVectorSDNode *BVN) {
3903 EVT VT = BVN->getValueType(0);
3904 unsigned NumElements = VT.getVectorNumElements();
3905
3906 // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3907 // on byte vectors. If there are non-EXTRACT_VECTOR_ELT elements that still
3908 // need a BUILD_VECTOR, add an additional placeholder operand for that
3909 // BUILD_VECTOR and store its operands in ResidueOps.
3910 GeneralShuffle GS(VT);
3911 SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3912 bool FoundOne = false;
3913 for (unsigned I = 0; I < NumElements; ++I) {
3914 SDValue Op = BVN->getOperand(I);
3915 if (Op.getOpcode() == ISD::TRUNCATE)
3916 Op = Op.getOperand(0);
3917 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
3918 Op.getOperand(1).getOpcode() == ISD::Constant) {
3919 unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3920 GS.add(Op.getOperand(0), Elem);
3921 FoundOne = true;
3922 } else if (Op.getOpcode() == ISD::UNDEF) {
3923 GS.addUndef();
3924 } else {
3925 GS.add(SDValue(), ResidueOps.size());
Ulrich Weigande861e642015-09-15 14:27:46 +00003926 ResidueOps.push_back(BVN->getOperand(I));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003927 }
3928 }
3929
3930 // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
3931 if (!FoundOne)
3932 return SDValue();
3933
3934 // Create the BUILD_VECTOR for the remaining elements, if any.
3935 if (!ResidueOps.empty()) {
3936 while (ResidueOps.size() < NumElements)
Ulrich Weigandf4d14f72015-10-08 17:46:59 +00003937 ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
Ulrich Weigandce4c1092015-05-05 19:25:42 +00003938 for (auto &Op : GS.Ops) {
3939 if (!Op.getNode()) {
3940 Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
3941 break;
3942 }
3943 }
3944 }
3945 return GS.getNode(DAG, SDLoc(BVN));
3946}
3947
3948// Combine GPR scalar values Elems into a vector of type VT.
3949static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3950 SmallVectorImpl<SDValue> &Elems) {
3951 // See whether there is a single replicated value.
3952 SDValue Single;
3953 unsigned int NumElements = Elems.size();
3954 unsigned int Count = 0;
3955 for (auto Elem : Elems) {
3956 if (Elem.getOpcode() != ISD::UNDEF) {
3957 if (!Single.getNode())
3958 Single = Elem;
3959 else if (Elem != Single) {
3960 Single = SDValue();
3961 break;
3962 }
3963 Count += 1;
3964 }
3965 }
3966 // There are three cases here:
3967 //
3968 // - if the only defined element is a loaded one, the best sequence
3969 // is a replicating load.
3970 //
3971 // - otherwise, if the only defined element is an i64 value, we will
3972 // end up with the same VLVGP sequence regardless of whether we short-cut
3973 // for replication or fall through to the later code.
3974 //
3975 // - otherwise, if the only defined element is an i32 or smaller value,
3976 // we would need 2 instructions to replicate it: VLVGP followed by VREPx.
3977 // This is only a win if the single defined element is used more than once.
3978 // In other cases we're better off using a single VLVGx.
3979 if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
3980 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
3981
3982 // The best way of building a v2i64 from two i64s is to use VLVGP.
3983 if (VT == MVT::v2i64)
3984 return joinDwords(DAG, DL, Elems[0], Elems[1]);
3985
Ulrich Weigandcd808232015-05-05 19:26:48 +00003986 // Use a 64-bit merge high to combine two doubles.
3987 if (VT == MVT::v2f64)
3988 return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
3989
Ulrich Weigand80b3af72015-05-05 19:27:45 +00003990 // Build v4f32 values directly from the FPRs:
3991 //
3992 // <Axxx> <Bxxx> <Cxxxx> <Dxxx>
3993 // V V VMRHF
3994 // <ABxx> <CDxx>
3995 // V VMRHG
3996 // <ABCD>
3997 if (VT == MVT::v4f32) {
3998 SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
3999 SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4000 // Avoid unnecessary undefs by reusing the other operand.
4001 if (Op01.getOpcode() == ISD::UNDEF)
4002 Op01 = Op23;
4003 else if (Op23.getOpcode() == ISD::UNDEF)
4004 Op23 = Op01;
4005 // Merging identical replications is a no-op.
4006 if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4007 return Op01;
4008 Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4009 Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4010 SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4011 DL, MVT::v2i64, Op01, Op23);
4012 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4013 }
4014
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004015 // Collect the constant terms.
4016 SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4017 SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4018
4019 unsigned NumConstants = 0;
4020 for (unsigned I = 0; I < NumElements; ++I) {
4021 SDValue Elem = Elems[I];
4022 if (Elem.getOpcode() == ISD::Constant ||
4023 Elem.getOpcode() == ISD::ConstantFP) {
4024 NumConstants += 1;
4025 Constants[I] = Elem;
4026 Done[I] = true;
4027 }
4028 }
4029 // If there was at least one constant, fill in the other elements of
4030 // Constants with undefs to get a full vector constant and use that
4031 // as the starting point.
4032 SDValue Result;
4033 if (NumConstants > 0) {
4034 for (unsigned I = 0; I < NumElements; ++I)
4035 if (!Constants[I].getNode())
4036 Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4037 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4038 } else {
4039 // Otherwise try to use VLVGP to start the sequence in order to
4040 // avoid a false dependency on any previous contents of the vector
4041 // register. This only makes sense if one of the associated elements
4042 // is defined.
4043 unsigned I1 = NumElements / 2 - 1;
4044 unsigned I2 = NumElements - 1;
4045 bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF);
4046 bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF);
4047 if (Def1 || Def2) {
4048 SDValue Elem1 = Elems[Def1 ? I1 : I2];
4049 SDValue Elem2 = Elems[Def2 ? I2 : I1];
4050 Result = DAG.getNode(ISD::BITCAST, DL, VT,
4051 joinDwords(DAG, DL, Elem1, Elem2));
4052 Done[I1] = true;
4053 Done[I2] = true;
4054 } else
4055 Result = DAG.getUNDEF(VT);
4056 }
4057
4058 // Use VLVGx to insert the other elements.
4059 for (unsigned I = 0; I < NumElements; ++I)
4060 if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF)
4061 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4062 DAG.getConstant(I, DL, MVT::i32));
4063 return Result;
4064}
4065
4066SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4067 SelectionDAG &DAG) const {
4068 const SystemZInstrInfo *TII =
4069 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4070 auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4071 SDLoc DL(Op);
4072 EVT VT = Op.getValueType();
4073
4074 if (BVN->isConstant()) {
4075 // Try using VECTOR GENERATE BYTE MASK. This is the architecturally-
4076 // preferred way of creating all-zero and all-one vectors so give it
4077 // priority over other methods below.
4078 uint64_t Mask = 0;
4079 if (tryBuildVectorByteMask(BVN, Mask)) {
4080 SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4081 DAG.getConstant(Mask, DL, MVT::i32));
4082 return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4083 }
4084
4085 // Try using some form of replication.
4086 APInt SplatBits, SplatUndef;
4087 unsigned SplatBitSize;
4088 bool HasAnyUndefs;
4089 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4090 8, true) &&
4091 SplatBitSize <= 64) {
4092 // First try assuming that any undefined bits above the highest set bit
4093 // and below the lowest set bit are 1s. This increases the likelihood of
4094 // being able to use a sign-extended element value in VECTOR REPLICATE
4095 // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4096 uint64_t SplatBitsZ = SplatBits.getZExtValue();
4097 uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4098 uint64_t Lower = (SplatUndefZ
4099 & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4100 uint64_t Upper = (SplatUndefZ
4101 & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4102 uint64_t Value = SplatBitsZ | Upper | Lower;
4103 SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4104 SplatBitSize);
4105 if (Op.getNode())
4106 return Op;
4107
4108 // Now try assuming that any undefined bits between the first and
4109 // last defined set bits are set. This increases the chances of
4110 // using a non-wraparound mask.
4111 uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4112 Value = SplatBitsZ | Middle;
4113 Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4114 if (Op.getNode())
4115 return Op;
4116 }
4117
4118 // Fall back to loading it from memory.
4119 return SDValue();
4120 }
4121
4122 // See if we should use shuffles to construct the vector from other vectors.
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004123 if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004124 return Res;
4125
Ulrich Weigandcd808232015-05-05 19:26:48 +00004126 // Detect SCALAR_TO_VECTOR conversions.
4127 if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4128 return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4129
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004130 // Otherwise use buildVector to build the vector up from GPRs.
4131 unsigned NumElements = Op.getNumOperands();
4132 SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4133 for (unsigned I = 0; I < NumElements; ++I)
4134 Ops[I] = Op.getOperand(I);
4135 return buildVector(DAG, DL, VT, Ops);
4136}
4137
4138SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4139 SelectionDAG &DAG) const {
4140 auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4141 SDLoc DL(Op);
4142 EVT VT = Op.getValueType();
4143 unsigned NumElements = VT.getVectorNumElements();
4144
4145 if (VSN->isSplat()) {
4146 SDValue Op0 = Op.getOperand(0);
4147 unsigned Index = VSN->getSplatIndex();
4148 assert(Index < VT.getVectorNumElements() &&
4149 "Splat index should be defined and in first operand");
4150 // See whether the value we're splatting is directly available as a scalar.
4151 if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4152 Op0.getOpcode() == ISD::BUILD_VECTOR)
4153 return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4154 // Otherwise keep it as a vector-to-vector operation.
4155 return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4156 DAG.getConstant(Index, DL, MVT::i32));
4157 }
4158
4159 GeneralShuffle GS(VT);
4160 for (unsigned I = 0; I < NumElements; ++I) {
4161 int Elt = VSN->getMaskElt(I);
4162 if (Elt < 0)
4163 GS.addUndef();
4164 else
4165 GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4166 unsigned(Elt) % NumElements);
4167 }
4168 return GS.getNode(DAG, SDLoc(VSN));
4169}
4170
4171SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4172 SelectionDAG &DAG) const {
4173 SDLoc DL(Op);
4174 // Just insert the scalar into element 0 of an undefined vector.
4175 return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4176 Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4177 Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4178}
4179
Ulrich Weigandcd808232015-05-05 19:26:48 +00004180SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4181 SelectionDAG &DAG) const {
4182 // Handle insertions of floating-point values.
4183 SDLoc DL(Op);
4184 SDValue Op0 = Op.getOperand(0);
4185 SDValue Op1 = Op.getOperand(1);
4186 SDValue Op2 = Op.getOperand(2);
4187 EVT VT = Op.getValueType();
4188
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004189 // Insertions into constant indices of a v2f64 can be done using VPDI.
4190 // However, if the inserted value is a bitcast or a constant then it's
4191 // better to use GPRs, as below.
4192 if (VT == MVT::v2f64 &&
4193 Op1.getOpcode() != ISD::BITCAST &&
Ulrich Weigandcd808232015-05-05 19:26:48 +00004194 Op1.getOpcode() != ISD::ConstantFP &&
4195 Op2.getOpcode() == ISD::Constant) {
4196 uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4197 unsigned Mask = VT.getVectorNumElements() - 1;
4198 if (Index <= Mask)
4199 return Op;
4200 }
4201
4202 // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4203 MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4204 MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4205 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4206 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4207 DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4208 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4209}
4210
4211SDValue
4212SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4213 SelectionDAG &DAG) const {
4214 // Handle extractions of floating-point values.
4215 SDLoc DL(Op);
4216 SDValue Op0 = Op.getOperand(0);
4217 SDValue Op1 = Op.getOperand(1);
4218 EVT VT = Op.getValueType();
4219 EVT VecVT = Op0.getValueType();
4220
4221 // Extractions of constant indices can be done directly.
4222 if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4223 uint64_t Index = CIndexN->getZExtValue();
4224 unsigned Mask = VecVT.getVectorNumElements() - 1;
4225 if (Index <= Mask)
4226 return Op;
4227 }
4228
4229 // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4230 MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4231 MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4232 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4233 DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4234 return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4235}
4236
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004237SDValue
4238SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004239 unsigned UnpackHigh) const {
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004240 SDValue PackedOp = Op.getOperand(0);
4241 EVT OutVT = Op.getValueType();
4242 EVT InVT = PackedOp.getValueType();
4243 unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4244 unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4245 do {
4246 FromBits *= 2;
4247 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4248 SystemZ::VectorBits / FromBits);
4249 PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4250 } while (FromBits != ToBits);
4251 return PackedOp;
4252}
4253
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004254SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4255 unsigned ByScalar) const {
4256 // Look for cases where a vector shift can use the *_BY_SCALAR form.
4257 SDValue Op0 = Op.getOperand(0);
4258 SDValue Op1 = Op.getOperand(1);
4259 SDLoc DL(Op);
4260 EVT VT = Op.getValueType();
4261 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4262
4263 // See whether the shift vector is a splat represented as BUILD_VECTOR.
4264 if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4265 APInt SplatBits, SplatUndef;
4266 unsigned SplatBitSize;
4267 bool HasAnyUndefs;
4268 // Check for constant splats. Use ElemBitSize as the minimum element
4269 // width and reject splats that need wider elements.
4270 if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4271 ElemBitSize, true) &&
4272 SplatBitSize == ElemBitSize) {
4273 SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4274 DL, MVT::i32);
4275 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4276 }
4277 // Check for variable splats.
4278 BitVector UndefElements;
4279 SDValue Splat = BVN->getSplatValue(&UndefElements);
4280 if (Splat) {
4281 // Since i32 is the smallest legal type, we either need a no-op
4282 // or a truncation.
4283 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4284 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4285 }
4286 }
4287
4288 // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4289 // and the shift amount is directly available in a GPR.
4290 if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4291 if (VSN->isSplat()) {
4292 SDValue VSNOp0 = VSN->getOperand(0);
4293 unsigned Index = VSN->getSplatIndex();
4294 assert(Index < VT.getVectorNumElements() &&
4295 "Splat index should be defined and in first operand");
4296 if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4297 VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4298 // Since i32 is the smallest legal type, we either need a no-op
4299 // or a truncation.
4300 SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4301 VSNOp0.getOperand(Index));
4302 return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4303 }
4304 }
4305 }
4306
4307 // Otherwise just treat the current form as legal.
4308 return Op;
4309}
4310
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004311SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4312 SelectionDAG &DAG) const {
4313 switch (Op.getOpcode()) {
4314 case ISD::BR_CC:
4315 return lowerBR_CC(Op, DAG);
4316 case ISD::SELECT_CC:
4317 return lowerSELECT_CC(Op, DAG);
Richard Sandifordf722a8e302013-10-16 11:10:55 +00004318 case ISD::SETCC:
4319 return lowerSETCC(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004320 case ISD::GlobalAddress:
4321 return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4322 case ISD::GlobalTLSAddress:
4323 return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4324 case ISD::BlockAddress:
4325 return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4326 case ISD::JumpTable:
4327 return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4328 case ISD::ConstantPool:
4329 return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4330 case ISD::BITCAST:
4331 return lowerBITCAST(Op, DAG);
4332 case ISD::VASTART:
4333 return lowerVASTART(Op, DAG);
4334 case ISD::VACOPY:
4335 return lowerVACOPY(Op, DAG);
4336 case ISD::DYNAMIC_STACKALLOC:
4337 return lowerDYNAMIC_STACKALLOC(Op, DAG);
Richard Sandiford7d86e472013-08-21 09:34:56 +00004338 case ISD::SMUL_LOHI:
4339 return lowerSMUL_LOHI(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004340 case ISD::UMUL_LOHI:
4341 return lowerUMUL_LOHI(Op, DAG);
4342 case ISD::SDIVREM:
4343 return lowerSDIVREM(Op, DAG);
4344 case ISD::UDIVREM:
4345 return lowerUDIVREM(Op, DAG);
4346 case ISD::OR:
4347 return lowerOR(Op, DAG);
Ulrich Weigandb4012182015-03-31 12:56:33 +00004348 case ISD::CTPOP:
4349 return lowerCTPOP(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004350 case ISD::CTLZ_ZERO_UNDEF:
4351 return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4352 Op.getValueType(), Op.getOperand(0));
4353 case ISD::CTTZ_ZERO_UNDEF:
4354 return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4355 Op.getValueType(), Op.getOperand(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004356 case ISD::ATOMIC_SWAP:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004357 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4358 case ISD::ATOMIC_STORE:
4359 return lowerATOMIC_STORE(Op, DAG);
4360 case ISD::ATOMIC_LOAD:
4361 return lowerATOMIC_LOAD(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004362 case ISD::ATOMIC_LOAD_ADD:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004363 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004364 case ISD::ATOMIC_LOAD_SUB:
Richard Sandiford41350a52013-12-24 15:18:04 +00004365 return lowerATOMIC_LOAD_SUB(Op, DAG);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004366 case ISD::ATOMIC_LOAD_AND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004367 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004368 case ISD::ATOMIC_LOAD_OR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004369 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004370 case ISD::ATOMIC_LOAD_XOR:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004371 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004372 case ISD::ATOMIC_LOAD_NAND:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004373 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004374 case ISD::ATOMIC_LOAD_MIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004375 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004376 case ISD::ATOMIC_LOAD_MAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004377 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004378 case ISD::ATOMIC_LOAD_UMIN:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004379 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004380 case ISD::ATOMIC_LOAD_UMAX:
Richard Sandifordbef3d7a2013-12-10 10:49:34 +00004381 return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004382 case ISD::ATOMIC_CMP_SWAP:
4383 return lowerATOMIC_CMP_SWAP(Op, DAG);
4384 case ISD::STACKSAVE:
4385 return lowerSTACKSAVE(Op, DAG);
4386 case ISD::STACKRESTORE:
4387 return lowerSTACKRESTORE(Op, DAG);
Richard Sandiford03481332013-08-23 11:36:42 +00004388 case ISD::PREFETCH:
4389 return lowerPREFETCH(Op, DAG);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004390 case ISD::INTRINSIC_W_CHAIN:
4391 return lowerINTRINSIC_W_CHAIN(Op, DAG);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004392 case ISD::INTRINSIC_WO_CHAIN:
4393 return lowerINTRINSIC_WO_CHAIN(Op, DAG);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004394 case ISD::BUILD_VECTOR:
4395 return lowerBUILD_VECTOR(Op, DAG);
4396 case ISD::VECTOR_SHUFFLE:
4397 return lowerVECTOR_SHUFFLE(Op, DAG);
4398 case ISD::SCALAR_TO_VECTOR:
4399 return lowerSCALAR_TO_VECTOR(Op, DAG);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004400 case ISD::INSERT_VECTOR_ELT:
4401 return lowerINSERT_VECTOR_ELT(Op, DAG);
4402 case ISD::EXTRACT_VECTOR_ELT:
4403 return lowerEXTRACT_VECTOR_ELT(Op, DAG);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004404 case ISD::SIGN_EXTEND_VECTOR_INREG:
4405 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4406 case ISD::ZERO_EXTEND_VECTOR_INREG:
4407 return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004408 case ISD::SHL:
4409 return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4410 case ISD::SRL:
4411 return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4412 case ISD::SRA:
4413 return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004414 default:
4415 llvm_unreachable("Unexpected node to lower");
4416 }
4417}
4418
4419const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4420#define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
Matthias Braund04893f2015-05-07 21:33:59 +00004421 switch ((SystemZISD::NodeType)Opcode) {
4422 case SystemZISD::FIRST_NUMBER: break;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004423 OPCODE(RET_FLAG);
4424 OPCODE(CALL);
Richard Sandiford709bda62013-08-19 12:42:31 +00004425 OPCODE(SIBCALL);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004426 OPCODE(TLS_GDCALL);
4427 OPCODE(TLS_LDCALL);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004428 OPCODE(PCREL_WRAPPER);
Richard Sandiford54b36912013-09-27 15:14:04 +00004429 OPCODE(PCREL_OFFSET);
Richard Sandiford57485472013-12-13 15:35:00 +00004430 OPCODE(IABS);
Richard Sandiford5bc670b2013-09-06 11:51:39 +00004431 OPCODE(ICMP);
4432 OPCODE(FCMP);
Richard Sandiford35b9be22013-08-28 10:31:43 +00004433 OPCODE(TM);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004434 OPCODE(BR_CCMASK);
4435 OPCODE(SELECT_CCMASK);
4436 OPCODE(ADJDYNALLOC);
4437 OPCODE(EXTRACT_ACCESS);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004438 OPCODE(POPCNT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004439 OPCODE(UMUL_LOHI64);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004440 OPCODE(SDIVREM32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004441 OPCODE(SDIVREM64);
4442 OPCODE(UDIVREM32);
4443 OPCODE(UDIVREM64);
Richard Sandifordd131ff82013-07-08 09:35:23 +00004444 OPCODE(MVC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004445 OPCODE(MVC_LOOP);
Richard Sandiford178273a2013-09-05 10:36:45 +00004446 OPCODE(NC);
4447 OPCODE(NC_LOOP);
4448 OPCODE(OC);
4449 OPCODE(OC_LOOP);
4450 OPCODE(XC);
4451 OPCODE(XC_LOOP);
Richard Sandiford761703a2013-08-12 10:17:33 +00004452 OPCODE(CLC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004453 OPCODE(CLC_LOOP);
Richard Sandifordbb83a502013-08-16 11:29:37 +00004454 OPCODE(STPCPY);
Ulrich Weigand1c6f07d2015-05-04 17:39:40 +00004455 OPCODE(STRCMP);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00004456 OPCODE(SEARCH_STRING);
Richard Sandiford564681c2013-08-12 10:28:10 +00004457 OPCODE(IPM);
Richard Sandiford9afe6132013-12-10 10:36:34 +00004458 OPCODE(SERIALIZE);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00004459 OPCODE(TBEGIN);
4460 OPCODE(TBEGIN_NOFLOAT);
4461 OPCODE(TEND);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004462 OPCODE(BYTE_MASK);
4463 OPCODE(ROTATE_MASK);
4464 OPCODE(REPLICATE);
4465 OPCODE(JOIN_DWORDS);
4466 OPCODE(SPLAT);
4467 OPCODE(MERGE_HIGH);
4468 OPCODE(MERGE_LOW);
4469 OPCODE(SHL_DOUBLE);
4470 OPCODE(PERMUTE_DWORDS);
4471 OPCODE(PERMUTE);
4472 OPCODE(PACK);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004473 OPCODE(PACKS_CC);
4474 OPCODE(PACKLS_CC);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004475 OPCODE(UNPACK_HIGH);
4476 OPCODE(UNPACKL_HIGH);
4477 OPCODE(UNPACK_LOW);
4478 OPCODE(UNPACKL_LOW);
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004479 OPCODE(VSHL_BY_SCALAR);
4480 OPCODE(VSRL_BY_SCALAR);
4481 OPCODE(VSRA_BY_SCALAR);
4482 OPCODE(VSUM);
4483 OPCODE(VICMPE);
4484 OPCODE(VICMPH);
4485 OPCODE(VICMPHL);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004486 OPCODE(VICMPES);
4487 OPCODE(VICMPHS);
4488 OPCODE(VICMPHLS);
Ulrich Weigandcd808232015-05-05 19:26:48 +00004489 OPCODE(VFCMPE);
4490 OPCODE(VFCMPH);
4491 OPCODE(VFCMPHE);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004492 OPCODE(VFCMPES);
4493 OPCODE(VFCMPHS);
4494 OPCODE(VFCMPHES);
4495 OPCODE(VFTCI);
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004496 OPCODE(VEXTEND);
4497 OPCODE(VROUND);
Ulrich Weigandc1708b22015-05-05 19:31:09 +00004498 OPCODE(VTM);
4499 OPCODE(VFAE_CC);
4500 OPCODE(VFAEZ_CC);
4501 OPCODE(VFEE_CC);
4502 OPCODE(VFEEZ_CC);
4503 OPCODE(VFENE_CC);
4504 OPCODE(VFENEZ_CC);
4505 OPCODE(VISTR_CC);
4506 OPCODE(VSTRC_CC);
4507 OPCODE(VSTRCZ_CC);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004508 OPCODE(ATOMIC_SWAPW);
4509 OPCODE(ATOMIC_LOADW_ADD);
4510 OPCODE(ATOMIC_LOADW_SUB);
4511 OPCODE(ATOMIC_LOADW_AND);
4512 OPCODE(ATOMIC_LOADW_OR);
4513 OPCODE(ATOMIC_LOADW_XOR);
4514 OPCODE(ATOMIC_LOADW_NAND);
4515 OPCODE(ATOMIC_LOADW_MIN);
4516 OPCODE(ATOMIC_LOADW_MAX);
4517 OPCODE(ATOMIC_LOADW_UMIN);
4518 OPCODE(ATOMIC_LOADW_UMAX);
4519 OPCODE(ATOMIC_CMP_SWAPW);
Richard Sandiford03481332013-08-23 11:36:42 +00004520 OPCODE(PREFETCH);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004521 }
Craig Topper062a2ba2014-04-25 05:30:21 +00004522 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004523#undef OPCODE
4524}
4525
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004526// Return true if VT is a vector whose elements are a whole number of bytes
4527// in width.
4528static bool canTreatAsByteVector(EVT VT) {
4529 return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4530}
4531
4532// Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4533// producing a result of type ResVT. Op is a possibly bitcast version
4534// of the input vector and Index is the index (based on type VecVT) that
4535// should be extracted. Return the new extraction if a simplification
4536// was possible or if Force is true.
4537SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4538 SDValue Op, unsigned Index,
4539 DAGCombinerInfo &DCI,
4540 bool Force) const {
4541 SelectionDAG &DAG = DCI.DAG;
4542
4543 // The number of bytes being extracted.
4544 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4545
4546 for (;;) {
4547 unsigned Opcode = Op.getOpcode();
4548 if (Opcode == ISD::BITCAST)
4549 // Look through bitcasts.
4550 Op = Op.getOperand(0);
4551 else if (Opcode == ISD::VECTOR_SHUFFLE &&
4552 canTreatAsByteVector(Op.getValueType())) {
4553 // Get a VPERM-like permute mask and see whether the bytes covered
4554 // by the extracted element are a contiguous sequence from one
4555 // source operand.
4556 SmallVector<int, SystemZ::VectorBytes> Bytes;
4557 getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4558 int First;
4559 if (!getShuffleInput(Bytes, Index * BytesPerElement,
4560 BytesPerElement, First))
4561 break;
4562 if (First < 0)
4563 return DAG.getUNDEF(ResVT);
4564 // Make sure the contiguous sequence starts at a multiple of the
4565 // original element size.
4566 unsigned Byte = unsigned(First) % Bytes.size();
4567 if (Byte % BytesPerElement != 0)
4568 break;
4569 // We can get the extracted value directly from an input.
4570 Index = Byte / BytesPerElement;
4571 Op = Op.getOperand(unsigned(First) / Bytes.size());
4572 Force = true;
4573 } else if (Opcode == ISD::BUILD_VECTOR &&
4574 canTreatAsByteVector(Op.getValueType())) {
4575 // We can only optimize this case if the BUILD_VECTOR elements are
4576 // at least as wide as the extracted value.
4577 EVT OpVT = Op.getValueType();
4578 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4579 if (OpBytesPerElement < BytesPerElement)
4580 break;
4581 // Make sure that the least-significant bit of the extracted value
4582 // is the least significant bit of an input.
4583 unsigned End = (Index + 1) * BytesPerElement;
4584 if (End % OpBytesPerElement != 0)
4585 break;
4586 // We're extracting the low part of one operand of the BUILD_VECTOR.
4587 Op = Op.getOperand(End / OpBytesPerElement - 1);
4588 if (!Op.getValueType().isInteger()) {
4589 EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4590 Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4591 DCI.AddToWorklist(Op.getNode());
4592 }
4593 EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4594 Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4595 if (VT != ResVT) {
4596 DCI.AddToWorklist(Op.getNode());
4597 Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4598 }
4599 return Op;
4600 } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004601 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4602 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4603 canTreatAsByteVector(Op.getValueType()) &&
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004604 canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4605 // Make sure that only the unextended bits are significant.
4606 EVT ExtVT = Op.getValueType();
4607 EVT OpVT = Op.getOperand(0).getValueType();
4608 unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4609 unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4610 unsigned Byte = Index * BytesPerElement;
4611 unsigned SubByte = Byte % ExtBytesPerElement;
4612 unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4613 if (SubByte < MinSubByte ||
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004614 SubByte + BytesPerElement > ExtBytesPerElement)
4615 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004616 // Get the byte offset of the unextended element
4617 Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4618 // ...then add the byte offset relative to that element.
4619 Byte += SubByte - MinSubByte;
4620 if (Byte % BytesPerElement != 0)
NAKAMURA Takumi0a7d0ad2015-09-22 11:15:07 +00004621 break;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004622 Op = Op.getOperand(0);
4623 Index = Byte / BytesPerElement;
4624 Force = true;
4625 } else
4626 break;
4627 }
4628 if (Force) {
4629 if (Op.getValueType() != VecVT) {
4630 Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4631 DCI.AddToWorklist(Op.getNode());
4632 }
4633 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4634 DAG.getConstant(Index, DL, MVT::i32));
4635 }
4636 return SDValue();
4637}
4638
4639// Optimize vector operations in scalar value Op on the basis that Op
4640// is truncated to TruncVT.
4641SDValue
4642SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4643 DAGCombinerInfo &DCI) const {
4644 // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4645 // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4646 // of type TruncVT.
4647 if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4648 TruncVT.getSizeInBits() % 8 == 0) {
4649 SDValue Vec = Op.getOperand(0);
4650 EVT VecVT = Vec.getValueType();
4651 if (canTreatAsByteVector(VecVT)) {
4652 if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4653 unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4654 unsigned TruncBytes = TruncVT.getStoreSize();
4655 if (BytesPerElement % TruncBytes == 0) {
4656 // Calculate the value of Y' in the above description. We are
4657 // splitting the original elements into Scale equal-sized pieces
4658 // and for truncation purposes want the last (least-significant)
4659 // of these pieces for IndexN. This is easiest to do by calculating
4660 // the start index of the following element and then subtracting 1.
4661 unsigned Scale = BytesPerElement / TruncBytes;
4662 unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4663
4664 // Defer the creation of the bitcast from X to combineExtract,
4665 // which might be able to optimize the extraction.
4666 VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4667 VecVT.getStoreSize() / TruncBytes);
4668 EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4669 return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4670 }
4671 }
4672 }
4673 }
4674 return SDValue();
4675}
4676
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004677SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4678 DAGCombinerInfo &DCI) const {
4679 SelectionDAG &DAG = DCI.DAG;
4680 unsigned Opcode = N->getOpcode();
4681 if (Opcode == ISD::SIGN_EXTEND) {
4682 // Convert (sext (ashr (shl X, C1), C2)) to
4683 // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4684 // cheap as narrower ones.
4685 SDValue N0 = N->getOperand(0);
4686 EVT VT = N->getValueType(0);
4687 if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4688 auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4689 SDValue Inner = N0.getOperand(0);
4690 if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4691 if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4692 unsigned Extra = (VT.getSizeInBits() -
4693 N0.getValueType().getSizeInBits());
4694 unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4695 unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4696 EVT ShiftVT = N0.getOperand(1).getValueType();
4697 SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4698 Inner.getOperand(0));
4699 SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004700 DAG.getConstant(NewShlAmt, SDLoc(Inner),
4701 ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004702 return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004703 DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004704 }
4705 }
4706 }
4707 }
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004708 if (Opcode == SystemZISD::MERGE_HIGH ||
4709 Opcode == SystemZISD::MERGE_LOW) {
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004710 SDValue Op0 = N->getOperand(0);
4711 SDValue Op1 = N->getOperand(1);
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004712 if (Op0.getOpcode() == ISD::BITCAST)
4713 Op0 = Op0.getOperand(0);
4714 if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4715 cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4716 // (z_merge_* 0, 0) -> 0. This is mostly useful for using VLLEZF
4717 // for v4f32.
4718 if (Op1 == N->getOperand(0))
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004719 return Op1;
Ulrich Weigandcd2a1b52015-05-05 19:29:21 +00004720 // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4721 EVT VT = Op1.getValueType();
4722 unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4723 if (ElemBytes <= 4) {
4724 Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4725 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4726 EVT InVT = VT.changeVectorElementTypeToInteger();
4727 EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4728 SystemZ::VectorBytes / ElemBytes / 2);
4729 if (VT != InVT) {
4730 Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4731 DCI.AddToWorklist(Op1.getNode());
4732 }
4733 SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4734 DCI.AddToWorklist(Op.getNode());
4735 return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4736 }
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004737 }
4738 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004739 // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4740 // for the extraction to be done on a vMiN value, so that we can use VSTE.
4741 // If X has wider elements then convert it to:
4742 // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4743 if (Opcode == ISD::STORE) {
4744 auto *SN = cast<StoreSDNode>(N);
4745 EVT MemVT = SN->getMemoryVT();
4746 if (MemVT.isInteger()) {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00004747 if (SDValue Value =
4748 combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00004749 DCI.AddToWorklist(Value.getNode());
4750
4751 // Rewrite the store with the new form of stored value.
4752 return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4753 SN->getBasePtr(), SN->getMemoryVT(),
4754 SN->getMemOperand());
4755 }
4756 }
4757 }
4758 // Try to simplify a vector extraction.
4759 if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4760 if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4761 SDValue Op0 = N->getOperand(0);
4762 EVT VecVT = Op0.getValueType();
4763 return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4764 IndexN->getZExtValue(), DCI, false);
4765 }
4766 }
4767 // (join_dwords X, X) == (replicate X)
4768 if (Opcode == SystemZISD::JOIN_DWORDS &&
4769 N->getOperand(0) == N->getOperand(1))
4770 return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4771 N->getOperand(0));
Ulrich Weigand80b3af72015-05-05 19:27:45 +00004772 // (fround (extract_vector_elt X 0))
4773 // (fround (extract_vector_elt X 1)) ->
4774 // (extract_vector_elt (VROUND X) 0)
4775 // (extract_vector_elt (VROUND X) 1)
4776 //
4777 // This is a special case since the target doesn't really support v2f32s.
4778 if (Opcode == ISD::FP_ROUND) {
4779 SDValue Op0 = N->getOperand(0);
4780 if (N->getValueType(0) == MVT::f32 &&
4781 Op0.hasOneUse() &&
4782 Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4783 Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4784 Op0.getOperand(1).getOpcode() == ISD::Constant &&
4785 cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4786 SDValue Vec = Op0.getOperand(0);
4787 for (auto *U : Vec->uses()) {
4788 if (U != Op0.getNode() &&
4789 U->hasOneUse() &&
4790 U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4791 U->getOperand(0) == Vec &&
4792 U->getOperand(1).getOpcode() == ISD::Constant &&
4793 cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4794 SDValue OtherRound = SDValue(*U->use_begin(), 0);
4795 if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4796 OtherRound.getOperand(0) == SDValue(U, 0) &&
4797 OtherRound.getValueType() == MVT::f32) {
4798 SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4799 MVT::v4f32, Vec);
4800 DCI.AddToWorklist(VRound.getNode());
4801 SDValue Extract1 =
4802 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4803 VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4804 DCI.AddToWorklist(Extract1.getNode());
4805 DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4806 SDValue Extract0 =
4807 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4808 VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4809 return Extract0;
4810 }
4811 }
4812 }
4813 }
4814 }
Richard Sandiford95bc5f92014-03-07 11:34:35 +00004815 return SDValue();
4816}
4817
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004818//===----------------------------------------------------------------------===//
4819// Custom insertion
4820//===----------------------------------------------------------------------===//
4821
4822// Create a new basic block after MBB.
4823static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4824 MachineFunction &MF = *MBB->getParent();
4825 MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004826 MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004827 return NewMBB;
4828}
4829
Richard Sandifordbe133a82013-08-28 09:01:51 +00004830// Split MBB after MI and return the new block (the one that contains
4831// instructions after MI).
4832static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4833 MachineBasicBlock *MBB) {
4834 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4835 NewMBB->splice(NewMBB->begin(), MBB,
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00004836 std::next(MachineBasicBlock::iterator(MI)), MBB->end());
Richard Sandifordbe133a82013-08-28 09:01:51 +00004837 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4838 return NewMBB;
4839}
4840
Richard Sandiford5e318f02013-08-27 09:54:29 +00004841// Split MBB before MI and return the new block (the one that contains MI).
4842static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4843 MachineBasicBlock *MBB) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004844 MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00004845 NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004846 NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4847 return NewMBB;
4848}
4849
Richard Sandiford5e318f02013-08-27 09:54:29 +00004850// Force base value Base into a register before MI. Return the register.
4851static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4852 const SystemZInstrInfo *TII) {
4853 if (Base.isReg())
4854 return Base.getReg();
4855
4856 MachineBasicBlock *MBB = MI->getParent();
4857 MachineFunction &MF = *MBB->getParent();
4858 MachineRegisterInfo &MRI = MF.getRegInfo();
4859
4860 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4861 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4862 .addOperand(Base).addImm(0).addReg(0);
4863 return Reg;
4864}
4865
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004866// Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4867MachineBasicBlock *
4868SystemZTargetLowering::emitSelect(MachineInstr *MI,
4869 MachineBasicBlock *MBB) const {
Eric Christophera6734172015-01-31 00:06:45 +00004870 const SystemZInstrInfo *TII =
4871 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004872
4873 unsigned DestReg = MI->getOperand(0).getReg();
4874 unsigned TrueReg = MI->getOperand(1).getReg();
4875 unsigned FalseReg = MI->getOperand(2).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004876 unsigned CCValid = MI->getOperand(3).getImm();
4877 unsigned CCMask = MI->getOperand(4).getImm();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004878 DebugLoc DL = MI->getDebugLoc();
4879
4880 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004881 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004882 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4883
4884 // StartMBB:
Richard Sandiford0fb90ab2013-05-28 10:41:11 +00004885 // BRC CCMask, JoinMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004886 // # fallthrough to FalseMBB
4887 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004888 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4889 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004890 MBB->addSuccessor(JoinMBB);
4891 MBB->addSuccessor(FalseMBB);
4892
4893 // FalseMBB:
4894 // # fallthrough to JoinMBB
4895 MBB = FalseMBB;
4896 MBB->addSuccessor(JoinMBB);
4897
4898 // JoinMBB:
4899 // %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4900 // ...
4901 MBB = JoinMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004902 BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004903 .addReg(TrueReg).addMBB(StartMBB)
4904 .addReg(FalseReg).addMBB(FalseMBB);
4905
4906 MI->eraseFromParent();
4907 return JoinMBB;
4908}
4909
Richard Sandifordb86a8342013-06-27 09:27:40 +00004910// Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
4911// StoreOpcode is the store to use and Invert says whether the store should
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004912// happen when the condition is false rather than true. If a STORE ON
4913// CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
Richard Sandifordb86a8342013-06-27 09:27:40 +00004914MachineBasicBlock *
4915SystemZTargetLowering::emitCondStore(MachineInstr *MI,
4916 MachineBasicBlock *MBB,
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004917 unsigned StoreOpcode, unsigned STOCOpcode,
4918 bool Invert) const {
Eric Christophera6734172015-01-31 00:06:45 +00004919 const SystemZInstrInfo *TII =
4920 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordb86a8342013-06-27 09:27:40 +00004921
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004922 unsigned SrcReg = MI->getOperand(0).getReg();
4923 MachineOperand Base = MI->getOperand(1);
4924 int64_t Disp = MI->getOperand(2).getImm();
4925 unsigned IndexReg = MI->getOperand(3).getReg();
Richard Sandiford3d768e32013-07-31 12:30:20 +00004926 unsigned CCValid = MI->getOperand(4).getImm();
4927 unsigned CCMask = MI->getOperand(5).getImm();
Richard Sandifordb86a8342013-06-27 09:27:40 +00004928 DebugLoc DL = MI->getDebugLoc();
4929
4930 StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
4931
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004932 // Use STOCOpcode if possible. We could use different store patterns in
4933 // order to avoid matching the index register, but the performance trade-offs
4934 // might be more complicated in that case.
Eric Christopher93bf97c2014-06-27 07:38:01 +00004935 if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004936 if (Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004937 CCMask ^= CCValid;
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004938 BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
Richard Sandifordfd7f4ae2013-08-01 10:39:40 +00004939 .addReg(SrcReg).addOperand(Base).addImm(Disp)
4940 .addImm(CCValid).addImm(CCMask);
Richard Sandiforda68e6f52013-07-25 08:57:02 +00004941 MI->eraseFromParent();
4942 return MBB;
4943 }
4944
Richard Sandifordb86a8342013-06-27 09:27:40 +00004945 // Get the condition needed to branch around the store.
4946 if (!Invert)
Richard Sandiford3d768e32013-07-31 12:30:20 +00004947 CCMask ^= CCValid;
Richard Sandifordb86a8342013-06-27 09:27:40 +00004948
4949 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00004950 MachineBasicBlock *JoinMBB = splitBlockBefore(MI, MBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004951 MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4952
4953 // StartMBB:
4954 // BRC CCMask, JoinMBB
4955 // # fallthrough to FalseMBB
Richard Sandifordb86a8342013-06-27 09:27:40 +00004956 MBB = StartMBB;
Richard Sandiford3d768e32013-07-31 12:30:20 +00004957 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4958 .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
Richard Sandifordb86a8342013-06-27 09:27:40 +00004959 MBB->addSuccessor(JoinMBB);
4960 MBB->addSuccessor(FalseMBB);
4961
4962 // FalseMBB:
4963 // store %SrcReg, %Disp(%Index,%Base)
4964 // # fallthrough to JoinMBB
4965 MBB = FalseMBB;
4966 BuildMI(MBB, DL, TII->get(StoreOpcode))
4967 .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
4968 MBB->addSuccessor(JoinMBB);
4969
4970 MI->eraseFromParent();
4971 return JoinMBB;
4972}
4973
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004974// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
4975// or ATOMIC_SWAP{,W} instruction MI. BinOpcode is the instruction that
4976// performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
4977// BitSize is the width of the field in bits, or 0 if this is a partword
4978// ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
4979// is one of the operands. Invert says whether the field should be
4980// inverted after performing BinOpcode (e.g. for NAND).
4981MachineBasicBlock *
4982SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
4983 MachineBasicBlock *MBB,
4984 unsigned BinOpcode,
4985 unsigned BitSize,
4986 bool Invert) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004987 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00004988 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00004989 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004990 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00004991 bool IsSubWord = (BitSize < 32);
4992
4993 // Extract the operands. Base can be a register or a frame index.
4994 // Src2 can be a register or immediate.
4995 unsigned Dest = MI->getOperand(0).getReg();
4996 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
4997 int64_t Disp = MI->getOperand(2).getImm();
4998 MachineOperand Src2 = earlyUseOperand(MI->getOperand(3));
4999 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5000 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5001 DebugLoc DL = MI->getDebugLoc();
5002 if (IsSubWord)
5003 BitSize = MI->getOperand(6).getImm();
5004
5005 // Subword operations use 32-bit registers.
5006 const TargetRegisterClass *RC = (BitSize <= 32 ?
5007 &SystemZ::GR32BitRegClass :
5008 &SystemZ::GR64BitRegClass);
5009 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5010 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5011
5012 // Get the right opcodes for the displacement.
5013 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5014 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5015 assert(LOpcode && CSOpcode && "Displacement out of range");
5016
5017 // Create virtual registers for temporary results.
5018 unsigned OrigVal = MRI.createVirtualRegister(RC);
5019 unsigned OldVal = MRI.createVirtualRegister(RC);
5020 unsigned NewVal = (BinOpcode || IsSubWord ?
5021 MRI.createVirtualRegister(RC) : Src2.getReg());
5022 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5023 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5024
5025 // Insert a basic block for the main loop.
5026 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005027 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005028 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5029
5030 // StartMBB:
5031 // ...
5032 // %OrigVal = L Disp(%Base)
5033 // # fall through to LoopMMB
5034 MBB = StartMBB;
5035 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5036 .addOperand(Base).addImm(Disp).addReg(0);
5037 MBB->addSuccessor(LoopMBB);
5038
5039 // LoopMBB:
5040 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5041 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5042 // %RotatedNewVal = OP %RotatedOldVal, %Src2
5043 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5044 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5045 // JNE LoopMBB
5046 // # fall through to DoneMMB
5047 MBB = LoopMBB;
5048 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5049 .addReg(OrigVal).addMBB(StartMBB)
5050 .addReg(Dest).addMBB(LoopMBB);
5051 if (IsSubWord)
5052 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5053 .addReg(OldVal).addReg(BitShift).addImm(0);
5054 if (Invert) {
5055 // Perform the operation normally and then invert every bit of the field.
5056 unsigned Tmp = MRI.createVirtualRegister(RC);
5057 BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5058 .addReg(RotatedOldVal).addOperand(Src2);
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005059 if (BitSize <= 32)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005060 // XILF with the upper BitSize bits set.
Richard Sandiford652784e2013-09-25 11:11:53 +00005061 BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
Alexey Samsonovfffd56ec2014-08-20 21:56:43 +00005062 .addReg(Tmp).addImm(-1U << (32 - BitSize));
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005063 else {
5064 // Use LCGR and add -1 to the result, which is more compact than
5065 // an XILF, XILH pair.
5066 unsigned Tmp2 = MRI.createVirtualRegister(RC);
5067 BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5068 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5069 .addReg(Tmp2).addImm(-1);
5070 }
5071 } else if (BinOpcode)
5072 // A simply binary operation.
5073 BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5074 .addReg(RotatedOldVal).addOperand(Src2);
5075 else if (IsSubWord)
5076 // Use RISBG to rotate Src2 into position and use it to replace the
5077 // field in RotatedOldVal.
5078 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5079 .addReg(RotatedOldVal).addReg(Src2.getReg())
5080 .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5081 if (IsSubWord)
5082 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5083 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5084 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5085 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005086 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5087 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005088 MBB->addSuccessor(LoopMBB);
5089 MBB->addSuccessor(DoneMBB);
5090
5091 MI->eraseFromParent();
5092 return DoneMBB;
5093}
5094
5095// Implement EmitInstrWithCustomInserter for pseudo
5096// ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI. CompareOpcode is the
5097// instruction that should be used to compare the current field with the
5098// minimum or maximum value. KeepOldMask is the BRC condition-code mask
5099// for when the current field should be kept. BitSize is the width of
5100// the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5101MachineBasicBlock *
5102SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5103 MachineBasicBlock *MBB,
5104 unsigned CompareOpcode,
5105 unsigned KeepOldMask,
5106 unsigned BitSize) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005107 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005108 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005109 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005110 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005111 bool IsSubWord = (BitSize < 32);
5112
5113 // Extract the operands. Base can be a register or a frame index.
5114 unsigned Dest = MI->getOperand(0).getReg();
5115 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5116 int64_t Disp = MI->getOperand(2).getImm();
5117 unsigned Src2 = MI->getOperand(3).getReg();
5118 unsigned BitShift = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5119 unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5120 DebugLoc DL = MI->getDebugLoc();
5121 if (IsSubWord)
5122 BitSize = MI->getOperand(6).getImm();
5123
5124 // Subword operations use 32-bit registers.
5125 const TargetRegisterClass *RC = (BitSize <= 32 ?
5126 &SystemZ::GR32BitRegClass :
5127 &SystemZ::GR64BitRegClass);
5128 unsigned LOpcode = BitSize <= 32 ? SystemZ::L : SystemZ::LG;
5129 unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5130
5131 // Get the right opcodes for the displacement.
5132 LOpcode = TII->getOpcodeForOffset(LOpcode, Disp);
5133 CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5134 assert(LOpcode && CSOpcode && "Displacement out of range");
5135
5136 // Create virtual registers for temporary results.
5137 unsigned OrigVal = MRI.createVirtualRegister(RC);
5138 unsigned OldVal = MRI.createVirtualRegister(RC);
5139 unsigned NewVal = MRI.createVirtualRegister(RC);
5140 unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5141 unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5142 unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5143
5144 // Insert 3 basic blocks for the loop.
5145 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005146 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005147 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5148 MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5149 MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5150
5151 // StartMBB:
5152 // ...
5153 // %OrigVal = L Disp(%Base)
5154 // # fall through to LoopMMB
5155 MBB = StartMBB;
5156 BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5157 .addOperand(Base).addImm(Disp).addReg(0);
5158 MBB->addSuccessor(LoopMBB);
5159
5160 // LoopMBB:
5161 // %OldVal = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5162 // %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5163 // CompareOpcode %RotatedOldVal, %Src2
Richard Sandiford312425f2013-05-20 14:23:08 +00005164 // BRC KeepOldMask, UpdateMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005165 MBB = LoopMBB;
5166 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5167 .addReg(OrigVal).addMBB(StartMBB)
5168 .addReg(Dest).addMBB(UpdateMBB);
5169 if (IsSubWord)
5170 BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5171 .addReg(OldVal).addReg(BitShift).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005172 BuildMI(MBB, DL, TII->get(CompareOpcode))
5173 .addReg(RotatedOldVal).addReg(Src2);
5174 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005175 .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005176 MBB->addSuccessor(UpdateMBB);
5177 MBB->addSuccessor(UseAltMBB);
5178
5179 // UseAltMBB:
5180 // %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5181 // # fall through to UpdateMMB
5182 MBB = UseAltMBB;
5183 if (IsSubWord)
5184 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5185 .addReg(RotatedOldVal).addReg(Src2)
5186 .addImm(32).addImm(31 + BitSize).addImm(0);
5187 MBB->addSuccessor(UpdateMBB);
5188
5189 // UpdateMBB:
5190 // %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5191 // [ %RotatedAltVal, UseAltMBB ]
5192 // %NewVal = RLL %RotatedNewVal, 0(%NegBitShift)
5193 // %Dest = CS %OldVal, %NewVal, Disp(%Base)
5194 // JNE LoopMBB
5195 // # fall through to DoneMMB
5196 MBB = UpdateMBB;
5197 BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5198 .addReg(RotatedOldVal).addMBB(LoopMBB)
5199 .addReg(RotatedAltVal).addMBB(UseAltMBB);
5200 if (IsSubWord)
5201 BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5202 .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5203 BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5204 .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005205 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5206 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005207 MBB->addSuccessor(LoopMBB);
5208 MBB->addSuccessor(DoneMBB);
5209
5210 MI->eraseFromParent();
5211 return DoneMBB;
5212}
5213
5214// Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5215// instruction MI.
5216MachineBasicBlock *
5217SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5218 MachineBasicBlock *MBB) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005219 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005220 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005221 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005222 MachineRegisterInfo &MRI = MF.getRegInfo();
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005223
5224 // Extract the operands. Base can be a register or a frame index.
5225 unsigned Dest = MI->getOperand(0).getReg();
5226 MachineOperand Base = earlyUseOperand(MI->getOperand(1));
5227 int64_t Disp = MI->getOperand(2).getImm();
5228 unsigned OrigCmpVal = MI->getOperand(3).getReg();
5229 unsigned OrigSwapVal = MI->getOperand(4).getReg();
5230 unsigned BitShift = MI->getOperand(5).getReg();
5231 unsigned NegBitShift = MI->getOperand(6).getReg();
5232 int64_t BitSize = MI->getOperand(7).getImm();
5233 DebugLoc DL = MI->getDebugLoc();
5234
5235 const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5236
5237 // Get the right opcodes for the displacement.
5238 unsigned LOpcode = TII->getOpcodeForOffset(SystemZ::L, Disp);
5239 unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5240 assert(LOpcode && CSOpcode && "Displacement out of range");
5241
5242 // Create virtual registers for temporary results.
5243 unsigned OrigOldVal = MRI.createVirtualRegister(RC);
5244 unsigned OldVal = MRI.createVirtualRegister(RC);
5245 unsigned CmpVal = MRI.createVirtualRegister(RC);
5246 unsigned SwapVal = MRI.createVirtualRegister(RC);
5247 unsigned StoreVal = MRI.createVirtualRegister(RC);
5248 unsigned RetryOldVal = MRI.createVirtualRegister(RC);
5249 unsigned RetryCmpVal = MRI.createVirtualRegister(RC);
5250 unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5251
5252 // Insert 2 basic blocks for the loop.
5253 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005254 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005255 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5256 MachineBasicBlock *SetMBB = emitBlockAfter(LoopMBB);
5257
5258 // StartMBB:
5259 // ...
5260 // %OrigOldVal = L Disp(%Base)
5261 // # fall through to LoopMMB
5262 MBB = StartMBB;
5263 BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5264 .addOperand(Base).addImm(Disp).addReg(0);
5265 MBB->addSuccessor(LoopMBB);
5266
5267 // LoopMBB:
5268 // %OldVal = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5269 // %CmpVal = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5270 // %SwapVal = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5271 // %Dest = RLL %OldVal, BitSize(%BitShift)
5272 // ^^ The low BitSize bits contain the field
5273 // of interest.
5274 // %RetryCmpVal = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5275 // ^^ Replace the upper 32-BitSize bits of the
5276 // comparison value with those that we loaded,
5277 // so that we can use a full word comparison.
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005278 // CR %Dest, %RetryCmpVal
5279 // JNE DoneMBB
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005280 // # Fall through to SetMBB
5281 MBB = LoopMBB;
5282 BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5283 .addReg(OrigOldVal).addMBB(StartMBB)
5284 .addReg(RetryOldVal).addMBB(SetMBB);
5285 BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5286 .addReg(OrigCmpVal).addMBB(StartMBB)
5287 .addReg(RetryCmpVal).addMBB(SetMBB);
5288 BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5289 .addReg(OrigSwapVal).addMBB(StartMBB)
5290 .addReg(RetrySwapVal).addMBB(SetMBB);
5291 BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5292 .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5293 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5294 .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
Richard Sandiford8a757bb2013-07-31 12:11:07 +00005295 BuildMI(MBB, DL, TII->get(SystemZ::CR))
5296 .addReg(Dest).addReg(RetryCmpVal);
5297 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
Richard Sandiford3d768e32013-07-31 12:30:20 +00005298 .addImm(SystemZ::CCMASK_ICMP)
5299 .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005300 MBB->addSuccessor(DoneMBB);
5301 MBB->addSuccessor(SetMBB);
5302
5303 // SetMBB:
5304 // %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5305 // ^^ Replace the upper 32-BitSize bits of the new
5306 // value with those that we loaded.
5307 // %StoreVal = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5308 // ^^ Rotate the new field to its proper position.
5309 // %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5310 // JNE LoopMBB
5311 // # fall through to ExitMMB
5312 MBB = SetMBB;
5313 BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5314 .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5315 BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5316 .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5317 BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5318 .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
Richard Sandiford3d768e32013-07-31 12:30:20 +00005319 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5320 .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005321 MBB->addSuccessor(LoopMBB);
5322 MBB->addSuccessor(DoneMBB);
5323
5324 MI->eraseFromParent();
5325 return DoneMBB;
5326}
5327
5328// Emit an extension from a GR32 or GR64 to a GR128. ClearEven is true
5329// if the high register of the GR128 value must be cleared or false if
Richard Sandiford87a44362013-09-30 10:28:35 +00005330// it's "don't care". SubReg is subreg_l32 when extending a GR32
5331// and subreg_l64 when extending a GR64.
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005332MachineBasicBlock *
5333SystemZTargetLowering::emitExt128(MachineInstr *MI,
5334 MachineBasicBlock *MBB,
5335 bool ClearEven, unsigned SubReg) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005336 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005337 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005338 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005339 MachineRegisterInfo &MRI = MF.getRegInfo();
5340 DebugLoc DL = MI->getDebugLoc();
5341
5342 unsigned Dest = MI->getOperand(0).getReg();
5343 unsigned Src = MI->getOperand(1).getReg();
5344 unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5345
5346 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5347 if (ClearEven) {
5348 unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5349 unsigned Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5350
5351 BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5352 .addImm(0);
5353 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
Richard Sandiford87a44362013-09-30 10:28:35 +00005354 .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005355 In128 = NewIn128;
5356 }
5357 BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5358 .addReg(In128).addReg(Src).addImm(SubReg);
5359
5360 MI->eraseFromParent();
5361 return MBB;
5362}
5363
Richard Sandifordd131ff82013-07-08 09:35:23 +00005364MachineBasicBlock *
Richard Sandiford564681c2013-08-12 10:28:10 +00005365SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5366 MachineBasicBlock *MBB,
5367 unsigned Opcode) const {
Richard Sandiford5e318f02013-08-27 09:54:29 +00005368 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005369 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005370 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandiford5e318f02013-08-27 09:54:29 +00005371 MachineRegisterInfo &MRI = MF.getRegInfo();
Richard Sandifordd131ff82013-07-08 09:35:23 +00005372 DebugLoc DL = MI->getDebugLoc();
5373
Richard Sandiford5e318f02013-08-27 09:54:29 +00005374 MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005375 uint64_t DestDisp = MI->getOperand(1).getImm();
Richard Sandiford5e318f02013-08-27 09:54:29 +00005376 MachineOperand SrcBase = earlyUseOperand(MI->getOperand(2));
Richard Sandifordd131ff82013-07-08 09:35:23 +00005377 uint64_t SrcDisp = MI->getOperand(3).getImm();
5378 uint64_t Length = MI->getOperand(4).getImm();
5379
Richard Sandifordbe133a82013-08-28 09:01:51 +00005380 // When generating more than one CLC, all but the last will need to
5381 // branch to the end when a difference is found.
5382 MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
Craig Topper062a2ba2014-04-25 05:30:21 +00005383 splitBlockAfter(MI, MBB) : nullptr);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005384
Richard Sandiford5e318f02013-08-27 09:54:29 +00005385 // Check for the loop form, in which operand 5 is the trip count.
5386 if (MI->getNumExplicitOperands() > 5) {
5387 bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5388
5389 uint64_t StartCountReg = MI->getOperand(5).getReg();
5390 uint64_t StartSrcReg = forceReg(MI, SrcBase, TII);
5391 uint64_t StartDestReg = (HaveSingleBase ? StartSrcReg :
5392 forceReg(MI, DestBase, TII));
5393
5394 const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5395 uint64_t ThisSrcReg = MRI.createVirtualRegister(RC);
5396 uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5397 MRI.createVirtualRegister(RC));
5398 uint64_t NextSrcReg = MRI.createVirtualRegister(RC);
5399 uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5400 MRI.createVirtualRegister(RC));
5401
5402 RC = &SystemZ::GR64BitRegClass;
5403 uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5404 uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5405
5406 MachineBasicBlock *StartMBB = MBB;
5407 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5408 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
Richard Sandifordbe133a82013-08-28 09:01:51 +00005409 MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005410
5411 // StartMBB:
5412 // # fall through to LoopMMB
5413 MBB->addSuccessor(LoopMBB);
5414
5415 // LoopMBB:
5416 // %ThisDestReg = phi [ %StartDestReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005417 // [ %NextDestReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005418 // %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005419 // [ %NextSrcReg, NextMBB ]
Richard Sandiford5e318f02013-08-27 09:54:29 +00005420 // %ThisCountReg = phi [ %StartCountReg, StartMBB ],
Richard Sandifordbe133a82013-08-28 09:01:51 +00005421 // [ %NextCountReg, NextMBB ]
5422 // ( PFD 2, 768+DestDisp(%ThisDestReg) )
Richard Sandiford5e318f02013-08-27 09:54:29 +00005423 // Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
Richard Sandifordbe133a82013-08-28 09:01:51 +00005424 // ( JLH EndMBB )
5425 //
5426 // The prefetch is used only for MVC. The JLH is used only for CLC.
5427 MBB = LoopMBB;
5428
5429 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5430 .addReg(StartDestReg).addMBB(StartMBB)
5431 .addReg(NextDestReg).addMBB(NextMBB);
5432 if (!HaveSingleBase)
5433 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5434 .addReg(StartSrcReg).addMBB(StartMBB)
5435 .addReg(NextSrcReg).addMBB(NextMBB);
5436 BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5437 .addReg(StartCountReg).addMBB(StartMBB)
5438 .addReg(NextCountReg).addMBB(NextMBB);
5439 if (Opcode == SystemZ::MVC)
5440 BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5441 .addImm(SystemZ::PFD_WRITE)
5442 .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5443 BuildMI(MBB, DL, TII->get(Opcode))
5444 .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5445 .addReg(ThisSrcReg).addImm(SrcDisp);
5446 if (EndMBB) {
5447 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5448 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5449 .addMBB(EndMBB);
5450 MBB->addSuccessor(EndMBB);
5451 MBB->addSuccessor(NextMBB);
5452 }
5453
5454 // NextMBB:
Richard Sandiford5e318f02013-08-27 09:54:29 +00005455 // %NextDestReg = LA 256(%ThisDestReg)
5456 // %NextSrcReg = LA 256(%ThisSrcReg)
5457 // %NextCountReg = AGHI %ThisCountReg, -1
5458 // CGHI %NextCountReg, 0
5459 // JLH LoopMBB
5460 // # fall through to DoneMMB
5461 //
5462 // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
Richard Sandifordbe133a82013-08-28 09:01:51 +00005463 MBB = NextMBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005464
Richard Sandiford5e318f02013-08-27 09:54:29 +00005465 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5466 .addReg(ThisDestReg).addImm(256).addReg(0);
5467 if (!HaveSingleBase)
5468 BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5469 .addReg(ThisSrcReg).addImm(256).addReg(0);
5470 BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5471 .addReg(ThisCountReg).addImm(-1);
5472 BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5473 .addReg(NextCountReg).addImm(0);
5474 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5475 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5476 .addMBB(LoopMBB);
5477 MBB->addSuccessor(LoopMBB);
5478 MBB->addSuccessor(DoneMBB);
5479
5480 DestBase = MachineOperand::CreateReg(NextDestReg, false);
5481 SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5482 Length &= 255;
5483 MBB = DoneMBB;
5484 }
5485 // Handle any remaining bytes with straight-line code.
5486 while (Length > 0) {
5487 uint64_t ThisLength = std::min(Length, uint64_t(256));
5488 // The previous iteration might have created out-of-range displacements.
5489 // Apply them using LAY if so.
5490 if (!isUInt<12>(DestDisp)) {
5491 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5492 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5493 .addOperand(DestBase).addImm(DestDisp).addReg(0);
5494 DestBase = MachineOperand::CreateReg(Reg, false);
5495 DestDisp = 0;
5496 }
5497 if (!isUInt<12>(SrcDisp)) {
5498 unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5499 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5500 .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5501 SrcBase = MachineOperand::CreateReg(Reg, false);
5502 SrcDisp = 0;
5503 }
5504 BuildMI(*MBB, MI, DL, TII->get(Opcode))
5505 .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5506 .addOperand(SrcBase).addImm(SrcDisp);
5507 DestDisp += ThisLength;
5508 SrcDisp += ThisLength;
5509 Length -= ThisLength;
Richard Sandifordbe133a82013-08-28 09:01:51 +00005510 // If there's another CLC to go, branch to the end if a difference
5511 // was found.
5512 if (EndMBB && Length > 0) {
5513 MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5514 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5515 .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5516 .addMBB(EndMBB);
5517 MBB->addSuccessor(EndMBB);
5518 MBB->addSuccessor(NextMBB);
5519 MBB = NextMBB;
5520 }
5521 }
5522 if (EndMBB) {
5523 MBB->addSuccessor(EndMBB);
5524 MBB = EndMBB;
5525 MBB->addLiveIn(SystemZ::CC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005526 }
Richard Sandifordd131ff82013-07-08 09:35:23 +00005527
5528 MI->eraseFromParent();
5529 return MBB;
5530}
5531
Richard Sandifordca232712013-08-16 11:21:54 +00005532// Decompose string pseudo-instruction MI into a loop that continually performs
5533// Opcode until CC != 3.
5534MachineBasicBlock *
5535SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5536 MachineBasicBlock *MBB,
5537 unsigned Opcode) const {
Richard Sandifordca232712013-08-16 11:21:54 +00005538 MachineFunction &MF = *MBB->getParent();
Eric Christopherfc6de422014-08-05 02:39:49 +00005539 const SystemZInstrInfo *TII =
Eric Christophera6734172015-01-31 00:06:45 +00005540 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
Richard Sandifordca232712013-08-16 11:21:54 +00005541 MachineRegisterInfo &MRI = MF.getRegInfo();
5542 DebugLoc DL = MI->getDebugLoc();
5543
5544 uint64_t End1Reg = MI->getOperand(0).getReg();
5545 uint64_t Start1Reg = MI->getOperand(1).getReg();
5546 uint64_t Start2Reg = MI->getOperand(2).getReg();
5547 uint64_t CharReg = MI->getOperand(3).getReg();
5548
5549 const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5550 uint64_t This1Reg = MRI.createVirtualRegister(RC);
5551 uint64_t This2Reg = MRI.createVirtualRegister(RC);
5552 uint64_t End2Reg = MRI.createVirtualRegister(RC);
5553
5554 MachineBasicBlock *StartMBB = MBB;
Richard Sandiford5e318f02013-08-27 09:54:29 +00005555 MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
Richard Sandifordca232712013-08-16 11:21:54 +00005556 MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5557
5558 // StartMBB:
Richard Sandifordca232712013-08-16 11:21:54 +00005559 // # fall through to LoopMMB
Richard Sandifordca232712013-08-16 11:21:54 +00005560 MBB->addSuccessor(LoopMBB);
5561
5562 // LoopMBB:
5563 // %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5564 // %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
Richard Sandiford7789b082013-09-30 08:48:38 +00005565 // R0L = %CharReg
5566 // %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
Richard Sandifordca232712013-08-16 11:21:54 +00005567 // JO LoopMBB
5568 // # fall through to DoneMMB
Richard Sandiford6f6d5512013-08-20 09:38:48 +00005569 //
Richard Sandiford7789b082013-09-30 08:48:38 +00005570 // The load of R0L can be hoisted by post-RA LICM.
Richard Sandifordca232712013-08-16 11:21:54 +00005571 MBB = LoopMBB;
Richard Sandifordca232712013-08-16 11:21:54 +00005572
5573 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5574 .addReg(Start1Reg).addMBB(StartMBB)
5575 .addReg(End1Reg).addMBB(LoopMBB);
5576 BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5577 .addReg(Start2Reg).addMBB(StartMBB)
5578 .addReg(End2Reg).addMBB(LoopMBB);
Richard Sandiford7789b082013-09-30 08:48:38 +00005579 BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
Richard Sandifordca232712013-08-16 11:21:54 +00005580 BuildMI(MBB, DL, TII->get(Opcode))
5581 .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5582 .addReg(This1Reg).addReg(This2Reg);
5583 BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5584 .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5585 MBB->addSuccessor(LoopMBB);
5586 MBB->addSuccessor(DoneMBB);
5587
5588 DoneMBB->addLiveIn(SystemZ::CC);
5589
5590 MI->eraseFromParent();
5591 return DoneMBB;
5592}
5593
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005594// Update TBEGIN instruction with final opcode and register clobbers.
5595MachineBasicBlock *
5596SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5597 MachineBasicBlock *MBB,
5598 unsigned Opcode,
5599 bool NoFloat) const {
5600 MachineFunction &MF = *MBB->getParent();
5601 const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5602 const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5603
5604 // Update opcode.
5605 MI->setDesc(TII->get(Opcode));
5606
5607 // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5608 // Make sure to add the corresponding GRSM bits if they are missing.
5609 uint64_t Control = MI->getOperand(2).getImm();
5610 static const unsigned GPRControlBit[16] = {
5611 0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5612 0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5613 };
5614 Control |= GPRControlBit[15];
5615 if (TFI->hasFP(MF))
5616 Control |= GPRControlBit[11];
5617 MI->getOperand(2).setImm(Control);
5618
5619 // Add GPR clobbers.
5620 for (int I = 0; I < 16; I++) {
5621 if ((Control & GPRControlBit[I]) == 0) {
5622 unsigned Reg = SystemZMC::GR64Regs[I];
5623 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5624 }
5625 }
5626
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005627 // Add FPR/VR clobbers.
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005628 if (!NoFloat && (Control & 4) != 0) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00005629 if (Subtarget.hasVector()) {
5630 for (int I = 0; I < 32; I++) {
5631 unsigned Reg = SystemZMC::VR128Regs[I];
5632 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5633 }
5634 } else {
5635 for (int I = 0; I < 16; I++) {
5636 unsigned Reg = SystemZMC::FP64Regs[I];
5637 MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5638 }
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005639 }
5640 }
5641
5642 return MBB;
5643}
5644
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005645MachineBasicBlock *
5646SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
NAKAMURA Takumi50df0c22015-11-02 01:38:12 +00005647 MachineBasicBlock *MBB,
5648 unsigned Opcode) const {
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005649 MachineFunction &MF = *MBB->getParent();
5650 MachineRegisterInfo *MRI = &MF.getRegInfo();
5651 const SystemZInstrInfo *TII =
5652 static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5653 DebugLoc DL = MI->getDebugLoc();
5654
5655 unsigned SrcReg = MI->getOperand(0).getReg();
5656
5657 // Create new virtual register of the same class as source.
5658 const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5659 unsigned DstReg = MRI->createVirtualRegister(RC);
5660
5661 // Replace pseudo with a normal load-and-test that models the def as
5662 // well.
5663 BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5664 .addReg(SrcReg);
5665 MI->eraseFromParent();
5666
5667 return MBB;
5668}
5669
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005670MachineBasicBlock *SystemZTargetLowering::
5671EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5672 switch (MI->getOpcode()) {
Richard Sandiford7c5c0ea2013-10-01 13:10:16 +00005673 case SystemZ::Select32Mux:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005674 case SystemZ::Select32:
5675 case SystemZ::SelectF32:
5676 case SystemZ::Select64:
5677 case SystemZ::SelectF64:
5678 case SystemZ::SelectF128:
5679 return emitSelect(MI, MBB);
5680
Richard Sandiford2896d042013-10-01 14:33:55 +00005681 case SystemZ::CondStore8Mux:
5682 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5683 case SystemZ::CondStore8MuxInv:
5684 return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5685 case SystemZ::CondStore16Mux:
5686 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5687 case SystemZ::CondStore16MuxInv:
5688 return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005689 case SystemZ::CondStore8:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005690 return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005691 case SystemZ::CondStore8Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005692 return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005693 case SystemZ::CondStore16:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005694 return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005695 case SystemZ::CondStore16Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005696 return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005697 case SystemZ::CondStore32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005698 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005699 case SystemZ::CondStore32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005700 return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005701 case SystemZ::CondStore64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005702 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005703 case SystemZ::CondStore64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005704 return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005705 case SystemZ::CondStoreF32:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005706 return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005707 case SystemZ::CondStoreF32Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005708 return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005709 case SystemZ::CondStoreF64:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005710 return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005711 case SystemZ::CondStoreF64Inv:
Richard Sandiforda68e6f52013-07-25 08:57:02 +00005712 return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
Richard Sandifordb86a8342013-06-27 09:27:40 +00005713
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005714 case SystemZ::AEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005715 return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005716 case SystemZ::ZEXT128_32:
Richard Sandiford87a44362013-09-30 10:28:35 +00005717 return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005718 case SystemZ::ZEXT128_64:
Richard Sandiford87a44362013-09-30 10:28:35 +00005719 return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005720
5721 case SystemZ::ATOMIC_SWAPW:
5722 return emitAtomicLoadBinary(MI, MBB, 0, 0);
5723 case SystemZ::ATOMIC_SWAP_32:
5724 return emitAtomicLoadBinary(MI, MBB, 0, 32);
5725 case SystemZ::ATOMIC_SWAP_64:
5726 return emitAtomicLoadBinary(MI, MBB, 0, 64);
5727
5728 case SystemZ::ATOMIC_LOADW_AR:
5729 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5730 case SystemZ::ATOMIC_LOADW_AFI:
5731 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5732 case SystemZ::ATOMIC_LOAD_AR:
5733 return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5734 case SystemZ::ATOMIC_LOAD_AHI:
5735 return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5736 case SystemZ::ATOMIC_LOAD_AFI:
5737 return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5738 case SystemZ::ATOMIC_LOAD_AGR:
5739 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5740 case SystemZ::ATOMIC_LOAD_AGHI:
5741 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5742 case SystemZ::ATOMIC_LOAD_AGFI:
5743 return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5744
5745 case SystemZ::ATOMIC_LOADW_SR:
5746 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5747 case SystemZ::ATOMIC_LOAD_SR:
5748 return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5749 case SystemZ::ATOMIC_LOAD_SGR:
5750 return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5751
5752 case SystemZ::ATOMIC_LOADW_NR:
5753 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5754 case SystemZ::ATOMIC_LOADW_NILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005755 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005756 case SystemZ::ATOMIC_LOAD_NR:
5757 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005758 case SystemZ::ATOMIC_LOAD_NILL:
5759 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5760 case SystemZ::ATOMIC_LOAD_NILH:
5761 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5762 case SystemZ::ATOMIC_LOAD_NILF:
5763 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005764 case SystemZ::ATOMIC_LOAD_NGR:
5765 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005766 case SystemZ::ATOMIC_LOAD_NILL64:
5767 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5768 case SystemZ::ATOMIC_LOAD_NILH64:
5769 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005770 case SystemZ::ATOMIC_LOAD_NIHL64:
5771 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5772 case SystemZ::ATOMIC_LOAD_NIHH64:
5773 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005774 case SystemZ::ATOMIC_LOAD_NILF64:
5775 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
Richard Sandiford70284282013-10-01 14:20:41 +00005776 case SystemZ::ATOMIC_LOAD_NIHF64:
5777 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005778
5779 case SystemZ::ATOMIC_LOADW_OR:
5780 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5781 case SystemZ::ATOMIC_LOADW_OILH:
Richard Sandiford652784e2013-09-25 11:11:53 +00005782 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005783 case SystemZ::ATOMIC_LOAD_OR:
5784 return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005785 case SystemZ::ATOMIC_LOAD_OILL:
5786 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5787 case SystemZ::ATOMIC_LOAD_OILH:
5788 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5789 case SystemZ::ATOMIC_LOAD_OILF:
5790 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005791 case SystemZ::ATOMIC_LOAD_OGR:
5792 return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005793 case SystemZ::ATOMIC_LOAD_OILL64:
5794 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5795 case SystemZ::ATOMIC_LOAD_OILH64:
5796 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005797 case SystemZ::ATOMIC_LOAD_OIHL64:
5798 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5799 case SystemZ::ATOMIC_LOAD_OIHH64:
5800 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005801 case SystemZ::ATOMIC_LOAD_OILF64:
5802 return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
Richard Sandiford6e96ac62013-10-01 13:22:41 +00005803 case SystemZ::ATOMIC_LOAD_OIHF64:
5804 return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005805
5806 case SystemZ::ATOMIC_LOADW_XR:
5807 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5808 case SystemZ::ATOMIC_LOADW_XILF:
Richard Sandiford652784e2013-09-25 11:11:53 +00005809 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005810 case SystemZ::ATOMIC_LOAD_XR:
5811 return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
Richard Sandiford652784e2013-09-25 11:11:53 +00005812 case SystemZ::ATOMIC_LOAD_XILF:
5813 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005814 case SystemZ::ATOMIC_LOAD_XGR:
5815 return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
Richard Sandiford652784e2013-09-25 11:11:53 +00005816 case SystemZ::ATOMIC_LOAD_XILF64:
5817 return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
Richard Sandiford5718dac2013-10-01 14:08:44 +00005818 case SystemZ::ATOMIC_LOAD_XIHF64:
5819 return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005820
5821 case SystemZ::ATOMIC_LOADW_NRi:
5822 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5823 case SystemZ::ATOMIC_LOADW_NILHi:
Richard Sandiford652784e2013-09-25 11:11:53 +00005824 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005825 case SystemZ::ATOMIC_LOAD_NRi:
5826 return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005827 case SystemZ::ATOMIC_LOAD_NILLi:
5828 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5829 case SystemZ::ATOMIC_LOAD_NILHi:
5830 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5831 case SystemZ::ATOMIC_LOAD_NILFi:
5832 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005833 case SystemZ::ATOMIC_LOAD_NGRi:
5834 return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005835 case SystemZ::ATOMIC_LOAD_NILL64i:
5836 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5837 case SystemZ::ATOMIC_LOAD_NILH64i:
5838 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005839 case SystemZ::ATOMIC_LOAD_NIHL64i:
5840 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5841 case SystemZ::ATOMIC_LOAD_NIHH64i:
5842 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
Richard Sandiford652784e2013-09-25 11:11:53 +00005843 case SystemZ::ATOMIC_LOAD_NILF64i:
5844 return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
Richard Sandiford70284282013-10-01 14:20:41 +00005845 case SystemZ::ATOMIC_LOAD_NIHF64i:
5846 return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005847
5848 case SystemZ::ATOMIC_LOADW_MIN:
5849 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5850 SystemZ::CCMASK_CMP_LE, 0);
5851 case SystemZ::ATOMIC_LOAD_MIN_32:
5852 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5853 SystemZ::CCMASK_CMP_LE, 32);
5854 case SystemZ::ATOMIC_LOAD_MIN_64:
5855 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5856 SystemZ::CCMASK_CMP_LE, 64);
5857
5858 case SystemZ::ATOMIC_LOADW_MAX:
5859 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5860 SystemZ::CCMASK_CMP_GE, 0);
5861 case SystemZ::ATOMIC_LOAD_MAX_32:
5862 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5863 SystemZ::CCMASK_CMP_GE, 32);
5864 case SystemZ::ATOMIC_LOAD_MAX_64:
5865 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5866 SystemZ::CCMASK_CMP_GE, 64);
5867
5868 case SystemZ::ATOMIC_LOADW_UMIN:
5869 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5870 SystemZ::CCMASK_CMP_LE, 0);
5871 case SystemZ::ATOMIC_LOAD_UMIN_32:
5872 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5873 SystemZ::CCMASK_CMP_LE, 32);
5874 case SystemZ::ATOMIC_LOAD_UMIN_64:
5875 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5876 SystemZ::CCMASK_CMP_LE, 64);
5877
5878 case SystemZ::ATOMIC_LOADW_UMAX:
5879 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5880 SystemZ::CCMASK_CMP_GE, 0);
5881 case SystemZ::ATOMIC_LOAD_UMAX_32:
5882 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5883 SystemZ::CCMASK_CMP_GE, 32);
5884 case SystemZ::ATOMIC_LOAD_UMAX_64:
5885 return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5886 SystemZ::CCMASK_CMP_GE, 64);
5887
5888 case SystemZ::ATOMIC_CMP_SWAPW:
5889 return emitAtomicCmpSwapW(MI, MBB);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005890 case SystemZ::MVCSequence:
5891 case SystemZ::MVCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005892 return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
Richard Sandiford178273a2013-09-05 10:36:45 +00005893 case SystemZ::NCSequence:
5894 case SystemZ::NCLoop:
5895 return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5896 case SystemZ::OCSequence:
5897 case SystemZ::OCLoop:
5898 return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5899 case SystemZ::XCSequence:
5900 case SystemZ::XCLoop:
5901 return emitMemMemWrapper(MI, MBB, SystemZ::XC);
Richard Sandiford5e318f02013-08-27 09:54:29 +00005902 case SystemZ::CLCSequence:
5903 case SystemZ::CLCLoop:
Richard Sandiford564681c2013-08-12 10:28:10 +00005904 return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
Richard Sandifordca232712013-08-16 11:21:54 +00005905 case SystemZ::CLSTLoop:
5906 return emitStringWrapper(MI, MBB, SystemZ::CLST);
Richard Sandifordbb83a502013-08-16 11:29:37 +00005907 case SystemZ::MVSTLoop:
5908 return emitStringWrapper(MI, MBB, SystemZ::MVST);
Richard Sandiford0dec06a2013-08-16 11:41:43 +00005909 case SystemZ::SRSTLoop:
5910 return emitStringWrapper(MI, MBB, SystemZ::SRST);
Ulrich Weigand57c85f52015-04-01 12:51:43 +00005911 case SystemZ::TBEGIN:
5912 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
5913 case SystemZ::TBEGIN_nofloat:
5914 return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
5915 case SystemZ::TBEGINC:
5916 return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
Jonas Paulsson7c5ce102015-10-08 07:40:16 +00005917 case SystemZ::LTEBRCompare_VecPseudo:
5918 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
5919 case SystemZ::LTDBRCompare_VecPseudo:
5920 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
5921 case SystemZ::LTXBRCompare_VecPseudo:
5922 return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
5923
Ulrich Weigand5f613df2013-05-06 16:15:19 +00005924 default:
5925 llvm_unreachable("Unexpected instr type to insert");
5926 }
5927}